1
1
import collections
2
2
import inspect
3
3
import sys
4
+ import types
4
5
import typing
5
6
from enum import Enum
6
7
from functools import partial
@@ -85,7 +86,7 @@ def is_constructible_from_str(type_: type) -> bool:
85
86
86
87
87
88
# NB: since `_GenericAlias` is a private attribute of the `typing` module, mypy doesn't find it
88
- TypeAnnotation : TypeAlias = Union [type , typing ._GenericAlias , UnionType ] # type: ignore[name-defined]
89
+ TypeAnnotation : TypeAlias = Union [type , typing ._GenericAlias , UnionType , types . GenericAlias ] # type: ignore[name-defined]
89
90
"""
90
91
A function parameter's type annotation may be any of the following:
91
92
1) `type`, when declaring any of the built-in Python types
@@ -101,20 +102,27 @@ def is_constructible_from_str(type_: type) -> bool:
101
102
# TODO When dropping support for Python 3.9, deprecate this in favor of performing instance checks
102
103
# directly on the `TypeAnnotation` union type.
103
104
# NB: since `_GenericAlias` is a private attribute of the `typing` module, mypy doesn't find it
104
- TYPE_ANNOTATION_TYPES = (type , typing ._GenericAlias , UnionType ) # type: ignore[attr-defined]
105
+ TYPE_ANNOTATION_TYPES = (type , typing ._GenericAlias , UnionType , types . GenericAlias ) # type: ignore[attr-defined]
105
106
106
107
107
108
def _is_optional (dtype : TypeAnnotation ) -> bool :
108
- """Check if a type is `Optional`.
109
+ """
110
+ Check if a type is `Optional`.
111
+
109
112
An optional type may be declared using three syntaxes: `Optional[T]`, `Union[T, None]`, or `T |
110
- None`. All of these syntaxes is supported by this function.
113
+ None`. All of these syntaxes are supported by this function.
114
+
111
115
Args:
112
116
dtype: A type.
117
+
113
118
Returns:
114
119
True if the type is a union type with exactly two elements, one of which is `None`.
115
120
False otherwise.
121
+
116
122
Raises:
117
- TypeError: If the input is not a valid `TypeAnnotation` type (see above).
123
+ TypeError: If the input is not a valid `TypeAnnotation` type.
124
+ Type annotations may be any of `type`, `types.UnionType`, `types.GenericAlias`,
125
+ or `typing._GenericAlias`.
118
126
"""
119
127
if not isinstance (dtype , TYPE_ANNOTATION_TYPES ):
120
128
raise TypeError (f"Expected type annotation, got { type (dtype )} : { dtype } " )
0 commit comments