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

Missing override annotation on DateTimeField from abstract model

Open basicdays opened this issue 7 months ago • 0 comments

Bug report

What's wrong

When the mypy rule explicit-override is enabled, mypy is reporting an error when a model inherits an abstract model that has a DateTimeField in it. This is the case of inheriting from AbstractUser for example. I'm working around it for now by adding # type: ignore[explicit-override] to the class line.

This can be reproduced with the following minimal model code:

from django.db import models


class AbstractTestDate(models.Model):
    a_date = models.DateTimeField()

    class Meta:
        abstract = True


class TestDate(AbstractTestDate):
    pass

This is the minimal mypy config from pyproject.toml:

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

mypy will report the following error:

project/models/__init__.py:21: error: Method "get_next_by_a_date" is not using @override but is overriding a method in class "project.models.AbstractTestDate"  [explicit-override]
project/models/__init__.py:21: error: Method "get_previous_by_a_date" is not using @override but is overriding a method in class "project.models.AbstractTestDate"  [explicit-override]
Found 2 errors in 1 file (checked 74 source files)

It looks like those methods are being added by django.db.models.fields.DateField::contribute_to_class in the django package. This error does not occur when the date field is directly on the concrete model.

How is that should be

The override errors should not occur.

System information

  • OS: Debian bullseye 11.8
  • python version: 3.12.1
  • django version: 4.2.0
  • mypy version: 1.7.0
  • django-stubs version: 4.2.7
  • django-stubs-ext version: 4.2.7

basicdays avatar Jan 18 '24 18:01 basicdays