Skip to content

[match-case] Fix narrowing of class pattern with union-argument. #19473

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

randolf-scholz
Copy link
Contributor

I added factorization over UnionType inside the conditional_types function.
Then try_expanding_sum_type_to_union needed to move to the start of the function so that we expand current_type before attempting factorizating the union type.

I also reformatted the docstring for better legibility.

@randolf-scholz randolf-scholz changed the title inital commit [match-case] Fix narrowing of class pattern with union-argument. Jul 17, 2025
current_type = try_expanding_sum_type_to_union(current_type, enum_name)

current_type = get_proper_type(current_type)
if isinstance(current_type, UnionType) and (default == current_type):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The default == current_type was necessary, otherwise quite a few tests break.
Adding or (default is None) and passing default=None in the recursive call also seems to break stuff. Not entirely sure what's going on there.

Copy link
Contributor

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

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.

[match-case] class pattern narrowing with Union-argument different from equivalent isinstance call.
1 participant