Skip to content

Commit

Permalink
further simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby committed Nov 29, 2022
1 parent 885d36e commit 984ebfa
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 32 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
38 changes: 9 additions & 29 deletions src/poetry/core/version/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
Expand All @@ -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:
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 984ebfa

Please sign in to comment.