|
1 | 1 | import inspect
|
| 2 | +import sys |
| 3 | +import typing |
2 | 4 | from contextlib import AsyncExitStack, contextmanager
|
3 | 5 | from copy import copy, deepcopy
|
4 | 6 | from dataclasses import dataclass
|
|
19 | 21 | )
|
20 | 22 |
|
21 | 23 | import anyio
|
| 24 | +import typing_extensions |
22 | 25 | from fastapi import params
|
23 | 26 | from fastapi._compat import (
|
24 | 27 | PYDANTIC_V2,
|
|
71 | 74 | from starlette.requests import HTTPConnection, Request
|
72 | 75 | from starlette.responses import Response
|
73 | 76 | from starlette.websockets import WebSocket
|
74 |
| -from typing_extensions import Annotated, get_args, get_origin |
| 77 | +from typing_extensions import Annotated, TypeAliasType, TypeGuard, get_args, get_origin |
75 | 78 |
|
76 | 79 | try:
|
77 |
| - from typing_extensions import TypeAliasType |
| 80 | + from types import GenericAlias |
78 | 81 | except ImportError: # pragma: no cover
|
79 |
| - TypeAliasType = None # type: ignore[misc,assignment] |
80 |
| - |
| 82 | + GenericAlias = None # type: ignore[misc,assignment] |
81 | 83 |
|
82 | 84 | multipart_not_installed_error = (
|
83 | 85 | 'Form data requires "python-multipart" to be installed. \n'
|
@@ -362,7 +364,7 @@ def analyze_param(
|
362 | 364 | depends = None
|
363 | 365 | type_annotation: Any = Any
|
364 | 366 | use_annotation: Any = Any
|
365 |
| - if TypeAliasType is not None and isinstance(annotation, TypeAliasType): |
| 367 | + if _is_typealiastype(annotation): |
366 | 368 | # unpack in case py3.12 type syntax is used
|
367 | 369 | annotation = annotation.__value__
|
368 | 370 | if annotation is not inspect.Signature.empty:
|
@@ -1008,3 +1010,26 @@ def get_body_field(
|
1008 | 1010 | field_info=BodyFieldInfo(**BodyFieldInfo_kwargs),
|
1009 | 1011 | )
|
1010 | 1012 | return final_field
|
| 1013 | + |
| 1014 | + |
| 1015 | +def _is_typealiastype(tp: Any, /) -> TypeGuard[TypeAliasType]: |
| 1016 | + in_typing = hasattr(typing, "TypeAliasType") |
| 1017 | + in_typing_extensions = hasattr(typing_extensions, "TypeAliasType") |
| 1018 | + is_typealiastype = False |
| 1019 | + if in_typing and in_typing_extensions: |
| 1020 | + if getattr(typing, "TypeAliasType", None) is getattr( |
| 1021 | + typing_extensions, "TypeAliasType", None |
| 1022 | + ): |
| 1023 | + is_typealiastype = isinstance(tp, typing.TypeAliasType) # type: ignore [attr-defined] |
| 1024 | + else: |
| 1025 | + is_typealiastype = isinstance( |
| 1026 | + tp, |
| 1027 | + (typing.TypeAliasType, typing_extensions.TypeAliasType), # type: ignore [attr-defined] |
| 1028 | + ) |
| 1029 | + elif in_typing and not in_typing_extensions: |
| 1030 | + is_typealiastype = isinstance(tp, typing.TypeAliasType) # type: ignore [attr-defined] |
| 1031 | + elif not in_typing and in_typing_extensions: |
| 1032 | + is_typealiastype = isinstance(tp, typing_extensions.TypeAliasType) |
| 1033 | + if sys.version_info[:2] == (3, 10): |
| 1034 | + return type(tp) is not GenericAlias and is_typealiastype |
| 1035 | + return is_typealiastype |
0 commit comments