Skip to content

Commit 9b51698

Browse files
committed
Fixed typing in ensure_schema
1 parent dab0a81 commit 9b51698

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/labthings/apispec/utilities.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from inspect import isclass
2-
from typing import Dict, Union
2+
from typing import Dict, Type, Union, cast
33

44
from apispec.ext.marshmallow import MarshmallowPlugin
55
from apispec.ext.marshmallow.field_converter import FieldConverterMixin
@@ -16,7 +16,7 @@ def field2property(field):
1616

1717

1818
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]]],
2020
name: str = "GeneratedFromDict",
2121
) -> Union[dict, Schema]:
2222
"""Create a Schema object, or OpenAPI dictionary, given a Field, Schema, or Dict.
@@ -33,18 +33,19 @@ def ensure_schema(
3333
return None
3434
if isinstance(schema, fields.Field):
3535
return field2property(schema)
36-
if isclass(schema) and issubclass(schema, fields.Field):
37-
return field2property(schema())
3836
elif isinstance(schema, dict):
3937
return Schema.from_dict(schema, name=name)()
4038
elif isinstance(schema, Schema):
4139
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+
)
4849

4950

5051
def get_marshmallow_plugin(spec):

0 commit comments

Comments
 (0)