Skip to content

Conversation

@AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Oct 31, 2025

Summary

Fixes astral-sh/ty#1456.

This PR backs out the changes made in #21130 and replaces them with a more generalised implementation of exhaustiveness checking for matches over type variables with upper bounds or constraints. The approach in #21130 works for type variables with an enum upper bound, and could have been adapted to work for type variables with a bool upper bound. But it would not work for a type variable with a union upper bound, because of the fact that we distribute over a union (to create a union of intersections) when adding a union type to an intersection.

Instead, the approach this PR takes is to speculatively solve all type variables in the positive part of an intersection to their upper bound/constraints when finally building the intersection. If that speculative intersection simplifies to Never, so must the original intersection.

Test Plan

New mdtests added.

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Oct 31, 2025
@github-actions
Copy link
Contributor

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@github-actions
Copy link
Contributor

mypy_primer results

No ecosystem changes detected ✅
No memory usage changes detected ✅

@AlexWaygood AlexWaygood marked this pull request as ready for review October 31, 2025 19:41
Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

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

Very nice!

@AlexWaygood AlexWaygood merged commit a32d5b8 into main Oct 31, 2025
42 checks passed
@AlexWaygood AlexWaygood deleted the alex/neg-intersection2 branch October 31, 2025 20: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.

Simplify intersections with typevars whose upper bound is covered by a union of negative elements

2 participants