Skip to content

Commit

Permalink
Merge pull request #282 from dimbleby/marker-simplifications
Browse files Browse the repository at this point in the history
Marker simplifications
  • Loading branch information
finswimmer committed Feb 26, 2022
2 parents 4e5207e + 9743333 commit 8ce6c6c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/poetry/core/version/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,8 @@ def of(cls, *markers: MarkerTypes) -> MarkerTypes:
if any(m.is_empty() for m in new_markers) or not new_markers:
return EmptyMarker()

if len(new_markers) == 1 and new_markers[0].is_any():
return AnyMarker()
if len(new_markers) == 1:
return new_markers[0]

return MultiMarker(*new_markers)

Expand All @@ -470,6 +470,9 @@ def intersect(self, other: MarkerTypes) -> MarkerTypes:
return MultiMarker.of(*new_markers)

def union(self, other: MarkerTypes) -> MarkerTypes:
if other in self._markers:
return other

if isinstance(other, (SingleMarker, MultiMarker)):
return MarkerUnion.of(self, other)

Expand Down
19 changes: 16 additions & 3 deletions tests/version/test_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,13 +685,13 @@ def test_without_extras(marker: str, expected: str):
(
'python_version >= "3.6" and (extra == "foo" or extra == "bar")',
"python_version",
'(extra == "foo" or extra == "bar")',
'extra == "foo" or extra == "bar"',
),
(
'python_version >= "3.6" and (extra == "foo" or extra == "bar") or'
' implementation_name == "pypy"',
"python_version",
'(extra == "foo" or extra == "bar") or implementation_name == "pypy"',
'extra == "foo" or extra == "bar" or implementation_name == "pypy"',
),
(
'python_version >= "3.6" and extra == "foo" or implementation_name =='
Expand All @@ -705,6 +705,11 @@ def test_without_extras(marker: str, expected: str):
"implementation_name",
'python_version >= "3.6" or extra == "foo" or extra == "bar"',
),
(
'extra == "foo" and python_version >= "3.6" or python_version >= "3.6"',
"extra",
'python_version >= "3.6"',
),
],
)
def test_exclude(marker: str, excluded: str, expected: str):
Expand All @@ -728,7 +733,7 @@ def test_exclude(marker: str, excluded: str, expected: str):
(
'python_version >= "3.6" and (extra == "foo" or extra == "bar")',
["extra"],
'(extra == "foo" or extra == "bar")',
'extra == "foo" or extra == "bar"',
),
(
'python_version >= "3.6" and (extra == "foo" or extra == "bar") or'
Expand Down Expand Up @@ -768,6 +773,14 @@ def test_union_of_a_single_marker_is_the_single_marker():
assert SingleMarker("python_version", ">= 2.7") == union


def test_union_of_multi_with_a_containing_single():
single = parse_marker('python_version >= "2.7"')
multi = parse_marker('python_version >= "2.7" and extra == "foo"')
union = multi.union(single)

assert union == single


@pytest.mark.parametrize(
"marker, inverse",
[
Expand Down

0 comments on commit 8ce6c6c

Please sign in to comment.