Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[ty] Recognize exhaustive matches over finite tuples#26132

Merged
charliermarsh merged 2 commits into
mainfrom
charlie/fix-bool-tuple-match-exhaustiveness
Jun 23, 2026
Merged

[ty] Recognize exhaustive matches over finite tuples#26132
charliermarsh merged 2 commits into
mainfrom
charlie/fix-bool-tuple-match-exhaustiveness

Conversation

@charliermarsh

Copy link
Copy Markdown
Member

Summary

Prior to this change, we could recognize exhaustive match statements when a fixed tuple's finite combinations were written as an explicit union, but not when those alternatives were hidden inside the tuple element types:

def f(b_1: bool, b_2: bool) -> int:
    match (b_1, b_2):
        case (True, True):
            return 0
        case (False, _):
            return 1
        case (_, False):
            return 2

We now share the existing bounded type-expansion logic used during overload resolution and use the expanded union when computing match reachability. For tuple[bool, bool], this lets each preceding case subtract the combinations it covers until no alternatives remain.

The expanded type is used only for reachability, so subject and capture inference remain unchanged. Tuple products remain capped at 64 alternatives; larger products keep the existing conservative behavior. This also resolves the existing tuple[int | str] sequence-pattern exhaustiveness TODO.

Closes astral-sh/ty#3807.

@astral-sh-bot astral-sh-bot Bot added the ty Multi-file analysis & type inference label Jun 18, 2026
@astral-sh-bot

astral-sh-bot Bot commented Jun 18, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 94.37%. The percentage of expected errors that received a diagnostic held steady at 89.00%. The number of fully passing files held steady at 94/134.

@astral-sh-bot

astral-sh-bot Bot commented Jun 18, 2026

Copy link
Copy Markdown

Memory usage report

Summary

Project Old New Diff Outcome
flake8 31.35MB 31.36MB +0.03% (10.71kB)
trio 78.61MB 78.61MB +0.00% (1.01kB)
sphinx 195.80MB 195.80MB -0.00% (4.51kB) ⬇️
prefect 524.31MB 524.30MB -0.00% (11.80kB) ⬇️

Significant changes

Click to expand detailed breakdown

flake8

Name Old New Diff Outcome
parsed_module 9.77MB 9.78MB +0.11% (10.71kB)

trio

Name Old New Diff Outcome
parsed_module 15.04MB 15.04MB +0.01% (1.01kB)

sphinx

Name Old New Diff Outcome
parsed_module 18.37MB 18.37MB -0.02% (4.53kB) ⬇️
analyze_pattern_predicate 2.29kB 2.31kB +1.03% (24.00B) ⬇️

prefect

Name Old New Diff Outcome
parsed_module 19.36MB 19.35MB -0.06% (11.80kB) ⬇️

@astral-sh-bot

astral-sh-bot Bot commented Jun 18, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

Lint rule Added Removed Changed
type-assertion-failure 0 1 0
Total 0 1 0

Raw diff:

mitmproxy (https://github.com/mitmproxy/mitmproxy)
- test/mitmproxy/addons/test_dns_resolver.py:155:17 error[type-assertion-failure] Type `@Todo` is not equivalent to `Never`

Full report with detailed diff (timing results)

@codspeed-hq

codspeed-hq Bot commented Jun 18, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 6.1%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 2 regressed benchmarks
✅ 129 untouched benchmarks
⏩ 4 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation ty_micro[large_union_narrowing] 235.8 ms 256.7 ms -8.11%
Memory ty_micro[large_union_narrowing] 13.9 MB 14.5 MB -4.04%

Tip

Investigate this regression with the CodSpeed MCP and your agent.


Comparing charlie/fix-bool-tuple-match-exhaustiveness (acad27b) with main (5dfa9ad)

Open in CodSpeed

Footnotes

  1. 4 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@astral-sh-bot

astral-sh-bot Bot commented Jun 18, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@charliermarsh charliermarsh marked this pull request as ready for review June 18, 2026 16:36
@charliermarsh charliermarsh requested a review from a team as a code owner June 18, 2026 16:36
@astral-sh-bot astral-sh-bot Bot requested a review from dcreager June 18, 2026 16:36
@charliermarsh

Copy link
Copy Markdown
Member Author

Some performance hit here is expected.

@charliermarsh charliermarsh force-pushed the charlie/fix-bool-tuple-match-exhaustiveness branch from 9a5b3b1 to acad27b Compare June 18, 2026 22:58
@charliermarsh charliermarsh merged commit 4e54b8b into main Jun 23, 2026
59 of 60 checks passed
@charliermarsh charliermarsh deleted the charlie/fix-bool-tuple-match-exhaustiveness branch June 23, 2026 00:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False positive: exhaustive match over bool tuple wrongly reported as implicitly returning None

1 participant