Skip to content

Commit c385b58

Browse files
authored
Patch python_full_version unconditionally (pypa#825)
* Extract a method for repairing the python_full_version. * Repair the version even if passed in. * Update test to reflect the new expectation.
1 parent ead9d34 commit c385b58

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

src/packaging/markers.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -309,17 +309,23 @@ 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"
318312
if environment is not None:
319313
current_environment.update(environment)
320314
# The API used to allow setting extra to None. We need to handle this
321315
# case for backwards compatibility.
322316
if current_environment["extra"] is None:
323317
current_environment["extra"] = ""
324318

325-
return _evaluate_markers(self._markers, current_environment)
319+
return _evaluate_markers(
320+
self._markers, _repair_python_full_version(current_environment)
321+
)
322+
323+
324+
def _repair_python_full_version(env: dict[str, str]) -> dict[str, str]:
325+
"""
326+
Work around platform.python_version() returning something that is not PEP 440
327+
compliant for non-tagged Python builds.
328+
"""
329+
if env["python_full_version"].endswith("+"):
330+
env["python_full_version"] += "local"
331+
return env

tests/test_markers.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
default_environment,
2121
format_full_version,
2222
)
23-
from packaging.version import InvalidVersion
2423

2524
VARIABLES = [
2625
"extra",
@@ -391,11 +390,10 @@ def test_extra_str_normalization(self):
391390
assert str(Marker(rhs)) == f'extra == "{normalized_name}"'
392391

393392
def test_python_full_version_untagged_user_provided(self):
394-
"""A user-provided python_full_version ending with a + fails to parse."""
395-
with pytest.raises(InvalidVersion):
396-
Marker("python_full_version < '3.12'").evaluate(
397-
{"python_full_version": "3.11.1+"}
398-
)
393+
"""A user-provided python_full_version ending with a + is also repaired."""
394+
assert Marker("python_full_version < '3.12'").evaluate(
395+
{"python_full_version": "3.11.1+"}
396+
)
399397

400398
def test_python_full_version_untagged(self):
401399
with mock.patch("platform.python_version", return_value="3.11.1+"):

0 commit comments

Comments
 (0)