Skip to content

Commit

Permalink
Add support for python 3.9 generic primitives
Browse files Browse the repository at this point in the history
Fixes #627, #631
  • Loading branch information
axnsan12 committed Oct 25, 2020
1 parent 64ceb91 commit dd192f6
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion tests/test_get_basic_type_info_from_hint.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys

import pytest
import uuid
from typing import Dict, List, Optional, Set, Union
Expand All @@ -6,13 +8,22 @@
from drf_yasg.inspectors.field import get_basic_type_info_from_hint


python39_generics_tests = []
if sys.version_info >= (3, 9):
python39_generics_tests = [
(dict[int, int], {'type': openapi.TYPE_OBJECT, 'format': None}),
(list[bool], {'type': openapi.TYPE_ARRAY, 'items': openapi.Items(openapi.TYPE_BOOLEAN)}),
]


@pytest.mark.parametrize('hint_class, expected_swagger_type_info', [
(int, {'type': openapi.TYPE_INTEGER, 'format': None}),
(str, {'type': openapi.TYPE_STRING, 'format': None}),
(bool, {'type': openapi.TYPE_BOOLEAN, 'format': None}),
(dict, {'type': openapi.TYPE_OBJECT, 'format': None}),
(Dict[int, int], {'type': openapi.TYPE_OBJECT, 'format': None}),
(uuid.UUID, {'type': openapi.TYPE_STRING, 'format': openapi.FORMAT_UUID}),
(list, {'type': openapi.TYPE_ARRAY, 'items': openapi.Items(openapi.TYPE_STRING)}),
(List[int], {'type': openapi.TYPE_ARRAY, 'items': openapi.Items(openapi.TYPE_INTEGER)}),
(List[str], {'type': openapi.TYPE_ARRAY, 'items': openapi.Items(openapi.TYPE_STRING)}),
(List[bool], {'type': openapi.TYPE_ARRAY, 'items': openapi.Items(openapi.TYPE_BOOLEAN)}),
Expand All @@ -31,7 +42,7 @@
(type('SomeType', (object,), {}), None),
(None, None),
(6, None),
])
] + python39_generics_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

0 comments on commit dd192f6

Please sign in to comment.