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

Getting mypy `[attr-defined]` errors for some Django basic modules

Open browser-bug opened this issue 8 months ago • 1 comments

I'm on the current environment:

Python 3.12.11
mypy==1.16.1
mypy_extensions==1.1.0
django-types==0.22.0
django-stubs-ext==5.2.2
djangorestframework-types==0.9.0
celery-types==0.23.0

I'm exploiting a Django cookiecutter template that splitted by configs in base, local, production and test. I've put the following in the local.py file but this is the part where I'm not totally sure where to put the monkey patch section.

import django_stubs_ext
from celery.app.task import Task
from tenant_schemas_celery.task import TenantTask

django_stubs_ext.monkeypatch()
Task.__class_getitem__ = classmethod(lambda cls, *args, **kwargs: cls)  # type: ignore[attr-defined]
TenantTask.__class_getitem__ = classmethod(lambda cls, *args, **kwargs: cls)

Both on VSCode via mypy extensions and via shell running mypy test_backend I get some attr-defined errors which look strange since they are refering some basic Django modules like admin, etc.

test_backend/tenants/admin.py:8: error: Module has no attribute "register"  [attr-defined]
test_backend/tenants/admin.py:9: error: Name "admin.ModelAdmin" is not defined  [name-defined]
test_backend/tenants/admin.py:17: error: Module has no attribute "register"  [attr-defined]
test_backend/tenants/admin.py:18: error: Name "admin.ModelAdmin" is not defined  [name-defined]
test_backend/tenants/admin.py:22: error: Module has no attribute "register"  [attr-defined]
test_backend/tenants/admin.py:23: error: Name "admin.ModelAdmin" is not defined  [name-defined]
test_backend/tenants/management/commands/make_tenant_superuser.py:2: error: Module "django.db" has no attribute "connection"  [attr-defined]
test_backend/users/admin.py:18: error: Module has no attribute "site"  [attr-defined]
test_backend/users/admin.py:19: error: Module has no attribute "site"  [attr-defined]
test_backend/users/admin.py:24: error: Module has no attribute "autodiscover"  [attr-defined]
test_backend/users/admin.py:25: error: Module has no attribute "site"  [attr-defined]
test_backend/users/admin.py:28: error: Module has no attribute "register"  [attr-defined]
test_backend/users/admin.py:30: error: Name "admin.TabularInline" is not defined  [name-defined]
test_backend/connections/admin.py:11: error: Module has no attribute "register"  [attr-defined]
test_backend/core/utils/base.py:8: error: Module "django.utils" has no attribute "formats"  [attr-defined]

Finally I can share this inside the pyproject.toml file:

# ==== mypy ====
[tool.mypy]
python_version = "3.12"
check_untyped_defs = true
ignore_missing_imports = true
warn_unused_ignores = true
warn_redundant_casts = true
warn_unused_configs = true
# plugins = ["mypy_django_plugin.main", "mypy_drf_plugin.main"]

[[tool.mypy.overrides]]
# Django migrations should not produce any errors:
module = "*.migrations.*"
ignore_errors = true

[tool.django-stubs]
django_settings_module = "config.settings.test"

I don't even know if [tool.django-stubs] is considered by django-types by the way.

browser-bug avatar Aug 07 '25 11:08 browser-bug