Skip to content

Commit

Permalink
further simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby committed Dec 3, 2022
1 parent 60d147e commit 24ef146
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src/poetry/core/packages/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
53 changes: 13 additions & 40 deletions src/poetry/core/version/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,54 +447,27 @@ 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):
common_markers = [
marker for marker in self.markers if marker in other.markers
]
if isinstance(other, MultiMarker):
our_markers = set(self.markers)
their_markers = set(other.markers)

unique_markers = [
marker for marker in self.markers if marker not in common_markers
]
if not unique_markers:
if our_markers.issubset(their_markers):
return self

other_unique_markers = [
marker for marker in other.markers if marker not in common_markers
]
if not other_unique_markers:
if their_markers.issubset(our_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:
Expand Down
5 changes: 4 additions & 1 deletion tests/packages/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"',
Expand Down

0 comments on commit 24ef146

Please sign in to comment.