diff --git a/src/poetry/core/version/markers.py b/src/poetry/core/version/markers.py index a5f50cf2c..e9db59266 100644 --- a/src/poetry/core/version/markers.py +++ b/src/poetry/core/version/markers.py @@ -21,6 +21,7 @@ from poetry.core.constraints.generic import UnionConstraint from poetry.core.constraints.generic.parser import STR_CMP_CONSTRAINT from poetry.core.constraints.version import VersionConstraint +from poetry.core.constraints.version import VersionRange from poetry.core.constraints.version import VersionUnion from poetry.core.constraints.version.exceptions import ParseConstraintError from poetry.core.version.grammars import GRAMMAR_PEP_508_MARKERS @@ -446,10 +447,15 @@ def reduce_by_python_constraint( self, python_constraint: VersionConstraint ) -> BaseMarker: if self.name in PYTHON_VERSION_MARKERS: + from poetry.core.packages.utils.utils import ( + get_python_constraint_from_marker, + ) + assert isinstance(self._constraint, VersionConstraint) - if self._constraint.allows_all(python_constraint): + constraint = get_python_constraint_from_marker(self) + if constraint.allows_all(python_constraint): return AnyMarker() - elif not self._constraint.allows_any(python_constraint): + elif not constraint.allows_any(python_constraint): return EmptyMarker() return self @@ -931,7 +937,7 @@ def reduce_by_python_constraint( from poetry.core.packages.utils.utils import get_python_constraint_from_marker markers: Iterable[BaseMarker] = self._markers - if isinstance(python_constraint, VersionUnion): + if isinstance(python_constraint, (VersionRange, VersionUnion)): python_only_markers = [] other_markers = [] for m in self._markers: diff --git a/tests/version/test_markers.py b/tests/version/test_markers.py index 280eecbab..4e1ea27a9 100644 --- a/tests/version/test_markers.py +++ b/tests/version/test_markers.py @@ -1439,7 +1439,12 @@ def test_only(marker: str, only: list[str], expected: str) -> None: ("", "~3.8", ""), ('sys_platform == "linux"', "~3.8", 'sys_platform == "linux"'), ('python_version >= "3.8"', "~3.8", ""), - ('python_version > "3.8"', "~3.8", 'python_version > "3.8"'), + ('python_version == "3.8"', ">=3.8.7,<3.9.0", ""), + ('python_version == "3.8" or python_version >= "3.9"', ">=3.8.0,<4.0.0", ""), + ('python_version == "3.8" or python_version >= "3.9"', ">=3.8.7,<4.0.0", ""), + ('python_version > "3.7"', "~3.8", ""), + ('python_version > "3.8"', "~3.8", ""), + ('python_version >= "3.8"', "~3.8", ""), ('python_version >= "3.9"', "~3.8", ""), ('python_full_version >= "3.8.0"', "~3.8", ""), ('python_full_version >= "3.8.1"', "~3.8", 'python_full_version >= "3.8.1"'), @@ -1503,6 +1508,16 @@ def test_only(marker: str, only: list[str], expected: str) -> None: "~3.7 || ~3.9", 'sys_platform == "linux" or sys_platform == "win32"', ), + ( + 'python_version == "3.8" or sys_platform == "linux" or python_version >= "3.9"', + ">=3.8.0,<4.0.0", + 'sys_platform == "linux"', + ), + ( + 'python_version == "3.8" or sys_platform == "linux" or python_version >= "3.9"', + ">=3.8.7,<4.0.0", + 'sys_platform == "linux"', + ), ], ) def test_reduce_by_python_constraint(