I have
class AnimalSerializer(serializers.ModelSerializer):
class Meta:
model = Animal
fields = "__all__"
Now i run mypy
[mypy]
# The mypy configurations: https://mypy.readthedocs.io/en/latest/config_file.html
python_version = 3.9
check_untyped_defs = True
# disallow_any_explicit = True
disallow_any_generics = True
disallow_untyped_calls = True
disallow_untyped_decorators = True
ignore_errors = False
ignore_missing_imports = True
implicit_reexport = False
strict_optional = True
strict_equality = True
no_implicit_optional = True
warn_unused_ignores = True
warn_redundant_casts = True
warn_unused_configs = True
warn_unreachable = True
warn_no_return = True
mypy_path = /home/simha/app/backend_django/src
plugins =
mypy_django_plugin.main,
mypy_drf_plugin.main
[mypy.plugins.django-stubs]
django_settings_module = petsproject.settings
(venv) $ mypy .
I get
error: Missing type parameters for generic type "ModelSerializer"
The stub file for
serializers.ModelSerializer
shows that it inherits from a generic base classBaseSerializer
. This means thatModelSerializer
is also generic:When run with the --disallow-any-generics option, MyPy will complain if you inherit from an unparameterised generic, as it is unclear whether you want your inherited class to also be considered generic, or whether you want it to be considered a more concrete version of the base class.
As you have the line
model = Animal
in theMeta
class in your derived class, and the stub file annotatesModelSerializer.Meta.model
as being of typeType[_MT]
, my guess is that you do not want want yourAnimalSerializer
class to be generic, and instead want it to be specific to anAnimal
class that is a subclass ofModel
. As such, you need to rewrite yourAnimalSerializer
class like this: