1
1
from inspect import isclass
2
- from typing import Dict , Union
2
+ from typing import Dict , Type , Union , cast
3
3
4
4
from apispec .ext .marshmallow import MarshmallowPlugin
5
5
from apispec .ext .marshmallow .field_converter import FieldConverterMixin
@@ -16,7 +16,7 @@ def field2property(field):
16
16
17
17
18
18
def ensure_schema (
19
- schema : Union [fields .Field , Schema , Dict [str , Union [fields .Field , type ]]],
19
+ schema : Union [fields .Field , Type [ fields . Field ], Schema , Type [ Schema ] , Dict [str , Union [fields .Field , type ]]],
20
20
name : str = "GeneratedFromDict" ,
21
21
) -> Union [dict , Schema ]:
22
22
"""Create a Schema object, or OpenAPI dictionary, given a Field, Schema, or Dict.
@@ -33,18 +33,19 @@ def ensure_schema(
33
33
return None
34
34
if isinstance (schema , fields .Field ):
35
35
return field2property (schema )
36
- if isclass (schema ) and issubclass (schema , fields .Field ):
37
- return field2property (schema ())
38
36
elif isinstance (schema , dict ):
39
37
return Schema .from_dict (schema , name = name )()
40
38
elif isinstance (schema , Schema ):
41
39
return schema
42
- elif isclass (schema ) and issubclass (schema , Schema ):
43
- return schema ()
44
- else :
45
- raise TypeError (
46
- f"Invalid schema type { type (schema )} . Must be a Schema or Mapping/dict"
47
- )
40
+ if isclass (schema ):
41
+ schema = cast (Type , schema )
42
+ if issubclass (schema , fields .Field ):
43
+ return field2property (schema ())
44
+ elif issubclass (schema , Schema ):
45
+ return schema ()
46
+ raise TypeError (
47
+ f"Invalid schema type { type (schema )} . Must be a Schema or Mapping/dict"
48
+ )
48
49
49
50
50
51
def get_marshmallow_plugin (spec ):
0 commit comments