@@ -140,34 +140,15 @@ def _unmarshal_properties(
140
140
141
141
class MultiTypeUnmarshaller (PrimitiveUnmarshaller ):
142
142
def __call__ (self , value : Any ) -> Any :
143
- unmarshaller = self ._get_best_unmarshaller (value )
143
+ primitive_type = self .schema_validator .get_primitive_type (value )
144
+ unmarshaller = self .schema_unmarshaller .get_type_unmarshaller (
145
+ primitive_type
146
+ )
144
147
return unmarshaller (value )
145
148
146
- @property
147
- def type (self ) -> List [str ]:
148
- types = self .schema .getkey ("type" , ["any" ])
149
- assert isinstance (types , list )
150
- return types
151
-
152
- def _get_best_unmarshaller (self , value : Any ) -> "PrimitiveUnmarshaller" :
153
- for schema_type in self .type :
154
- result = self .schema_validator .type_validator (
155
- value , type_override = schema_type
156
- )
157
- if not result :
158
- continue
159
- result = self .schema_validator .format_validator (value )
160
- if not result :
161
- continue
162
- return self .schema_unmarshaller .get_type_unmarshaller (schema_type )
163
-
164
- raise UnmarshallerError ("Unmarshaller not found for type(s)" )
165
-
166
149
167
150
class AnyUnmarshaller (MultiTypeUnmarshaller ):
168
- @property
169
- def type (self ) -> List [str ]:
170
- return self .schema_unmarshaller .types_unmarshaller .get_types ()
151
+ pass
171
152
172
153
173
154
class TypesUnmarshaller :
@@ -187,7 +168,7 @@ def __init__(
187
168
def get_types (self ) -> List [str ]:
188
169
return list (self .unmarshallers .keys ())
189
170
190
- def get_unmarshaller (
171
+ def get_unmarshaller_cls (
191
172
self ,
192
173
schema_type : Optional [Union [Iterable [str ], str ]],
193
174
) -> Type ["PrimitiveUnmarshaller" ]:
@@ -222,8 +203,8 @@ def unmarshal(self, schema_format: str, value: Any) -> Any:
222
203
return value
223
204
try :
224
205
return format_unmarshaller (value )
225
- except (ValueError , TypeError ) as exc :
226
- raise FormatUnmarshalError ( value , schema_format , exc )
206
+ except (AttributeError , ValueError , TypeError ) as exc :
207
+ return value
227
208
228
209
def get_unmarshaller (
229
210
self , schema_format : str
@@ -281,19 +262,32 @@ def unmarshal(self, value: Any) -> Any:
281
262
(isinstance (value , bytes ) and schema_format in ["binary" , "byte" ])
282
263
):
283
264
return typed
284
- return self .formats_unmarshaller .unmarshal (schema_format , typed )
265
+
266
+ format_unmarshaller = self .get_format_unmarshaller (schema_format )
267
+ if format_unmarshaller is None :
268
+ return typed
269
+ try :
270
+ return format_unmarshaller (typed )
271
+ except (AttributeError , ValueError , TypeError ):
272
+ return typed
285
273
286
274
def get_type_unmarshaller (
287
275
self ,
288
276
schema_type : Optional [Union [Iterable [str ], str ]],
289
277
) -> PrimitiveUnmarshaller :
290
- klass = self .types_unmarshaller .get_unmarshaller (schema_type )
278
+ klass = self .types_unmarshaller .get_unmarshaller_cls (schema_type )
291
279
return klass (
292
280
self .schema ,
293
281
self .schema_validator ,
294
282
self ,
295
283
)
296
284
285
+ def get_format_unmarshaller (
286
+ self ,
287
+ schema_format : str ,
288
+ ) -> Optional [FormatUnmarshaller ]:
289
+ return self .formats_unmarshaller .get_unmarshaller (schema_format )
290
+
297
291
def evolve (self , schema : Spec ) -> "SchemaUnmarshaller" :
298
292
cls = self .__class__
299
293
@@ -305,7 +299,10 @@ def evolve(self, schema: Spec) -> "SchemaUnmarshaller":
305
299
)
306
300
307
301
def find_format (self , value : Any ) -> Optional [str ]:
302
+ primitive_type = self .schema_validator .get_primitive_type (value )
303
+ if primitive_type != "string" :
304
+ return None
308
305
for schema in self .schema_validator .iter_valid_schemas (value ):
309
- if "format" in schema :
306
+ if "format" in schema and schema . getkey ( "type" ) == primitive_type :
310
307
return str (schema .getkey ("format" ))
311
308
return None
0 commit comments