Skip to content

Commit

Permalink
Patch python_full_version unconditionally (#825)
Browse files Browse the repository at this point in the history
* Extract a method for repairing the python_full_version.

* Repair the version even if passed in.

* Update test to reflect the new expectation.
  • Loading branch information
jaraco committed Aug 27, 2024
1 parent ead9d34 commit c385b58
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
20 changes: 13 additions & 7 deletions src/packaging/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,17 +309,23 @@ def evaluate(self, environment: dict[str, str] | None = None) -> bool:
"""
current_environment = cast("dict[str, str]", default_environment())
current_environment["extra"] = ""
# Work around platform.python_version() returning something that is not PEP 440
# compliant for non-tagged Python builds. We preserve default_environment()'s
# behavior of returning platform.python_version() verbatim, and leave it to the
# caller to provide a syntactically valid version if they want to override it.
if current_environment["python_full_version"].endswith("+"):
current_environment["python_full_version"] += "local"
if environment is not None:
current_environment.update(environment)
# The API used to allow setting extra to None. We need to handle this
# case for backwards compatibility.
if current_environment["extra"] is None:
current_environment["extra"] = ""

return _evaluate_markers(self._markers, current_environment)
return _evaluate_markers(
self._markers, _repair_python_full_version(current_environment)
)


def _repair_python_full_version(env: dict[str, str]) -> dict[str, str]:
"""
Work around platform.python_version() returning something that is not PEP 440
compliant for non-tagged Python builds.
"""
if env["python_full_version"].endswith("+"):
env["python_full_version"] += "local"
return env
10 changes: 4 additions & 6 deletions tests/test_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
default_environment,
format_full_version,
)
from packaging.version import InvalidVersion

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

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

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

0 comments on commit c385b58

Please sign in to comment.