Skip to content

Commit 3e67fc7

Browse files
authored
Work around platform.python_version() returning non PEP 440 compliant version for non-tagged CPython builds (#802)
1 parent 32deafe commit 3e67fc7

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/packaging/markers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,12 @@ def evaluate(self, environment: dict[str, str] | None = None) -> bool:
309309
"""
310310
current_environment = cast("dict[str, str]", default_environment())
311311
current_environment["extra"] = ""
312+
# Work around platform.python_version() returning something that is not PEP 440
313+
# compliant for non-tagged Python builds. We preserve default_environment()'s
314+
# behavior of returning platform.python_version() verbatim, and leave it to the
315+
# caller to provide a syntactically valid version if they want to override it.
316+
if current_environment["python_full_version"].endswith("+"):
317+
current_environment["python_full_version"] += "local"
312318
if environment is not None:
313319
current_environment.update(environment)
314320
# The API used to allow setting extra to None. We need to handle this

tests/test_markers.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import platform
99
import sys
1010
from typing import cast
11+
from unittest import mock
1112

1213
import pytest
1314

@@ -19,6 +20,7 @@
1920
default_environment,
2021
format_full_version,
2122
)
23+
from packaging.version import InvalidVersion
2224

2325
VARIABLES = [
2426
"extra",
@@ -384,3 +386,14 @@ def test_extra_str_normalization(self):
384386

385387
assert str(Marker(lhs)) == f'"{normalized_name}" == extra'
386388
assert str(Marker(rhs)) == f'extra == "{normalized_name}"'
389+
390+
def test_python_full_version_untagged_user_provided(self):
391+
"""A user-provided python_full_version ending with a + fails to parse."""
392+
with pytest.raises(InvalidVersion):
393+
Marker("python_full_version < '3.12'").evaluate(
394+
{"python_full_version": "3.11.1+"}
395+
)
396+
397+
def test_python_full_version_untagged(self):
398+
with mock.patch("platform.python_version", return_value="3.11.1+"):
399+
assert Marker("python_full_version < '3.12'").evaluate()

0 commit comments

Comments
 (0)