41
41
"is_valid_pylock_path" ,
42
42
]
43
43
44
- T = TypeVar ("T " )
45
- T2 = TypeVar ("T2 " )
44
+ _T = TypeVar ("_T " )
45
+ _T2 = TypeVar ("_T2 " )
46
46
47
47
48
- class FromMappingProtocol (Protocol ): # pragma: no cover
48
+ class _FromMappingProtocol (Protocol ): # pragma: no cover
49
49
@classmethod
50
50
def _from_dict (cls , d : Mapping [str , Any ]) -> Self : ...
51
51
52
52
53
- FromMappingProtocolT = TypeVar ("FromMappingProtocolT " , bound = FromMappingProtocol )
53
+ _FromMappingProtocolT = TypeVar ("_FromMappingProtocolT " , bound = _FromMappingProtocol )
54
54
55
55
56
- PYLOCK_FILE_NAME_RE = re .compile (r"^pylock\.([^.]+)\.toml$" )
56
+ _PYLOCK_FILE_NAME_RE = re .compile (r"^pylock\.([^.]+)\.toml$" )
57
57
58
58
59
59
def is_valid_pylock_path (path : Path ) -> bool :
60
- return path .name == "pylock.toml" or bool (PYLOCK_FILE_NAME_RE .match (path .name ))
60
+ return path .name == "pylock.toml" or bool (_PYLOCK_FILE_NAME_RE .match (path .name ))
61
61
62
62
63
63
def _str_to_normalized_name (name : str ) -> NormalizedName :
@@ -87,7 +87,7 @@ def _toml_dict_factory(data: list[tuple[str, Any]]) -> dict[str, Any]:
87
87
}
88
88
89
89
90
- def _get (d : Mapping [str , Any ], expected_type : type [T ], key : str ) -> T | None :
90
+ def _get (d : Mapping [str , Any ], expected_type : type [_T ], key : str ) -> _T | None :
91
91
"""Get a value from the dictionary and verify it's the expected type."""
92
92
if (value := d .get (key )) is None :
93
93
return None
@@ -100,16 +100,16 @@ def _get(d: Mapping[str, Any], expected_type: type[T], key: str) -> T | None:
100
100
return value
101
101
102
102
103
- def _get_required (d : Mapping [str , Any ], expected_type : type [T ], key : str ) -> T :
103
+ def _get_required (d : Mapping [str , Any ], expected_type : type [_T ], key : str ) -> _T :
104
104
"""Get a required value from the dictionary and verify it's the expected type."""
105
105
if (value := _get (d , expected_type , key )) is None :
106
106
raise PylockRequiredKeyError (key )
107
107
return value
108
108
109
109
110
110
def _get_sequence (
111
- d : Mapping [str , Any ], expected_item_type : type [T ], key : str
112
- ) -> Sequence [T ] | None :
111
+ d : Mapping [str , Any ], expected_item_type : type [_T ], key : str
112
+ ) -> Sequence [_T ] | None :
113
113
"""Get a list value from the dictionary and verify it's the expected items type."""
114
114
value = _get (d , Sequence , key ) # type: ignore[type-abstract]
115
115
if value is None :
@@ -126,10 +126,10 @@ def _get_sequence(
126
126
127
127
def _get_as (
128
128
d : Mapping [str , Any ],
129
- expected_type : type [T ],
130
- target_type : Callable [[T ], T2 ],
129
+ expected_type : type [_T ],
130
+ target_type : Callable [[_T ], _T2 ],
131
131
key : str ,
132
- ) -> T2 | None :
132
+ ) -> _T2 | None :
133
133
"""Get a value from the dictionary, verify it's the expected type,
134
134
and convert to the target type.
135
135
@@ -145,10 +145,10 @@ def _get_as(
145
145
146
146
def _get_required_as (
147
147
d : Mapping [str , Any ],
148
- expected_type : type [T ],
149
- target_type : Callable [[T ], T2 ],
148
+ expected_type : type [_T ],
149
+ target_type : Callable [[_T ], _T2 ],
150
150
key : str ,
151
- ) -> T2 :
151
+ ) -> _T2 :
152
152
"""Get a required value from the dict, verify it's the expected type,
153
153
and convert to the target type."""
154
154
if (value := _get_as (d , expected_type , target_type , key )) is None :
@@ -158,10 +158,10 @@ def _get_required_as(
158
158
159
159
def _get_sequence_as (
160
160
d : Mapping [str , Any ],
161
- expected_item_type : type [T ],
162
- target_item_type : Callable [[T ], T2 ],
161
+ expected_item_type : type [_T ],
162
+ target_item_type : Callable [[_T ], _T2 ],
163
163
key : str ,
164
- ) -> Sequence [T2 ] | None :
164
+ ) -> Sequence [_T2 ] | None :
165
165
"""Get list value from dictionary and verify expected items type."""
166
166
if (value := _get_sequence (d , expected_item_type , key )) is None :
167
167
return None
@@ -177,8 +177,8 @@ def _get_sequence_as(
177
177
178
178
179
179
def _get_object (
180
- d : Mapping [str , Any ], target_type : type [FromMappingProtocolT ], key : str
181
- ) -> FromMappingProtocolT | None :
180
+ d : Mapping [str , Any ], target_type : type [_FromMappingProtocolT ], key : str
181
+ ) -> _FromMappingProtocolT | None :
182
182
"""Get a dictionary value from the dictionary and convert it to a dataclass."""
183
183
value = _get (d , Mapping , key ) # type: ignore[type-abstract]
184
184
if value is None :
@@ -190,8 +190,8 @@ def _get_object(
190
190
191
191
192
192
def _get_sequence_of_objects (
193
- d : Mapping [str , Any ], target_item_type : type [FromMappingProtocolT ], key : str
194
- ) -> Sequence [FromMappingProtocolT ] | None :
193
+ d : Mapping [str , Any ], target_item_type : type [_FromMappingProtocolT ], key : str
194
+ ) -> Sequence [_FromMappingProtocolT ] | None :
195
195
"""Get a list value from the dictionary and convert its items to a dataclass."""
196
196
value = _get (d , Sequence , key ) # type: ignore[type-abstract]
197
197
if value is None :
@@ -213,8 +213,8 @@ def _get_sequence_of_objects(
213
213
214
214
215
215
def _get_required_list_of_objects (
216
- d : Mapping [str , Any ], target_type : type [FromMappingProtocolT ], key : str
217
- ) -> Sequence [FromMappingProtocolT ]:
216
+ d : Mapping [str , Any ], target_type : type [_FromMappingProtocolT ], key : str
217
+ ) -> Sequence [_FromMappingProtocolT ]:
218
218
"""Get a required list value from the dictionary and convert its items to a
219
219
dataclass."""
220
220
if (result := _get_sequence_of_objects (d , target_type , key )) is None :
0 commit comments