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:
  • python version: 3.9.2
  • django version: 2.2.17
  • mypy version: 0.910
  • django-stubs version: 1.9.0
  • django-stubs-ext version: 0.3.1

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 18
  • Comments: 29 (15 by maintainers)

Most upvoted comments

It’s still happening:

  • mypy==1.3.1, django-stubs[compatible-mypy]==4.2.1
  • mypy==1.4.1, django-stubs[compatible-mypy]==4.2.3

Anyone found workaround for this issue without deleting .mypy_cache dir 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 post

a 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_cache directory it worked again

Any updates/solutions? It may not be the most prudent decision to consistently remove or disable the cache.

Not sure I understand what that would accomplish? I think code that doesn’t use WithAnnotations can still use the cache just fine?

You don’t have to have WithAnnotations anywhere 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 infer WithAnnotations

I’d guess that most of the people here are running in to a caching issue as the plugin doesn’t write WithAnnotations to cache:

https://github.com/typeddjango/django-stubs/blob/9874789bea016a567cf8372cb0e695d9615a5324/mypy_django_plugin/transformers/models.py#L677-L683

Which would be the no_serialize=True controlling it.

Now, if one attempts to go with no_serialize=False and start writing to cache. It opens up a whole new can of worms -> #700

WithAnnotations introduced here: #398 Attempted fix for #700: #725 Caching removed here: #881

When mypy tries to load WithAnnotations from cache, while the plugin is running with no_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-incremental all the time.