From 72ffb89778359825c68f467c0f3e9983b9dcac91 Mon Sep 17 00:00:00 2001 From: David Hotham Date: Sat, 16 Jul 2022 17:03:38 +0100 Subject: [PATCH 1/2] equal markers should have the same hash --- src/poetry/core/version/markers.py | 2 +- tests/version/test_markers.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/poetry/core/version/markers.py b/src/poetry/core/version/markers.py index 1b5630e55..c7edc5a81 100644 --- a/src/poetry/core/version/markers.py +++ b/src/poetry/core/version/markers.py @@ -362,7 +362,7 @@ def __eq__(self, other: object) -> bool: return self._name == other.name and self._constraint == other.constraint def __hash__(self) -> int: - return hash((self._name, self._constraint_string)) + return hash((self._name, self._constraint)) def __str__(self) -> str: return f'{self._name} {self._operator} "{self._value}"' diff --git a/tests/version/test_markers.py b/tests/version/test_markers.py index 364cbed88..efafe7b60 100644 --- a/tests/version/test_markers.py +++ b/tests/version/test_markers.py @@ -83,6 +83,13 @@ def test_single_marker() -> None: ) +def test_single_marker_normalisation() -> None: + m1 = SingleMarker("python_version", ">=3.6") + m2 = SingleMarker("python_version", ">= 3.6") + assert m1 == m2 + assert hash(m1) == hash(m2) + + def test_single_marker_intersect() -> None: m = parse_marker('sys_platform == "darwin"') From a01be613520fab7b346d5de3b04c2b4477c1f4a2 Mon Sep 17 00:00:00 2001 From: David Hotham Date: Sat, 16 Jul 2022 17:03:50 +0100 Subject: [PATCH 2/2] remove marker.constraint_string --- src/poetry/core/version/markers.py | 16 ++++------------ tests/version/test_markers.py | 14 +------------- 2 files changed, 5 insertions(+), 25 deletions(-) diff --git a/src/poetry/core/version/markers.py b/src/poetry/core/version/markers.py index c7edc5a81..5f64b1462 100644 --- a/src/poetry/core/version/markers.py +++ b/src/poetry/core/version/markers.py @@ -192,12 +192,12 @@ def __init__( self._constraint: BaseConstraint | VersionConstraint self._parser: Callable[[str], BaseConstraint | VersionConstraint] self._name = ALIASES.get(name, name) - self._constraint_string = str(constraint) + constraint_string = str(constraint) # Extract operator and value - m = self._CONSTRAINT_RE.match(self._constraint_string) + m = self._CONSTRAINT_RE.match(constraint_string) if m is None: - raise ValueError(f"Invalid marker '{self._constraint_string}'") + raise ValueError(f"Invalid marker '{constraint_string}'") self._operator = m.group(1) if self._operator is None: @@ -227,11 +227,10 @@ def __init__( self._constraint = self._parser(glue.join(versions)) else: - self._constraint = self._parser(self._constraint_string) + self._constraint = self._parser(constraint_string) else: # if we have a in/not in operator we split the constraint # into a union/multi-constraint of single constraint - constraint_string = self._constraint_string if self._operator in {"in", "not in"}: op, glue = ("==", " || ") if self._operator == "in" else ("!=", ", ") values = re.split("[ ,]+", self._value) @@ -243,13 +242,6 @@ def __init__( def name(self) -> str: return self._name - @property - def constraint_string(self) -> str: - if self._operator in {"in", "not in"}: - return f"{self._operator} {self._value}" - - return self._constraint_string - @property def constraint(self) -> BaseConstraint | VersionConstraint: return self._constraint diff --git a/tests/version/test_markers.py b/tests/version/test_markers.py index efafe7b60..43e945baf 100644 --- a/tests/version/test_markers.py +++ b/tests/version/test_markers.py @@ -24,27 +24,24 @@ def test_single_marker() -> None: assert isinstance(m, SingleMarker) assert m.name == "sys_platform" - assert m.constraint_string == "==darwin" + assert str(m.constraint) == "darwin" m = parse_marker('python_version in "2.7, 3.0, 3.1"') assert isinstance(m, SingleMarker) assert m.name == "python_version" - assert m.constraint_string == "in 2.7, 3.0, 3.1" assert str(m.constraint) == ">=2.7.0,<2.8.0 || >=3.0.0,<3.2.0" m = parse_marker('"2.7" in python_version') assert isinstance(m, SingleMarker) assert m.name == "python_version" - assert m.constraint_string == "in 2.7" assert str(m.constraint) == ">=2.7.0,<2.8.0" m = parse_marker('python_version not in "2.7, 3.0, 3.1"') assert isinstance(m, SingleMarker) assert m.name == "python_version" - assert m.constraint_string == "not in 2.7, 3.0, 3.1" assert str(m.constraint) == "<2.7.0 || >=2.8.0,<3.0.0 || >=3.2.0" m = parse_marker( @@ -54,10 +51,6 @@ def test_single_marker() -> None: assert isinstance(m, SingleMarker) assert m.name == "platform_machine" - assert ( - m.constraint_string - == "in x86_64 X86_64 aarch64 AARCH64 ppc64le PPC64LE amd64 AMD64 win32 WIN32" - ) assert ( str(m.constraint) == "x86_64 || X86_64 || aarch64 || AARCH64 || ppc64le || PPC64LE || amd64 ||" @@ -71,11 +64,6 @@ def test_single_marker() -> None: assert isinstance(m, SingleMarker) assert m.name == "platform_machine" - assert ( - m.constraint_string - == "not in x86_64 X86_64 aarch64 AARCH64 ppc64le PPC64LE amd64 AMD64 win32" - " WIN32" - ) assert ( str(m.constraint) == "!=x86_64, !=X86_64, !=aarch64, !=AARCH64, !=ppc64le, !=PPC64LE, !=amd64,"