11from typing import Any
22from typing import Generic
33from typing import Iterable
4- from typing import List
54from typing import Mapping
65from typing import Optional
76from typing import Type
@@ -28,6 +27,14 @@ def __init__(
2827 self .schema_caster = schema_caster
2928
3029 def __call__ (self , value : Any ) -> Any :
30+ self .validate (value )
31+
32+ return self .cast (value )
33+
34+ def validate (self , value : Any ) -> None :
35+ pass
36+
37+ def cast (self , value : Any ) -> Any :
3138 return value
3239
3340
@@ -37,18 +44,9 @@ def __call__(self, value: Any) -> Any:
3744class PrimitiveTypeCaster (Generic [PrimitiveType ], PrimitiveCaster ):
3845 primitive_type : Type [PrimitiveType ] = NotImplemented
3946
40- def __call__ (self , value : Union [str , bytes ]) -> Any :
41- self .validate (value )
42-
47+ def cast (self , value : Union [str , bytes ]) -> PrimitiveType :
4348 return self .primitive_type (value ) # type: ignore [call-arg]
4449
45- def validate (self , value : Any ) -> None :
46- # FIXME: don't cast data from media type deserializer
47- # See https://github.com/python-openapi/openapi-core/issues/706
48- # if not isinstance(value, (str, bytes)):
49- # raise ValueError("should cast only from string or bytes")
50- pass
51-
5250
5351class IntegerCaster (PrimitiveTypeCaster [int ]):
5452 primitive_type = int
@@ -61,22 +59,18 @@ class NumberCaster(PrimitiveTypeCaster[float]):
6159class BooleanCaster (PrimitiveTypeCaster [bool ]):
6260 primitive_type = bool
6361
64- def __call__ (self , value : Union [str , bytes ]) -> Any :
65- self .validate (value )
66-
67- return self .primitive_type (forcebool (value ))
68-
6962 def validate (self , value : Any ) -> None :
7063 super ().validate (value )
7164
72- # FIXME: don't cast data from media type deserializer
73- # See https://github.com/python-openapi/openapi-core/issues/706
7465 if isinstance (value , bool ):
7566 return
7667
7768 if value .lower () not in ["false" , "true" ]:
7869 raise ValueError ("not a boolean format" )
7970
71+ def cast (self , value : Union [str , bytes ]) -> bool :
72+ return self .primitive_type (forcebool (value ))
73+
8074
8175class ArrayCaster (PrimitiveCaster ):
8276 @property
@@ -85,19 +79,21 @@ def items_caster(self) -> "SchemaCaster":
8579 items_schema = self .schema .get ("items" , SchemaPath .from_dict ({}))
8680 return self .schema_caster .evolve (items_schema )
8781
88- def __call__ (self , value : Any ) -> List [ Any ] :
82+ def validate (self , value : Any ) -> None :
8983 # str and bytes are not arrays according to the OpenAPI spec
9084 if isinstance (value , (str , bytes )) or not isinstance (value , Iterable ):
91- raise CastError ( value , self . schema [ "type" ] )
85+ raise ValueError ( "not an array format" )
9286
93- try :
94- return list (map (self .items_caster .cast , value ))
95- except (ValueError , TypeError ):
96- raise CastError (value , self .schema ["type" ])
87+ def cast (self , value : list [Any ]) -> list [Any ]:
88+ return list (map (self .items_caster .cast , value ))
9789
9890
9991class ObjectCaster (PrimitiveCaster ):
100- def __call__ (self , value : Any ) -> Any :
92+ def validate (self , value : Any ) -> None :
93+ if not isinstance (value , dict ):
94+ raise ValueError ("not an object format" )
95+
96+ def cast (self , value : dict [str , Any ]) -> dict [str , Any ]:
10197 return self ._cast_proparties (value )
10298
10399 def evolve (self , schema : SchemaPath ) -> "ObjectCaster" :
@@ -109,9 +105,11 @@ def evolve(self, schema: SchemaPath) -> "ObjectCaster":
109105 self .schema_caster .evolve (schema ),
110106 )
111107
112- def _cast_proparties (self , value : Any , schema_only : bool = False ) -> Any :
108+ def _cast_proparties (
109+ self , value : dict [str , Any ], schema_only : bool = False
110+ ) -> dict [str , Any ]:
113111 if not isinstance (value , dict ):
114- raise CastError ( value , self . schema [ "type" ] )
112+ raise ValueError ( "not an object format" )
115113
116114 all_of_schemas = self .schema_validator .iter_all_of_schemas (value )
117115 for all_of_schema in all_of_schemas :
0 commit comments