Skip to content

Flatten union before contracting literals when checking subtyping #18898

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

Merged

Conversation

sterliakov
Copy link
Collaborator

Fixes #18896

Copy link
Contributor

github-actions bot commented Apr 7, 2025

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

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! Let's wait a bit for some extra feedback.

Copy link
Collaborator

@A5rocks A5rocks left a comment

Choose a reason for hiding this comment

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

Makes sense to me. One minor comment.

@@ -1069,6 +1069,8 @@ class Status(Enum):
def try_contracting_literals_in_union(types: Sequence[Type]) -> list[ProperType]:
"""Contracts any literal types back into a sum type if possible.

Requires a flattened union and does not descend into children.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe it's better to add an assert or transparently handle this correctly? Both usages of this in mypy seem fine but I think preventing misuse is good.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'd rather not - this function is called twice in the whole codebase, one of them in a rather hot function (subtyping check). While failure to pass a flattened union may result in some false positives, it isn't dangerous and can't cause any errors down the road.

@sobolevn sobolevn merged commit c3ed5e0 into python:master Apr 9, 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.

Inconsistent assignability of enum literal unions vs enum
3 participants