Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support new X | Y union syntax of Python 3.10 (PEP 604) #758

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/drf_yasg/inspectors/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import inspect
import logging
import operator
import types
import typing
import uuid
from collections import OrderedDict
Expand Down Expand Up @@ -459,7 +460,11 @@ def decimal_return_type():


def get_origin_type(hint_class):
return getattr(hint_class, '__origin__', None) or hint_class
# Added in Python 3.8
if hasattr(typing, 'get_origin'):
return typing.get_origin(hint_class)
else:
return getattr(hint_class, '__origin__', None) or hint_class


def hint_class_issubclass(hint_class, check_class):
Expand Down Expand Up @@ -502,10 +507,16 @@ def inspect_collection_hint_class(hint_class):

hinting_type_info.append(((typing.Sequence, typing.AbstractSet), inspect_collection_hint_class))

# types.UnionType was added in Python 3.10 for new PEP 604 pipe union syntax
if hasattr(types, 'UnionType'):
UNION_TYPES = (typing.Union, types.UnionType)
else:
UNION_TYPES = (typing.Union,)


def _get_union_types(hint_class):
origin_type = get_origin_type(hint_class)
if origin_type is typing.Union:
if origin_type in UNION_TYPES:
return hint_class.__args__


Expand Down
15 changes: 14 additions & 1 deletion tests/test_get_basic_type_info_from_hint.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@
]


python310_union_tests = []
if sys.version_info >= (3, 10):
# New PEP 604 union syntax in Python 3.10+
python310_union_tests = [
(bool | None, {'type': openapi.TYPE_BOOLEAN, 'format': None, 'x-nullable': True}),
(list[int] | None, {
'type': openapi.TYPE_ARRAY, 'items': openapi.Items(openapi.TYPE_INTEGER), 'x-nullable': True
}),
# Following case is not handled, but it should not crash.
(int | float, None),
]


@pytest.mark.parametrize('hint_class, expected_swagger_type_info', [
(int, {'type': openapi.TYPE_INTEGER, 'format': None}),
(str, {'type': openapi.TYPE_STRING, 'format': None}),
Expand All @@ -41,7 +54,7 @@
(type('SomeType', (object,), {}), None),
(None, None),
(6, None),
] + python39_generics_tests)
] + python39_generics_tests + python310_union_tests)
def test_get_basic_type_info_from_hint(hint_class, expected_swagger_type_info):
type_info = get_basic_type_info_from_hint(hint_class)
assert type_info == expected_swagger_type_info