35
35
from pip ._vendor .typing_extensions import Self
36
36
37
37
T = TypeVar ("T" )
38
- T2 = TypeVar ("T2" )
39
38
40
39
41
40
class FromDictProtocol (Protocol ):
42
41
@classmethod
43
- def from_dict (cls , d : Dict [str , Any ]) -> "Self" :
44
- pass
42
+ def from_dict (cls , d : Dict [str , Any ]) -> "Self" : ...
45
43
46
44
47
45
FromDictProtocolT = TypeVar ("FromDictProtocolT" , bound = FromDictProtocol )
48
46
47
+
48
+ class SingleArgConstructor (Protocol ):
49
+ def __init__ (self , value : Any ) -> None : ...
50
+
51
+
52
+ SingleArgConstructorT = TypeVar ("SingleArgConstructorT" , bound = SingleArgConstructor )
53
+
49
54
PYLOCK_FILE_NAME_RE = re .compile (r"^pylock\.([^.]+)\.toml$" )
50
55
51
56
@@ -94,8 +99,11 @@ def _get_required(d: Dict[str, Any], expected_type: Type[T], key: str) -> T:
94
99
95
100
96
101
def _get_as (
97
- d : Dict [str , Any ], expected_type : Type [T ], target_type : Type [T2 ], key : str
98
- ) -> Optional [T2 ]:
102
+ d : Dict [str , Any ],
103
+ expected_type : Type [T ],
104
+ target_type : Type [SingleArgConstructorT ],
105
+ key : str ,
106
+ ) -> Optional [SingleArgConstructorT ]:
99
107
"""Get value from dictionary, verify expected type, convert to target type.
100
108
101
109
This assumes the target_type constructor accepts the value.
@@ -104,14 +112,17 @@ def _get_as(
104
112
if value is None :
105
113
return None
106
114
try :
107
- return target_type (value ) # type: ignore[call-arg]
115
+ return target_type (value )
108
116
except Exception as e :
109
117
raise PylockValidationError (f"Error parsing value of { key !r} : { e } " ) from e
110
118
111
119
112
120
def _get_required_as (
113
- d : Dict [str , Any ], expected_type : Type [T ], target_type : Type [T2 ], key : str
114
- ) -> T2 :
121
+ d : Dict [str , Any ],
122
+ expected_type : Type [T ],
123
+ target_type : Type [SingleArgConstructorT ],
124
+ key : str ,
125
+ ) -> SingleArgConstructorT :
115
126
"""Get required value from dictionary, verify expected type,
116
127
convert to target type."""
117
128
value = _get_as (d , expected_type , target_type , key )
@@ -121,8 +132,11 @@ def _get_required_as(
121
132
122
133
123
134
def _get_list_as (
124
- d : Dict [str , Any ], expected_type : Type [T ], target_type : Type [T2 ], key : str
125
- ) -> Optional [List [T2 ]]:
135
+ d : Dict [str , Any ],
136
+ expected_type : Type [T ],
137
+ target_type : Type [SingleArgConstructorT ],
138
+ key : str ,
139
+ ) -> Optional [List [SingleArgConstructorT ]]:
126
140
"""Get list value from dictionary and verify expected items type."""
127
141
value = _get (d , list , key )
128
142
if value is None :
@@ -135,7 +149,7 @@ def _get_list_as(
135
149
f"(expected { expected_type } )"
136
150
)
137
151
try :
138
- result .append (target_type (item )) # type: ignore[call-arg]
152
+ result .append (target_type (item ))
139
153
except Exception as e :
140
154
raise PylockValidationError (
141
155
f"Error parsing item { i } of { key !r} : { e } "
0 commit comments