@@ -94,6 +94,22 @@ def _get_required(d: Dict[str, Any], expected_type: Type[T], key: str) -> T:
94
94
return value
95
95
96
96
97
+ def _get_list (
98
+ d : Dict [str , Any ], expected_item_type : Type [T ], key : str
99
+ ) -> Optional [List [T ]]:
100
+ """Get list value from dictionary and verify expected items type."""
101
+ value = _get (d , list , key )
102
+ if value is None :
103
+ return None
104
+ for i , item in enumerate (value ):
105
+ if not isinstance (item , expected_item_type ):
106
+ raise PylockValidationError (
107
+ f"Item { i } of { key !r} has unexpected type { type (item ).__name__ } "
108
+ f"(expected { expected_item_type .__name__ } )"
109
+ )
110
+ return value
111
+
112
+
97
113
def _get_as (
98
114
d : Dict [str , Any ],
99
115
expected_type : Type [T ],
@@ -129,23 +145,18 @@ def _get_required_as(
129
145
130
146
def _get_list_as (
131
147
d : Dict [str , Any ],
132
- expected_type : Type [T ],
133
- target_type : Type [SingleArgConstructorT ],
148
+ expected_item_type : Type [T ],
149
+ target_item_type : Type [SingleArgConstructorT ],
134
150
key : str ,
135
151
) -> Optional [List [SingleArgConstructorT ]]:
136
152
"""Get list value from dictionary and verify expected items type."""
137
- value = _get (d , list , key )
153
+ value = _get_list (d , expected_item_type , key )
138
154
if value is None :
139
155
return None
140
156
result = []
141
157
for i , item in enumerate (value ):
142
- if not isinstance (item , expected_type ):
143
- raise PylockValidationError (
144
- f"Item { i } of { key !r} has unexpected type { type (item ).__name__ } "
145
- f"(expected { expected_type .__name__ } )"
146
- )
147
158
try :
148
- result .append (target_type (item ))
159
+ result .append (target_item_type (item ))
149
160
except Exception as e :
150
161
raise PylockValidationError (f"Error in item { i } of { key !r} : { e } " ) from e
151
162
return result
@@ -165,7 +176,7 @@ def _get_object(
165
176
166
177
167
178
def _get_list_of_objects (
168
- d : Dict [str , Any ], target_type : Type [FromDictProtocolT ], key : str
179
+ d : Dict [str , Any ], target_item_type : Type [FromDictProtocolT ], key : str
169
180
) -> Optional [List [FromDictProtocolT ]]:
170
181
"""Get list value from dictionary and convert items to dataclass."""
171
182
value = _get (d , list , key )
@@ -176,7 +187,7 @@ def _get_list_of_objects(
176
187
if not isinstance (item , dict ):
177
188
raise PylockValidationError (f"Item { i } of { key !r} is not a table" )
178
189
try :
179
- result .append (target_type .from_dict (item ))
190
+ result .append (target_item_type .from_dict (item ))
180
191
except Exception as e :
181
192
raise PylockValidationError (f"Error in item { i } of { key !r} : { e } " ) from e
182
193
return result
0 commit comments