{"id":2007,"library":"djangorestframework-stubs","title":"Django REST Framework Type Stubs","description":"djangorestframework-stubs provides PEP-484 type stubs for the Django REST Framework (DRF) library, enabling static type checking with tools like MyPy. It ensures type correctness for DRF components, improving code quality and maintainability. The current version is 3.16.9, with new releases often accompanying updates to DRF, Django, or MyPy.","status":"active","version":"3.16.9","language":"en","source_language":"en","source_url":"https://github.com/typeddjango/djangorestframework-stubs","tags":["django","drf","mypy","type-stubs","typing","static-analysis"],"install":[{"cmd":"pip install djangorestframework-stubs","lang":"bash","label":"Base installation"},{"cmd":"pip install djangorestframework-stubs[requests]","lang":"bash","label":"With requests client support (for RequestsClient)"}],"dependencies":[{"reason":"Provides type stubs for Django, which DRF depends on.","package":"django-stubs"},{"reason":"The core library for which these stubs are provided.","package":"djangorestframework"},{"reason":"The primary static type checker these stubs are designed for.","package":"mypy"},{"reason":"Optional dependency for type hints related to `rest_framework.test.RequestsClient`.","package":"types-requests","optional":true}],"imports":[{"note":"Stubs provide type hints for symbols imported from djangorestframework itself; you do not import directly from `djangorestframework-stubs`.","symbol":"ModelSerializer","correct":"from rest_framework.serializers import ModelSerializer"},{"note":"Stubs provide type hints for symbols imported from djangorestframework itself; you do not import directly from `djangorestframework-stubs`.","symbol":"ViewSet","correct":"from rest_framework.viewsets import ViewSet"},{"note":"Stubs provide type hints for symbols imported from djangorestframework itself; you do not import directly from `djangorestframework-stubs`.","symbol":"Request","correct":"from rest_framework.request import Request"}],"quickstart":{"code":"import os\nfrom django.db import models\nfrom rest_framework import serializers, viewsets\nfrom rest_framework.response import Response\nfrom rest_framework.request import Request # For type hinting\n\n# A minimal Django model (assuming Django and django-stubs are installed)\nclass MyModel(models.Model):\n    name: models.CharField = models.CharField(max_length=100)\n    value: models.IntegerField = models.IntegerField()\n\n    def __str__(self) -> str:\n        return self.name\n\n# A DRF ModelSerializer for MyModel\nclass MyModelSerializer(serializers.ModelSerializer):\n    class Meta:\n        model = MyModel\n        fields = ['id', 'name', 'value']\n\n# A DRF ViewSet for MyModel\nclass MyModelViewSet(viewsets.ModelViewSet):\n    queryset = MyModel.objects.all()\n    serializer_class = MyModelSerializer\n\n    # Example of a custom action with type hints\n    def list(self, request: Request, *args, **kwargs) -> Response:\n        # With djangorestframework-stubs installed, MyPy can check types here.\n        # For instance, ensuring 'request.user' (if present) is accessed correctly.\n        queryset = self.filter_queryset(self.get_queryset())\n        serializer = self.get_serializer(queryset, many=True)\n        return Response(serializer.data)\n\n# To utilize these stubs, configure MyPy (e.g., in pyproject.toml or mypy.ini):\n# [tool.mypy]\n# plugins = [\n#     \"mypy_django_plugin.main\",\n#     \"mypy_drf_plugin.main\"\n# ]\n# strict = true\n\n# Then run: `mypy your_app_name/`\n\n# This code focuses on demonstrating type hinting for DRF components,\n# not on running a live Django/DRF application.","lang":"python","description":"This example showcases type-hinted Django REST Framework components. When `djangorestframework-stubs` is installed alongside `django-stubs` and `mypy` with the respective plugins configured, your static type checker will enforce type correctness for DRF-specific constructs like `serializers`, `views`, and `requests` at development time."},"warnings":[{"fix":"Plan to upgrade `djangorestframework` and `django-stubs` alongside `djangorestframework-stubs` to maintain type checking compatibility and prevent errors.","message":"Version 3.16.9 is the *last* release that officially supports `django-stubs` 5.2 and `djangorestframework` 3.16. Subsequent versions will target newer DRF (3.17+) and `django-stubs` (6.0+) versions, potentially requiring concurrent upgrades.","severity":"breaking","affected_versions":">=3.16.9"},{"fix":"Review `ModelSerializer` implementations, especially those with `many=True`, and adjust their typing to align with DRF's standard type expectations. Consult GitHub issue #827 if type errors or regressions occur.","message":"The workaround for the `ModelSerializer.instance` field when `many=True` was removed in version 3.16.3. This change might affect custom `ModelSerializer` implementations that relied on the previous behavior for correct typing.","severity":"breaking","affected_versions":">=3.16.3"},{"fix":"If `RequestsClient` is used, update your installation command to `pip install djangorestframework-stubs[requests]`.","message":"The `types-requests` dependency became optional in version 3.16.8. If your project uses `rest_framework.test.RequestsClient` (e.g., in test suites), you must now install `djangorestframework-stubs` with the `[requests]` extra to ensure proper type hints.","severity":"gotcha","affected_versions":">=3.16.8"},{"fix":"Always refer to the `djangorestframework-stubs` release notes and documentation for recommended version ranges for its dependencies, especially when performing upgrades. Pinning versions in `requirements.txt` is advisable.","message":"Maintaining compatibility across `djangorestframework-stubs`, `djangorestframework`, `django-stubs`, and `mypy` is crucial. Mismatched versions can lead to incorrect type checking results, MyPy errors, or missing type information.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}