Skip to content

Commit b0e59b2

Browse files
jhancesobolevn
andauthored
Implement basic constraints unit tests. (#13210)
This adds some basic constraints tests with the intent of adding more complex ones for variadic generics later. Co-authored-by: Nikita Sobolev <[email protected]>
1 parent 15e4de5 commit b0e59b2

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

mypy/constraints.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ def __repr__(self) -> str:
5454
op_str = ':>'
5555
return f'{self.type_var} {op_str} {self.target}'
5656

57+
def __hash__(self) -> int:
58+
return hash((self.type_var, self.op, self.target))
59+
60+
def __eq__(self, other: object) -> bool:
61+
if not isinstance(other, Constraint):
62+
return False
63+
return (self.type_var, self.op, self.target) == (other.type_var, other.op, other.target)
64+
5765

5866
def infer_constraints_for_callable(
5967
callee: CallableType,

mypy/test/testconstraints.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from mypy.test.helpers import Suite
2+
from mypy.test.typefixture import TypeFixture
3+
from mypy.constraints import infer_constraints, SUBTYPE_OF, SUPERTYPE_OF, Constraint
4+
5+
6+
class ConstraintsSuite(Suite):
7+
def setUp(self) -> None:
8+
self.fx = TypeFixture()
9+
10+
def test_no_type_variables(self) -> None:
11+
assert not infer_constraints(self.fx.o, self.fx.o, SUBTYPE_OF)
12+
13+
def test_basic_type_variable(self) -> None:
14+
fx = self.fx
15+
for direction in [SUBTYPE_OF, SUPERTYPE_OF]:
16+
assert infer_constraints(fx.gt, fx.ga, direction) == [
17+
Constraint(
18+
type_var=fx.t.id,
19+
op=direction,
20+
target=fx.a,
21+
)
22+
]

0 commit comments

Comments
 (0)