KEMBAR78
Unwrap `type[Union[...]]` when solving typevar constraints by sterliakov · Pull Request #18266 · python/mypy · GitHub
Skip to content

Conversation

sterliakov
Copy link
Collaborator

Closes #18265, closes #12115.

type[A | B] is internally represented as type[A] | type[B], and this causes problems for a typevar solver. Prevent using meet in such cases by unwraping type[...] if both sides have such shape.

@github-actions

This comment has been minimized.

@sterliakov
Copy link
Collaborator Author

Primer diff is fine, it was a false positive.

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! I had a few comments but looks good overall.

@github-actions

This comment has been minimized.

@sterliakov
Copy link
Collaborator Author

Hm, and this new diff is weird. I'll try to figure out the reason for the colour diff - don't merge yet.

… be unwrapped, so type should be restored when passing it further
@sterliakov
Copy link
Collaborator Author

The upstream code has changed since, but I checked the previous version locally and confirmed the bug is gone. This should be ready for another review now.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Thanks for the updates!



def _unwrap_type_type(tp: TypeType | UnionType) -> ProperType:
"""Rewrite `type[A] | type[B]` as `type[A | B]`.
Copy link
Collaborator

Choose a reason for hiding this comment

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

The docstring seems incorrect or at least misleading. This converts type[A] | type[B] into A | B, right?

@github-actions
Copy link
Contributor

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

Tanjun (https://github.com/FasterSpeeding/Tanjun)
- tanjun/annotations.py:1064: error: Incompatible return value type (got "object", expected "User | Role | Snowflake | _T")  [return-value]

@JukkaL JukkaL merged commit 469b4e4 into python:master Jan 13, 2025
18 checks passed
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.

Generic type falling back to object - possibly related to type[ Union ]? Cannot deduce generic type of bound TypeVar through Type of Union

2 participants