django-filter-stubs
django-filter-stubs provides PEP-484 compatible type stubs for the popular `django-filter` library, enabling static type checking with tools like Mypy. The project is currently at version 0.2.0, but it has been officially archived by its maintainers, meaning no further development, updates, or releases are expected.
Common errors
-
error: Library 'django_filters' has no type annotations or does not export 'FilterSet' (or receives 'Any')
cause Mypy cannot find type definitions for `django-filter` in your project, or the installed stubs are not being picked up correctly.fixEnsure `django-filter-stubs` (or `types-django-filter`) is installed: `pip install django-filter-stubs`. Verify your Mypy configuration correctly integrates with Django if using `django-stubs`. -
error: "FilterSet" has no attribute "[some_attribute]" [attr-defined]
cause The installed `django-filter-stubs` version is likely outdated and does not include type definitions for newer attributes or methods introduced in a later version of `django-filter`.fixUpgrade to a maintained stub package like `types-django-filter` (`pip install types-django-filter`). If the issue persists, the `types-django-filter` package might also be lagging behind the latest `django-filter` release. Consider adding `# type: ignore` comments temporarily or contributing to the `typeshed` project. -
Mypy crashes or reports unexpected errors during type checking after installing django-filter-stubs.
cause An unmaintained stub package might have compatibility issues with newer versions of Mypy or Python interpreters, leading to crashes or incorrect type inference.fixRemove `django-filter-stubs` and install `types-django-filter` instead. If using `django-stubs` for core Django types, ensure its version is compatible with your Mypy and Django versions. Run `mypy --no-incremental` to rule out caching issues.
Warnings
- breaking The `django-filter-stubs` project has been officially archived by its maintainers. This means no further development, bug fixes, or compatibility updates are expected.
- breaking Version 0.2.0 of `django-filter-stubs` dropped support for Python 3.7. Attempting to use this version with Python 3.7 may lead to unexpected errors or missing type information.
- gotcha Due to the project's archived status, the provided type stubs may become outdated quickly and might not accurately reflect the latest APIs or features of newer `django-filter` or Django versions. This can result in false positives or missed type errors in Mypy.
Install
-
pip install django-filter-stubs
Imports
- FilterSet
from django_filters import FilterSet
- DjangoFilterBackend
from django_filters.rest_framework import DjangoFilterBackend
Quickstart
# mypy.ini or pyproject.toml
# For mypy.ini:
[mypy]
plugins = mypy_django_plugin.main # If using django-stubs
# For pyproject.toml:
[tool.mypy]
plugins = ["mypy_django_plugin.main"] # If using django-stubs
# In your Django project's Python code:
# This code would benefit from django-filter-stubs for type checking
from django import forms
from django_filters import FilterSet, CharFilter
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
class Meta:
app_label = 'myapp'
class ProductFilter(FilterSet):
name = CharFilter(field_name='name', lookup_expr='icontains')
class Meta:
model = Product
fields = ['name']
def get_filtered_products(request, queryset):
product_filter = ProductFilter(request.GET, queryset=queryset)
return product_filter.qs