From 6e823619d94c268a71469c8257493d39ef20c7bc Mon Sep 17 00:00:00 2001 From: David Hotham Date: Fri, 4 Feb 2022 01:12:45 +0000 Subject: [PATCH 1/2] A multimarker with one marker is just that marker --- src/poetry/core/version/markers.py | 4 ++-- tests/version/test_markers.py | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/poetry/core/version/markers.py b/src/poetry/core/version/markers.py index 7b2e98627..ed545e6a8 100644 --- a/src/poetry/core/version/markers.py +++ b/src/poetry/core/version/markers.py @@ -449,8 +449,8 @@ def of(cls, *markers: MarkerTypes) -> MarkerTypes: if any(m.is_empty() for m in new_markers) or not new_markers: return EmptyMarker() - if len(new_markers) == 1 and new_markers[0].is_any(): - return AnyMarker() + if len(new_markers) == 1: + return new_markers[0] return MultiMarker(*new_markers) diff --git a/tests/version/test_markers.py b/tests/version/test_markers.py index 408501284..8f67234a4 100644 --- a/tests/version/test_markers.py +++ b/tests/version/test_markers.py @@ -685,13 +685,13 @@ def test_without_extras(marker: str, expected: str): ( 'python_version >= "3.6" and (extra == "foo" or extra == "bar")', "python_version", - '(extra == "foo" or extra == "bar")', + 'extra == "foo" or extra == "bar"', ), ( 'python_version >= "3.6" and (extra == "foo" or extra == "bar") or' ' implementation_name == "pypy"', "python_version", - '(extra == "foo" or extra == "bar") or implementation_name == "pypy"', + 'extra == "foo" or extra == "bar" or implementation_name == "pypy"', ), ( 'python_version >= "3.6" and extra == "foo" or implementation_name ==' @@ -705,6 +705,11 @@ def test_without_extras(marker: str, expected: str): "implementation_name", 'python_version >= "3.6" or extra == "foo" or extra == "bar"', ), + ( + 'extra == "foo" and python_version >= "3.6" or python_version >= "3.6"', + "extra", + 'python_version >= "3.6"', + ), ], ) def test_exclude(marker: str, excluded: str, expected: str): @@ -728,7 +733,7 @@ def test_exclude(marker: str, excluded: str, expected: str): ( 'python_version >= "3.6" and (extra == "foo" or extra == "bar")', ["extra"], - '(extra == "foo" or extra == "bar")', + 'extra == "foo" or extra == "bar"', ), ( 'python_version >= "3.6" and (extra == "foo" or extra == "bar") or' From 9743333aaa9806b4509fb12f90d0cbe5a92d20dc Mon Sep 17 00:00:00 2001 From: David Hotham Date: Fri, 4 Feb 2022 09:31:58 +0000 Subject: [PATCH 2/2] simplify obvious unions --- src/poetry/core/version/markers.py | 3 +++ tests/version/test_markers.py | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/src/poetry/core/version/markers.py b/src/poetry/core/version/markers.py index ed545e6a8..e12e880ac 100644 --- a/src/poetry/core/version/markers.py +++ b/src/poetry/core/version/markers.py @@ -470,6 +470,9 @@ def intersect(self, other: MarkerTypes) -> MarkerTypes: return MultiMarker.of(*new_markers) def union(self, other: MarkerTypes) -> MarkerTypes: + if other in self._markers: + return other + if isinstance(other, (SingleMarker, MultiMarker)): return MarkerUnion.of(self, other) diff --git a/tests/version/test_markers.py b/tests/version/test_markers.py index 8f67234a4..b3f867206 100644 --- a/tests/version/test_markers.py +++ b/tests/version/test_markers.py @@ -773,6 +773,14 @@ def test_union_of_a_single_marker_is_the_single_marker(): assert SingleMarker("python_version", ">= 2.7") == union +def test_union_of_multi_with_a_containing_single(): + single = parse_marker('python_version >= "2.7"') + multi = parse_marker('python_version >= "2.7" and extra == "foo"') + union = multi.union(single) + + assert union == single + + @pytest.mark.parametrize( "marker, inverse", [