diff --git a/src/poetry/core/constraints/generic/base_constraint.py b/src/poetry/core/constraints/generic/base_constraint.py index 73f44cab7..df5826994 100644 --- a/src/poetry/core/constraints/generic/base_constraint.py +++ b/src/poetry/core/constraints/generic/base_constraint.py @@ -29,6 +29,9 @@ def is_empty(self) -> bool: def __repr__(self) -> str: return f"<{self.__class__.__name__} {str(self)}>" + def __str__(self) -> str: + raise NotImplementedError() + def __hash__(self) -> int: raise NotImplementedError() diff --git a/src/poetry/core/constraints/version/empty_constraint.py b/src/poetry/core/constraints/version/empty_constraint.py index 5fd1733cb..5c9f6f5c7 100644 --- a/src/poetry/core/constraints/version/empty_constraint.py +++ b/src/poetry/core/constraints/version/empty_constraint.py @@ -51,3 +51,6 @@ def __eq__(self, other: object) -> bool: return False return other.is_empty() + + def __hash__(self) -> int: + return hash("empty") diff --git a/src/poetry/core/constraints/version/version.py b/src/poetry/core/constraints/version/version.py index 6f84ed6b4..faba17392 100644 --- a/src/poetry/core/constraints/version/version.py +++ b/src/poetry/core/constraints/version/version.py @@ -144,9 +144,6 @@ def flatten(self) -> list[VersionRangeConstraint]: def __str__(self) -> str: return self.text - def __repr__(self) -> str: - return f"" - def __eq__(self, other: object) -> bool: from poetry.core.constraints.version.version_range import VersionRange diff --git a/src/poetry/core/constraints/version/version_constraint.py b/src/poetry/core/constraints/version/version_constraint.py index 089a9b094..cc1cda8c2 100644 --- a/src/poetry/core/constraints/version/version_constraint.py +++ b/src/poetry/core/constraints/version/version_constraint.py @@ -51,3 +51,15 @@ def difference(self, other: VersionConstraint) -> VersionConstraint: @abstractmethod def flatten(self) -> list[VersionRangeConstraint]: raise NotImplementedError() + + def __repr__(self) -> str: + return f"<{self.__class__.__name__} {str(self)}>" + + def __str__(self) -> str: + raise NotImplementedError() + + def __hash__(self) -> int: + raise NotImplementedError() + + def __eq__(self, other: object) -> bool: + raise NotImplementedError() diff --git a/src/poetry/core/constraints/version/version_range.py b/src/poetry/core/constraints/version/version_range.py index cbda6fac2..eb1f6e2c4 100644 --- a/src/poetry/core/constraints/version/version_range.py +++ b/src/poetry/core/constraints/version/version_range.py @@ -417,9 +417,6 @@ def __str__(self) -> str: return text - def __repr__(self) -> str: - return f"" - def __hash__(self) -> int: return ( hash(self.min) diff --git a/src/poetry/core/constraints/version/version_union.py b/src/poetry/core/constraints/version/version_union.py index 022aa8b35..57ca178bf 100644 --- a/src/poetry/core/constraints/version/version_union.py +++ b/src/poetry/core/constraints/version/version_union.py @@ -420,6 +420,3 @@ def __str__(self) -> str: return self._exclude_single_wildcard_range_string() except ValueError: return " || ".join([str(r) for r in self._ranges]) - - def __repr__(self) -> str: - return f"" diff --git a/tests/constraints/generic/test_constraint.py b/tests/constraints/generic/test_constraint.py index c96ef4d98..73a2a73c1 100644 --- a/tests/constraints/generic/test_constraint.py +++ b/tests/constraints/generic/test_constraint.py @@ -172,16 +172,14 @@ def test_difference() -> None: @pytest.mark.parametrize( "constraint", [ - (EmptyConstraint()), - (AnyConstraint()), - (Constraint("win32")), - (UnionConstraint(Constraint("win32"), Constraint("linux"))), - (MultiConstraint(Constraint("win32", "!="), Constraint("linux", "!="))), + EmptyConstraint(), + AnyConstraint(), + Constraint("win32"), + UnionConstraint(Constraint("win32"), Constraint("linux")), + MultiConstraint(Constraint("win32", "!="), Constraint("linux", "!=")), ], ) -def test_constraints_are_hashable( - constraint: BaseConstraint, -) -> None: +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) diff --git a/tests/constraints/version/test_version_constraint.py b/tests/constraints/version/test_version_constraint.py new file mode 100644 index 000000000..49834dee1 --- /dev/null +++ b/tests/constraints/version/test_version_constraint.py @@ -0,0 +1,32 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING + +import pytest + +from poetry.core.constraints.version import EmptyConstraint +from poetry.core.constraints.version import Version +from poetry.core.constraints.version import VersionRange +from poetry.core.constraints.version import VersionUnion + + +if TYPE_CHECKING: + from poetry.core.constraints.version import VersionConstraint + + +@pytest.mark.parametrize( + "constraint", + [ + EmptyConstraint(), + Version.parse("1"), + VersionRange(Version.parse("1"), Version.parse("2")), + VersionUnion( + VersionRange(Version.parse("1"), Version.parse("2")), + VersionRange(Version.parse("3"), Version.parse("4")), + ), + ], +) +def test_constraints_are_hashable(constraint: VersionConstraint) -> None: + # We're just testing that constraints are hashable, there's nothing much to say + # about the result. + hash(constraint)