-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Update "constraint implication" relation to work on constraints between two typevars #21068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
fdea956 to
c15f58c
Compare
c15f58c to
eed87d1
Compare
Diagnostic diff on typing conformance testsNo changes detected when running ty on typing conformance tests ✅ |
|
5e98232 to
e8fd554
Compare
7227293 to
64402d1
Compare
CodSpeed Performance ReportMerging #21068 will not alter performanceComparing Summary
Footnotes
|
| static_assert(not given_int.implies_subtype_of(T, str)) | ||
|
|
||
| # If [T ≤ U ∧ U ≤ int], then [T ≤ int] must be true as well. | ||
| given_int = ConstraintSet.range(Never, T, U) & ConstraintSet.range(Never, U, int) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More an understand question for me. What's the difference between Never and passing U?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
range(Never, T, U) is Never ≤ T ≤ U, which simplifies to T ≤ U since every type is a supertype of Never.
range(U, T, U) is U ≤ T ≤ U, which simplifies to T = U.
So the first one is "T must be a subtype of U", and the second one is "T must be exactly the same type as U"
MichaReiser
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense to me
* origin/main: (21 commits) [ty] Update "constraint implication" relation to work on constraints between two typevars (#21068) [`flake8-type-checking`] Fix `TC003` false positive with `future-annotations` (#21125) [ty] Fix lookup of `__new__` on instances (#21147) Fix syntax error false positive on nested alternative patterns (#21104) [`pyupgrade`] Fix false positive for `TypeVar` with default on Python <3.13 (`UP046`,`UP047`) (#21045) [ty] Reachability and narrowing for enum methods (#21130) [ty] Use `range` instead of custom `IntIterable` (#21138) [`ruff`] Add support for additional eager conversion patterns (`RUF065`) (#20657) [`ruff-ecosystem`] Fix CLI crash on Python 3.14 (#21092) [ty] Infer type of `self` for decorated methods and properties (#21123) [`flake8-bandit`] Fix correct example for `S308` (#21128) [ty] Dont provide goto definition for definitions which are not reexported in builtins (#21127) [`airflow`] warning `airflow....DAG.create_dagrun` has been removed (`AIR301`) (#21093) [ty] follow the breaking API changes made in salsa-rs/salsa#1015 (#21117) [ty] Rename `Type::into_nominal_instance` (#21124) [ty] Filter out "unimported" from the current module [ty] Add evaluation test for auto-import including symbols in current module [ty] Refactor `ty_ide` completion tests [ty] Render `import <...>` in completions when "label details" isn't supported [`refurb`] Preserve digit separators in `Decimal` constructor (`FURB157`) (#20588) ...
It's possible for a constraint to mention two typevars. For instance, in the body of
the baseline constraint set would be
(T ≤ S) ∧ (S ≤ int). That is,Smust specialize to some subtype ofint, andTmust specialize to a subtype of the type thatSspecializes to.This PR updates the new "constraint implication" relationship from #21010 to work on these kinds of constraint sets. For instance, in the example above, we should be able to see that
T ≤ intmust always hold:This did not require major changes to the implementation of
implies_subtype_of. That method already relies on how oursimplifyanddomainmethods expand a constraint set to include the transitive closure of the constraints that it mentions, and to mark certain combinations of constraints as impossible. Previously, that transitive closure logic only looked at pairs of constraints that constrain the same typevar. (For instance, to notice that(T ≤ bool) ∧ ¬(T ≤ int)is impossible.)Now we also look at pairs of constraints that constraint different typevars, if one of the constraints is bound by the other — that is, pairs of the form
T ≤ SandS ≤ something, orS ≤ Tandsomething ≤ S. In those cases, transitivity lets us add a new derived constraint thatT ≤ somethingorsomething ≤ T, respectively. Having done that, our existingimplies_subtype_oflogic finds and takes into account that derived constraint.