Skip to content

Mypy Crashes with newest version of Django. #13005

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

Closed
Thorbenl opened this issue Jun 21, 2022 · 9 comments
Closed

Mypy Crashes with newest version of Django. #13005

Thorbenl opened this issue Jun 21, 2022 · 9 comments
Labels

Comments

@Thorbenl
Copy link

Thorbenl commented Jun 21, 2022

Crash Report

AttributeError: 'NoneType' object has no attribute 'name'

Looks similar to typeddjango/django-stubs#625

Traceback

Daemon started
Daemon crashed!
Traceback (most recent call last):
  File "mypy/dmypy_server.py", line 230, in serve
  File "mypy/dmypy_server.py", line 273, in run_command
  File "mypy/dmypy_server.py", line 332, in cmd_run
  File "mypy/dmypy_server.py", line 389, in check
  File "mypy/dmypy_server.py", line 424, in initialize_fine_grained
  File "mypy/build.py", line 154, in build
  File "mypy/build.py", line 230, in _build
  File "mypy/build.py", line 2729, in dispatch
  File "mypy/build.py", line 3087, in process_graph
  File "mypy/build.py", line 3185, in process_stale_scc
  File "mypy/build.py", line 2180, in type_check_first_pass
  File "mypy/checker.py", line 325, in check_first_pass
  File "mypy/checker.py", line 433, in accept
  File "mypy/errors.py", line 962, in report_internal_error
  File "mypy/checker.py", line 431, in accept
  File "mypy/nodes.py", line 1028, in accept
  File "mypy/checker.py", line 1816, in visit_class_def
  File "mypy/checker.py", line 433, in accept
  File "mypy/errors.py", line 962, in report_internal_error
  File "mypy/checker.py", line 431, in accept
  File "mypy/nodes.py", line 1099, in accept
  File "mypy/checker.py", line 2178, in visit_block
  File "mypy/checker.py", line 433, in accept
  File "mypy/errors.py", line 962, in report_internal_error
  File "mypy/checker.py", line 431, in accept
  File "mypy/nodes.py", line 839, in accept
  File "mypy/checker.py", line 3984, in visit_decorator
  File "mypy/checker.py", line 848, in check_func_item
  File "mypy/checker.py", line 1033, in check_func_def
  File "mypy/checker.py", line 433, in accept
  File "mypy/errors.py", line 962, in report_internal_error
  File "mypy/checker.py", line 431, in accept
  File "mypy/nodes.py", line 1099, in accept
  File "mypy/checker.py", line 2178, in visit_block
  File "mypy/checker.py", line 433, in accept
  File "mypy/errors.py", line 962, in report_internal_error
  File "mypy/checker.py", line 431, in accept
  File "mypy/nodes.py", line 1243, in accept
  File "mypy/checker.py", line 3899, in visit_for_stmt
  File "mypy/checker.py", line 447, in accept_loop
  File "mypy/checker.py", line 433, in accept
  File "mypy/errors.py", line 962, in report_internal_error
  File "mypy/checker.py", line 431, in accept
  File "mypy/nodes.py", line 1099, in accept
  File "mypy/checker.py", line 2178, in visit_block
  File "mypy/checker.py", line 433, in accept
  File "mypy/errors.py", line 962, in report_internal_error
  File "mypy/checker.py", line 431, in accept
  File "mypy/nodes.py", line 1165, in accept
  File "mypy/checker.py", line 2220, in visit_assignment_stmt
  File "mypy/checker.py", line 2414, in check_assignment
  File "mypy/checkexpr.py", line 3993, in accept
  File "mypy/errors.py", line 962, in report_internal_error
  File "mypy/checkexpr.py", line 3991, in accept
  File "mypy/nodes.py", line 1760, in accept
  File "mypy/checkexpr.py", line 305, in visit_call_expr
  File "mypy/checkexpr.py", line 383, in visit_call_expr_inner
  File "mypy/checkexpr.py", line 892, in check_call_expr_with_callee_type
  File "mypy/checkexpr.py", line 957, in check_call
  File "mypy/checkexpr.py", line 1085, in check_callable_call
  File "mypy/checkexpr.py", line 772, in apply_function_plugin
  File "/Users/thorbenluepkes/work/venv/lib/python3.10/site-packages/mypy_django_plugin/transformers/orm_lookups.py", line 40, in typecheck_queryset_filter
    lookup_type = django_context.resolve_lookup_expected_type(ctx, model_cls, lookup_kwarg)
  File "/Users/thorbenluepkes/work/venv/lib/python3.10/site-packages/mypy_django_plugin/django/context.py", line 375, in resolve_lookup_expected_type
    lookup_parts, field_parts, is_expression = query.solve_lookup_type(lookup)
  File "/Users/thorbenluepkes/work/venv/lib/python3.10/site-packages/django/db/models/sql/query.py", line 1187, in solve_lookup_type
    _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
  File "/Users/thorbenluepkes/work/venv/lib/python3.10/site-packages/django/db/models/sql/query.py", line 1626, in names_to_path
    name = opts.pk.name
AttributeError: 'NoneType' object has no attribute 'name'

To Reproduce

  • django==4.0.5
  • mypy==0.960
  • django-stubs==1.12.0
  • pydantic==1.9.1

Instal these packages and run mypy

Your Environment

  • Mypy version used: 0.960
  • Mypy command-line flags: --pretty --show-error-codes
  • Mypy configuration options from mypy.ini (and other config files): ```
[mypy]
check_untyped_defs = True
#disallow_any_generics = True
#disallow_untyped_calls = True
#disallow_untyped_decorators = True
ignore_errors = False
ignore_missing_imports = True
#implicit_reexport = False
strict_optional = True
allow_redefinition = True
strict_equality = True
no_implicit_optional = True
warn_unused_ignores = True
warn_redundant_casts = True
warn_unused_configs = True
warn_unreachable = True
warn_no_return = True

plugins =
    mypy_django_plugin.main

[mypy.plugins.django-stubs]
django_settings_module = newmarkets.settings
  • Python version used: 3.10.2
  • Operating system and version: Mac OS 12.5

Using a bash script to run dmypy

FILES=$(git ls-files --cached --modified --others \
  --exclude 'venv' \
  --exclude 'node_modules' \
   "$(git rev-parse --show-toplevel)/*.py" | grep -v /migrations/ | grep -v 'test.*.py' | sort | uniq)
CHECK_OPTIONS="--pretty --show-error-codes"
dmypy run -- $CHECK_OPTIONS $FILES

Filters out some django-specific files, and exlucdes nodes modules and virtual env.

It is not an issue on version 0.931 and before

@Thorbenl Thorbenl added the crash label Jun 21, 2022
@AlexWaygood
Copy link
Member

AlexWaygood commented Jun 21, 2022

This looks like an issue with mypy-django-plugin, which is a subpackage of typeddjango/django-stubs. Have you reported an issue there?

https://github.com/typeddjango/django-stubs

@Thorbenl
Copy link
Author

@AlexWaygood I will do that :) Sorry for the inconvenience.

@AlexWaygood
Copy link
Member

@AlexWaygood I will do that :) Sorry for the inconvenience.

Great, no worries! I'll close this for now, but happy to reopen if I'm proven wrong and it is an issue with mypy core :)

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Jun 22, 2022
@Thorbenl
Copy link
Author

Thorbenl commented Aug 5, 2022

@AlexWaygood Sorry to reopen this, but is there a way for me to debug this further?

Traceback (most recent call last):
  File "mypy/dmypy_server.py", line 229, in serve
  File "mypy/dmypy_server.py", line 272, in run_command
  File "mypy/dmypy_server.py", line 331, in cmd_run
  File "mypy/dmypy_server.py", line 388, in check
  File "mypy/dmypy_server.py", line 423, in initialize_fine_grained
  File "mypy/build.py", line 180, in build
  File "mypy/build.py", line 256, in _build
  File "mypy/build.py", line 2717, in dispatch
  File "mypy/build.py", line 3048, in process_graph
  File "mypy/build.py", line 3146, in process_stale_scc
  File "mypy/build.py", line 2185, in type_check_first_pass
  File "mypy/checker.py", line 314, in check_first_pass
  File "mypy/checker.py", line 422, in accept
  File "mypy/errors.py", line 844, in report_internal_error
  File "mypy/checker.py", line 420, in accept
  File "mypy/nodes.py", line 1005, in accept
  File "mypy/checker.py", line 1785, in visit_class_def
  File "mypy/checker.py", line 422, in accept
  File "mypy/errors.py", line 844, in report_internal_error
  File "mypy/checker.py", line 420, in accept
  File "mypy/nodes.py", line 1074, in accept
  File "mypy/checker.py", line 2047, in visit_block
  File "mypy/checker.py", line 422, in accept
  File "mypy/errors.py", line 844, in report_internal_error
  File "mypy/checker.py", line 420, in accept
  File "mypy/nodes.py", line 819, in accept
  File "mypy/checker.py", line 3748, in visit_decorator
  File "mypy/checker.py", line 836, in check_func_item
  File "mypy/checker.py", line 1021, in check_func_def
  File "mypy/checker.py", line 422, in accept
  File "mypy/errors.py", line 844, in report_internal_error
  File "mypy/checker.py", line 420, in accept
  File "mypy/nodes.py", line 1074, in accept
  File "mypy/checker.py", line 2047, in visit_block
  File "mypy/checker.py", line 422, in accept
  File "mypy/errors.py", line 844, in report_internal_error
  File "mypy/checker.py", line 420, in accept
  File "mypy/nodes.py", line 1218, in accept
  File "mypy/checker.py", line 3663, in visit_for_stmt
  File "mypy/checker.py", line 436, in accept_loop
  File "mypy/checker.py", line 422, in accept
  File "mypy/errors.py", line 844, in report_internal_error
  File "mypy/checker.py", line 420, in accept
  File "mypy/nodes.py", line 1074, in accept
  File "mypy/checker.py", line 2047, in visit_block
  File "mypy/checker.py", line 422, in accept
  File "mypy/errors.py", line 844, in report_internal_error
  File "mypy/checker.py", line 420, in accept
  File "mypy/nodes.py", line 1140, in accept
  File "mypy/checker.py", line 2089, in visit_assignment_stmt
  File "mypy/checker.py", line 2271, in check_assignment
  File "mypy/checkexpr.py", line 3964, in accept
  File "mypy/errors.py", line 844, in report_internal_error
  File "mypy/checkexpr.py", line 3962, in accept
  File "mypy/nodes.py", line 1713, in accept
  File "mypy/checkexpr.py", line 288, in visit_call_expr
  File "mypy/checkexpr.py", line 371, in visit_call_expr_inner
  File "mypy/checkexpr.py", line 878, in check_call_expr_with_callee_type
  File "mypy/checkexpr.py", line 938, in check_call
  File "mypy/checkexpr.py", line 1068, in check_callable_call
  File "mypy/checkexpr.py", line 758, in apply_function_plugin
  File "/Users/thorbenluepkes/work/newmarkets/venv/lib/python3.10/site-packages/mypy_django_plugin/transformers/orm_lookups.py", line 42, in typecheck_queryset_filter
    lookup_type = django_context.resolve_lookup_expected_type(ctx, model_cls, lookup_kwarg)
  File "/Users/thorbenluepkes/work/newmarkets/venv/lib/python3.10/site-packages/mypy_django_plugin/django/context.py", line 375, in resolve_lookup_expected_type
    lookup_parts, field_parts, is_expression = query.solve_lookup_type(lookup)
  File "/Users/thorbenluepkes/work/newmarkets/venv/lib/python3.10/site-packages/django/db/models/sql/query.py", line 1217, in solve_lookup_type
    _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
  File "/Users/thorbenluepkes/work/newmarkets/venv/lib/python3.10/site-packages/django/db/models/sql/query.py", line 1658, in names_to_path
    name = opts.pk.name
AttributeError: 'NoneType' object has no attribute 'name'

I cannot for the live of me find out where it is, so it makes it super difficult to actually create a minimal repo. Is there any way mypy can help me debug here? It seems like a model makes problems, but I cant really see where or which. I sadly cannot share the repo, so I would love to give some other insight if possible with mypy..

Running:
mypy 0.931
django-stubs 1.12.0 does NOT happen with 1.9.0tho
python 3.10.2
django 3.0.7

@AlexWaygood
Copy link
Member

If you clone mypy and activate a virtual environment with an editable install, you could try adding print() statements at various points along the traceback to help you figure out which one of your models is causing the crash in the typeddjango plugin.

And when all else fails, try randomly deleting files/classes/functions/whatever from your codebase until you can no longer reproduce the problem. When you get to that point, you'll have your minimal repro :)

@Thorbenl
Copy link
Author

Thorbenl commented Aug 5, 2022

Thanks for the quick reply.
I think we will stay on the old versions then and not upgrade anytime soon! :)
Our codebase is large and I simply dont think we have the time deleting random folders!

I will try printing, maybe that will do something, otherwise we will leave it :)

@AlexWaygood
Copy link
Member

You could also try bisecting which specific commit in the typeddjango plugin started causing the crash. That might give some clues about which of your models is causing the plugin to crash (and would also be very helpful for identifying the root cause of the problem).

@Thorbenl
Copy link
Author

Thorbenl commented Aug 5, 2022

typeddjango

That could be an idea too. I will read how to bisect then.

  File "mypy/checkexpr.py", line 758, in apply_function_plugin
  File "/Users/thorbenluepkes/work/newmarkets/venv/lib/python3.10/site-packages/mypy_django_plugin/transformers/orm_lookups.py", line 40, in typecheck_queryset_filter
    lookup_type = django_context.resolve_lookup_expected_type(ctx, model_cls, lookup_kwarg)
  File "/Users/thorbenluepkes/work/newmarkets/venv/lib/python3.10/site-packages/mypy_django_plugin/django/context.py", line 375, in resolve_lookup_expected_type
    lookup_parts, field_parts, is_expression = query.solve_lookup_type(lookup)
  File "/Users/thorbenluepkes/work/newmarkets/venv/lib/python3.10/site-packages/django/db/models/sql/query.py", line 1217, in solve_lookup_type
    _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
  File "/Users/thorbenluepkes/work/newmarkets/venv/lib/python3.10/site-packages/django/db/models/sql/query.py", line 1658, in names_to_path
    name = opts.pk.name
AttributeError: 'NoneType' object has no attribute 'name'

I tried printing in File "mypy/checkexpr.py", line 758, in apply_function_plugin but that also didnt really yield any output. Maybe I am misunderstanding your definition of an editable install, but I can delete and put prints in the installed packages in venv :)

Ohter than that, how would i connect the local django-stubs that i am bisecting with my current project?

@flaeppe
Copy link

flaeppe commented Aug 7, 2022

Since the trace is pointing you towards the Django plugin, what if you clone it and do an editable install in your mypy env?

pip install -e /path/to/django-stubs/clone

Then you should be able to breakpoint/print-debug in the plugin code, where your trace is showing you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants