Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Marker simplifications #282

Merged
merged 2 commits into from
Feb 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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