Skip to content

Commit

Permalink
make constraints hashable
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby authored and radoering committed Sep 6, 2022
1 parent 6c202be commit 0a1d5f1
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/poetry/core/packages/constraints/any_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ def __str__(self) -> str:

def __eq__(self, other: object) -> bool:
return isinstance(other, BaseConstraint) and other.is_any()

def __hash__(self) -> int:
return hash("any")
3 changes: 3 additions & 0 deletions src/poetry/core/packages/constraints/base_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@ def is_empty(self) -> bool:
def __repr__(self) -> str:
return f"<{self.__class__.__name__} {str(self)}>"

def __hash__(self) -> int:
raise NotImplementedError()

def __eq__(self, other: object) -> bool:
raise NotImplementedError()
3 changes: 3 additions & 0 deletions src/poetry/core/packages/constraints/empty_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@ def __eq__(self, other: object) -> bool:

return other.is_empty()

def __hash__(self) -> int:
return hash("empty")

def __str__(self) -> str:
return ""
7 changes: 7 additions & 0 deletions src/poetry/core/packages/constraints/multi_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ def __eq__(self, other: object) -> bool:

return set(self._constraints) == set(other._constraints)

def __hash__(self) -> int:
h = hash("multi")
for constraint in self._constraints:
h ^= hash(constraint)

return h

def __str__(self) -> str:
constraints = []
for constraint in self._constraints:
Expand Down
7 changes: 7 additions & 0 deletions src/poetry/core/packages/constraints/union_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ def __eq__(self, other: object) -> bool:

return set(self._constraints) == set(other._constraints)

def __hash__(self) -> int:
h = hash("union")
for constraint in self._constraints:
h ^= hash(constraint)

return h

def __str__(self) -> str:
constraints = []
for constraint in self._constraints:
Expand Down
18 changes: 18 additions & 0 deletions tests/packages/constraints/test_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,21 @@ def test_difference() -> None:

assert c.difference(Constraint("win32")).is_empty()
assert c.difference(Constraint("linux")) == c


@pytest.mark.parametrize(
"constraint",
[
(EmptyConstraint()),
(AnyConstraint()),
(Constraint("win32")),
(UnionConstraint(Constraint("win32"), Constraint("linux"))),
(MultiConstraint(Constraint("win32", "!="), Constraint("linux", "!="))),
],
)
def test_constraints_are_hashable(
constraint: BaseConstraint,
) -> None:
# We're just testing that constraints are hashable, there's nothing much to say
# about the result.
hash(constraint)

0 comments on commit 0a1d5f1

Please sign in to comment.