-
-
Notifications
You must be signed in to change notification settings - Fork 454
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
Need type annotation for ManyToManyField with string class names #1802
Comments
Lazy references are supported if you use the format |
Ah OK yes that's the difference between this module and the ManyToManyFields in our other apps. Just to clarify and for the next person that finds this, implicit app name is supported by ForeignKey and OneToOneField, but not ManyToManyField? As I don't see issues with those relationships. |
Same here. I only have this error since I updated mypy from [Edit] Seems closely related to this #1719 change |
I have a related_field: models.ManyToManyField[ToModel, 'ThroughModel'] = models.ManyToManyField(to=ToModel, through='ThroughModel') Sorry, this is slightly unrelated: What's the correct type annotation for the through model when it is not a custom one? |
Short answer: There is no correct type annotation. Long answer: The plugin mimics Django's dynamic creation of the intermediate(through) model, ref: https://github.com/django/django/blob/40b3975e7d3e1464a733c69171ad7d38f8814280/django/db/models/fields/related.py#L1281-L1319 So you'll need a declaration identical to that, but once you type it out you might as well use what you type out explicitly as a |
typeddjango/django-stubs#1802 Signed-off-by: Anders Kaseorg <[email protected]>
any resolution on this issue? |
typeddjango/django-stubs#1802 Signed-off-by: Anders Kaseorg <[email protected]>
typeddjango/django-stubs#1802 Signed-off-by: Anders Kaseorg <[email protected]>
typeddjango/django-stubs#1802 Signed-off-by: Anders Kaseorg <[email protected]>
Hello @masarliev , try with
|
not working. |
Did you do the same for |
django-stubs requires "app.model" instead of just "model" for ManyToManyField lazy model references. See typeddjango/django-stubs#1802
* chore: Unpin django-stubs / update mypy * test: Use "app.model" for ManyToManyField django-stubs requires "app.model" instead of just "model" for ManyToManyField lazy model references. See typeddjango/django-stubs#1802
Per typeddjango/django-stubs#1802 (comment), `ManyToManyField`s with lazy (string) models but no app label don't work. In this case, we can just use the class directly, not the string, so do that.
In newer mypy[1][2], `ManyToManyField`s with lazy (string) models but no app label don't work. For the `to` model, we can just use `Person` directly (and do on the line above), so do that. For the `through` model, add the app label -- Django doesn't care, and it makes mypy happy. Also, in mypy, well-structured wildcard patterns match without a submodule too, so simplify some config. [1] typeddjango/django-stubs#1802 (comment), [2] typeddjango/django-stubs#1719 (comment) [3] https://mypy.readthedocs.io/en/stable/config_file.html#config-file-format
Bug report
What's wrong
I'm looking at updating from 4.2.4 to 4.2.6, and am seeing some new errors that I'm not sure how to handle. They're all in one single models.py file, are all ManyToManyFields, and each of those contain the related class name as a string not as the class itself. For the ones I can convert from strings to classes without having to re-order models, this fixes the error. I don't see this particular issue discussed in a any release notes / issues / MRs hence I'm asking here. Is this expected with this update, and what should I be doing about this error? The fact that I only see this in one file is suspicious to me, but we have very few other ManyToManyFields so I can't narrow it down to something breaking in this file just yet.
models.py
destinations = models.ManyToManyField('Destination')
now leads to:
error: Need type annotation for "destinations" [var-annotated]
and when I change the model to
destinations = models.ManyToManyField(Destination)
the error is gone.
How is that should be
Not sure tbh. If strings are not supported then I'd expect that to be mentioned somewhere (if it is, apologies!)
System information
python
version: 3.11django
version: 4.2.6mypy
version: 1.6.1django-stubs
version: 4.2.6django-stubs-ext
version: 4.2.5The text was updated successfully, but these errors were encountered: