Skip to content

Commit

Permalink
Fix union type hint checks (#318)
Browse files Browse the repository at this point in the history
Fix some obscure edge cases related to typing.Union type args.

Fixes #304.
  • Loading branch information
rsichnyi authored and axnsan12 committed Feb 21, 2019
1 parent 3d43ee6 commit 76c8fe0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/drf_yasg/inspectors/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,20 @@ def inspect_collection_hint_class(hint_class):
hinting_type_info.append(((typing.Sequence, typing.AbstractSet), inspect_collection_hint_class))


def _get_union_types(hint_class):
if typing:
origin_type = get_origin_type(hint_class)
if origin_type is typing.Union:
return hint_class.__args__
try:
# python 3.5.2 and lower compatibility
if issubclass(origin_type, typing.Union):
return hint_class.__union_params__
except TypeError:
pass
return None


def get_basic_type_info_from_hint(hint_class):
"""Given a class (eg from a SerializerMethodField's return type hint,
return its basic type information - ``type``, ``format``, ``pattern``,
Expand All @@ -499,11 +513,11 @@ def get_basic_type_info_from_hint(hint_class):
:return: the extracted attributes as a dictionary, or ``None`` if the field type is not known
:rtype: OrderedDict
"""
if typing and get_origin_type(hint_class) == typing.Union:
union_types = _get_union_types(hint_class)
if typing and union_types:
# Optional is implemented as Union[T, None]
if len(hint_class.__args__) == 2 and hint_class.__args__[1] == type(None): # noqa: E721
child_type = hint_class.__args__[0]
result = get_basic_type_info_from_hint(child_type)
if len(union_types) == 2 and isinstance(None, union_types[1]):
result = get_basic_type_info_from_hint(union_types[0])
result['x-nullable'] = True
return result

Expand Down
4 changes: 4 additions & 0 deletions tests/test_get_basic_type_info_from_hint.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
# Following cases are not 100% correct, but it should work somehow and not crash.
(Union[int, float], None),
(List, {'type': openapi.TYPE_ARRAY, 'items': openapi.Items(openapi.TYPE_STRING)}),
('SomeType', None),
(type('SomeType', (object,), {}), None),
(None, None),
(6, None),
])
def test_get_basic_type_info_from_hint(hint_class, expected_swagger_type_info):
type_info = get_basic_type_info_from_hint(hint_class)
Expand Down

0 comments on commit 76c8fe0

Please sign in to comment.