@@ -49,15 +49,19 @@ def _toml_key(key: str) -> str:
49
49
return key .replace ("_" , "-" )
50
50
51
51
52
- def _toml_value (value : T ) -> Union [str , T ]:
52
+ def _toml_value (key : str , value : T ) -> Union [str , List [ str ] , T ]:
53
53
if isinstance (value , (Version , Marker , SpecifierSet )):
54
54
return str (value )
55
+ if isinstance (value , list ) and key == "environments" :
56
+ return [str (v ) for v in value ]
55
57
return value
56
58
57
59
58
60
def _toml_dict_factory (data : List [Tuple [str , Any ]]) -> Dict [str , Any ]:
59
61
return {
60
- _toml_key (key ): _toml_value (value ) for key , value in data if value is not None
62
+ _toml_key (key ): _toml_value (key , value )
63
+ for key , value in data
64
+ if value is not None
61
65
}
62
66
63
67
@@ -110,6 +114,27 @@ def _get_marker(d: Dict[str, Any], key: str) -> Optional[Marker]:
110
114
raise PylockValidationError (f"invalid marker { value !r} " )
111
115
112
116
117
+ def _get_list_of_markers (d : Dict [str , Any ], key : str ) -> Optional [List [Marker ]]:
118
+ """Get list value from dictionary and verify expected items type."""
119
+ if key not in d :
120
+ return None
121
+ value = d [key ]
122
+ if not isinstance (value , list ):
123
+ raise PylockValidationError (f"{ key !r} is not a list" )
124
+ result = []
125
+ for i , item in enumerate (value ):
126
+ if not isinstance (item , str ):
127
+ raise PylockValidationError (f"Item { i } in list { key !r} is not a string" )
128
+ try :
129
+ result .append (Marker (item ))
130
+ except InvalidMarker :
131
+ raise PylockValidationError (
132
+ f"Item { i } in list { key !r} "
133
+ f"is not a valid environment marker: { item !r} "
134
+ )
135
+ return result
136
+
137
+
113
138
def _get_specifier_set (d : Dict [str , Any ], key : str ) -> Optional [SpecifierSet ]:
114
139
value = _get (d , str , key )
115
140
if value is None :
@@ -418,7 +443,7 @@ def from_install_requirement(cls, ireq: InstallRequirement, base_dir: Path) -> S
418
443
@dataclass
419
444
class Pylock :
420
445
lock_version : Version = Version ("1.0" )
421
- # (not supported) environments: Optional[List[str]]
446
+ environments : Optional [List [Marker ]] = None
422
447
requires_python : Optional [SpecifierSet ] = None
423
448
# (not supported) extras: List[str] = []
424
449
# (not supported) dependency_groups: List[str] = []
@@ -449,6 +474,7 @@ def to_dict(self) -> Dict[str, Any]:
449
474
def from_dict (cls , d : Dict [str , Any ]) -> Self :
450
475
return cls (
451
476
lock_version = _get_required_version (d , "lock-version" ),
477
+ environments = _get_list_of_markers (d , "environments" ),
452
478
created_by = _get_required (d , str , "created-by" ),
453
479
requires_python = _get_specifier_set (d , "requires-python" ),
454
480
packages = _get_required_list_of_objects (d , Package , "packages" ),
0 commit comments