Skip to content

Commit

Permalink
markers: fix get_python_constraint_by_marker() for multi markers and …
Browse files Browse the repository at this point in the history
…marker unions without python_version by fixing Marker.only()
  • Loading branch information
radoering authored and neersighted committed Jan 20, 2023
1 parent 978fb55 commit 087ef84
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 42 deletions.
31 changes: 2 additions & 29 deletions src/poetry/core/version/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,22 +489,7 @@ def exclude(self, marker_name: str) -> BaseMarker:
return self.of(*new_markers)

def only(self, *marker_names: str) -> BaseMarker:
new_markers = []

for m in self._markers:
if isinstance(m, SingleMarker) and m.name not in marker_names:
# The marker is not relevant since it's not one we want
continue

marker = m.only(*marker_names)

if not marker.is_empty():
new_markers.append(marker)

if not new_markers:
return EmptyMarker()

return self.of(*new_markers)
return self.of(*(m.only(*marker_names) for m in self._markers))

def invert(self) -> BaseMarker:
markers = [marker.invert() for marker in self._markers]
Expand Down Expand Up @@ -635,19 +620,7 @@ def exclude(self, marker_name: str) -> BaseMarker:
return self.of(*new_markers)

def only(self, *marker_names: str) -> BaseMarker:
new_markers = []

for m in self._markers:
if isinstance(m, SingleMarker) and m.name not in marker_names:
# The marker is not relevant since it's not one we want
continue

marker = m.only(*marker_names)

if not marker.is_empty():
new_markers.append(marker)

return self.of(*new_markers)
return self.of(*(m.only(*marker_names) for m in self._markers))

def invert(self) -> BaseMarker:
markers = [marker.invert() for marker in self._markers]
Expand Down
2 changes: 2 additions & 0 deletions tests/packages/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ def test_create_nested_marker_version_constraint(
),
# no python_version
('sys_platform == "linux"', "*"),
('sys_platform != "linux" and sys_platform != "win32"', "*"),
('sys_platform == "linux" or sys_platform == "win32"', "*"),
# no relevant python_version
('python_version >= "3.9" or sys_platform == "linux"', "*"),
# relevant python_version
Expand Down
29 changes: 16 additions & 13 deletions tests/version/test_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,9 @@ def test_exclude(marker: str, excluded: str, expected: str) -> None:
["python_version"],
'python_version >= "3.6"',
),
('python_version >= "3.6" and extra == "foo"', ["sys_platform"], ""),
('python_version >= "3.6" or extra == "foo"', ["sys_platform"], ""),
('python_version >= "3.6" or extra == "foo"', ["python_version"], ""),
(
'python_version >= "3.6" and (extra == "foo" or extra == "bar")',
["extra"],
Expand All @@ -1028,31 +1031,31 @@ def test_exclude(marker: str, excluded: str, expected: str) -> None:
' implementation_name == "pypy"'
),
["implementation_name"],
'implementation_name == "pypy"',
"",
),
(
(
'python_version >= "3.6" and extra == "foo" or implementation_name =='
' "pypy" and extra == "bar"'
'python_version >= "3.6" and (extra == "foo" or extra == "bar") or'
' implementation_name == "pypy"'
),
["implementation_name"],
'implementation_name == "pypy"',
["implementation_name", "extra"],
'extra == "foo" or extra == "bar" or implementation_name == "pypy"',
),
(
(
'python_version >= "3.6" or extra == "foo" and implementation_name =='
' "pypy" or extra == "bar"'
'python_version >= "3.6" and (extra == "foo" or extra == "bar") or'
' implementation_name == "pypy"'
),
["implementation_name"],
'implementation_name == "pypy"',
["implementation_name", "python_version"],
'python_version >= "3.6" or implementation_name == "pypy"',
),
(
(
'python_version >= "3.6" or extra == "foo" and implementation_name =='
' "pypy" or extra == "bar"'
'python_version >= "3.6" and extra == "foo" or implementation_name =='
' "pypy" and extra == "bar"'
),
["implementation_name", "python_version"],
'python_version >= "3.6" or implementation_name == "pypy"',
["implementation_name", "extra"],
'extra == "foo" or implementation_name == "pypy" and extra == "bar"',
),
],
)
Expand Down

0 comments on commit 087ef84

Please sign in to comment.