-
-
Notifications
You must be signed in to change notification settings - Fork 553
Description
As a followup to #68 I've noticed that it would not work with:
from django.db import models
from ...base.models import BaseModelMixin
# class BaseModelMixin(models.Model):
# class Meta:
# abstract = True
class TestModel(BaseModelMixin):
foo = models.CharField(max_length=123)error: Need type annotation for "foo" [var-annotated]
The imported model is the same as the commented one.
If not importing it, but defining it there (uncommenting it), it works:
from django.db import models
# from ...base.models import BaseModelMixin
class BaseModelMixin(models.Model):
class Meta:
abstract = True
class TestModel(BaseModelMixin):
foo = models.CharField(max_length=123)Note that it also fails when overwriting the import:
from django.db import models
# from ...base.models import BaseModelMixin
class BaseModelMixin(models.Model):
class Meta:
abstract = True
class TestModel(BaseModelMixin):
foo = models.CharField(max_length=123)…/models.py:5: error: Name "BaseModelMixin" already defined (possibly by an import) [no-redef]
…/models.py:11: error: Need type annotation for "foo" [var-annotated]
I've not investigated much yet, but it appears to be caused by the models fullname not containing the first part of the (namespaced) module name: app.models.TestModel vs project.app.models.TestModel (via django_context.all_registered_model_class_fullnames)..!
This results in the is_model_subclass_info check only working if it is defined in the same file (via info.has_base check - shouldn't that work better maybe in general?):
django-stubs/mypy_django_plugin/lib/helpers.py
Lines 304 to 307 in cf6952c
| def is_model_subclass_info(info: TypeInfo, django_context: "DjangoContext") -> bool: | |
| return info.fullname in django_context.all_registered_model_class_fullnames or info.has_base( | |
| fullnames.MODEL_CLASS_FULLNAME | |
| ) |
Note that the namespace ("project") is added/used via AppConfig:
class MyAppConfig(AppConfig):
name = "project.app"project/__init__.py does not exist, but creating it and/or changing ProductConfig.name to not include the namespace changes the behavior.
System information
- OS: Arch Linux
pythonversion: 3.9.5djangoversion: 4.0.dev20210602105309mypyversion: 0.910django-stubsversion: 1.8.0django-stubs-extversion: 0.2.0