Skip to content
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

Make enum values final - fixes #11919 #11962

Merged
merged 1 commit into from Jan 10, 2022

Conversation

joey-laminar
Copy link
Contributor

Description

Fixes #11919

This 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.

Test Plan

Added a test and fixed tests that were broken by this feature

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.
Copy link
Collaborator

@97littleleaf11 97littleleaf11 left a comment

Choose a reason for hiding this comment

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

lgtm

@97littleleaf11
Copy link
Collaborator

btw, could you please add the test case you mentioned in comment?

@97littleleaf11
Copy link
Collaborator

cc @sobolevn

@joey-laminar
Copy link
Contributor Author

btw, could you please add the test case you mentioned in comment?

I was referring to testEnumLiteralValues that's already in the PR

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.

Great! Thank you!

@JelleZijlstra JelleZijlstra merged commit 48d810d into python:master Jan 10, 2022
@joey-laminar joey-laminar deleted the issue-11919 branch January 10, 2022 15:09
@hauntsaninja
Copy link
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.
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.

Make enum values final
5 participants