8000 Implement basic constraints unit tests. by jhance · Pull Request #13210 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Implement basic constraints unit tests. #13210

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

Merged
merged 2 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implement basic constraints unit tests.
  • Loading branch information
jhance committed Jul 20, 2022
commit 9bb1fda267aa09a697e4b4a8ae2d28b1fb994292
8 changes: 8 additions & 0 deletions mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ def __repr__(self) -> str:
op_str = ':>'
return f'{self.type_var} {op_str} {self.target}'

def __hash__(self) -> int:
return hash((self.type_var, self.op, self.target))

def __eq__(self, other: object) -> bool:
if not isinstance(other, Constraint):
return False
return (self.type_var, self.op, self.target) == (other.type_var, other.op, other.target)


def infer_constraints_for_callable(
callee: CallableType,
Expand Down
21 changes: 21 additions & 0 deletions mypy/test/testconstraints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from mypy.test.helpers import Suite
from mypy.test.typefixture import TypeFixture
from mypy.constraints import infer_constraints, SUBTYPE_OF, SUPERTYPE_OF, Constraint

class ConstraintsSuite(Suite):
def setUp(self) -> None:
self.fx = TypeFixture()

def test_no_type_variables(self) -> None:
assert not infer_constraints(self.fx.o, self.fx.o, SUBTYPE_OF)

def test_basic_type_variable(self) -> None:
fx = self.fx
for direction in [SUBTYPE_OF, SUPERTYPE_OF]:
assert infer_constraints(fx.gt, fx.ga, direction) == [
Constraint(
type_var=fx.t.id,
op=direction,
target=fx.a,
)
]
0