Make enum values final - fixes #11919#11962
Merged
JelleZijlstra merged 1 commit intopython:masterfrom Jan 10, 2022
Merged
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.
Collaborator
|
btw, could you please add the test case you mentioned in comment? |
Collaborator
|
cc @sobolevn |
Contributor
Author
I was referring to |
Collaborator
|
Hooray, this is a nice change! |
tushar-deepsource
pushed a commit
to DeepSourceCorp/mypy
that referenced
this pull request
Jan 20, 2022
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
JukkaL
added a commit
that referenced
this pull request
Jan 24, 2022
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.
JukkaL
added a commit
that referenced
this pull request
Jan 25, 2022
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.
JukkaL
added a commit
that referenced
this pull request
Apr 5, 2022
#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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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