Skip to content

Commit

Permalink
constraints: make EmptyConstraint hashable and tidy up a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
radoering authored and neersighted committed Oct 30, 2022
1 parent 45dcad4 commit 3905d3e
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 17 deletions.
3 changes: 3 additions & 0 deletions src/poetry/core/constraints/generic/base_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
3 changes: 3 additions & 0 deletions src/poetry/core/constraints/version/empty_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@ def __eq__(self, other: object) -> bool:
return False

return other.is_empty()

def __hash__(self) -> int:
return hash("empty")
3 changes: 0 additions & 3 deletions src/poetry/core/constraints/version/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,6 @@ def flatten(self) -> list[VersionRangeConstraint]:
def __str__(self) -> str:
return self.text

def __repr__(self) -> str:
return f"<Version {str(self)}>"

def __eq__(self, other: object) -> bool:
from poetry.core.constraints.version.version_range import VersionRange

Expand Down
12 changes: 12 additions & 0 deletions src/poetry/core/constraints/version/version_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
3 changes: 0 additions & 3 deletions src/poetry/core/constraints/version/version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,6 @@ def __str__(self) -> str:

return text

def __repr__(self) -> str:
return f"<VersionRange ({str(self)})>"

def __hash__(self) -> int:
return (
hash(self.min)
Expand Down
3 changes: 0 additions & 3 deletions src/poetry/core/constraints/version/version_union.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"<VersionUnion {str(self)}>"
14 changes: 6 additions & 8 deletions tests/constraints/generic/test_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
32 changes: 32 additions & 0 deletions tests/constraints/version/test_version_constraint.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 3905d3e

Please sign in to comment.