Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Unexpected type inference changes related to enum value attributes #12051

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

Closed
JukkaL opened this issue Jan 24, 2022 · 0 comments · Fixed by #12068
Closed

Unexpected type inference changes related to enum value attributes #12051

JukkaL opened this issue Jan 24, 2022 · 0 comments · Fixed by #12068
Assignees
Labels
bug mypy got something wrong priority-0-high

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 24, 2022

In this example, the behavior changed in #11962:

from enum import IntEnum
from typing import Any

class C(IntEnum):
    X = 0
    Y = 1
    Z = 2

def f1(c: C) -> None:
    x = {'x': c.value}
    # Old: builtins.dict[builtins.str*, builtins.int*]
    # New: builtins.dict[builtins.str*, Union[builtins.int, builtins.int, builtins.int]]
    reveal_type(x)

def f2(c: C, a: Any) -> None:
    x = {'x': c.value, 'y': a}
    # Old: builtins.dict[builtins.str*, Any]
    # New: builtins.dict[builtins.str*, Union[Any, builtins.int, builtins.int, builtins.int]]
    reveal_type(x)

These seem like regressions. The duplicate int types in unions seem like an unrelated issue that #11962 exposed. In some cases it can result in unions with dozens of items, which look pretty confusing. I'm not sure yet whether the second change is an issue with #11962 or something else.

@JukkaL JukkaL added bug mypy got something wrong priority-0-high labels Jan 24, 2022
@JukkaL JukkaL self-assigned this Jan 24, 2022
JukkaL added a commit that referenced this issue 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 issue 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 issue Jan 25, 2022
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.

This is one of the fixes extracted from #12054. Since some of the
changes may cause regressions, it's better to split the PR.

Work on #12051.

Co-authored-by: Nikita Sobolev <[email protected]>
JukkaL added a commit that referenced this issue Jan 25, 2022
Make the join of a union type and Any commutative.
Previously the result dependend on the order of the operands,
which was clearly incorrect.

Fix #12051, but other use cases are affected as well.
JukkaL added a commit that referenced this issue Jan 25, 2022
Make the join of a union type and Any commutative.
Previously the result depended on the order of the operands,
which was clearly incorrect.

Fix #12051, but other use cases are affected as well.

This change was split off from #12054.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-0-high
Projects
None yet
1 participant