Skip to content

explicit-override error on generated get_FOO_display, get_next_by_FOO, get_previous_by_FOO #1698

Description

@andersk

When configured to enable the optional explicit-override error, mypy unexpectedly flags explicit-override errors for the generated get_FOO_display, get_next_by_FOO, get_previous_by_FOO methods on subclasses of models having fields with choices, DateFields, and DateTimeFields.

Example

pyproject.toml

[tool.mypy]
enable_error_code = ["explicit-override"]
plugins = ["mypy_django_plugin.main"]

[tool.django-stubs]
django_settings_module = "myapp.settings"

myapp/__init__.py

myapp/settings.py

INSTALLED_APPS = ["myapp"]

myapp/models.py

from django.db import models

class A(models.Model):
    which = models.IntegerField(choices=((1, "one"), (2, "two")))
    when = models.DateTimeField()

class B(A):
    pass

mypy output

$ mypy .
myapp/models.py:7: error: Method "get_which_display" is not using @override but is overriding a method in class "myapp.models.A"  [explicit-override]
myapp/models.py:7: error: Method "get_next_by_when" is not using @override but is overriding a method in class "myapp.models.A"  [explicit-override]
myapp/models.py:7: error: Method "get_previous_by_when" is not using @override but is overriding a method in class "myapp.models.A"  [explicit-override]
Found 3 errors in 1 file (checked 3 source files)

(Expected no errors.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions