From 22c09fc780d6289da2f7c3c8c43763783e59db25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Tue, 27 Dec 2022 15:36:31 +0100 Subject: [PATCH] markers: fix get_python_constraint_by_marker() for multi markers and marker unions without python_version by fixing Marker.only() --- src/poetry/core/version/markers.py | 31 ++---------------------------- tests/packages/utils/test_utils.py | 2 ++ tests/version/test_markers.py | 29 +++++++++++++++------------- 3 files changed, 20 insertions(+), 42 deletions(-) diff --git a/src/poetry/core/version/markers.py b/src/poetry/core/version/markers.py index d50d2c058..047d8fa71 100644 --- a/src/poetry/core/version/markers.py +++ b/src/poetry/core/version/markers.py @@ -489,22 +489,7 @@ def exclude(self, marker_name: str) -> BaseMarker: return self.of(*new_markers) def only(self, *marker_names: str) -> BaseMarker: - new_markers = [] - - for m in self._markers: - if isinstance(m, SingleMarker) and m.name not in marker_names: - # The marker is not relevant since it's not one we want - continue - - marker = m.only(*marker_names) - - if not marker.is_empty(): - new_markers.append(marker) - - if not new_markers: - return EmptyMarker() - - return self.of(*new_markers) + return self.of(*(m.only(*marker_names) for m in self._markers)) def invert(self) -> BaseMarker: markers = [marker.invert() for marker in self._markers] @@ -635,19 +620,7 @@ def exclude(self, marker_name: str) -> BaseMarker: return self.of(*new_markers) def only(self, *marker_names: str) -> BaseMarker: - new_markers = [] - - for m in self._markers: - if isinstance(m, SingleMarker) and m.name not in marker_names: - # The marker is not relevant since it's not one we want - continue - - marker = m.only(*marker_names) - - if not marker.is_empty(): - new_markers.append(marker) - - return self.of(*new_markers) + return self.of(*(m.only(*marker_names) for m in self._markers)) def invert(self) -> BaseMarker: markers = [marker.invert() for marker in self._markers] diff --git a/tests/packages/utils/test_utils.py b/tests/packages/utils/test_utils.py index 1006031de..96db7e07e 100644 --- a/tests/packages/utils/test_utils.py +++ b/tests/packages/utils/test_utils.py @@ -212,6 +212,8 @@ def test_create_nested_marker_version_constraint( ), # no python_version ('sys_platform == "linux"', "*"), + ('sys_platform != "linux" and sys_platform != "win32"', "*"), + ('sys_platform == "linux" or sys_platform == "win32"', "*"), # no relevant python_version ('python_version >= "3.9" or sys_platform == "linux"', "*"), # relevant python_version diff --git a/tests/version/test_markers.py b/tests/version/test_markers.py index 64fd94446..2e7de7a24 100644 --- a/tests/version/test_markers.py +++ b/tests/version/test_markers.py @@ -1017,6 +1017,9 @@ def test_exclude(marker: str, excluded: str, expected: str) -> None: ["python_version"], 'python_version >= "3.6"', ), + ('python_version >= "3.6" and extra == "foo"', ["sys_platform"], ""), + ('python_version >= "3.6" or extra == "foo"', ["sys_platform"], ""), + ('python_version >= "3.6" or extra == "foo"', ["python_version"], ""), ( 'python_version >= "3.6" and (extra == "foo" or extra == "bar")', ["extra"], @@ -1028,31 +1031,31 @@ def test_exclude(marker: str, excluded: str, expected: str) -> None: ' implementation_name == "pypy"' ), ["implementation_name"], - 'implementation_name == "pypy"', + "", ), ( ( - 'python_version >= "3.6" and extra == "foo" or implementation_name ==' - ' "pypy" and extra == "bar"' + 'python_version >= "3.6" and (extra == "foo" or extra == "bar") or' + ' implementation_name == "pypy"' ), - ["implementation_name"], - 'implementation_name == "pypy"', + ["implementation_name", "extra"], + 'extra == "foo" or extra == "bar" or implementation_name == "pypy"', ), ( ( - 'python_version >= "3.6" or extra == "foo" and implementation_name ==' - ' "pypy" or extra == "bar"' + 'python_version >= "3.6" and (extra == "foo" or extra == "bar") or' + ' implementation_name == "pypy"' ), - ["implementation_name"], - 'implementation_name == "pypy"', + ["implementation_name", "python_version"], + 'python_version >= "3.6" or implementation_name == "pypy"', ), ( ( - 'python_version >= "3.6" or extra == "foo" and implementation_name ==' - ' "pypy" or extra == "bar"' + 'python_version >= "3.6" and extra == "foo" or implementation_name ==' + ' "pypy" and extra == "bar"' ), - ["implementation_name", "python_version"], - 'python_version >= "3.6" or implementation_name == "pypy"', + ["implementation_name", "extra"], + 'extra == "foo" or implementation_name == "pypy" and extra == "bar"', ), ], )