Skip to content

Commit e063759

Browse files
committed
style fixes
1 parent 6d8b2e5 commit e063759

File tree

10 files changed

+22
-26
lines changed

10 files changed

+22
-26
lines changed

openapi_core/casting/schemas/casters.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
class PrimitiveCaster(object):
66

7-
def __init__(self, caster_callable):
7+
def __init__(self, schema, caster_callable):
8+
self.schema = schema
89
self.caster_callable = caster_callable
910

1011
def __call__(self, value):
@@ -13,7 +14,7 @@ def __call__(self, value):
1314
try:
1415
return self.caster_callable(value)
1516
except (ValueError, TypeError) as exc:
16-
raise CastError(exc, self.caster_callable)
17+
raise CastError(value, self.schema.type.value)
1718

1819

1920
class DummyCaster(object):

openapi_core/casting/schemas/factories.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,24 @@
88

99
class SchemaCastersFactory(object):
1010

11-
DUMMY_CASTER = DummyCaster()
11+
DUMMY_CASTERS = [
12+
SchemaType.STRING, SchemaType.OBJECT, SchemaType.ANY,
13+
]
1214
PRIMITIVE_CASTERS = {
13-
SchemaType.STRING: DUMMY_CASTER,
14-
SchemaType.INTEGER: PrimitiveCaster(int),
15-
SchemaType.NUMBER: PrimitiveCaster(float),
16-
SchemaType.BOOLEAN: PrimitiveCaster(forcebool),
17-
SchemaType.OBJECT: DUMMY_CASTER,
18-
SchemaType.ANY: DUMMY_CASTER,
15+
SchemaType.INTEGER: int,
16+
SchemaType.NUMBER: float,
17+
SchemaType.BOOLEAN: forcebool,
1918
}
2019
COMPLEX_CASTERS = {
2120
SchemaType.ARRAY: ArrayCaster,
2221
}
2322

2423
def create(self, schema):
25-
if schema.type in self.PRIMITIVE_CASTERS:
26-
return self.PRIMITIVE_CASTERS[schema.type]
24+
if schema.type in self.DUMMY_CASTERS:
25+
return DummyCaster()
26+
elif schema.type in self.PRIMITIVE_CASTERS:
27+
caster_callable = self.PRIMITIVE_CASTERS[schema.type]
28+
return PrimitiveCaster(schema, caster_callable)
2729
elif schema.type in self.COMPLEX_CASTERS:
2830
caster_class = self.COMPLEX_CASTERS[schema.type]
29-
return caster_class(schema=schema, casters_factory=self)
31+
return caster_class(schema, self)

openapi_core/deserializing/media_types/deserializers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ def __init__(self, mimetype, deserializer_callable):
1010
def __call__(self, value):
1111
try:
1212
return self.deserializer_callable(value)
13-
except (ValueError, TypeError, AttributeError) as exc:
13+
except (ValueError, TypeError, AttributeError):
1414
raise DeserializeError(value, self.mimetype)

openapi_core/deserializing/parameters/deserializers.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
EmptyParameterValue,
44
)
55
from openapi_core.schema.parameters.enums import ParameterLocation
6-
from openapi_core.schema.schemas.types import NoValue
76

87

98
class PrimitiveDeserializer(object):
@@ -22,5 +21,5 @@ def __call__(self, value):
2221
return value
2322
try:
2423
return self.deserializer_callable(value)
25-
except (ValueError, TypeError, AttributeError) as exc:
24+
except (ValueError, TypeError, AttributeError):
2625
raise DeserializeError(value, self.param.style)

openapi_core/schema/media_types/models.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""OpenAPI core media types models module"""
2+
3+
24
class MediaType(object):
35
"""Represents an OpenAPI MediaType."""
46

openapi_core/schema/parameters/models.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""OpenAPI core parameters models module"""
22
import logging
3-
import warnings
43

54
from openapi_core.schema.parameters.enums import (
65
ParameterLocation, ParameterStyle,

openapi_core/validation/request/validators.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
from openapi_core.deserializing.exceptions import DeserializeError
77
from openapi_core.schema.media_types.exceptions import InvalidContentType
88
from openapi_core.schema.operations.exceptions import InvalidOperation
9-
from openapi_core.schema.parameters.enums import ParameterLocation
109
from openapi_core.schema.parameters.exceptions import (
11-
OpenAPIParameterError, MissingRequiredParameter, MissingParameter,
10+
MissingRequiredParameter, MissingParameter,
1211
)
1312
from openapi_core.schema.paths.exceptions import InvalidPath
1413
from openapi_core.schema.request_bodies.exceptions import MissingRequestBody
@@ -192,7 +191,6 @@ def _deserialise_media_type(self, media_type, value):
192191
deserializer = deserializers_factory.create(media_type)
193192
return deserializer(value)
194193

195-
196194
def _deserialise_parameter(self, param, value):
197195
from openapi_core.deserializing.parameters.factories import (
198196
ParameterDeserializersFactory,
@@ -206,7 +204,6 @@ def _cast(self, param_or_media_type, value):
206204
if not param_or_media_type.schema:
207205
return value
208206

209-
from openapi_core.casting.schemas.exceptions import CastError
210207
from openapi_core.casting.schemas.factories import SchemaCastersFactory
211208
casters_factory = SchemaCastersFactory()
212209
caster = casters_factory.create(param_or_media_type.schema)

openapi_core/validation/response/validators.py

-2
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,11 @@ def _cast(self, param_or_media_type, value):
121121
if not param_or_media_type.schema:
122122
return value
123123

124-
from openapi_core.casting.schemas.exceptions import CastError
125124
from openapi_core.casting.schemas.factories import SchemaCastersFactory
126125
casters_factory = SchemaCastersFactory()
127126
caster = casters_factory.create(param_or_media_type.schema)
128127
return caster(value)
129128

130-
131129
def _unmarshal(self, param_or_media_type, value):
132130
if not param_or_media_type.schema:
133131
return value

tests/integration/contrib/flask/test_flask_decorator.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ def test_endpoint_error(self, client):
105105
),
106106
'status': 400,
107107
'title': (
108-
"Failed to cast value invalid literal for int() with "
109-
"base 10: 'invalidparameter' to type <class 'int'>"
108+
"Failed to cast value invalidparameter to type integer"
110109
)
111110
}
112111
]

tests/integration/contrib/flask/test_flask_views.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ def test_endpoint_error(self, client):
9292
),
9393
'status': 400,
9494
'title': (
95-
"Failed to cast value invalid literal for int() with "
96-
"base 10: 'invalidparameter' to type <class 'int'>"
95+
"Failed to cast value invalidparameter to type integer"
9796
)
9897
}
9998
]

0 commit comments

Comments
 (0)