django-stubs icon indicating copy to clipboard operation
django-stubs copied to clipboard

`explicit-override` error on generated `get_FOO_display`, `get_next_by_FOO`, `get_previous_by_FOO`

Open andersk opened this issue 2 years ago • 1 comments

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.)

andersk avatar Sep 08 '23 22:09 andersk

Same issue here. We also wanted to start using this explicit-override feature provided by mypy + python3.12 and these strange errors happening. Unfortunately cannot suppress these errors anyhow but inline what is not practical for us. Waiting for the resolution...

sshishov avatar Nov 08 '23 23:11 sshishov

This was re-reported as

  • #2226

and seems to have been fixed in mypy 1.11.0:

  • python/mypy#17433

andersk avatar Aug 02 '24 00:08 andersk