Skip to content

Commit 4d2ee41

Browse files
authored
[ty] Move constraint set mdtest functions into ConstraintSet class (#21108)
We have several functions in `ty_extensions` for testing our constraint set implementation. This PR refactors those functions so that they are all methods of the `ConstraintSet` class, rather than being standalone top-level functions. 🎩 to @sharkdp for pointing out that `KnownBoundMethod` gives us what we need to implement that!
1 parent 7b959ef commit 4d2ee41

File tree

8 files changed

+408
-298
lines changed

8 files changed

+408
-298
lines changed

crates/ty_python_semantic/resources/mdtest/type_properties/constraints.md

Lines changed: 105 additions & 105 deletions
Large diffs are not rendered by default.

crates/ty_python_semantic/resources/mdtest/type_properties/is_subtype_of_given.md renamed to crates/ty_python_semantic/resources/mdtest/type_properties/implies_subtype_of.md

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
python-version = "3.12"
66
```
77

8-
This file tests the _constraint implication_ relationship between types, aka `is_subtype_of_given`,
8+
This file tests the _constraint implication_ relationship between types, aka `implies_subtype_of`,
99
which tests whether one type is a [subtype][subtyping] of another _assuming that the constraints in
1010
a particular constraint set hold_.
1111

@@ -16,14 +16,14 @@ fully static type that is not a typevar. It can _contain_ a typevar, though —
1616
considered concrete.)
1717

1818
```py
19-
from ty_extensions import is_subtype_of, is_subtype_of_given, static_assert
19+
from ty_extensions import ConstraintSet, is_subtype_of, static_assert
2020

2121
def equivalent_to_other_relationships[T]():
2222
static_assert(is_subtype_of(bool, int))
23-
static_assert(is_subtype_of_given(True, bool, int))
23+
static_assert(ConstraintSet.always().implies_subtype_of(bool, int))
2424

2525
static_assert(not is_subtype_of(bool, str))
26-
static_assert(not is_subtype_of_given(True, bool, str))
26+
static_assert(not ConstraintSet.always().implies_subtype_of(bool, str))
2727
```
2828

2929
Moreover, for concrete types, the answer does not depend on which constraint set we are considering.
@@ -32,16 +32,16 @@ there isn't a valid specialization for the typevars we are considering.
3232

3333
```py
3434
from typing import Never
35-
from ty_extensions import range_constraint
35+
from ty_extensions import ConstraintSet
3636

3737
def even_given_constraints[T]():
38-
constraints = range_constraint(Never, T, int)
39-
static_assert(is_subtype_of_given(constraints, bool, int))
40-
static_assert(not is_subtype_of_given(constraints, bool, str))
38+
constraints = ConstraintSet.range(Never, T, int)
39+
static_assert(constraints.implies_subtype_of(bool, int))
40+
static_assert(not constraints.implies_subtype_of(bool, str))
4141

4242
def even_given_unsatisfiable_constraints():
43-
static_assert(is_subtype_of_given(False, bool, int))
44-
static_assert(not is_subtype_of_given(False, bool, str))
43+
static_assert(ConstraintSet.never().implies_subtype_of(bool, int))
44+
static_assert(not ConstraintSet.never().implies_subtype_of(bool, str))
4545
```
4646

4747
## Type variables
@@ -141,58 +141,58 @@ considering.
141141

142142
```py
143143
from typing import Never
144-
from ty_extensions import is_subtype_of_given, range_constraint, static_assert
144+
from ty_extensions import ConstraintSet, static_assert
145145

146146
def given_constraints[T]():
147-
static_assert(not is_subtype_of_given(True, T, int))
148-
static_assert(not is_subtype_of_given(True, T, bool))
149-
static_assert(not is_subtype_of_given(True, T, str))
147+
static_assert(not ConstraintSet.always().implies_subtype_of(T, int))
148+
static_assert(not ConstraintSet.always().implies_subtype_of(T, bool))
149+
static_assert(not ConstraintSet.always().implies_subtype_of(T, str))
150150

151151
# These are vacuously true; false implies anything
152-
static_assert(is_subtype_of_given(False, T, int))
153-
static_assert(is_subtype_of_given(False, T, bool))
154-
static_assert(is_subtype_of_given(False, T, str))
152+
static_assert(ConstraintSet.never().implies_subtype_of(T, int))
153+
static_assert(ConstraintSet.never().implies_subtype_of(T, bool))
154+
static_assert(ConstraintSet.never().implies_subtype_of(T, str))
155155

156-
given_int = range_constraint(Never, T, int)
157-
static_assert(is_subtype_of_given(given_int, T, int))
158-
static_assert(not is_subtype_of_given(given_int, T, bool))
159-
static_assert(not is_subtype_of_given(given_int, T, str))
156+
given_int = ConstraintSet.range(Never, T, int)
157+
static_assert(given_int.implies_subtype_of(T, int))
158+
static_assert(not given_int.implies_subtype_of(T, bool))
159+
static_assert(not given_int.implies_subtype_of(T, str))
160160

161-
given_bool = range_constraint(Never, T, bool)
162-
static_assert(is_subtype_of_given(given_bool, T, int))
163-
static_assert(is_subtype_of_given(given_bool, T, bool))
164-
static_assert(not is_subtype_of_given(given_bool, T, str))
161+
given_bool = ConstraintSet.range(Never, T, bool)
162+
static_assert(given_bool.implies_subtype_of(T, int))
163+
static_assert(given_bool.implies_subtype_of(T, bool))
164+
static_assert(not given_bool.implies_subtype_of(T, str))
165165

166166
given_both = given_bool & given_int
167-
static_assert(is_subtype_of_given(given_both, T, int))
168-
static_assert(is_subtype_of_given(given_both, T, bool))
169-
static_assert(not is_subtype_of_given(given_both, T, str))
170-
171-
given_str = range_constraint(Never, T, str)
172-
static_assert(not is_subtype_of_given(given_str, T, int))
173-
static_assert(not is_subtype_of_given(given_str, T, bool))
174-
static_assert(is_subtype_of_given(given_str, T, str))
167+
static_assert(given_both.implies_subtype_of(T, int))
168+
static_assert(given_both.implies_subtype_of(T, bool))
169+
static_assert(not given_both.implies_subtype_of(T, str))
170+
171+
given_str = ConstraintSet.range(Never, T, str)
172+
static_assert(not given_str.implies_subtype_of(T, int))
173+
static_assert(not given_str.implies_subtype_of(T, bool))
174+
static_assert(given_str.implies_subtype_of(T, str))
175175
```
176176

177177
This might require propagating constraints from other typevars.
178178

179179
```py
180180
def mutually_constrained[T, U]():
181181
# If [T = U ∧ U ≤ int], then [T ≤ int] must be true as well.
182-
given_int = range_constraint(U, T, U) & range_constraint(Never, U, int)
182+
given_int = ConstraintSet.range(U, T, U) & ConstraintSet.range(Never, U, int)
183183
# TODO: no static-assert-error
184184
# error: [static-assert-error]
185-
static_assert(is_subtype_of_given(given_int, T, int))
186-
static_assert(not is_subtype_of_given(given_int, T, bool))
187-
static_assert(not is_subtype_of_given(given_int, T, str))
185+
static_assert(given_int.implies_subtype_of(T, int))
186+
static_assert(not given_int.implies_subtype_of(T, bool))
187+
static_assert(not given_int.implies_subtype_of(T, str))
188188

189189
# If [T ≤ U ∧ U ≤ int], then [T ≤ int] must be true as well.
190-
given_int = range_constraint(Never, T, U) & range_constraint(Never, U, int)
190+
given_int = ConstraintSet.range(Never, T, U) & ConstraintSet.range(Never, U, int)
191191
# TODO: no static-assert-error
192192
# error: [static-assert-error]
193-
static_assert(is_subtype_of_given(given_int, T, int))
194-
static_assert(not is_subtype_of_given(given_int, T, bool))
195-
static_assert(not is_subtype_of_given(given_int, T, str))
193+
static_assert(given_int.implies_subtype_of(T, int))
194+
static_assert(not given_int.implies_subtype_of(T, bool))
195+
static_assert(not given_int.implies_subtype_of(T, str))
196196
```
197197

198198
[subtyping]: https://typing.python.org/en/latest/spec/concepts.html#subtype-supertype-and-type-equivalence

0 commit comments

Comments
 (0)