Skip to content

Commit ea9f595

Browse files
committed
pylock: move toml export function to utils
1 parent d3beb7f commit ea9f595

File tree

4 files changed

+75
-7
lines changed

4 files changed

+75
-7
lines changed

src/pip/_internal/commands/lock.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from pip._internal.utils.misc import (
2020
get_pip_version,
2121
)
22-
from pip._internal.utils.pylock import pylock_from_install_requirements
22+
from pip._internal.utils.pylock import pylock_from_install_requirements, pylock_to_toml
2323
from pip._internal.utils.temp_dir import TempDirectory
2424

2525
logger = getLogger(__name__)
@@ -161,9 +161,10 @@ def run(self, options: Values, args: List[str]) -> int:
161161
output_file_path,
162162
)
163163
base_dir = output_file_path.parent
164-
pylock_toml = pylock_from_install_requirements(
164+
pylock = pylock_from_install_requirements(
165165
requirement_set.requirements.values(), base_dir=base_dir
166-
).as_toml()
166+
)
167+
pylock_toml = pylock_to_toml(pylock)
167168
if options.output_file == "-":
168169
sys.stdout.write(pylock_toml)
169170
else:

src/pip/_internal/models/pylock.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
Union,
1919
)
2020

21-
from pip._vendor import tomli_w
2221
from pip._vendor.packaging.markers import Marker
2322
from pip._vendor.packaging.specifiers import SpecifierSet
2423
from pip._vendor.packaging.version import Version
@@ -409,9 +408,6 @@ def __post_init__(self) -> None:
409408
"pylock minor version %s is not supported", self.lock_version
410409
)
411410

412-
def as_toml(self) -> str:
413-
return tomli_w.dumps(self.to_dict())
414-
415411
def to_dict(self) -> Dict[str, Any]:
416412
return dataclasses.asdict(self, dict_factory=_toml_dict_factory)
417413

src/pip/_internal/utils/pylock.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from pathlib import Path
22
from typing import Iterable
33

4+
from pip._vendor import tomli_w
45
from pip._vendor.packaging.version import Version
56

67
from pip._internal.models.direct_url import ArchiveInfo, DirInfo, VcsInfo
@@ -126,3 +127,7 @@ def pylock_from_install_requirements(
126127
key=lambda p: p.name,
127128
),
128129
)
130+
131+
132+
def pylock_to_toml(pylock: Pylock) -> str:
133+
return tomli_w.dumps(pylock.to_dict())

tests/unit/test_utils_pylock.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from textwrap import dedent
2+
3+
from pip._vendor import tomli_w
4+
5+
from pip._internal.models.pylock import Pylock
6+
from pip._internal.utils.compat import tomllib
7+
from pip._internal.utils.pylock import pylock_to_toml
8+
9+
# This is the PEP 751 example, with a minor modification to the 'environments'
10+
# field to use double quotes instead of single quotes, since that is what
11+
# 'packaging' does when serializing markers.
12+
13+
14+
PEP751_EXAMPLE = dedent(
15+
"""\
16+
lock-version = '1.0'
17+
environments = ["sys_platform == \\"win32\\"", "sys_platform == \\"linux\\""]
18+
requires-python = '==3.12'
19+
created-by = 'mousebender'
20+
21+
[[packages]]
22+
name = 'attrs'
23+
version = '25.1.0'
24+
requires-python = '>=3.8'
25+
wheels = [
26+
{name = 'attrs-25.1.0-py3-none-any.whl', upload-time = 2025-01-25T11:30:10.164985+00:00, url = 'https://files.pythonhosted.org/packages/fc/30/d4986a882011f9df997a55e6becd864812ccfcd821d64aac8570ee39f719/attrs-25.1.0-py3-none-any.whl', size = 63152, hashes = {sha256 = 'c75a69e28a550a7e93789579c22aa26b0f5b83b75dc4e08fe092980051e1090a'}},
27+
]
28+
[[packages.attestation-identities]]
29+
environment = 'release-pypi'
30+
kind = 'GitHub'
31+
repository = 'python-attrs/attrs'
32+
workflow = 'pypi-package.yml'
33+
34+
[[packages]]
35+
name = 'cattrs'
36+
version = '24.1.2'
37+
requires-python = '>=3.8'
38+
dependencies = [
39+
{name = 'attrs'},
40+
]
41+
wheels = [
42+
{name = 'cattrs-24.1.2-py3-none-any.whl', upload-time = 2024-09-22T14:58:34.812643+00:00, url = 'https://files.pythonhosted.org/packages/c8/d5/867e75361fc45f6de75fe277dd085627a9db5ebb511a87f27dc1396b5351/cattrs-24.1.2-py3-none-any.whl', size = 66446, hashes = {sha256 = '67c7495b760168d931a10233f979b28dc04daf853b30752246f4f8471c6d68d0'}},
43+
]
44+
45+
[[packages]]
46+
name = 'numpy'
47+
version = '2.2.3'
48+
requires-python = '>=3.10'
49+
wheels = [
50+
{name = 'numpy-2.2.3-cp312-cp312-win_amd64.whl', upload-time = 2025-02-13T16:51:21.821880+00:00, url = 'https://files.pythonhosted.org/packages/42/6e/55580a538116d16ae7c9aa17d4edd56e83f42126cb1dfe7a684da7925d2c/numpy-2.2.3-cp312-cp312-win_amd64.whl', size = 12626357, hashes = {sha256 = '83807d445817326b4bcdaaaf8e8e9f1753da04341eceec705c001ff342002e5d'}},
51+
{name = 'numpy-2.2.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl', upload-time = 2025-02-13T16:50:00.079662+00:00, url = 'https://files.pythonhosted.org/packages/39/04/78d2e7402fb479d893953fb78fa7045f7deb635ec095b6b4f0260223091a/numpy-2.2.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl', size = 16116679, hashes = {sha256 = '3b787adbf04b0db1967798dba8da1af07e387908ed1553a0d6e74c084d1ceafe'}},
52+
]
53+
54+
[tool.mousebender]
55+
command = ['.', 'lock', '--platform', 'cpython3.12-windows-x64', '--platform', 'cpython3.12-manylinux2014-x64', 'cattrs', 'numpy']
56+
run-on = 2025-03-06T12:28:57.760769
57+
""" # noqa: E501
58+
)
59+
60+
61+
def test_toml_roundtrip() -> None:
62+
pylock_dict = tomllib.loads(PEP751_EXAMPLE)
63+
pylock = Pylock.from_dict(pylock_dict)
64+
# Check that the roundrip via Pylock dataclasses produces the same toml
65+
# output, modulo TOML serialization differences.
66+
assert pylock_to_toml(pylock) == tomli_w.dumps(pylock_dict)

0 commit comments

Comments
 (0)