-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Make enum values final - fixes #11919 #11962
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
Conversation
This fix allows using enum values in place of literals. For example: ``` def is_a(a: Literal["a"]): return None class MyStr(Enum): a = "a" b = "b" is_a(MyStr.a.value) ``` Currently this fails with ``` error: Argument 1 to "is_a" has incompatible type "str"; expected "Literal['a']" ``` The fix itself is simple - the special casing of final status for enums was being called too late to have an effect. Moving the function call up solves the problem.
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
btw, could you please add the test case you mentioned in comment? |
cc @sobolevn |
I was referring to |
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.
Great! Thank you!
Hooray, this is a nice change! |
This fix allows using enum values in place of literals. For example: ``` def is_a(a: Literal["a"]): return None class MyStr(Enum): a = "a" b = "b" is_a(MyStr.a.value) ``` Currently this fails with ``` error: Argument 1 to "is_a" has incompatible type "str"; expected "Literal['a']" ``` The fix itself is simple - the special casing of final status for enums was being called too late to have an effect. Moving the function call up solves the problem. Fixes python#11919
A few bugs in type operations were exposed by #11962. This fixes them. Fix #12051, but other use cases are affected as well. First, when we erase last known values in an union with multiple Instance types, make sure that the resulting union doesn't have duplicate erased types. The duplicate items weren't incorrect as such, but they could cause overly complex error messages and potentially slow type checking performance. Second, make the join of a union type and Any commutative. Previously the result dependend on the order of the operands, which was clearly incorrect.
A few bugs in type operations were exposed by #11962. This fixes them. Fix #12051, but other use cases are affected as well. First, when we erase last known values in an union with multiple Instance types, make sure that the resulting union doesn't have duplicate erased types. The duplicate items weren't incorrect as such, but they could cause overly complex error messages and potentially slow type checking performance. Second, make the join of a union type and Any commutative. Previously the result dependend on the order of the operands, which was clearly incorrect.
#11962 can generate large unions with many Instance types with last_known_value set. This caused our union simplification algorithm to be extremely slow, as it hit an O(n**2) code path. We already had a fast code path for unions of regular literal types. This generalizes it for unions containing Instance types with last known values (which behave similarly to literals in a literal type context). Also fix a union simplification bug that I encountered while writing tests for this change. Work on #12408.
Description
Fixes #11919
This allows using enum values in place of literals.
For example:
Currently this fails with
The fix itself is simple - the special casing of final status for enums was being
called too late to have an effect. Moving the function call up solves
the problem.
Test Plan
Added a test and fixed tests that were broken by this feature