-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Recombine complete union of enum literals into original type (#9063) #9097
Conversation
74be4c5
to
9ce8a55
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.
Awesome feature! 👍
@JukkaL Is there any chance I could receive a review on this PR? |
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.
Thanks for working on this! I like this cleaner message output. Left a few minor comments and/or uncertainties.
test-data/unit/check-enum.test
Outdated
@@ -632,6 +632,7 @@ elif x is Foo.C: | |||
reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.C]' | |||
else: | |||
reveal_type(x) # No output here: this branch is unreachable | |||
reveal_type(y) # N: Revealed type is '__main__.Foo' |
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.
This doesn't make much sense to check the type of y
here
test-data/unit/check-enum.test
Outdated
@@ -641,6 +642,7 @@ elif Foo.C is x: | |||
reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.C]' | |||
else: | |||
reveal_type(x) # No output here: this branch is unreachable | |||
reveal_type(y) # N: Revealed type is '__main__.Foo' |
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.
same as above
test-data/unit/check-enum.test
Outdated
@@ -877,7 +889,7 @@ def func(x: Union[int, None, Empty] = _empty) -> int: | |||
# E: Unsupported left operand type for + ("Empty") \ | |||
# N: Left operand is of type "Union[int, None, Empty]" | |||
if x is _empty: | |||
reveal_type(x) # N: Revealed type is 'Literal[__main__.Empty.token]' | |||
reveal_type(x) # N: Revealed type is '__main__.Empty' |
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.
I'm not sure whether should we display the original type when there's only one enum item. Displaying the only enum item seems like an acceptable result here.
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.
That's a fair point, I can add a special case in the logic for 1-item enums 👍
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.
Cool!
9ce8a55
to
c172fdd
Compare
c172fdd
to
a624117
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.
LGTM!
For example, consider this enum: ``` class E(Enum): A = 1 B = 1 ``` This PR makes `E` compatible with `Literal[E.A, E.B]`. Also fix mutation of the argument list in `try_contracting_literals_in_union`. This fixes some regressions introduced in #9097.
For example, consider this enum: ``` class E(Enum): A = 1 B = 1 ``` This PR makes `E` compatible with `Literal[E.A, E.B]`. Also fix mutation of the argument list in `try_contracting_literals_in_union`. This fixes some regressions introduced in #9097.
Closes #9063