[ty] Fix negation upper bounds in constraint sets#21897
Merged
Conversation
Diagnostic diff on typing conformance testsNo changes detected when running ty on typing conformance tests ✅ |
|
sharkdp
approved these changes
Dec 10, 2025
Comment on lines
+214
to
+216
| negated_type = ConstraintSet.range(Never, T, Not[int]) | ||
| negated_constraint = ~ConstraintSet.range(Never, T, int) | ||
| static_assert(negated_type != negated_constraint) |
| result = result.and( | ||
| db, | ||
| ConstrainedTypeVar::new_node(db, typevar, lower, upper_element).negate(db), | ||
| ConstrainedTypeVar::new_node(db, typevar, lower, upper_element.negate(db)), |
dcreager
added a commit
that referenced
this pull request
Dec 10, 2025
…-cycle * origin/main: [ty] Support implicit type of `cls` in signatures (#21771) [ty] add `SyntheticTypedDictType` and implement `normalized` and `is_equivalent_to` (#21784) [ty] Fix disjointness checks with type-of `@final` classes (#21770) [ty] Fix negation upper bounds in constraint sets (#21897)
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.
This fixes the logic error that @sharkdp found in the constraint set upper bound normalization logic I introduced in #21871.
I had originally claimed that
(T ≤ α & ~β)should simplify into(T ≤ α) ∧ ¬(T ≤ β). But that also suggests thatT ≤ ~βshould simplify to¬(T ≤ β)on its own, and that's not correct.The correct simplification is that
~αis an "atomic" type, not an "intersection" for the purposes of our upper bound simplifcation. So(T ≤ α & ~β)should simplify to(T ≤ α) ∧ (T ≤ ~β). That is, break apart the elements of a (proper) intersection, regardless of whether each element is negated or not.This PR fixes the logic, adds a test case, and updates the comments to be hopefully more clear and accurate.