Skip to content
Open
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
10 changes: 8 additions & 2 deletions src/poetry/core/version/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "~="

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · One-segment markers export invalidly

When a dependency uses python_full_version ~= "3", SingleMarker preserves python_full_version ~= "3", and the version parser accepts it as a range when the release has fewer than two parts. The resulting marker violates PEP 440’s minimum precision for ~=, so downstream packaging tools reject it when evaluating the exported marker.

Reject one-segment compatible-release markers, or otherwise ensure they are emitted in a valid PEP 440 form.

Prompt for AI agents
In `src/poetry/core/version/markers.py` at line 453:

**One-segment markers export invalidly**

When a dependency uses `python_full_version ~= "3"`, `SingleMarker` preserves `python_full_version ~= "3"`, and the version parser accepts it as a range when the release has fewer than two parts. The resulting marker violates PEP 440’s minimum precision for `~=`, so downstream packaging tools reject it when evaluating the exported marker.

Reject one-segment compatible-release markers, or otherwise ensure they are emitted in a valid PEP 440 form.

):
# fix precision of python_full_version marker
precision = self._value.count(".") + 1
if precision < 3:
Expand Down Expand Up @@ -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(
Expand Down
46 changes: 46 additions & 0 deletions tests/version/test_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading