Skip to content

Commit 6ae54d3

Browse files
committed
pylock: move toml export function to utils
1 parent 2bd12c4 commit 6ae54d3

File tree

4 files changed

+74
-7
lines changed

4 files changed

+74
-7
lines changed

src/pip/_internal/commands/lock.py

+4-3
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

-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
Union,
2020
)
2121

22-
from pip._vendor import tomli_w
2322
from pip._vendor.packaging.markers import Marker
2423
from pip._vendor.packaging.specifiers import SpecifierSet
2524
from pip._vendor.packaging.version import Version
@@ -416,9 +415,6 @@ def __post_init__(self) -> None:
416415
"pylock minor version %s is not supported", self.lock_version
417416
)
418417

419-
def as_toml(self) -> str:
420-
return tomli_w.dumps(self.to_dict())
421-
422418
def to_dict(self) -> Dict[str, Any]:
423419
return dataclasses.asdict(self, dict_factory=_toml_dict_factory)
424420

src/pip/_internal/utils/pylock.py

+5
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
@@ -133,3 +134,7 @@ def pylock_from_install_requirements(
133134
),
134135
tool=None,
135136
)
137+
138+
139+
def pylock_to_toml(pylock: Pylock) -> str:
140+
return tomli_w.dumps(pylock.to_dict())

tests/unit/test_utils_pylock.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
PEP751_EXAMPLE = dedent(
14+
"""\
15+
lock-version = '1.0'
16+
environments = ["sys_platform == \\"win32\\"", "sys_platform == \\"linux\\""]
17+
requires-python = '==3.12'
18+
created-by = 'mousebender'
19+
20+
[[packages]]
21+
name = 'attrs'
22+
version = '25.1.0'
23+
requires-python = '>=3.8'
24+
wheels = [
25+
{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'}},
26+
]
27+
[[packages.attestation-identities]]
28+
environment = 'release-pypi'
29+
kind = 'GitHub'
30+
repository = 'python-attrs/attrs'
31+
workflow = 'pypi-package.yml'
32+
33+
[[packages]]
34+
name = 'cattrs'
35+
version = '24.1.2'
36+
requires-python = '>=3.8'
37+
dependencies = [
38+
{name = 'attrs'},
39+
]
40+
wheels = [
41+
{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'}},
42+
]
43+
44+
[[packages]]
45+
name = 'numpy'
46+
version = '2.2.3'
47+
requires-python = '>=3.10'
48+
wheels = [
49+
{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'}},
50+
{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'}},
51+
]
52+
53+
[tool.mousebender]
54+
command = ['.', 'lock', '--platform', 'cpython3.12-windows-x64', '--platform', 'cpython3.12-manylinux2014-x64', 'cattrs', 'numpy']
55+
run-on = 2025-03-06T12:28:57.760769
56+
""" # noqa: E501
57+
)
58+
59+
60+
def test_toml_roundtrip() -> None:
61+
pylock_dict = tomllib.loads(PEP751_EXAMPLE)
62+
pylock = Pylock.from_dict(pylock_dict)
63+
# Check that the roundrip via Pylock dataclasses produces the same toml
64+
# output, modulo TOML serialization differences.
65+
assert pylock_to_toml(pylock) == tomli_w.dumps(pylock_dict)

0 commit comments

Comments
 (0)