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

AttributeError: 'NoneType' object has no attribute '__module__'. Did you mean: '__reduce__'?

Open random1st opened this issue 1 year ago • 2 comments

Bug report

What's wrong

mypy . --strict --explicit-package-bases

  File "/Users/xx/Library/Caches/pypoetry/virtualenvs/xxx-CJszIqT8-py3.11/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
             ^^^^^^^^^^^^^^^
  File "/Users/xx/Library/Caches/pypoetry/virtualenvs/xxx-CJszIqT8-py3.11/lib/python3.11/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "mypy/main.py", line 100, in main
  File "mypy/main.py", line 182, in run_build
  File "mypy/build.py", line 191, in build
  File "mypy/build.py", line 265, in _build
  File "mypy/build.py", line 2890, in dispatch
  File "mypy/build.py", line 3076, in load_graph
  File "mypy/build.py", line 1998, in __init__
  File "mypy/build.py", line 2292, in compute_dependencies
  File "mypy/plugin.py", line 844, in get_additional_deps
  File "/Users/xx/Library/Caches/pypoetry/virtualenvs/xxx-CJszIqT8-py3.11/lib/python3.11/site-packages/mypy_django_plugin/main.py", line 141, in get_additional_deps
    related_model_module = related_model_cls.__module__
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute '__module__'. Did you mean: '__reduce__'?

How is that should be

No errors

System information

  • OS:
  • python 3.11:
  • django 5.0.2:
  • mypy 1.8.0:
  • django-stubs 4.2.7:
  • django-stubs-ext 4.2.7:

random1st avatar Feb 16 '24 09:02 random1st

I'm seeing this also. There's a similar report in https://github.com/typeddjango/django-stubs/issues/196 which was fixed in https://github.com/typeddjango/django-stubs/pull/199. However, I suspect https://github.com/typeddjango/django-stubs/pull/1495 may have regressed this.

Looking through https://github.com/typeddjango/django-stubs/pull/1495

  • This refactors get_field_related_model_cls to raise an exception instead of returning None. However, the function still appears to be returning None in some cases.
  • These lines remove the None check in favour of catching the new exception. However, the following line is now failing for me and OP as we're getting None returned rather than an exception.
  • I've verified on my local that restoring the None check mentioned above fixes the crash and lets mypy continue for my project.

SingingTree avatar Feb 18 '24 04:02 SingingTree

@SingingTree I confirmed, it works for me as well.

random1st avatar Feb 19 '24 17:02 random1st