From 984ebfa2ba734669ff7c0e36052218a3862efd1c Mon Sep 17 00:00:00 2001 From: David Hotham Date: Tue, 29 Nov 2022 19:41:47 +0000 Subject: [PATCH] further simplification --- src/poetry/core/packages/utils/utils.py | 4 +-- src/poetry/core/version/markers.py | 38 ++++++------------------- tests/packages/utils/test_utils.py | 5 +++- 3 files changed, 15 insertions(+), 32 deletions(-) diff --git a/src/poetry/core/packages/utils/utils.py b/src/poetry/core/packages/utils/utils.py index 4b4853ca7..f81fb6b93 100644 --- a/src/poetry/core/packages/utils/utils.py +++ b/src/poetry/core/packages/utils/utils.py @@ -179,8 +179,8 @@ def add_constraint( for i, sub_marker in enumerate(conjunctions): if isinstance(sub_marker, MultiMarker): for m in sub_marker.markers: - if isinstance(m, SingleMarker): - add_constraint(m.name, (m.operator, m.value), i) + assert isinstance(m, SingleMarker) + add_constraint(m.name, (m.operator, m.value), i) elif isinstance(sub_marker, SingleMarker): add_constraint(sub_marker.name, (sub_marker.operator, sub_marker.value), i) diff --git a/src/poetry/core/version/markers.py b/src/poetry/core/version/markers.py index 89b3eb85a..a731d69bf 100644 --- a/src/poetry/core/version/markers.py +++ b/src/poetry/core/version/markers.py @@ -444,31 +444,18 @@ def union(self, other: BaseMarker) -> BaseMarker: def union_simplify(self, other: BaseMarker) -> BaseMarker | None: """ - In contrast to the standard union method, which prefers to return - a MarkerUnion of MultiMarkers, this version prefers to return - a MultiMarker of MarkerUnions. + Finds a couple of easy simplifications for union on MultiMarkers: - The rationale behind this approach is to find additional simplifications. - In order to avoid endless recursions, this method returns None - if it cannot find a simplification. + - union with any marker that appears as part of the multi is just that + marker + + - union between two multimarkers where one is contained by the other is just + the larger of the two """ - if isinstance(other, SingleMarker): - new_markers = [] - for marker in self._markers: - union = marker.union(other) - if not union.is_any(): - new_markers.append(union) - - if len(new_markers) == 1: - return new_markers[0] - - if other in new_markers and all( - other == m or isinstance(m, MarkerUnion) and other in m.markers - for m in new_markers - ): - return other + if other in self._markers: + return other - elif isinstance(other, MultiMarker): + if isinstance(other, MultiMarker): common_markers = [ marker for marker in self.markers if marker in other.markers ] @@ -485,13 +472,6 @@ def union_simplify(self, other: BaseMarker) -> BaseMarker | None: if not other_unique_markers: return other - if common_markers: - unique_union = MultiMarker(*unique_markers).union( - MultiMarker(*other_unique_markers) - ) - if not isinstance(unique_union, MarkerUnion): - return MultiMarker(*common_markers).intersect(unique_union) - return None def validate(self, environment: dict[str, Any] | None) -> bool: diff --git a/tests/packages/utils/test_utils.py b/tests/packages/utils/test_utils.py index a9949752e..9a96f2100 100644 --- a/tests/packages/utils/test_utils.py +++ b/tests/packages/utils/test_utils.py @@ -41,7 +41,10 @@ ' "win32" and python_version < "3.6" and python_version >= "3.3" or' ' sys_platform == "win32" and python_version < "3.3"' ), - {"python_version": [[("<", "3.6")]], "sys_platform": [[("==", "win32")]]}, + { + "python_version": [[("<", "3.6")], [("<", "3.3")]], + "sys_platform": [[("==", "win32")]], + }, ), ( 'python_version == "2.7" or python_version == "2.6"',