Repository navigation
Conversation
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
chrikrah
approved these changes
Oct 29, 2026
chrikrah
left a comment
There was a problem hiding this comment.
@nlsfnr nothing blocks this from my side. Like #20149, a constrained TypeVar still reports: expand_typevars() checks copies, so the original MatchStmt keeps is_exhaustive=False.
# git archive fee4b07, Python 3.12, mypy 2.5.0+dev resolves into the export
$ python -m pytest -q -n 4 mypy/test/testcheck.py -k "PossiblyUndefined or Match"
264 passed, 1 skipped # head fee4b07
$ python -m pytest -q mypy/test/testcheck.py -k testExhaustiveMatchNoFallThrough
1 failed # partially_defined.py skip_branch() removed: lines 15, 23, 31 unexpectedly report
$ python -m mypy --no-incremental --enable-error-code possibly-undefined t.py
t.py:11: error: Name "y" may be undefined [possibly-undefined] # f(x: T): match, case int() / case str()
t.py:26: error: Name "y" may be undefined [possibly-undefined] # h(x: T): if isinstance int / elif str
# g(x: int | str), the same match as f: clean
# not run: mypyc build, self-check
non-blocking: a separate issue?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fixes #13926 and #16992 (duplicates), i.e. false possibly-undefined errors after exhaustive match statements.
This PR applies the same approach as #20149, which did the same for if statements.
Adds a new attribute
MatchStmt.is_exhaustive, which is set by the type checker when the remaining subject type is empty after the last case. This allowspartially_defined.pyto skip the implicit fall-through branch, which it already does for trailing catch-all cases.