@@ -142,34 +142,13 @@ def _unmarshal_properties(
142
142
143
143
class MultiTypeUnmarshaller (PrimitiveUnmarshaller ):
144
144
def __call__ (self , value : Any ) -> Any :
145
- unmarshaller = self ._get_best_unmarshaller (value )
145
+ primitive_type = self .schema_validator .get_primitive_type (value )
146
+ unmarshaller = self .schema_unmarshaller .get_type_unmarshaller (primitive_type )
146
147
return unmarshaller (value )
147
148
148
- @property
149
- def type (self ) -> List [str ]:
150
- types = self .schema .getkey ("type" , ["any" ])
151
- assert isinstance (types , list )
152
- return types
153
-
154
- def _get_best_unmarshaller (self , value : Any ) -> "PrimitiveUnmarshaller" :
155
- for schema_type in self .type :
156
- result = self .schema_validator .type_validator (
157
- value , type_override = schema_type
158
- )
159
- if not result :
160
- continue
161
- result = self .schema_validator .format_validator (value )
162
- if not result :
163
- continue
164
- return self .schema_unmarshaller .get_type_unmarshaller (schema_type )
165
-
166
- raise UnmarshallerError ("Unmarshaller not found for type(s)" )
167
-
168
149
169
150
class AnyUnmarshaller (MultiTypeUnmarshaller ):
170
- @property
171
- def type (self ) -> List [str ]:
172
- return self .schema_unmarshaller .types_unmarshaller .get_types ()
151
+ pass
173
152
174
153
175
154
class TypesUnmarshaller :
@@ -189,7 +168,7 @@ def __init__(
189
168
def get_types (self ) -> List [str ]:
190
169
return list (self .unmarshallers .keys ())
191
170
192
- def get_unmarshaller (
171
+ def get_unmarshaller_cls (
193
172
self ,
194
173
schema_type : Optional [Union [Iterable [str ], str ]],
195
174
) -> Type ["PrimitiveUnmarshaller" ]:
@@ -228,8 +207,8 @@ def unmarshal(self, schema_format: str, value: Any) -> Any:
228
207
return value
229
208
try :
230
209
return format_unmarshaller (value )
231
- except (ValueError , TypeError ) as exc :
232
- raise FormatUnmarshalError ( value , schema_format , exc )
210
+ except (AttributeError , ValueError , TypeError ) as exc :
211
+ return value
233
212
234
213
def get_unmarshaller (
235
214
self , schema_format : str
@@ -288,22 +267,33 @@ def unmarshal(self, value: Any) -> Any:
288
267
schema_type = self .schema .getkey ("type" )
289
268
type_unmarshaller = self .get_type_unmarshaller (schema_type )
290
269
typed = type_unmarshaller (value )
270
+
291
271
schema_format = self .find_format (value )
292
- if schema_format is None :
272
+ format_unmarshaller = self .get_format_unmarshaller (schema_format )
273
+ if format_unmarshaller is None :
274
+ return typed
275
+ try :
276
+ return format_unmarshaller (typed )
277
+ except (AttributeError , ValueError , TypeError ):
293
278
return typed
294
- return self .formats_unmarshaller .unmarshal (schema_format , typed )
295
279
296
280
def get_type_unmarshaller (
297
281
self ,
298
282
schema_type : Optional [Union [Iterable [str ], str ]],
299
283
) -> PrimitiveUnmarshaller :
300
- klass = self .types_unmarshaller .get_unmarshaller (schema_type )
284
+ klass = self .types_unmarshaller .get_unmarshaller_cls (schema_type )
301
285
return klass (
302
286
self .schema ,
303
287
self .schema_validator ,
304
288
self ,
305
289
)
306
290
291
+ def get_format_unmarshaller (
292
+ self ,
293
+ schema_format : str ,
294
+ ) -> Optional [FormatUnmarshaller ]:
295
+ return self .formats_unmarshaller .get_unmarshaller (schema_format )
296
+
307
297
def evolve (self , schema : Spec ) -> "SchemaUnmarshaller" :
308
298
cls = self .__class__
309
299
@@ -315,8 +305,11 @@ def evolve(self, schema: Spec) -> "SchemaUnmarshaller":
315
305
)
316
306
317
307
def find_format (self , value : Any ) -> Optional [str ]:
318
- for schema in self .iter_valid_schemas (value ):
319
- if "format" in schema :
308
+ primitive_type = self .schema_validator .get_primitive_type (value )
309
+ if primitive_type != "string" :
310
+ return None
311
+ for schema in self .schema_validator .iter_valid_schemas (value ):
312
+ if "format" in schema and schema .getkey ("type" ) == primitive_type :
320
313
return str (schema .getkey ("format" ))
321
314
return None
322
315
0 commit comments