8000 Fix recently added enum value type prediction by taljeth · Pull Request #10057 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Fix recently added enum value type prediction #10057

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 1 commit into from
Mar 2, 2021

Conversation

taljeth
Copy link
Contributor
@taljeth taljeth commented Feb 10, 2021

Description

In #9443, some code was added to predict the type of enum values where it is not explicitly when all enum members have the same type.

However, it didn't consider that subclasses of Enum that have a custom __new__ implementation may use any type for the enum value (typically it would use only one of their parameters instead of a whole tuple that is specified in the definition of the member). Fix this by avoiding to guess the enum value type in classes that implement __new__.

In addition, the added code was buggy in that it didn't only consider class attributes as enum members, but also instance attributes assigned to self.* in __init__. Fix this by ignoring implicit nodes when checking the enum members.

Fixes #10000.

Test Plan

Apart from manually testing the example from #10000, new unit tests are included for the changed behaviour that fail on master (when running runtests.py) and pass with this fix applied. Unchanged behaviour is already tested by existing unit tests.

In python#9443, some code was added to predict the type of enum values where
it is not explicitly when all enum members have the same type.

However, it didn't consider that subclasses of Enum that have a custom
__new__ implementation may use any type for the enum value (typically it
would use only one of their parameters instead of a whole tuple that is
specified in the definition of the member). Fix this by avoiding to
guess the enum value type in classes that implement __new__.

In addition, the added code was buggy in that it didn't only consider
class attributes as enum members, but also instance attributes assigned
to self.* in __init__. Fix this by ignoring implicit nodes when checking
the enum members.

Fixes python#10000.
Copy link
Collaborator
@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great. Thank you for fixing this and following up! (And congratulations on the lucky #10000)

@hauntsaninja hauntsaninja merged commit 0aa2caf into python:master Mar 2, 2021
JukkaL added a commit that referenced this pull request May 4, 2021
If the enum value initializer has a NewType type, it should be
reflected in the type of the `value` attribute. It was broken because
the special casing for `__new__` introduced in #10057 didn't consider
the `__new__` in `IntEnum` as special.

Fixes #10411.
JukkaL added a commit that referenced this pull request May 4, 2021
If the enum value initializer has a NewType type, it should be
reflected in the type of the `value` attribute. It was broken because
the special casing for `__new__` introduced in #10057 didn't consider
the `__new__` in `IntEnum` as special.

Fixes #10411.
embray added a commit to embray/mypy that referenced this pull request Oct 24, 2023
user-defined data type class (with __new__).

This fixes a regression introduced by python#10057 to fix python#10000.  The
`not ti.fullname.startswith("builtins.") clause seemed to be intended
to catch enums with a built-in data type like int or bytes, but this is
overly broad.  It should allow any type so long as it is not itself an
enum.Enum subclass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrong enum value type inferred for members defined in a subclass (regression)
2 participants
0