Skip to content
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

Hashes #466

Merged
merged 3 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
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 ""
11 changes: 8 additions & 3 deletions src/poetry/core/packages/constraints/multi_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,14 @@ def __eq__(self, other: object) -> bool:
if not isinstance(other, MultiConstraint):
return False

return sorted(
self._constraints, key=lambda c: (c.operator, c.version)
) == sorted(other.constraints, key=lambda c: (c.operator, c.version))
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 = []
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
4 changes: 2 additions & 2 deletions src/poetry/core/version/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ def __eq__(self, other: object) -> bool:
def __hash__(self) -> int:
h = hash("multi")
for m in self._markers:
h |= hash(m)
h ^= hash(m)

return h

Expand Down Expand Up @@ -763,7 +763,7 @@ def __eq__(self, other: object) -> bool:
def __hash__(self) -> int:
h = hash("union")
for m in self._markers:
h |= hash(m)
h ^= hash(m)

return h

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)