From a739a08f30b2dd3bbd8d215835c54b6ae702745e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Sat, 17 Dec 2022 10:09:41 +0100 Subject: [PATCH] add intersection and union function, which take an arbitrary amount of markers, and move duplicated code into these functions --- src/poetry/core/version/markers.py | 42 +++++++++++++----------------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/src/poetry/core/version/markers.py b/src/poetry/core/version/markers.py index ad0b1a39b..9090dcd0c 100644 --- a/src/poetry/core/version/markers.py +++ b/src/poetry/core/version/markers.py @@ -437,16 +437,10 @@ def of(cls, *markers: BaseMarker) -> BaseMarker: return MultiMarker(*new_markers) def intersect(self, other: BaseMarker) -> BaseMarker: - multi = MultiMarker(self, other) - return dnf(multi) + return intersection(self, other) def union(self, other: BaseMarker) -> BaseMarker: - union = MarkerUnion(self, other) - conjunction = cnf(union) - if not isinstance(conjunction, MultiMarker): - return conjunction - - return dnf(conjunction) + return union(self, other) def union_simplify(self, other: BaseMarker) -> BaseMarker | None: """ @@ -612,17 +606,10 @@ def append(self, marker: BaseMarker) -> None: self._markers.append(marker) def intersect(self, other: BaseMarker) -> BaseMarker: - multi = MultiMarker(self, other) - return dnf(multi) + return intersection(self, other) def union(self, other: BaseMarker) -> BaseMarker: - union = MarkerUnion(self, other) - - conjunction = cnf(union) - if not isinstance(conjunction, MultiMarker): - return conjunction - - return dnf(conjunction) + return union(self, other) def validate(self, environment: dict[str, Any] | None) -> bool: return any(m.validate(environment) for m in self._markers) @@ -744,18 +731,13 @@ def _compact_markers( # Combine the groups. sub_markers = [MultiMarker(*group) for group in groups] - union = MarkerUnion(*sub_markers) # This function calls itself recursively. In the inner calls we don't perform any # simplification, instead doing it all only when we have the complete marker. if not top_level: - return union - - conjunction = cnf(union) - if not isinstance(conjunction, MultiMarker): - return conjunction + return MarkerUnion(*sub_markers) - return dnf(conjunction) + return union(*sub_markers) def cnf(marker: BaseMarker) -> BaseMarker: @@ -790,6 +772,18 @@ def dnf(marker: BaseMarker) -> BaseMarker: return marker +def intersection(*markers: BaseMarker) -> BaseMarker: + return dnf(MultiMarker(*markers)) + + +def union(*markers: BaseMarker) -> BaseMarker: + conjunction = cnf(MarkerUnion.of(*markers)) + if not isinstance(conjunction, MultiMarker): + return conjunction + + return dnf(conjunction) + + def _merge_single_markers( marker1: SingleMarker, marker2: SingleMarker,