-
-
Notifications
You must be signed in to change notification settings - Fork 450
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
WithAnnotations Crash in 1.9.0 #760
Comments
I'm seeing this issue too. It seems that the first run if there is no cache works, and subsequent runs work if files have changed. But if nothing has changed then I see this error. Is there any plan to fix this? |
Maybe related to #725? |
This still seems to be a huge problem for me and preventing me from using the mypy cache. |
Same problem with django-stubs 1.12.0. I need to run |
After removing the |
I'm also having this issue.
Edit: I was wrong, the effect is only temporary. |
Still having this issue in 1.13.0. |
I seem to still get this with django-stubs 0.13.0 and mypy 0.990, python 3.10.1 |
Do we have a way to consistently reproduce this issue? I was pretty sure that my codebase wasn't impacted by it, because I couldn't reproduce it locally, but after modifying my CI to use the cache I was able to immediately reproduce it when the cache crossed between two branches. Can we reproduce it with |
I'm running into this issue, but it doesn't appear to happen with no changes between runs |
Not that I know of. I could try to reduce this down to a minimal example if you're interested in debugging this. |
I'd guess that most of the people here are running in to a caching issue as the plugin doesn't write django-stubs/mypy_django_plugin/transformers/models.py Lines 677 to 683 in 9874789
Which would be the Now, if one attempts to go with
When mypy tries to load |
I'm only guessing now, but a way to reproduce, manually, could perhaps be to have 2 files. Where file 1 would e.g. declare a function signature including Steps:
Disclaimer: I have not tried this, only took it from the top of my head from when I've tried to debug mypy cache issues previously.. |
I think you're right. I put together a minimal repro here: https://github.com/christianbundy/django-stubs-760 Calling $ rm -rf .mypy_cache
$ mypy foo.py bar.py
Success: no issues found in 2 source files
$ echo 'x = 42' >> bar.py
$ mypy foo.py bar.py
Traceback (most recent call last):
File "/opt/homebrew/bin/mypy", line 8, in <module>
sys.exit(console_entry())
File "/opt/homebrew/lib/python3.10/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 193, in build
File "mypy/build.py", line 276, in _build
File "mypy/build.py", line 2903, in dispatch
File "mypy/build.py", line 3284, in process_graph
File "mypy/build.py", line 3365, in process_fresh_modules
File "mypy/build.py", line 2112, in fix_cross_refs
File "mypy/fixup.py", line 53, in fixup_module
File "mypy/fixup.py", line 127, in visit_symbol_table
File "mypy/nodes.py", line 1054, in accept
File "mypy/fixup.py", line 179, in visit_var
File "mypy/types.py", line 1283, in accept
File "mypy/fixup.py", line 205, in visit_instance
File "mypy/types.py", line 1283, in accept
File "mypy/fixup.py", line 196, in visit_instance
File "mypy/fixup.py", line 331, in lookup_fully_qualified_typeinfo
File "mypy/lookup.py", line 49, in lookup_fully_qualified
AssertionError: Cannot find component 'WithAnnotations[django__contrib__auth__models__User]' for 'django_stubs_ext.WithAnnotations[django__contrib__auth__models__User]' |
Is there more to it than this? I've edited the file locally and changed
I don't see any reference to this type in |
In case you don't have a code to reproduce this, I could reproduce in my project with a code like this: def export(queryset: QuerySet):
queryset: FooQuerySet
return queryset.annotate(...) |
Yep, full reproduction repository is linked above: #760 (comment) |
Just wanted to quickly note two findings:
class F(TypedDict):
foo: str
annotated: WithAnnotations[User, F] = User.objects.annotate(foo=Value("")).get() AssertionError: Cannot find component "WithAnnotations[django__contrib__auth__models__User, TypedDict('foo" for "django_stubs_ext.WithAnnotations[django__contrib__auth__models__User, TypedDict('foo.F', {'foo': builtins.str})]" |
Would anyone be opposed to a plugin option to disable |
Not sure I understand what that would accomplish? I think code that doesn't use |
You don't have to have |
Ahh, wow. So this bug is worse than I thought. Is it just me or does it seem like it shouldn't be that hard to fix... |
I would pay somebody $ to fix it |
I would welcome it, and also prefer if Quick investigation gave me the following 2 places to branch at in order to make it configurable: 1 django-stubs/mypy_django_plugin/main.py Lines 224 to 227 in c75ada3
2 django-stubs/mypy_django_plugin/main.py Lines 307 to 315 in c75ada3
|
A minor boilerplate to, quite cheaply, attach types to annotations (which I often use myself) is an approach that hides the annotated attribute behind a property, which instead controls the attribute during runtime. class MyQuerySet(models.QuerySet["MyModel"]):
def with_foo(self) -> MyQuerySet:
return self.annotate(foo_value=Sum(...))
MyManager = models.Manager.from_queryset(MyQuerySet)
class MyModel(models.Model):
...
objects = MyManager()
@property
def foo(self) -> int:
assert hasattr(self, "foo_value"), "Call queryset with `.with_foo()`"
return self.foo_value
x: int = MyModel.objects.with_foo().get().foo As long as one covers the grounds touching |
It's still happening:
Anyone found workaround for this issue without deleting |
Any updates/solutions? It may not be the most prudent decision to consistently remove or disable the cache. |
I've just hit this same issue on current versions of all packages. |
please stop bumping the thread -- if you're interested in updates click a lot of us are subscribed to this thread to get updates and sending extra emails does not help it get prioritized or fixed! |
#2319 introduces a (breaking!) fix to this issue. But, a correct one (at least it seems this way) :) |
Will there be a release with Django 4.2 support that has this fix? |
@AustinScola nope, sorry. We don't have a capacity for backports and multiple versions support. |
An additional note is that it's also depending on |
It's possible to use django-stubs 5.0.x with Django 4.2, but there are some caveats to be aware of. See this post: |
Is there an upcoming release in pypi with this change? |
Bug report
I have upgraded to 1.9.0 and I'm getting this crash when I run
mypy .
inside my src folderThe crash is generated by this piece of code.
When typing mypy doesn't crash
System information
python
version: 3.9.2django
version: 2.2.17mypy
version: 0.910django-stubs
version: 1.9.0django-stubs-ext
version: 0.3.1The text was updated successfully, but these errors were encountered: