django-stubs: WithAnnotations Crash in 1.9.0
Bug report
I have upgraded to 1.9.0 and I’m getting this crash when I run mypy . inside my src folder
Traceback (most recent call last):
File "/home/parallels/Development/quickcheck/vitualenv/bin/mypy", line 8, in <module>
sys.exit(console_entry())
File "/home/parallels/Development/quickcheck/vitualenv/lib/python3.9/site-packages/mypy/__main__.py", line 11, in console_entry
main(None, sys.stdout, sys.stderr)
File "mypy/main.py", line 87, in main
File "mypy/main.py", line 165, in run_build
File "mypy/build.py", line 179, in build
File "mypy/build.py", line 254, in _build
File "mypy/build.py", line 2697, in dispatch
File "mypy/build.py", line 3014, in process_graph
File "mypy/build.py", line 3092, in process_fresh_modules
File "mypy/build.py", line 1991, in fix_cross_refs
File "mypy/fixup.py", line 26, in fixup_module
File "mypy/fixup.py", line 90, in visit_symbol_table
File "mypy/fixup.py", line 46, in visit_type_info
File "mypy/fixup.py", line 92, in visit_symbol_table
File "mypy/nodes.py", line 885, in accept
File "mypy/fixup.py", line 137, in visit_var
File "mypy/types.py", line 846, in accept
File "mypy/fixup.py", line 161, in visit_instance
File "mypy/types.py", line 846, in accept
File "mypy/fixup.py", line 154, in visit_instance
File "mypy/fixup.py", line 269, in lookup_qualified_typeinfo
File "mypy/fixup.py", line 297, in lookup_qualified
File "mypy/fixup.py", line 306, in lookup_qualified_stnode
File "mypy/lookup.py", line 47, in lookup_fully_qualified
AssertionError: Cannot find component 'WithAnnotations[admin' for "django_stubs_ext.WithAnnotations[admin.models.MyModel, TypedDict({'count_loan_id': Any})]"
The crash is generated by this piece of code.
self.var1 = (
MyModel.objects.filter(
to_process_date__gte=timezone.localtime() - timedelta(days=5),
last_attempted_datetime__isnull=False,
)
.values('loan_id')
.annotate(count_loan_id=Count('loan_id'))
.filter(count_loan_id__gte=3)
.values_list('loan_id', flat=True)
)
When typing mypy doesn’t crash
self.var1: 'QuerySet[Any]' = ...
System information
- OS:
pythonversion: 3.9.2djangoversion: 2.2.17mypyversion: 0.910django-stubsversion: 1.9.0django-stubs-extversion: 0.3.1
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 18
- Comments: 29 (15 by maintainers)
It’s still happening:
Anyone found workaround for this issue without deleting
.mypy_cachedir every run?please stop bumping the thread – if you’re interested in updates click
[subscribe]on the right. if you’re also hitting this issue click:thumbsup:on the original posta lot of us are subscribed to this thread to get updates and sending extra emails does not help it get prioritized or fixed!
Would anyone be opposed to a plugin option to disable
WithAnnotations? This bug has been around for 18+ months and disabling the cache has a significant performance costs, and in codebases where this feature is net negative it seems like it might be best to disable it until we have an opportunity to fix it.I’ve just hit this same issue on current versions of all packages.
After removing the
.mypy_cachedirectory it worked againAny updates/solutions? It may not be the most prudent decision to consistently remove or disable the cache.
You don’t have to have
WithAnnotationsanywhere in your code base to run into this issue. The reproduction linked earlier doesn’t. I assume it can be enough for the plugin to inferWithAnnotationsYep, full reproduction repository is linked above: https://github.com/typeddjango/django-stubs/issues/760#issuecomment-1418245576
I’d guess that most of the people here are running in to a caching issue as the plugin doesn’t write
WithAnnotationsto cache:https://github.com/typeddjango/django-stubs/blob/9874789bea016a567cf8372cb0e695d9615a5324/mypy_django_plugin/transformers/models.py#L677-L683
Which would be the
no_serialize=Truecontrolling it.Now, if one attempts to go with
no_serialize=Falseand start writing to cache. It opens up a whole new can of worms -> #700WithAnnotationsintroduced here: #398 Attempted fix for #700: #725 Caching removed here: #881When mypy tries to load
WithAnnotationsfrom cache, while the plugin is running withno_serialize=True, I’m suspecting it results in a traceback looking like:I seem to still get this with django-stubs 0.13.0 and mypy 0.990, python 3.10.1
Same problem with django-stubs 1.12.0. I need to run
mypy --no-incrementalall the time.