Skip to content

Commit 8e535ff

Browse files
committed
pylock: read tool sections
1 parent d3beb7f commit 8e535ff

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

src/pip/_internal/models/pylock.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -343,18 +343,18 @@ def from_dict(cls, d: Dict[str, Any]) -> "Self":
343343
@dataclass
344344
class Package:
345345
name: str
346-
version: Optional[Version] = None
347-
marker: Optional[Marker] = None
348-
requires_python: Optional[SpecifierSet] = None
346+
version: Optional[Version]
347+
marker: Optional[Marker]
348+
requires_python: Optional[SpecifierSet]
349349
# (not supported) dependencies
350-
vcs: Optional[PackageVcs] = None
351-
directory: Optional[PackageDirectory] = None
352-
archive: Optional[PackageArchive] = None
350+
vcs: Optional[PackageVcs]
351+
directory: Optional[PackageDirectory]
352+
archive: Optional[PackageArchive]
353353
# (not supported) index: Optional[str]
354-
sdist: Optional[PackageSdist] = None
355-
wheels: Optional[List[PackageWheel]] = None
354+
sdist: Optional[PackageSdist]
355+
wheels: Optional[List[PackageWheel]]
356356
# (not supported) attestation_identities: Optional[List[Dict[str, Any]]]
357-
# (not supported) tool: Optional[Dict[str, Any]]
357+
tool: Optional[Dict[str, Any]]
358358

359359
def __post_init__(self) -> None:
360360
if self.sdist or self.wheels:
@@ -383,6 +383,7 @@ def from_dict(cls, d: Dict[str, Any]) -> "Self":
383383
archive=_get_object(d, PackageArchive, "archive"),
384384
sdist=_get_object(d, PackageSdist, "sdist"),
385385
wheels=_get_list_of_objects(d, PackageWheel, "wheels"),
386+
tool=_get(d, dict, "tool"),
386387
)
387388
return package
388389

@@ -397,7 +398,7 @@ class Pylock:
397398
default_groups: List[str]
398399
created_by: str
399400
packages: List[Package]
400-
# (not supported) tool: Optional[Dict[str, Any]]
401+
tool: Optional[Dict[str, Any]]
401402

402403
def __post_init__(self) -> None:
403404
if self.lock_version < Version("1") or self.lock_version >= Version("2"):
@@ -426,4 +427,5 @@ def from_dict(cls, d: Dict[str, Any]) -> "Self":
426427
created_by=_get_required(d, str, "created-by"),
427428
requires_python=_get_as(d, str, SpecifierSet, "requires-python"),
428429
packages=_get_required_list_of_objects(d, Package, "packages"),
430+
tool=_get(d, dict, "tool"),
429431
)

src/pip/_internal/utils/pylock.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def _pylock_package_from_install_requirement(
104104
archive=package_archive,
105105
sdist=package_sdist,
106106
wheels=package_wheels,
107+
tool=None,
107108
)
108109

109110

@@ -125,4 +126,5 @@ def pylock_from_install_requirements(
125126
),
126127
key=lambda p: p.name,
127128
),
129+
tool=None,
128130
)

tests/unit/test_pylock.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,26 @@ def test_pylock_extras_and_groups() -> None:
227227
assert pylock.extras == ["feat1", "feat2"]
228228
assert pylock.dependency_groups == ["dev", "docs"]
229229
assert pylock.default_groups == ["dev"]
230+
231+
232+
def test_pylock_tool() -> None:
233+
data = {
234+
"lock-version": "1.0",
235+
"created-by": "pip",
236+
"packages": [
237+
{
238+
"name": "example",
239+
"sdist": {
240+
"name": "example-1.0.tar.gz",
241+
"path": "./example-1.0.tar.gz",
242+
"hashes": {"sha256": "f" * 40},
243+
},
244+
"tool": {"pip": {"foo": "bar"}},
245+
}
246+
],
247+
"tool": {"pip": {"version": "25.2"}},
248+
}
249+
pylock = Pylock.from_dict(data)
250+
assert pylock.tool == {"pip": {"version": "25.2"}}
251+
package = pylock.packages[0]
252+
assert package.tool == {"pip": {"foo": "bar"}}

0 commit comments

Comments
 (0)