Skip to content

Fix false unreachable warnings for sequence patterns over tuple unions - #22517

Closed
Sudhakar7J wants to merge 1 commit into
python:masterfrom
Sudhakar7J:fix/match-sequence-union-unreachable
Closed

Sudhakar7J wants to merge 1 commit into
python:masterfrom
Sudhakar7J:fix/match-sequence-union-unreachable

Conversation

@Sudhakar7J

Copy link
Copy Markdown

When a sequence pattern is checked against a union of tuple types, PatternChecker.visit_sequence_pattern evaluates the pattern against each union alternative. For alternatives that do not match, conditional_types_with_intersection can emit impossible-intersection diagnostics (reported as [unreachable] with --warn-unreachable) even though the overall case is reachable via other union members.

This suppresses diagnostics while matching individual union alternatives in the sequence-pattern decomposition path, preserving the computed match/rest/capture types.

Fixes #22150

Test Plan

  • Added regression test: testMatchSequencePatternUnionTupleNoUnreachable in test-data/unit/check-python310.test (--warn-unreachable, fixtures/tuple.pyi) covering tuple[None, int] | tuple[int, None] | tuple[int, int] with (None, int(y)), (int(x), None), (int(x), int(y)).
  • Verified the original repro from False positive unreachable for None sub-pattern when matching a union of tuples #22150 reports Success: no issues found with --warn-unreachable --strict.
  • Targeted tests (Windows, Python 3.14.10 via uv run):
    • pytest -n0 -k testMatchSequencePatternUnionTupleNoUnreachable mypy/test/testcheck.py — 1 passed
    • pytest -n0 -k "MatchSequencePattern or testMatchSequence" mypy/test/testcheck.py — 34 passed, 8359 deselected

…atching

When matching sequence patterns against union types, avoid reporting impossible-intersection diagnostics for individual non-matching union alternatives.

Fixes python#22150
@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. ✅

@Sudhakar7J

Copy link
Copy Markdown
Author

CI update: all checks have completed successfully except Test suite with py314t-ubuntu, mypyc-compiled (failed on mypyc/test/test_run.py::TestRun::run-exceptions.test::testTracebackCodeObjectReuse: assert tbs[0].tb_frame.f_code is tbs[1].tb_frame.f_code in test_traceback_entries).

This appears unrelated to this PR, which only adjusts per-alternative error filtering during sequence-pattern union matching in mypy/checkpattern.py and adds a check-python310 regression test for #22150. Happy to rebase or take any follow-up steps if maintainers think this is related.

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.

False positive unreachable for None sub-pattern when matching a union of tuples

2 participants