-
-
Notifications
You must be signed in to change notification settings - Fork 485
Fixes to choices support in plugin #2646
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
Fixes to choices support in plugin #2646
Conversation
)" This reverts commit 7b4145d.
Without these we cannot import from other modules properly which is useful for testing the behaviour of resolving types in the plugin.
Add some comments to explain what is being tested in each block of `assert_type()` calls.
The resolution of the attribute type was too naive and didn't handle where a module is imported and the choices type is accessed as an attribute from that module. Eliminates 11,087 errors in a large repository that were raised when upgrading from django-stubs v5.1.3 to v5.2.0 which included the changes in a3f2a55. Co-authored-by: Stephen Moore <[email protected]>
The resolution of the attribute type was too naive and didn't handle where a choices type is aliased and then used via that alias. Eliminates 53 errors in a large repository that were raised when upgrading from django-stubs v5.1.3 to v5.2.0 which included the changes in a3f2a55. Co-authored-by: Stephen Moore <[email protected]>
The resolution of the attribute type was too naive and didn't handle where a choices type is aliased by type and then used via that alias. Eliminates 37 errors in a large repository that were raised when upgrading from django-stubs v5.1.3 to v5.2.0 which included the changes in a3f2a55. Co-authored-by: Stephen Moore <[email protected]>
The resolution of the attribute type was too naive and didn't handle where a choices type is bound in a type variable. Eliminates 10 errors in a large repository that were raised when upgrading from django-stubs v5.1.3 to v5.2.0 which included the changes in a3f2a55. Co-authored-by: Stephen Moore <[email protected]>
The resolution of the attribute type was too naive and didn't handle where a choices type is decomposed to a union of enum literals when a comparison to a member is made in a branch. Eliminates 33 errors in a large repository that were raised when upgrading from django-stubs v5.1.3 to v5.2.0 which included the changes in a3f2a55. Co-authored-by: Stephen Moore <[email protected]>
The resolution of the attribute type was too naive and didn't handle where a choices type overrides a property, e.g. label, and makes use of `super()`. Eliminates 1 error in a large repository that was raised when upgrading from django-stubs v5.1.3 to v5.2.0 which included the changes in a3f2a55.
The resolution of the attribute type was too naive and didn't handle where a choices type is decomposed to a single enum literal when a comparison to a member is made in a branch. Eliminates 1 error in a large repository that was raised when upgrading from django-stubs v5.1.3 to v5.2.0 which included the changes in a3f2a55.
Ahead of a future commit that alters handling of labels, perform this simplification.
Because of the change to use `django.utils.functional._StrOrPromise` for labels to improve accuracy, it can be a little painful - especially in a very large codebase where translations / lazy strings are not used. This change keeps the stubs unchanged for correctness, e.g. in pyright for where there is no alternative, but tweaks the mypy plugin to handle introspecting the members to determine whether there are no lazy strings such that the types can be narrowed. Eliminates 1,166 errors in a large repository that were raised when upgrading from django-stubs v5.1.3 to v5.2.0 which included the changes in a3f2a55.
This case is a little complex as we have a mix of choices types. In theory we could handle this, but it would require more rework to handle multiple types. For now, just make no changes and return the original type. Eliminates 11 errors in a large repository that were raised when upgrading from django-stubs v5.1.3 to v5.2.0 which included the changes in a3f2a55.
0102d08
to
ddd6a2b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow! That's a lot of changes :)
I would like to see some second opinion from @adamchainz or @flaeppe
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super thanks for making these changes! I love the way you've split it up, this is great :)
Currently mypy v1.13 is the minimum supported version, but the fix made to the choices plugin code in typeddjango#2646 made use of `TypeInfo.enum_members` which was introduced in v1.14. This commit adds a backport of the version introduced in v1.14 which can be removed when `django-stubs` bumps its minimum version.
Currently mypy v1.13 is the minimum supported version, but the fix made to the choices plugin code in #2646 made use of `TypeInfo.enum_members` which was introduced in v1.14. This commit adds a backport of the version introduced in v1.14 which can be removed when `django-stubs` bumps its minimum version.
I have made things!
So I was far too naive in #2582 🤦🏻
I've run this on a very large repository and this now fixes all of the
Unable to resolve type of ... property
errors I encountered. I also made a change to narrow the label type from_StrOrPromise
tostr
where all members labels and__empty__
are not using lazy strings. Although the changes to use_StrOrPromise
are correct according to the implementation, it can be annoying to fix up in large projects that are only using plain strings for labels. This change to narrow the type will vastly reduce the number of errors raised and make upgrading less painful.Related issues
Unable to resolve type of choices property
for.value
on Choices #2643.value
of a Choices variable #2644