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
In the project that I'm working on I have an application with the following structure:
app1
└── models
├── bar.py
├── baz.py
└── foo.py
There is a model ModelA defined in bar.py and ModelB defined in foo.py that has FK to ModelA:
class ModelB(models.Model):
field1 = models.ForeignKey('ModelA')
This is a valid syntax for referencing model in the same application but when I run mypy I get an error.
What's wrong
Traceback (most recent call last):
File "/usr/local/bin/mypy", line 10, in <module>
sys.exit(console_entry())
File "/usr/local/lib/python3.6/site-packages/mypy/__main__.py", line 8, in console_entry
main(None, sys.stdout, sys.stderr)
File "/usr/local/lib/python3.6/site-packages/mypy/main.py", line 83, in main
res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 164, in build
result = _build(sources, options, alt_lib_path, flush_errors, fscache, stdout, stderr)
File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 224, in _build
graph = dispatch(sources, manager, stdout)
File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 2526, in dispatch
graph = load_graph(sources, manager)
File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 2748, in load_graph
caller_state=st, caller_line=st.dep_line_map.get(dep, 1))
File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 1772, in __init__
self.compute_dependencies()
File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 2059, in compute_dependencies
self.manager.plugin.get_additional_deps(self.tree))
File "/usr/local/lib/python3.6/site-packages/mypy/plugin.py", line 743, in get_additional_deps
deps.extend(plugin.get_additional_deps(file))
File "/usr/local/lib/python3.6/site-packages/mypy_django_plugin/main.py", line 152, in get_additional_deps
related_model_module = related_model_cls.__module__
AttributeError: 'NoneType' object has no attribute '__module__'
The problem is that mypy_django_plugin.django.context.DjangoContext.get_model_class_by_fullname in mypy_django_plugin.django.context.DjangoFieldsContext.get_related_model_cls returns None for relative reference in this case.
The only solution that I found is to use absolute reference models.ForeignKey('app1.ModelA') instead.
How is that should be
Relative model reference should be supported even in case if models.py is replaced with a module.
System information
OS: Docker container based on python:3.6-alpine
python version: 3.6.6
django version: 2.2.5
mypy version: 0.720
django-stubs version: 1.1.0
The text was updated successfully, but these errors were encountered:
This finishes the work done in typeddjango#126. Commits are cherry-picked to give the original author credit.
I also removed psycopg2 tests as they are typeshed's problem now.
Fixestypeddjango#74Closestypeddjango#126
Bug report
In the project that I'm working on I have an application with the following structure:
There is a model
ModelA
defined inbar.py
andModelB
defined infoo.py
that has FK toModelA
:This is a valid syntax for referencing model in the same application but when I run mypy I get an error.
What's wrong
The problem is that
mypy_django_plugin.django.context.DjangoContext.get_model_class_by_fullname
inmypy_django_plugin.django.context.DjangoFieldsContext.get_related_model_cls
returnsNone
for relative reference in this case.The only solution that I found is to use absolute reference
models.ForeignKey('app1.ModelA')
instead.How is that should be
Relative model reference should be supported even in case if
models.py
is replaced with a module.System information
python
version: 3.6.6django
version: 2.2.5mypy
version: 0.720django-stubs
version: 1.1.0The text was updated successfully, but these errors were encountered: