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

Skip to content

Fix two type inference bugs related to enum value attributes #12054

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
wants to merge 1 commit into from

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented 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.

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

LGTM 👍

@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

steam.py (https://github.com/Gobot1234/steam.py)
- steam/invite.py:74: error: Argument 1 to "append" of "list" has incompatible type "Union[User, SteamID]"; expected "User"  [arg-type]
+ steam/invite.py:74: error: Argument 1 to "append" of "list" has incompatible type "SteamID"; expected "User"  [arg-type]
- steam/state.py:650: error: Argument "participant" to "DMChannel" has incompatible type "Union[User, SteamID]"; expected "User"  [arg-type]
+ steam/state.py:650: error: Argument "participant" to "DMChannel" has incompatible type "SteamID"; expected "User"  [arg-type]
- steam/ext/commands/converters.py:352: error: Argument 2 to "convert" of "ClanConverter" has incompatible type "Optional[int]"; expected "str"  [arg-type]
+ steam/ext/commands/converters.py:352: error: Argument 2 to "convert" of "ClanConverter" has incompatible type "int"; expected "str"  [arg-type]

jax (https://github.com/google/jax)
- jax/_src/dispatch.py:609: error: Item Device? of "Optional[Device?]" has no attribute "id"  [union-attr]
- jax/_src/dispatch.py:609: error: Item "None" of "Optional[Device?]" has no attribute "id"  [union-attr]

pip (https://github.com/pypa/pip)
+ src/pip/_internal/resolution/resolvelib/requirements.py:49: error: Cannot determine type of "_ireq"
+ src/pip/_internal/resolution/resolvelib/requirements.py:54: error: Cannot determine type of "_ireq"
+ src/pip/_internal/resolution/resolvelib/requirements.py:59: error: Cannot determine type of "_ireq"
+ src/pip/_internal/resolution/resolvelib/requirements.py:60: error: Cannot determine type of "_ireq"
+ src/pip/_internal/resolution/resolvelib/requirements.py:64: error: Cannot determine type of "_extras"
+ src/pip/_internal/resolution/resolvelib/requirements.py:81: error: Cannot determine type of "_ireq"
+ src/pip/_internal/resolution/resolvelib/requirements.py:91: error: Cannot determine type of "_ireq"
+ src/pip/_internal/resolution/resolvelib/requirements.py:92: error: Cannot determine type of "_ireq"

xarray (https://github.com/pydata/xarray)
+ xarray/core/dataset.py:228: error: Incompatible types in assignment (expression has type "List[Hashable]", variable has type "Hashable")  [assignment]
+ xarray/core/dataset.py:228: note: Following member(s) of "List[Hashable]" have conflicts:
+ xarray/core/dataset.py:228: note:     __hash__: expected "Callable[[], int]", got "None"
+ xarray/core/dataset.py:235: error: "Hashable" has no attribute "__iter__" (not iterable)  [attr-defined]
+ xarray/core/dataset.py:260: error: Argument 1 to "len" has incompatible type "Hashable"; expected "Sized"  [arg-type]
+ xarray/core/dataset.py:261: error: Value of type "Hashable" is not indexable  [index]
+ xarray/core/dataset.py:264: error: "Hashable" has no attribute "__iter__" (not iterable)  [attr-defined]
+ xarray/core/dataset.py:279: error: Argument 1 to "extend" of "list" has incompatible type "Hashable"; expected "Iterable[Hashable]"  [arg-type]

@JukkaL
Copy link
Collaborator Author

JukkaL commented Jan 24, 2022

I'll need to investigate the mypy primer output.

JukkaL added a commit that referenced this pull request 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
Copy link
Collaborator Author

JukkaL commented Jan 25, 2022

Split and reworked into two PRs, #12064 and #12068.

@JukkaL JukkaL closed this Jan 25, 2022
JukkaL added a commit that referenced this pull request 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.
@97littleleaf11 97littleleaf11 deleted the fix-literal-stuff branch March 25, 2022 10:25
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.

Unexpected type inference changes related to enum value attributes
2 participants