You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As a followup to #68 I've noticed that it would not work with:
fromdjango.dbimportmodelsfrom ...base.modelsimportBaseModelMixin# class BaseModelMixin(models.Model):# class Meta:# abstract = TrueclassTestModel(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:
fromdjango.dbimportmodels# from ...base.models import BaseModelMixinclassBaseModelMixin(models.Model):
classMeta:
abstract=TrueclassTestModel(BaseModelMixin):
foo=models.CharField(max_length=123)
Note that it also fails when overwriting the import:
fromdjango.dbimportmodels# from ...base.models import BaseModelMixinclassBaseModelMixin(models.Model):
classMeta:
abstract=TrueclassTestModel(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?):
Monkey-patching as following seems to be fixing the issue:
importmypy_django_plugin.lib.helpersfrommypy_django_plugin.lib.helpersimportis_model_subclass_infoaspatchPREFIX="project."# name of your namespace moduledefis_model_subclass_info(info, django_context):
ifinfo.fullname.startswith(PREFIX):
name=info.fullname[len(PREFIX) :] # use removeprefix if py>=39ifnameindjango_context.all_registered_model_class_fullnames:
returnTruereturnpatch(info, django_context)
mypy_django_plugin.lib.helpers.is_model_subclass_info=is_model_subclass_info
running mypy like so:
mypy -p project
@sobolevn I'm guessing we need to prefix namespace modules in all_registered_model_class_fullnames property? I'm not sure if how would we fetch that information in that context.
As a followup to #68 I've noticed that it would not work with:
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:
Note that it also fails when overwriting the import:
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
vsproject.app.models.TestModel
(viadjango_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 (viainfo.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
Note that the namespace ("project") is added/used via
AppConfig
:project/__init__.py
does not exist, but creating it and/or changingProductConfig.name
to not include the namespace changes the behavior.System information
python
version: 3.9.5django
version: 4.0.dev20210602105309mypy
version: 0.910django-stubs
version: 1.8.0django-stubs-ext
version: 0.2.0The text was updated successfully, but these errors were encountered: