Skip to content

[ty] Use partial-order-friendly representation of typevar constraints#20306

Merged
dcreager merged 17 commits intomainfrom
dcreager/incomparable
Sep 9, 2025
Merged

[ty] Use partial-order-friendly representation of typevar constraints#20306
dcreager merged 17 commits intomainfrom
dcreager/incomparable

Conversation

@dcreager
Copy link
Member

@dcreager dcreager commented Sep 8, 2025

The constraint representation that we added in #19997 was subtly wrong, in that it didn't correctly model that type assignability is a partial order — it's possible for two types to be incomparable, with neither a subtype of the other. That means the negation of a constraint like T ≤ t (typevar T must be a subtype of t) is not t < T, but rather t < T ∨ T ≁ t (using ≁ to mean "not comparable to").

That means we need to update our constraint representation to be an enum, so that we can track both range constraints (upper/lower bound on the typevar), and these new incomparable constraints.

Since we need an enum now, that also lets us simplify how we were modeling range constraints. Before, we let the lower/upper bounds be either open (<) or closed (≤). Now, range constraints are always closed, and we add a third kind of constraint for not equivalent (≠). We can translate an open upper bound T < t into T ≤ t ∧ T ≠ t.

We already had the logic for doing adding clauses to a set by doing a pairwise simplification. We copy that over to where we add constraints to a clause. To calculate the intersection or union of two constraints, the new enum representation makes it easy to break down all of the possibilities into a small number of cases: intersect range with range, intersect range with not-equivalent, etc. I've done the math here to show that the simplifications for each of these cases is correct.

@dcreager dcreager added internal An internal refactor or improvement ty Multi-file analysis & type inference labels Sep 8, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Sep 8, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@github-actions
Copy link
Contributor

github-actions bot commented Sep 8, 2025

mypy_primer results

No ecosystem changes detected ✅
No memory usage changes detected ✅

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.

Looks reasonable!

I'm sure if I tell you that all this math seems well-suited to a nice unit test suite, it won't be the first you've thought of it ;)

@AlexWaygood AlexWaygood removed their request for review September 8, 2025 22:12
@dcreager
Copy link
Member Author

dcreager commented Sep 9, 2025

I'm sure if I tell you that all this math seems well-suited to a nice unit test suite, it won't be the first you've thought of it ;)

I'm tackling this in a separate PR, #20319

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.

🚀

@dcreager dcreager merged commit ed06fb5 into main Sep 9, 2025
38 checks passed
@dcreager dcreager deleted the dcreager/incomparable branch September 9, 2025 19:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal An internal refactor or improvement ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants