Skip to content

Commit

Permalink
add intersection and union function, which take an arbitrary amount o…
Browse files Browse the repository at this point in the history
…f markers, and move duplicated code into these functions
  • Loading branch information
radoering committed Dec 18, 2022
1 parent dea4676 commit 744e720
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions src/poetry/core/version/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(*markers))
if not isinstance(conjunction, MultiMarker):
return conjunction

return dnf(conjunction)


def _merge_single_markers(
marker1: SingleMarker,
marker2: SingleMarker,
Expand Down

0 comments on commit 744e720

Please sign in to comment.