18
18
19
19
from pip ._vendor import tomli_w
20
20
from pip ._vendor .packaging .markers import InvalidMarker , Marker
21
+ from pip ._vendor .packaging .specifiers import InvalidSpecifier , SpecifierSet
21
22
from pip ._vendor .packaging .version import InvalidVersion , Version
22
23
from pip ._vendor .typing_extensions import Self
23
24
@@ -49,7 +50,7 @@ def _toml_key(key: str) -> str:
49
50
50
51
51
52
def _toml_value (value : T ) -> Union [str , T ]:
52
- if isinstance (value , (Version , Marker )):
53
+ if isinstance (value , (Version , Marker , SpecifierSet )):
53
54
return str (value )
54
55
return value
55
56
@@ -109,6 +110,16 @@ def _get_marker(d: Dict[str, Any], key: str) -> Optional[Marker]:
109
110
raise PylockValidationError (f"invalid marker { value !r} " )
110
111
111
112
113
+ def _get_specifier_set (d : Dict [str , Any ], key : str ) -> Optional [SpecifierSet ]:
114
+ value = _get (d , str , key )
115
+ if value is None :
116
+ return None
117
+ try :
118
+ return SpecifierSet (value )
119
+ except InvalidSpecifier :
120
+ raise PylockValidationError (f"invalid version specifier { value !r} " )
121
+
122
+
112
123
def _get_object (
113
124
d : Dict [str , Any ], expected_type : Type [PylockDataClassT ], key : str
114
125
) -> Optional [PylockDataClassT ]:
@@ -286,7 +297,7 @@ class Package:
286
297
name : str
287
298
version : Optional [Version ] = None
288
299
marker : Optional [Marker ] = None
289
- # (not supported) requires_python: Optional[str]
300
+ requires_python : Optional [SpecifierSet ] = None
290
301
# (not supported) dependencies
291
302
vcs : Optional [PackageVcs ] = None
292
303
directory : Optional [PackageDirectory ] = None
@@ -307,6 +318,7 @@ def from_dict(cls, d: Dict[str, Any]) -> Self:
307
318
package = cls (
308
319
name = _get_required (d , str , "name" ),
309
320
version = _get_version (d , "version" ),
321
+ requires_python = _get_specifier_set (d , "requires-python" ),
310
322
marker = _get_marker (d , "marker" ),
311
323
vcs = _get_object (d , PackageVcs , "vcs" ),
312
324
directory = _get_object (d , PackageDirectory , "directory" ),
@@ -394,6 +406,7 @@ def from_install_requirement(cls, ireq: InstallRequirement, base_dir: Path) -> S
394
406
name = dist .canonical_name ,
395
407
version = package_version ,
396
408
marker = None ,
409
+ requires_python = None ,
397
410
vcs = package_vcs ,
398
411
directory = package_directory ,
399
412
archive = package_archive ,
@@ -406,7 +419,7 @@ def from_install_requirement(cls, ireq: InstallRequirement, base_dir: Path) -> S
406
419
class Pylock :
407
420
lock_version : Version = Version ("1.0" )
408
421
# (not supported) environments: Optional[List[str]]
409
- # (not supported) requires_python: Optional[str]
422
+ requires_python : Optional [SpecifierSet ] = None
410
423
# (not supported) extras: List[str] = []
411
424
# (not supported) dependency_groups: List[str] = []
412
425
created_by : str = "pip"
@@ -434,6 +447,7 @@ def from_dict(cls, d: Dict[str, Any]) -> Self:
434
447
return cls (
435
448
lock_version = _get_required_version (d , "lock-version" ),
436
449
created_by = _get_required (d , str , "created-by" ),
450
+ requires_python = _get_specifier_set (d , "requires-python" ),
437
451
packages = _get_required_list_of_objects (d , Package , "packages" ),
438
452
)
439
453
0 commit comments