Registry / type-stubs / djangorestframework-types

djangorestframework-types

JSON →
library0.9.0pypypiunverified

djangorestframework-types provides essential type stubs for Django REST Framework, enabling robust static type checking with tools like MyPy. As of version 0.9.0, it targets DRF 3.14+ and Python 3.8+, with updates typically released to support new DRF versions and features.

pip install djangorestframework-types mypy
INSTALL
IMPORT
SIG · DJANGORESTFRAMEWOR
D
djangorestframework-types
type-stubspythonv0.9.0
Install
3.6s avg
Import
Disk
80MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.9.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 82.1MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 3.6s · import 0.000s · 81MB
80MB installed
● package 80MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

DrfPlugin
plugins = drf_types.plugin.DrfPlugin
This symbol is referenced in `mypy.ini` for configuration, not directly imported into Python code at runtime.

To enable type checking for Django REST Framework, you *must* configure your `mypy.ini` file in your project root, adding the `drf_types.plugin.DrfPlugin` (see warnings section for details on `mypy.ini`). The Python example demonstrates how `djangorestframework-types` allows MyPy to correctly infer types for DRF serializers and their validated data, catching potential issues at development time. Save this as a Python file and run `mypy your_file.py` after installation and `mypy.ini` setup.

# mypy.ini (create or update this file in your project root): # [mypy] # plugins = # drf_types.plugin.DrfPlugin # # [drf-types] # # Optional: List your custom DRF apps for deeper introspection # # apps = # # my_app # # another_app # quickstart_example.py (save this as a Python file): from rest_framework import serializers import os # For os.environ.get if needed for real data/auth class UserSerializer(serializers.Serializer): id: serializers.IntegerField = serializers.IntegerField(read_only=True) username: serializers.CharField = serializers.CharField(max_length=150) email: serializers.EmailField = serializers.EmailField() def create(self, validated_data: dict[str, str]) -> dict[str, str]: # In a real application, this would create a User model instance. # This example just returns the data for demonstration. print(f"Creating user: {validated_data.get('username', 'N/A')}") return validated_data def update(self, instance: dict, validated_data: dict[str, str]) -> dict: # In a real application, this would update a User model instance. print(f"Updating user: {instance.get('id', 'N/A')}") instance['username'] = validated_data.get('username', instance.get('username', '')) instance['email'] = validated_data.get('email', instance.get('email', '')) return instance # Example usage with type checking: initial_data = { "username": "testuser_" + os.environ.get('TEST_ID', '1'), "email": "test@example.com" } serializer = UserSerializer(data=initial_data) serializer.is_valid(raise_exception=True) # `validated_data` is now correctly typed thanks to djangorestframework-types validated_data: dict[str, str] = serializer.validated_data # MyPy will detect issues like accessing non-existent keys: # print(validated_data['non_existent_field']) # Mypy would flag this as an error print(f"Validated username: {validated_data['username']}") # To run type checking, ensure mypy.ini is configured as above, then: # mypy quickstart_example.py
Debug
Known issues
breakingAs of version 0.8.0, the `drf_types.plugin.DrfPlugin` must be explicitly listed in your `mypy.ini` under the `[mypy]` section. Omitting it will result in incomplete or incorrect type checking for DRF components.
fix
Create or update `mypy.ini` in your project root with:
```ini
[mypy]
plugins =
    drf_types.plugin.DrfPlugin
```
affects: >=0.8.0
breakingVersion 0.7.0 dropped support for Django REST Framework versions older than 3.14. Using `djangorestframework-types` with DRF < 3.14 may lead to type errors or unexpected behavior.
fix
Ensure your `djangorestframework` package is version 3.14 or newer (`pip install 'djangorestframework>=3.14'`).
affects: >=0.7.0
gotchaFor comprehensive type checking in a Django project, it is highly recommended to also install and configure `django-stubs`. `djangorestframework-types` builds upon `django-stubs` for core Django types.
fix
Install `django-stubs` via `pip install django-stubs` and ensure its plugin (if applicable) is also configured in `mypy.ini`.
affects: *
gotchaIf you have custom DRF applications that define their own serializers, views, or models, MyPy's type checking might not fully understand them without explicit configuration. List them under the `[drf-types]` section in your `mypy.ini`.
fix
In your `mypy.ini`, add an `apps` key under `[drf-types]` (e.g., `apps = my_custom_app, another_app`).
affects: *
Upgrade
Version history
0.9.0latest on PyPI · released Oct 10, 2024
Audit
Dependencies
djangorestframeworkrequiredProvides type stubs for this library; an existing DRF project is assumed.
django-stubsoptionalHighly recommended for full type coverage as DRF builds on Django's core types.
mypyrequiredRequired to actually perform static type checking using these stubs.
Agent activity
35 hits · last 30 days
node
32
OpenAI (training)
1
Resources
djangorestframework-types — pip install djangorestframework-types · libregistry