Skip to content

Commit

Permalink
improve reduce_by_python_constraint (#841)
Browse files Browse the repository at this point in the history
  • Loading branch information
radoering authored Feb 23, 2025
1 parent c8a4bb6 commit db5b8d4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/poetry/core/version/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ def reduce_by_python_constraint(
self, python_constraint: VersionConstraint
) -> BaseMarker:
if self.name in PYTHON_VERSION_MARKERS:
from poetry.core.packages.utils.utils import create_nested_marker
from poetry.core.packages.utils.utils import (
get_python_constraint_from_marker,
)
Expand All @@ -467,6 +468,13 @@ def reduce_by_python_constraint(
elif not constraint.allows_any(python_constraint):
return EmptyMarker()

python_marker = parse_marker(
create_nested_marker("python_version", python_constraint)
)
intersection = self.intersect(python_marker)
if isinstance(intersection, SingleMarker):
return intersection

return self

def invert(self) -> BaseMarker:
Expand Down
9 changes: 7 additions & 2 deletions tests/version/test_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1495,6 +1495,11 @@ def test_only(marker: str, only: list[str], expected: str) -> None:
('python_version > "3.8"', "~3.8", "<empty>"),
('python_version >= "3.8"', "~3.8", ""),
('python_version >= "3.9"', "~3.8", "<empty>"),
('python_version == "3.9"', ">=3.9", 'python_version == "3.9"'),
('python_version <= "3.9"', ">=3.9", 'python_version == "3.9"'),
('python_version < "3.10"', ">=3.9", 'python_version == "3.9"'),
('python_version <= "3.10"', ">=3.9", 'python_version <= "3.10"'),
('python_version < "3.11"', ">=3.9", 'python_version < "3.11"'),
('python_full_version >= "3.8.0"', "~3.8", ""),
('python_full_version >= "3.8.1"', "~3.8", 'python_full_version >= "3.8.1"'),
('python_full_version < "3.8.0"', "~3.8", "<empty>"),
Expand All @@ -1519,7 +1524,7 @@ def test_only(marker: str, only: list[str], expected: str) -> None:
(
'python_version < "3.8" or python_version >= "3.9"',
">=3.7",
'python_version < "3.8" or python_version >= "3.9"',
'python_version == "3.7" or python_version >= "3.9"',
),
('python_version < "3.8" or python_version >= "3.9"', "~3.7", ""),
(
Expand All @@ -1539,7 +1544,7 @@ def test_only(marker: str, only: list[str], expected: str) -> None:
(
'python_version < "3.8" or python_version >= "3.9"',
"~3.6 || ~3.8",
'python_version < "3.8"',
'python_version == "3.6"',
),
(
(
Expand Down

0 comments on commit db5b8d4

Please sign in to comment.