Skip to content

Further improve match statement narrowing against unions - #20744

Merged
hauntsaninja merged 3 commits into
python:masterfrom
hauntsaninja:matchtup
Feb 11, 2026
Merged

Further improve match statement narrowing against unions#20744
hauntsaninja merged 3 commits into
python:masterfrom
hauntsaninja:matchtup

Conversation

@hauntsaninja

@hauntsaninjahauntsaninja commented Feb 5, 2026

Copy link
Copy Markdown
Collaborator

I realised there is a simpler and more complete approach than the one taken in #19600.
This adds the new "Step 2" to the original code. Best way to review the diff is probably to check it out and review it squashed with #19600 , but really the net logic change is just "Step 2"

Fixes#15190
Fixes#17549
Fixes#17600
Mostly fixes#18039
Helps with things in #19081

I realised there is a simpler and more powerful approach than the one
taken in python#19600. This basically adds the new "Step 2" to the original
code.
@github-actions

This comment has been minimized.

@hauntsaninja
hauntsaninja marked this pull request as draft February 5, 2026 08:24
@github-actions

This comment has been minimized.

@hauntsaninja
hauntsaninja marked this pull request as ready for review February 5, 2026 21:40
@hauntsaninjahauntsaninja mentioned this pull request Feb 5, 2026

@JukkaLJukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, looks good -- just one potential code path that seems redundant.

Comment threadmypy/checkpattern.py Outdated
elif isinstance(current_type, AnyType):
inner_type: Type = AnyType(TypeOfAny.from_another_any, current_type)
inner_types = [inner_type] * len(o.patterns)
elif isinstance(current_type, TupleType):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Second check against TupleType that can never be taken?

@github-actions

Copy link
Copy Markdown
Contributor

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

@hauntsaninja
hauntsaninja merged commit 9752e19 into python:masterFeb 11, 2026
23 checks passed
@hauntsaninja
hauntsaninja deleted the matchtup branch February 11, 2026 19:39
hauntsaninja added a commit that referenced this pull request Mar 4, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants

@hauntsaninja@JukkaL