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

Add support for TypeAliasType #1214

Merged
merged 4 commits into from
Mar 30, 2024
Merged
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
7 changes: 7 additions & 0 deletions drf_spectacular/plumbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,18 @@

LITERAL_TYPES: Tuple[Any, ...] = ()
TYPED_DICT_META_TYPES: Tuple[Any, ...] = ()
TYPE_ALIAS_TYPES: Tuple[Any, ...] = ()

if sys.version_info >= (3, 8):
from typing import Literal as _PyLiteral
from typing import _TypedDictMeta as _PyTypedDictMeta # type: ignore[attr-defined]
LITERAL_TYPES += (_PyLiteral,)
TYPED_DICT_META_TYPES += (_PyTypedDictMeta,)

if sys.version_info >= (3, 12):
from typing import TypeAliasType
TYPE_ALIAS_TYPES += (TypeAliasType,)

Check warning on line 88 in drf_spectacular/plumbing.py

View check run for this annotation

Codecov / codecov/patch

drf_spectacular/plumbing.py#L87-L88

Added lines #L87 - L88 were not covered by tests

try:
from typing_extensions import Literal as _PxLiteral
from typing_extensions import _TypedDictMeta as _PxTypedDictMeta # type: ignore[attr-defined]
Expand Down Expand Up @@ -1347,6 +1352,8 @@
return schema
elif isinstance(hint, TYPED_DICT_META_TYPES):
return _resolve_typeddict(hint)
elif isinstance(hint, TYPE_ALIAS_TYPES):
return resolve_type_hint(hint.__value__)

Check warning on line 1356 in drf_spectacular/plumbing.py

View check run for this annotation

Codecov / codecov/patch

drf_spectacular/plumbing.py#L1356

Added line #L1356 was not covered by tests
elif origin in UNION_TYPES:
type_args = [arg for arg in args if arg is not type(None)] # noqa: E721
if len(type_args) > 1:
Expand Down
20 changes: 20 additions & 0 deletions tests/test_plumbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,26 @@
)
])

if sys.version_info >= (3, 12):
exec("type MyAlias = typing.Literal['x', 'y']")
exec("type MyAliasNested = MyAlias | list[int | str]")

Check warning on line 326 in tests/test_plumbing.py

View check run for this annotation

Codecov / codecov/patch

tests/test_plumbing.py#L325-L326

Added lines #L325 - L326 were not covered by tests

TYPE_HINT_TEST_PARAMS.extend([

Check warning on line 328 in tests/test_plumbing.py

View check run for this annotation

Codecov / codecov/patch

tests/test_plumbing.py#L328

Added line #L328 was not covered by tests
(
MyAlias, # noqa: F821
{'enum': ['x', 'y'], 'type': 'string'}
),
(
MyAliasNested, # noqa: F821
{
'oneOf': [
{'enum': ['x', 'y'], 'type': 'string'},
{"type": "array", "items": {"oneOf": [{"type": "integer"}, {"type": "string"}]}}
]
}
)
])


@pytest.mark.parametrize(['type_hint', 'ref_schema'], TYPE_HINT_TEST_PARAMS)
def test_type_hint_extraction(no_warnings, type_hint, ref_schema):
Expand Down
Loading