Skip to content

Commit 3905d3e

Browse files
radoeringneersighted
authored andcommitted
constraints: make EmptyConstraint hashable and tidy up a bit
1 parent 45dcad4 commit 3905d3e

File tree

8 files changed

+56
-17
lines changed

8 files changed

+56
-17
lines changed

src/poetry/core/constraints/generic/base_constraint.py

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ def is_empty(self) -> bool:
2929
def __repr__(self) -> str:
3030
return f"<{self.__class__.__name__} {str(self)}>"
3131

32+
def __str__(self) -> str:
33+
raise NotImplementedError()
34+
3235
def __hash__(self) -> int:
3336
raise NotImplementedError()
3437

src/poetry/core/constraints/version/empty_constraint.py

+3
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,6 @@ def __eq__(self, other: object) -> bool:
5151
return False
5252

5353
return other.is_empty()
54+
55+
def __hash__(self) -> int:
56+
return hash("empty")

src/poetry/core/constraints/version/version.py

-3
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,6 @@ def flatten(self) -> list[VersionRangeConstraint]:
144144
def __str__(self) -> str:
145145
return self.text
146146

147-
def __repr__(self) -> str:
148-
return f"<Version {str(self)}>"
149-
150147
def __eq__(self, other: object) -> bool:
151148
from poetry.core.constraints.version.version_range import VersionRange
152149

src/poetry/core/constraints/version/version_constraint.py

+12
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,15 @@ def difference(self, other: VersionConstraint) -> VersionConstraint:
5151
@abstractmethod
5252
def flatten(self) -> list[VersionRangeConstraint]:
5353
raise NotImplementedError()
54+
55+
def __repr__(self) -> str:
56+
return f"<{self.__class__.__name__} {str(self)}>"
57+
58+
def __str__(self) -> str:
59+
raise NotImplementedError()
60+
61+
def __hash__(self) -> int:
62+
raise NotImplementedError()
63+
64+
def __eq__(self, other: object) -> bool:
65+
raise NotImplementedError()

src/poetry/core/constraints/version/version_range.py

-3
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,6 @@ def __str__(self) -> str:
417417

418418
return text
419419

420-
def __repr__(self) -> str:
421-
return f"<VersionRange ({str(self)})>"
422-
423420
def __hash__(self) -> int:
424421
return (
425422
hash(self.min)

src/poetry/core/constraints/version/version_union.py

-3
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,3 @@ def __str__(self) -> str:
420420
return self._exclude_single_wildcard_range_string()
421421
except ValueError:
422422
return " || ".join([str(r) for r in self._ranges])
423-
424-
def __repr__(self) -> str:
425-
return f"<VersionUnion {str(self)}>"

tests/constraints/generic/test_constraint.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,14 @@ def test_difference() -> None:
172172
@pytest.mark.parametrize(
173173
"constraint",
174174
[
175-
(EmptyConstraint()),
176-
(AnyConstraint()),
177-
(Constraint("win32")),
178-
(UnionConstraint(Constraint("win32"), Constraint("linux"))),
179-
(MultiConstraint(Constraint("win32", "!="), Constraint("linux", "!="))),
175+
EmptyConstraint(),
176+
AnyConstraint(),
177+
Constraint("win32"),
178+
UnionConstraint(Constraint("win32"), Constraint("linux")),
179+
MultiConstraint(Constraint("win32", "!="), Constraint("linux", "!=")),
180180
],
181181
)
182-
def test_constraints_are_hashable(
183-
constraint: BaseConstraint,
184-
) -> None:
182+
def test_constraints_are_hashable(constraint: BaseConstraint) -> None:
185183
# We're just testing that constraints are hashable, there's nothing much to say
186184
# about the result.
187185
hash(constraint)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
5+
import pytest
6+
7+
from poetry.core.constraints.version import EmptyConstraint
8+
from poetry.core.constraints.version import Version
9+
from poetry.core.constraints.version import VersionRange
10+
from poetry.core.constraints.version import VersionUnion
11+
12+
13+
if TYPE_CHECKING:
14+
from poetry.core.constraints.version import VersionConstraint
15+
16+
17+
@pytest.mark.parametrize(
18+
"constraint",
19+
[
20+
EmptyConstraint(),
21+
Version.parse("1"),
22+
VersionRange(Version.parse("1"), Version.parse("2")),
23+
VersionUnion(
24+
VersionRange(Version.parse("1"), Version.parse("2")),
25+
VersionRange(Version.parse("3"), Version.parse("4")),
26+
),
27+
],
28+
)
29+
def test_constraints_are_hashable(constraint: VersionConstraint) -> None:
30+
# We're just testing that constraints are hashable, there's nothing much to say
31+
# about the result.
32+
hash(constraint)

0 commit comments

Comments
 (0)