Skip to content

Commit 58aaa7f

Browse files
authored
pylock support for Python pre-releases (#1179)
1 parent e7b42de commit 58aaa7f

3 files changed

Lines changed: 40 additions & 5 deletions

File tree

src/packaging/markers.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,16 @@ def evaluate(
484484
)
485485

486486

487+
def _pep440_python_full_version(python_full_version: str) -> str:
488+
"""
489+
Work around platform.python_version() returning something that is not PEP 440
490+
compliant for non-tagged Python builds.
491+
"""
492+
if python_full_version.endswith("+"):
493+
return f"{python_full_version}local"
494+
return python_full_version
495+
496+
487497
def _repair_python_full_version(
488498
env: dict[str, str | AbstractSet[str]],
489499
) -> dict[str, str | AbstractSet[str]]:
@@ -492,6 +502,5 @@ def _repair_python_full_version(
492502
compliant for non-tagged Python builds.
493503
"""
494504
python_full_version = cast("str", env["python_full_version"])
495-
if python_full_version.endswith("+"):
496-
env["python_full_version"] = f"{python_full_version}local"
505+
env["python_full_version"] = _pep440_python_full_version(python_full_version)
497506
return env

src/packaging/pylock.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
)
1717
from urllib.parse import urlparse
1818

19-
from .markers import Environment, Marker, default_environment
19+
from .markers import (
20+
Environment,
21+
Marker,
22+
_pep440_python_full_version,
23+
default_environment,
24+
)
2025
from .specifiers import SpecifierSet
2126
from .tags import create_compatible_tags_selector, sys_tags
2227
from .utils import (
@@ -782,7 +787,7 @@ def select(
782787
),
783788
),
784789
)
785-
env_python_full_version = (
790+
env_python_full_version = _pep440_python_full_version(
786791
environment["python_full_version"]
787792
if environment
788793
else default_environment()["python_full_version"]

tests/test_pylock_select.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import pytest
99

10-
from packaging.markers import Marker
10+
from packaging.markers import Marker, default_environment
1111
from packaging.pylock import (
1212
Package,
1313
PackageArchive,
@@ -369,3 +369,24 @@ def test_extras_and_groups(
369369
)
370370
]
371371
assert selected_names == expected
372+
373+
374+
def test_python_prerelease() -> None:
375+
"""Python pre-release versions are not PEP 440 compliant.
376+
Test that Pylock.requires_python supports that.
377+
"""
378+
pylock = Pylock(
379+
lock_version=Version("1.0"),
380+
created_by="repro",
381+
requires_python=SpecifierSet(">=3.12"),
382+
packages=[
383+
Package(
384+
name=cast("NormalizedName", "pkga"),
385+
requires_python=SpecifierSet(">=3.12"),
386+
directory=PackageDirectory(path="./pkga"),
387+
)
388+
],
389+
)
390+
env = default_environment()
391+
env["python_full_version"] = "3.15.0a8+"
392+
list(pylock.select(environment=env))

0 commit comments

Comments
 (0)