Skip to content

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

Merged
merged 13 commits into from
Apr 30, 2025

Conversation

ngnpope
Copy link
Contributor

@ngnpope ngnpope commented Apr 29, 2025

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 to str 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

ngnpope and others added 11 commits April 29, 2025 09:12
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.
@ngnpope ngnpope changed the title Tweaks to choices plugin Fixes to choices support in plugin Apr 29, 2025
ngnpope added 2 commits April 29, 2025 18:03
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.
@ngnpope ngnpope force-pushed the tweaks-to-choices-plugin branch from 0102d08 to ddd6a2b Compare April 29, 2025 17:04
@ngnpope ngnpope marked this pull request as ready for review April 29, 2025 17:10
Copy link
Member

@sobolevn sobolevn left a 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!

Copy link
Contributor

@delfick delfick left a 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 :)

@sobolevn sobolevn merged commit c37850b into typeddjango:master Apr 30, 2025
36 checks passed
@ngnpope ngnpope deleted the tweaks-to-choices-plugin branch April 30, 2025 06:02
ngnpope added a commit to ngnpope/django-stubs that referenced this pull request May 9, 2025
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.
sobolevn pushed a commit that referenced this pull request May 9, 2025
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants