From 3a77c1021222706bce6d55e19276e6dc0915d807 Mon Sep 17 00:00:00 2001 From: sohailshk <122166523+sohailshk@users.noreply.github.com> Date: Sat, 21 Nov 2026 16:42:12 +0530 Subject: [PATCH] Keep the precision of ~= in python_full_version markers A two-segment value such as python_full_version ~= "3.8" was padded to "3.8.0", which turned >=3.8,==3.* into >=3.8.0,==3.8.*. --- src/poetry/core/version/markers.py | 10 +++++-- tests/version/test_markers.py | 46 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/poetry/core/version/markers.py b/src/poetry/core/version/markers.py index de053a392..030aa5e24 100644 --- a/src/poetry/core/version/markers.py +++ b/src/poetry/core/version/markers.py @@ -446,7 +446,12 @@ def __init__( glue = " || " constraint_string = glue.join(versions) - elif name == "python_full_version" and not swapped_name_value: + elif ( + name == "python_full_version" + and not swapped_name_value + # the precision of a compatible release clause is significant + and self._operator != "~=" + ): # fix precision of python_full_version marker precision = self._value.count(".") + 1 if precision < 3: @@ -1415,7 +1420,8 @@ def _merge_python_version_single_markers( marker_string = str(merged_marker) precision = marker_string.count(".") + 1 target_precision = 3 - if precision < target_precision: + # the precision of a compatible release clause is significant + if precision < target_precision and merged_marker.operator != "~=": if merged_marker.operator in {"<", ">="}: target_precision = 2 marker_string = marker_string.replace( diff --git a/tests/version/test_markers.py b/tests/version/test_markers.py index dbdc5d864..2b172d292 100644 --- a/tests/version/test_markers.py +++ b/tests/version/test_markers.py @@ -160,6 +160,52 @@ def test_parse_single_marker( assert str(m.constraint) == expected_constraint +@pytest.mark.parametrize( + ("marker", "python_full_version", "expected"), + [ + ('python_full_version ~= "3.8"', "3.7.9", False), + ('python_full_version ~= "3.8"', "3.8.0", True), + ('python_full_version ~= "3.8"', "3.9.1", True), + ('python_full_version ~= "3.8"', "4.0.0", False), + ('python_full_version ~= "3.8.0"', "3.8.5", True), + ('python_full_version ~= "3.8.0"', "3.9.1", False), + ('python_full_version ~= "3.8.2"', "3.8.1", False), + ('python_full_version ~= "3.8.2"', "3.8.3", True), + ], +) +def test_python_full_version_compatible_release_keeps_precision( + marker: str, python_full_version: str, expected: bool +) -> None: + # "~= 3.8" means ">= 3.8, == 3.*" and must not be read as "~= 3.8.0" + m = parse_marker(marker) + environment = { + "python_full_version": python_full_version, + "python_version": ".".join(python_full_version.split(".")[:2]), + } + + assert m.validate(environment) is expected + + +@pytest.mark.parametrize( + "marker2", + ['python_version == "3.9"', 'python_version >= "3.9"', 'python_version > "3.8"'], +) +def test_python_version_markers_with_compatible_release_keep_semantics( + marker2: str, +) -> None: + m1 = parse_marker('python_full_version ~= "3.9"') + m2 = parse_marker(marker2) + for python_full_version in ["3.8.9", "3.9.0", "3.9.7", "3.10.1", "3.12.0", "4.0.0"]: + environment = { + "python_full_version": python_full_version, + "python_version": ".".join(python_full_version.split(".")[:2]), + } + v1 = m1.validate(environment) + v2 = m2.validate(environment) + assert m1.union(m2).validate(environment) is (v1 or v2) + assert m1.intersect(m2).validate(environment) is (v1 and v2) + + def test_single_marker_normalisation() -> None: m1 = SingleMarker("python_version", ">=3.6") m2 = SingleMarker("python_version", ">= 3.6")