Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'NoneType' object has no attribute '__module__' while referencing model as string in ForeignKey #196

Closed
linevych opened this issue Oct 2, 2019 · 2 comments · Fixed by #199
Labels
bug Something isn't working
Milestone

Comments

@linevych
Copy link

linevych commented Oct 2, 2019

Bug report

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
@linevych linevych added the bug Something isn't working label Oct 2, 2019
@mkurnikov mkurnikov added this to the 1.2.0 milestone Oct 2, 2019
@mkurnikov
Copy link
Member

mkurnikov commented Oct 5, 2019

Sure it's valid? I thought that for Django to be able to find models, they all has to be reimported in the models/__init__.py file.
https://docs.djangoproject.com/en/2.2/topics/db/models/#organizing-models-in-a-package

UPD. I've reproduced a crash though, thanks!

@armanckeser
Copy link
Contributor

I am getting the same error on mypy=1.4.1 and django-stubs=4.2.3

Output:
Traceback (most recent call last):
  File "/Users/ahmetkeser/Library/Caches/pypoetry/virtualenvs/venv/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/Users/ahmetkeser/Library/Caches/pypoetry/virtualenvs/venv/lib/python3.9/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "mypy/main.py", line 95, in main
  File "mypy/main.py", line 174, in run_build
  File "mypy/build.py", line 197, in build
  File "mypy/build.py", line 270, in _build
  File "mypy/build.py", line 2874, in dispatch
  File "mypy/build.py", line 3060, in load_graph
  File "mypy/build.py", line 2001, in __init__
  File "mypy/build.py", line 2293, in compute_dependencies
  File "mypy/plugin.py", line 839, in get_additional_deps
  File "/Users/ahmetkeser/Library/Caches/pypoetry/virtualenvs/venv/lib/python3.9/site-packages/mypy_django_plugin/main.py", line 163, in get_additional_deps
    related_model_module = related_model_cls.__module__
AttributeError: 'NoneType' object has no attribute '__module__'

Downgrading makes it work again.
Package operations: 0 installs, 3 updates, 0 removals

• Updating mypy (1.4.1 -> 1.3.0)
• Updating django-stubs (4.2.3 -> 4.2.1)

I should say that the error is the same but might not be because of using a string in ForeignKey since my project does not do that.

voidus pushed a commit to voidus/django-stubs that referenced this issue Apr 23, 2024
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.

Fixes typeddjango#74
Closes typeddjango#126
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
3 participants