Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1 | """ |
| 2 | The typing module: Support for gradual typing as defined by PEP 484. |
| 3 | |
| 4 | At large scale, the structure of the module is following: |
Tim McNamara | 5265b3a | 2018-09-01 20:56:58 +1200 | [diff] [blame] | 5 | * Imports and exports, all public names should be explicitly added to __all__. |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 6 | * Internal helper functions: these should never be used in code outside this module. |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 7 | * _SpecialForm and its instances (special forms): |
| 8 | Any, NoReturn, ClassVar, Union, Optional, Concatenate |
| 9 | * Classes whose instances can be type arguments in addition to types: |
| 10 | ForwardRef, TypeVar and ParamSpec |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 11 | * The core of internal generics API: _GenericAlias and _VariadicGenericAlias, the latter is |
| 12 | currently only used by Tuple and Callable. All subscripted types like X[int], Union[int, str], |
| 13 | etc., are instances of either of these classes. |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 14 | * The public counterpart of the generics API consists of two classes: Generic and Protocol. |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 15 | * Public helper functions: get_type_hints, overload, cast, no_type_check, |
| 16 | no_type_check_decorator. |
| 17 | * Generic aliases for collections.abc ABCs and few additional protocols. |
ananthan-123 | ab6423f | 2020-02-19 10:03:05 +0530 | [diff] [blame] | 18 | * Special types: NewType, NamedTuple, TypedDict. |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 19 | * Wrapper submodules for re and io related types. |
| 20 | """ |
| 21 | |
HongWeipeng | 6ce03ec | 2019-09-27 15:54:26 +0800 | [diff] [blame] | 22 | from abc import abstractmethod, ABCMeta |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 23 | import collections |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 24 | import collections.abc |
Brett Cannon | f3ad042 | 2016-04-15 10:51:30 -0700 | [diff] [blame] | 25 | import contextlib |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 26 | import functools |
Serhiy Storchaka | 09f3221 | 2018-05-26 21:19:26 +0300 | [diff] [blame] | 27 | import operator |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 28 | import re as stdlib_re # Avoid confusion with the re we export. |
| 29 | import sys |
| 30 | import types |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 31 | from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType, GenericAlias |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 32 | |
| 33 | # Please keep __all__ alphabetized within each category. |
| 34 | __all__ = [ |
| 35 | # Super-special typing primitives. |
Jakub Stasiak | cf5b109 | 2020-02-05 02:10:19 +0100 | [diff] [blame] | 36 | 'Annotated', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 37 | 'Any', |
| 38 | 'Callable', |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 39 | 'ClassVar', |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 40 | 'Concatenate', |
Ivan Levkivskyi | f367242 | 2019-05-26 09:37:07 +0100 | [diff] [blame] | 41 | 'Final', |
Anthony Sottile | d30da5d | 2019-05-29 11:19:38 -0700 | [diff] [blame] | 42 | 'ForwardRef', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 43 | 'Generic', |
Ivan Levkivskyi | b891c46 | 2019-05-26 09:37:48 +0100 | [diff] [blame] | 44 | 'Literal', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 45 | 'Optional', |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 46 | 'ParamSpec', |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 47 | 'Protocol', |
Guido van Rossum | eb9aca3 | 2016-05-24 16:38:22 -0700 | [diff] [blame] | 48 | 'Tuple', |
| 49 | 'Type', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 50 | 'TypeVar', |
| 51 | 'Union', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 52 | |
| 53 | # ABCs (from collections.abc). |
| 54 | 'AbstractSet', # collections.abc.Set. |
| 55 | 'ByteString', |
| 56 | 'Container', |
Ivan Levkivskyi | 29fda8d | 2017-06-10 21:57:56 +0200 | [diff] [blame] | 57 | 'ContextManager', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 58 | 'Hashable', |
| 59 | 'ItemsView', |
| 60 | 'Iterable', |
| 61 | 'Iterator', |
| 62 | 'KeysView', |
| 63 | 'Mapping', |
| 64 | 'MappingView', |
| 65 | 'MutableMapping', |
| 66 | 'MutableSequence', |
| 67 | 'MutableSet', |
| 68 | 'Sequence', |
| 69 | 'Sized', |
| 70 | 'ValuesView', |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 71 | 'Awaitable', |
| 72 | 'AsyncIterator', |
| 73 | 'AsyncIterable', |
| 74 | 'Coroutine', |
| 75 | 'Collection', |
| 76 | 'AsyncGenerator', |
| 77 | 'AsyncContextManager', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 78 | |
| 79 | # Structural checks, a.k.a. protocols. |
| 80 | 'Reversible', |
| 81 | 'SupportsAbs', |
Ivan Levkivskyi | f06e021 | 2017-05-02 19:14:07 +0200 | [diff] [blame] | 82 | 'SupportsBytes', |
| 83 | 'SupportsComplex', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 84 | 'SupportsFloat', |
Paul Dagnelie | 4c7a46e | 2019-05-22 07:23:01 -0700 | [diff] [blame] | 85 | 'SupportsIndex', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 86 | 'SupportsInt', |
| 87 | 'SupportsRound', |
| 88 | |
| 89 | # Concrete collection types. |
Anthony Sottile | d30da5d | 2019-05-29 11:19:38 -0700 | [diff] [blame] | 90 | 'ChainMap', |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 91 | 'Counter', |
Raymond Hettinger | 8049052 | 2017-01-16 22:42:37 -0800 | [diff] [blame] | 92 | 'Deque', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 93 | 'Dict', |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 94 | 'DefaultDict', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 95 | 'List', |
Anthony Sottile | d30da5d | 2019-05-29 11:19:38 -0700 | [diff] [blame] | 96 | 'OrderedDict', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 97 | 'Set', |
Guido van Rossum | efa798d | 2016-08-23 11:01:50 -0700 | [diff] [blame] | 98 | 'FrozenSet', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 99 | 'NamedTuple', # Not really a type. |
Ivan Levkivskyi | 135c6a5 | 2019-05-26 09:39:24 +0100 | [diff] [blame] | 100 | 'TypedDict', # Not really a type. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 101 | 'Generator', |
Miss Islington (bot) | e1bcc88 | 2021-05-04 02:51:33 -0700 | [diff] [blame^] | 102 | |
| 103 | # Other concrete types. |
| 104 | 'BinaryIO', |
| 105 | 'IO', |
| 106 | 'Match', |
| 107 | 'Pattern', |
| 108 | 'TextIO', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 109 | |
| 110 | # One-off things. |
| 111 | 'AnyStr', |
| 112 | 'cast', |
Ivan Levkivskyi | f367242 | 2019-05-26 09:37:07 +0100 | [diff] [blame] | 113 | 'final', |
Ivan Levkivskyi | 4c23aff | 2019-05-31 00:10:07 +0100 | [diff] [blame] | 114 | 'get_args', |
| 115 | 'get_origin', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 116 | 'get_type_hints', |
Patrick Reader | 0705ec8 | 2020-09-16 05:58:32 +0100 | [diff] [blame] | 117 | 'is_typeddict', |
Guido van Rossum | 91185fe | 2016-06-08 11:19:11 -0700 | [diff] [blame] | 118 | 'NewType', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 119 | 'no_type_check', |
| 120 | 'no_type_check_decorator', |
aetracht | 4573820 | 2018-03-19 14:41:32 -0400 | [diff] [blame] | 121 | 'NoReturn', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 122 | 'overload', |
Jelle Zijlstra | 5224336 | 2021-04-10 19:57:05 -0700 | [diff] [blame] | 123 | 'ParamSpecArgs', |
| 124 | 'ParamSpecKwargs', |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 125 | 'runtime_checkable', |
Guido van Rossum | 0e0563c | 2016-04-05 14:54:25 -0700 | [diff] [blame] | 126 | 'Text', |
Guido van Rossum | 91185fe | 2016-06-08 11:19:11 -0700 | [diff] [blame] | 127 | 'TYPE_CHECKING', |
Mikhail Golubev | 4f3c250 | 2020-10-08 00:44:31 +0300 | [diff] [blame] | 128 | 'TypeAlias', |
Ken Jin | 05ab4b6 | 2021-04-27 22:31:04 +0800 | [diff] [blame] | 129 | 'TypeGuard', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 130 | ] |
| 131 | |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 132 | # The pseudo-submodules 're' and 'io' are part of the public |
| 133 | # namespace, but excluded from __all__ because they might stomp on |
| 134 | # legitimate imports of those modules. |
| 135 | |
kj | 463c7d3 | 2020-12-14 02:38:24 +0800 | [diff] [blame] | 136 | |
| 137 | def _type_convert(arg): |
| 138 | """For converting None to type(None), and strings to ForwardRef.""" |
| 139 | if arg is None: |
| 140 | return type(None) |
| 141 | if isinstance(arg, str): |
| 142 | return ForwardRef(arg) |
| 143 | return arg |
| 144 | |
| 145 | |
Nina Zakharenko | 0e61dff | 2018-05-22 20:32:10 -0700 | [diff] [blame] | 146 | def _type_check(arg, msg, is_argument=True): |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 147 | """Check that the argument is a type, and return it (internal helper). |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 148 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 149 | As a special case, accept None and return type(None) instead. Also wrap strings |
| 150 | into ForwardRef instances. Consider several corner cases, for example plain |
| 151 | special forms like Union are not valid, while Union[int, str] is OK, etc. |
| 152 | The msg argument is a human-readable error message, e.g:: |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 153 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 154 | "Union[arg, ...]: arg should be a type." |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 155 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 156 | We append the repr() of the actual value (truncated to 100 chars). |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 157 | """ |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 158 | invalid_generic_forms = (Generic, Protocol) |
Nina Zakharenko | 0e61dff | 2018-05-22 20:32:10 -0700 | [diff] [blame] | 159 | if is_argument: |
Ivan Levkivskyi | f367242 | 2019-05-26 09:37:07 +0100 | [diff] [blame] | 160 | invalid_generic_forms = invalid_generic_forms + (ClassVar, Final) |
Nina Zakharenko | 2d2d3b1 | 2018-05-16 12:27:03 -0400 | [diff] [blame] | 161 | |
kj | 463c7d3 | 2020-12-14 02:38:24 +0800 | [diff] [blame] | 162 | arg = _type_convert(arg) |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 163 | if (isinstance(arg, _GenericAlias) and |
Nina Zakharenko | 2d2d3b1 | 2018-05-16 12:27:03 -0400 | [diff] [blame] | 164 | arg.__origin__ in invalid_generic_forms): |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 165 | raise TypeError(f"{arg} is not valid as type argument") |
Serhiy Storchaka | 40ded94 | 2020-04-23 21:26:48 +0300 | [diff] [blame] | 166 | if arg in (Any, NoReturn): |
| 167 | return arg |
| 168 | if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol): |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 169 | raise TypeError(f"Plain {arg} is not valid as type argument") |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 170 | if isinstance(arg, (type, TypeVar, ForwardRef, types.Union, ParamSpec)): |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 171 | return arg |
| 172 | if not callable(arg): |
| 173 | raise TypeError(f"{msg} Got {arg!r:.100}.") |
| 174 | return arg |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 175 | |
| 176 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 177 | def _type_repr(obj): |
| 178 | """Return the repr() of an object, special-casing types (internal helper). |
| 179 | |
| 180 | If obj is a type, we return a shorter version than the default |
| 181 | type.__repr__, based on the module and qualified name, which is |
| 182 | typically enough to uniquely identify a type. For everything |
| 183 | else, we fall back on repr(obj). |
| 184 | """ |
kj | 1f7dfb2 | 2020-11-02 02:13:38 +0800 | [diff] [blame] | 185 | if isinstance(obj, types.GenericAlias): |
| 186 | return repr(obj) |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 187 | if isinstance(obj, type): |
| 188 | if obj.__module__ == 'builtins': |
| 189 | return obj.__qualname__ |
| 190 | return f'{obj.__module__}.{obj.__qualname__}' |
| 191 | if obj is ...: |
| 192 | return('...') |
| 193 | if isinstance(obj, types.FunctionType): |
| 194 | return obj.__name__ |
| 195 | return repr(obj) |
| 196 | |
| 197 | |
| 198 | def _collect_type_vars(types): |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 199 | """Collect all type variable-like variables contained |
| 200 | in types in order of first appearance (lexicographic order). For example:: |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 201 | |
| 202 | _collect_type_vars((T, List[S, T])) == (T, S) |
| 203 | """ |
| 204 | tvars = [] |
| 205 | for t in types: |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 206 | if isinstance(t, _TypeVarLike) and t not in tvars: |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 207 | tvars.append(t) |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 208 | if isinstance(t, (_GenericAlias, GenericAlias)): |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 209 | tvars.extend([t for t in t.__parameters__ if t not in tvars]) |
| 210 | return tuple(tvars) |
| 211 | |
| 212 | |
Serhiy Storchaka | fcb2856 | 2020-05-10 11:53:16 +0300 | [diff] [blame] | 213 | def _check_generic(cls, parameters, elen): |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 214 | """Check correct count for parameters of a generic cls (internal helper). |
| 215 | This gives a nice error message in case of count mismatch. |
| 216 | """ |
Serhiy Storchaka | fcb2856 | 2020-05-10 11:53:16 +0300 | [diff] [blame] | 217 | if not elen: |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 218 | raise TypeError(f"{cls} is not a generic class") |
| 219 | alen = len(parameters) |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 220 | if alen != elen: |
| 221 | raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};" |
| 222 | f" actual {alen}, expected {elen}") |
| 223 | |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 224 | def _prepare_paramspec_params(cls, params): |
| 225 | """Prepares the parameters for a Generic containing ParamSpec |
| 226 | variables (internal helper). |
| 227 | """ |
| 228 | # Special case where Z[[int, str, bool]] == Z[int, str, bool] in PEP 612. |
| 229 | if len(cls.__parameters__) == 1 and len(params) > 1: |
| 230 | return (params,) |
| 231 | else: |
| 232 | _params = [] |
| 233 | # Convert lists to tuples to help other libraries cache the results. |
| 234 | for p, tvar in zip(params, cls.__parameters__): |
| 235 | if isinstance(tvar, ParamSpec) and isinstance(p, list): |
| 236 | p = tuple(p) |
| 237 | _params.append(p) |
| 238 | return tuple(_params) |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 239 | |
Yurii Karabas | f03d318 | 2020-11-17 04:23:19 +0200 | [diff] [blame] | 240 | def _deduplicate(params): |
| 241 | # Weed out strict duplicates, preserving the first of each occurrence. |
| 242 | all_params = set(params) |
| 243 | if len(all_params) < len(params): |
| 244 | new_params = [] |
| 245 | for t in params: |
| 246 | if t in all_params: |
| 247 | new_params.append(t) |
| 248 | all_params.remove(t) |
| 249 | params = new_params |
| 250 | assert not all_params, all_params |
| 251 | return params |
| 252 | |
| 253 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 254 | def _remove_dups_flatten(parameters): |
Ivan Levkivskyi | f65e31f | 2018-05-18 16:00:38 -0700 | [diff] [blame] | 255 | """An internal helper for Union creation and substitution: flatten Unions |
| 256 | among parameters, then remove duplicates. |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 257 | """ |
| 258 | # Flatten out Union[Union[...], ...]. |
| 259 | params = [] |
| 260 | for p in parameters: |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 261 | if isinstance(p, (_UnionGenericAlias, types.Union)): |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 262 | params.extend(p.__args__) |
| 263 | elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union: |
| 264 | params.extend(p[1:]) |
| 265 | else: |
| 266 | params.append(p) |
Yurii Karabas | f03d318 | 2020-11-17 04:23:19 +0200 | [diff] [blame] | 267 | |
| 268 | return tuple(_deduplicate(params)) |
| 269 | |
| 270 | |
| 271 | def _flatten_literal_params(parameters): |
| 272 | """An internal helper for Literal creation: flatten Literals among parameters""" |
| 273 | params = [] |
| 274 | for p in parameters: |
| 275 | if isinstance(p, _LiteralGenericAlias): |
| 276 | params.extend(p.__args__) |
| 277 | else: |
| 278 | params.append(p) |
Ivan Levkivskyi | f65e31f | 2018-05-18 16:00:38 -0700 | [diff] [blame] | 279 | return tuple(params) |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 280 | |
| 281 | |
| 282 | _cleanups = [] |
| 283 | |
| 284 | |
Yurii Karabas | f03d318 | 2020-11-17 04:23:19 +0200 | [diff] [blame] | 285 | def _tp_cache(func=None, /, *, typed=False): |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 286 | """Internal wrapper caching __getitem__ of generic types with a fallback to |
| 287 | original function for non-hashable arguments. |
| 288 | """ |
Yurii Karabas | f03d318 | 2020-11-17 04:23:19 +0200 | [diff] [blame] | 289 | def decorator(func): |
| 290 | cached = functools.lru_cache(typed=typed)(func) |
| 291 | _cleanups.append(cached.cache_clear) |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 292 | |
Yurii Karabas | f03d318 | 2020-11-17 04:23:19 +0200 | [diff] [blame] | 293 | @functools.wraps(func) |
| 294 | def inner(*args, **kwds): |
| 295 | try: |
| 296 | return cached(*args, **kwds) |
| 297 | except TypeError: |
| 298 | pass # All real errors (not unhashable args) are raised below. |
| 299 | return func(*args, **kwds) |
| 300 | return inner |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 301 | |
Yurii Karabas | f03d318 | 2020-11-17 04:23:19 +0200 | [diff] [blame] | 302 | if func is not None: |
| 303 | return decorator(func) |
| 304 | |
| 305 | return decorator |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 306 | |
wyfo | 653f420 | 2020-07-22 21:47:28 +0200 | [diff] [blame] | 307 | def _eval_type(t, globalns, localns, recursive_guard=frozenset()): |
Graham Bleaney | 84ef33c | 2020-09-08 18:41:10 -0400 | [diff] [blame] | 308 | """Evaluate all forward references in the given type t. |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 309 | For use of globalns and localns see the docstring for get_type_hints(). |
wyfo | 653f420 | 2020-07-22 21:47:28 +0200 | [diff] [blame] | 310 | recursive_guard is used to prevent prevent infinite recursion |
| 311 | with recursive ForwardRef. |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 312 | """ |
| 313 | if isinstance(t, ForwardRef): |
wyfo | 653f420 | 2020-07-22 21:47:28 +0200 | [diff] [blame] | 314 | return t._evaluate(globalns, localns, recursive_guard) |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 315 | if isinstance(t, (_GenericAlias, GenericAlias)): |
wyfo | 653f420 | 2020-07-22 21:47:28 +0200 | [diff] [blame] | 316 | ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__) |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 317 | if ev_args == t.__args__: |
| 318 | return t |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 319 | if isinstance(t, GenericAlias): |
| 320 | return GenericAlias(t.__origin__, ev_args) |
| 321 | else: |
| 322 | return t.copy_with(ev_args) |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 323 | return t |
| 324 | |
| 325 | |
| 326 | class _Final: |
| 327 | """Mixin to prohibit subclassing""" |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 328 | |
Guido van Rossum | 83ec302 | 2017-01-17 20:43:28 -0800 | [diff] [blame] | 329 | __slots__ = ('__weakref__',) |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 330 | |
Serhiy Storchaka | 2085bd0 | 2019-06-01 11:00:15 +0300 | [diff] [blame] | 331 | def __init_subclass__(self, /, *args, **kwds): |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 332 | if '_root' not in kwds: |
| 333 | raise TypeError("Cannot subclass special typing classes") |
| 334 | |
Ivan Levkivskyi | 8349403 | 2018-03-26 23:01:12 +0100 | [diff] [blame] | 335 | class _Immutable: |
| 336 | """Mixin to indicate that object should not be copied.""" |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 337 | __slots__ = () |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 338 | |
Ivan Levkivskyi | 8349403 | 2018-03-26 23:01:12 +0100 | [diff] [blame] | 339 | def __copy__(self): |
| 340 | return self |
| 341 | |
| 342 | def __deepcopy__(self, memo): |
| 343 | return self |
| 344 | |
| 345 | |
Serhiy Storchaka | 40ded94 | 2020-04-23 21:26:48 +0300 | [diff] [blame] | 346 | # Internal indicator of special typing constructs. |
| 347 | # See __doc__ instance attribute for specific docs. |
| 348 | class _SpecialForm(_Final, _root=True): |
| 349 | __slots__ = ('_name', '__doc__', '_getitem') |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 350 | |
Serhiy Storchaka | 40ded94 | 2020-04-23 21:26:48 +0300 | [diff] [blame] | 351 | def __init__(self, getitem): |
| 352 | self._getitem = getitem |
| 353 | self._name = getitem.__name__ |
| 354 | self.__doc__ = getitem.__doc__ |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 355 | |
Serhiy Storchaka | 40ded94 | 2020-04-23 21:26:48 +0300 | [diff] [blame] | 356 | def __mro_entries__(self, bases): |
| 357 | raise TypeError(f"Cannot subclass {self!r}") |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 358 | |
| 359 | def __repr__(self): |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 360 | return 'typing.' + self._name |
| 361 | |
Ivan Levkivskyi | 8349403 | 2018-03-26 23:01:12 +0100 | [diff] [blame] | 362 | def __reduce__(self): |
| 363 | return self._name |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 364 | |
| 365 | def __call__(self, *args, **kwds): |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 366 | raise TypeError(f"Cannot instantiate {self!r}") |
| 367 | |
| 368 | def __instancecheck__(self, obj): |
| 369 | raise TypeError(f"{self} cannot be used with isinstance()") |
| 370 | |
| 371 | def __subclasscheck__(self, cls): |
| 372 | raise TypeError(f"{self} cannot be used with issubclass()") |
| 373 | |
| 374 | @_tp_cache |
| 375 | def __getitem__(self, parameters): |
Serhiy Storchaka | 40ded94 | 2020-04-23 21:26:48 +0300 | [diff] [blame] | 376 | return self._getitem(self, parameters) |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 377 | |
Yurii Karabas | f03d318 | 2020-11-17 04:23:19 +0200 | [diff] [blame] | 378 | |
| 379 | class _LiteralSpecialForm(_SpecialForm, _root=True): |
| 380 | @_tp_cache(typed=True) |
| 381 | def __getitem__(self, parameters): |
| 382 | return self._getitem(self, parameters) |
| 383 | |
| 384 | |
Serhiy Storchaka | 40ded94 | 2020-04-23 21:26:48 +0300 | [diff] [blame] | 385 | @_SpecialForm |
| 386 | def Any(self, parameters): |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 387 | """Special type indicating an unconstrained type. |
Guido van Rossum | b47c9d2 | 2016-10-03 08:40:50 -0700 | [diff] [blame] | 388 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 389 | - Any is compatible with every type. |
| 390 | - Any assumed to have all methods. |
| 391 | - All values assumed to be instances of Any. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 392 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 393 | Note that all the above statements are true from the point of view of |
| 394 | static type checkers. At runtime, Any should not be used with instance |
| 395 | or class checks. |
Serhiy Storchaka | 40ded94 | 2020-04-23 21:26:48 +0300 | [diff] [blame] | 396 | """ |
| 397 | raise TypeError(f"{self} is not subscriptable") |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 398 | |
Serhiy Storchaka | 40ded94 | 2020-04-23 21:26:48 +0300 | [diff] [blame] | 399 | @_SpecialForm |
| 400 | def NoReturn(self, parameters): |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 401 | """Special type indicating functions that never return. |
| 402 | Example:: |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 403 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 404 | from typing import NoReturn |
| 405 | |
| 406 | def stop() -> NoReturn: |
| 407 | raise Exception('no way') |
| 408 | |
| 409 | This type is invalid in other positions, e.g., ``List[NoReturn]`` |
| 410 | will fail in static type checkers. |
Serhiy Storchaka | 40ded94 | 2020-04-23 21:26:48 +0300 | [diff] [blame] | 411 | """ |
| 412 | raise TypeError(f"{self} is not subscriptable") |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 413 | |
Serhiy Storchaka | 40ded94 | 2020-04-23 21:26:48 +0300 | [diff] [blame] | 414 | @_SpecialForm |
| 415 | def ClassVar(self, parameters): |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 416 | """Special type construct to mark class variables. |
| 417 | |
| 418 | An annotation wrapped in ClassVar indicates that a given |
| 419 | attribute is intended to be used as a class variable and |
| 420 | should not be set on instances of that class. Usage:: |
| 421 | |
| 422 | class Starship: |
| 423 | stats: ClassVar[Dict[str, int]] = {} # class variable |
| 424 | damage: int = 10 # instance variable |
| 425 | |
| 426 | ClassVar accepts only types and cannot be further subscribed. |
| 427 | |
| 428 | Note that ClassVar is not a class itself, and should not |
| 429 | be used with isinstance() or issubclass(). |
Serhiy Storchaka | 40ded94 | 2020-04-23 21:26:48 +0300 | [diff] [blame] | 430 | """ |
| 431 | item = _type_check(parameters, f'{self} accepts only single type.') |
| 432 | return _GenericAlias(self, (item,)) |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 433 | |
Serhiy Storchaka | 40ded94 | 2020-04-23 21:26:48 +0300 | [diff] [blame] | 434 | @_SpecialForm |
| 435 | def Final(self, parameters): |
Ivan Levkivskyi | f367242 | 2019-05-26 09:37:07 +0100 | [diff] [blame] | 436 | """Special typing construct to indicate final names to type checkers. |
| 437 | |
| 438 | A final name cannot be re-assigned or overridden in a subclass. |
| 439 | For example: |
| 440 | |
| 441 | MAX_SIZE: Final = 9000 |
| 442 | MAX_SIZE += 1 # Error reported by type checker |
| 443 | |
| 444 | class Connection: |
| 445 | TIMEOUT: Final[int] = 10 |
| 446 | |
| 447 | class FastConnector(Connection): |
| 448 | TIMEOUT = 1 # Error reported by type checker |
| 449 | |
| 450 | There is no runtime checking of these properties. |
Serhiy Storchaka | 40ded94 | 2020-04-23 21:26:48 +0300 | [diff] [blame] | 451 | """ |
| 452 | item = _type_check(parameters, f'{self} accepts only single type.') |
| 453 | return _GenericAlias(self, (item,)) |
Ivan Levkivskyi | f367242 | 2019-05-26 09:37:07 +0100 | [diff] [blame] | 454 | |
Serhiy Storchaka | 40ded94 | 2020-04-23 21:26:48 +0300 | [diff] [blame] | 455 | @_SpecialForm |
| 456 | def Union(self, parameters): |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 457 | """Union type; Union[X, Y] means either X or Y. |
| 458 | |
| 459 | To define a union, use e.g. Union[int, str]. Details: |
| 460 | - The arguments must be types and there must be at least one. |
| 461 | - None as an argument is a special case and is replaced by |
| 462 | type(None). |
| 463 | - Unions of unions are flattened, e.g.:: |
| 464 | |
| 465 | Union[Union[int, str], float] == Union[int, str, float] |
| 466 | |
| 467 | - Unions of a single argument vanish, e.g.:: |
| 468 | |
| 469 | Union[int] == int # The constructor actually returns int |
| 470 | |
| 471 | - Redundant arguments are skipped, e.g.:: |
| 472 | |
| 473 | Union[int, str, int] == Union[int, str] |
| 474 | |
| 475 | - When comparing unions, the argument order is ignored, e.g.:: |
| 476 | |
| 477 | Union[int, str] == Union[str, int] |
| 478 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 479 | - You cannot subclass or instantiate a union. |
| 480 | - You can use Optional[X] as a shorthand for Union[X, None]. |
Serhiy Storchaka | 40ded94 | 2020-04-23 21:26:48 +0300 | [diff] [blame] | 481 | """ |
| 482 | if parameters == (): |
| 483 | raise TypeError("Cannot take a Union of no types.") |
| 484 | if not isinstance(parameters, tuple): |
| 485 | parameters = (parameters,) |
| 486 | msg = "Union[arg, ...]: each arg must be a type." |
| 487 | parameters = tuple(_type_check(p, msg) for p in parameters) |
| 488 | parameters = _remove_dups_flatten(parameters) |
| 489 | if len(parameters) == 1: |
| 490 | return parameters[0] |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 491 | return _UnionGenericAlias(self, parameters) |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 492 | |
Serhiy Storchaka | 40ded94 | 2020-04-23 21:26:48 +0300 | [diff] [blame] | 493 | @_SpecialForm |
| 494 | def Optional(self, parameters): |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 495 | """Optional type. |
| 496 | |
| 497 | Optional[X] is equivalent to Union[X, None]. |
Serhiy Storchaka | 40ded94 | 2020-04-23 21:26:48 +0300 | [diff] [blame] | 498 | """ |
| 499 | arg = _type_check(parameters, f"{self} requires a single type.") |
| 500 | return Union[arg, type(None)] |
Guido van Rossum | b7dedc8 | 2016-10-29 12:44:29 -0700 | [diff] [blame] | 501 | |
Yurii Karabas | f03d318 | 2020-11-17 04:23:19 +0200 | [diff] [blame] | 502 | @_LiteralSpecialForm |
Serhiy Storchaka | 40ded94 | 2020-04-23 21:26:48 +0300 | [diff] [blame] | 503 | def Literal(self, parameters): |
Ivan Levkivskyi | b891c46 | 2019-05-26 09:37:48 +0100 | [diff] [blame] | 504 | """Special typing form to define literal types (a.k.a. value types). |
| 505 | |
| 506 | This form can be used to indicate to type checkers that the corresponding |
| 507 | variable or function parameter has a value equivalent to the provided |
| 508 | literal (or one of several literals): |
| 509 | |
| 510 | def validate_simple(data: Any) -> Literal[True]: # always returns True |
| 511 | ... |
| 512 | |
| 513 | MODE = Literal['r', 'rb', 'w', 'wb'] |
| 514 | def open_helper(file: str, mode: MODE) -> str: |
| 515 | ... |
| 516 | |
| 517 | open_helper('/some/path', 'r') # Passes type check |
| 518 | open_helper('/other/path', 'typo') # Error in type checker |
| 519 | |
Serhiy Storchaka | 40ded94 | 2020-04-23 21:26:48 +0300 | [diff] [blame] | 520 | Literal[...] cannot be subclassed. At runtime, an arbitrary value |
| 521 | is allowed as type argument to Literal[...], but type checkers may |
| 522 | impose restrictions. |
| 523 | """ |
| 524 | # There is no '_type_check' call because arguments to Literal[...] are |
| 525 | # values, not types. |
Yurii Karabas | f03d318 | 2020-11-17 04:23:19 +0200 | [diff] [blame] | 526 | if not isinstance(parameters, tuple): |
| 527 | parameters = (parameters,) |
| 528 | |
| 529 | parameters = _flatten_literal_params(parameters) |
| 530 | |
| 531 | try: |
| 532 | parameters = tuple(p for p, _ in _deduplicate(list(_value_and_type_iter(parameters)))) |
| 533 | except TypeError: # unhashable parameters |
| 534 | pass |
| 535 | |
| 536 | return _LiteralGenericAlias(self, parameters) |
Ivan Levkivskyi | b891c46 | 2019-05-26 09:37:48 +0100 | [diff] [blame] | 537 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 538 | |
Mikhail Golubev | 4f3c250 | 2020-10-08 00:44:31 +0300 | [diff] [blame] | 539 | @_SpecialForm |
| 540 | def TypeAlias(self, parameters): |
| 541 | """Special marker indicating that an assignment should |
| 542 | be recognized as a proper type alias definition by type |
| 543 | checkers. |
| 544 | |
| 545 | For example:: |
| 546 | |
| 547 | Predicate: TypeAlias = Callable[..., bool] |
| 548 | |
| 549 | It's invalid when used anywhere except as in the example above. |
| 550 | """ |
| 551 | raise TypeError(f"{self} is not subscriptable") |
| 552 | |
| 553 | |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 554 | @_SpecialForm |
| 555 | def Concatenate(self, parameters): |
Ken Jin | 11276cd | 2021-01-02 08:45:50 +0800 | [diff] [blame] | 556 | """Used in conjunction with ``ParamSpec`` and ``Callable`` to represent a |
| 557 | higher order function which adds, removes or transforms parameters of a |
| 558 | callable. |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 559 | |
| 560 | For example:: |
| 561 | |
| 562 | Callable[Concatenate[int, P], int] |
| 563 | |
| 564 | See PEP 612 for detailed information. |
| 565 | """ |
| 566 | if parameters == (): |
| 567 | raise TypeError("Cannot take a Concatenate of no types.") |
| 568 | if not isinstance(parameters, tuple): |
| 569 | parameters = (parameters,) |
| 570 | if not isinstance(parameters[-1], ParamSpec): |
| 571 | raise TypeError("The last parameter to Concatenate should be a " |
| 572 | "ParamSpec variable.") |
| 573 | msg = "Concatenate[arg, ...]: each arg must be a type." |
| 574 | parameters = tuple(_type_check(p, msg) for p in parameters) |
| 575 | return _ConcatenateGenericAlias(self, parameters) |
| 576 | |
| 577 | |
Ken Jin | 05ab4b6 | 2021-04-27 22:31:04 +0800 | [diff] [blame] | 578 | @_SpecialForm |
| 579 | def TypeGuard(self, parameters): |
| 580 | """Special typing form used to annotate the return type of a user-defined |
| 581 | type guard function. ``TypeGuard`` only accepts a single type argument. |
| 582 | At runtime, functions marked this way should return a boolean. |
| 583 | |
| 584 | ``TypeGuard`` aims to benefit *type narrowing* -- a technique used by static |
| 585 | type checkers to determine a more precise type of an expression within a |
| 586 | program's code flow. Usually type narrowing is done by analyzing |
| 587 | conditional code flow and applying the narrowing to a block of code. The |
| 588 | conditional expression here is sometimes referred to as a "type guard". |
| 589 | |
| 590 | Sometimes it would be convenient to use a user-defined boolean function |
| 591 | as a type guard. Such a function should use ``TypeGuard[...]`` as its |
| 592 | return type to alert static type checkers to this intention. |
| 593 | |
| 594 | Using ``-> TypeGuard`` tells the static type checker that for a given |
| 595 | function: |
| 596 | |
| 597 | 1. The return value is a boolean. |
| 598 | 2. If the return value is ``True``, the type of its argument |
| 599 | is the type inside ``TypeGuard``. |
| 600 | |
| 601 | For example:: |
| 602 | |
| 603 | def is_str(val: Union[str, float]): |
| 604 | # "isinstance" type guard |
| 605 | if isinstance(val, str): |
| 606 | # Type of ``val`` is narrowed to ``str`` |
| 607 | ... |
| 608 | else: |
| 609 | # Else, type of ``val`` is narrowed to ``float``. |
| 610 | ... |
| 611 | |
| 612 | Strict type narrowing is not enforced -- ``TypeB`` need not be a narrower |
| 613 | form of ``TypeA`` (it can even be a wider form) and this may lead to |
| 614 | type-unsafe results. The main reason is to allow for things like |
| 615 | narrowing ``List[object]`` to ``List[str]`` even though the latter is not |
| 616 | a subtype of the former, since ``List`` is invariant. The responsibility of |
| 617 | writing type-safe type guards is left to the user. |
| 618 | |
| 619 | ``TypeGuard`` also works with type variables. For more information, see |
| 620 | PEP 647 (User-Defined Type Guards). |
| 621 | """ |
| 622 | item = _type_check(parameters, f'{self} accepts only single type.') |
| 623 | return _GenericAlias(self, (item,)) |
| 624 | |
| 625 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 626 | class ForwardRef(_Final, _root=True): |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 627 | """Internal wrapper to hold a forward reference.""" |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 628 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 629 | __slots__ = ('__forward_arg__', '__forward_code__', |
Nina Zakharenko | 2d2d3b1 | 2018-05-16 12:27:03 -0400 | [diff] [blame] | 630 | '__forward_evaluated__', '__forward_value__', |
| 631 | '__forward_is_argument__') |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 632 | |
Nina Zakharenko | 0e61dff | 2018-05-22 20:32:10 -0700 | [diff] [blame] | 633 | def __init__(self, arg, is_argument=True): |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 634 | if not isinstance(arg, str): |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 635 | raise TypeError(f"Forward reference must be a string -- got {arg!r}") |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 636 | try: |
| 637 | code = compile(arg, '<string>', 'eval') |
| 638 | except SyntaxError: |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 639 | raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}") |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 640 | self.__forward_arg__ = arg |
| 641 | self.__forward_code__ = code |
| 642 | self.__forward_evaluated__ = False |
| 643 | self.__forward_value__ = None |
Nina Zakharenko | 2d2d3b1 | 2018-05-16 12:27:03 -0400 | [diff] [blame] | 644 | self.__forward_is_argument__ = is_argument |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 645 | |
wyfo | 653f420 | 2020-07-22 21:47:28 +0200 | [diff] [blame] | 646 | def _evaluate(self, globalns, localns, recursive_guard): |
| 647 | if self.__forward_arg__ in recursive_guard: |
| 648 | return self |
Guido van Rossum | dad1790 | 2016-11-10 08:29:18 -0800 | [diff] [blame] | 649 | if not self.__forward_evaluated__ or localns is not globalns: |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 650 | if globalns is None and localns is None: |
| 651 | globalns = localns = {} |
| 652 | elif globalns is None: |
| 653 | globalns = localns |
| 654 | elif localns is None: |
| 655 | localns = globalns |
wyfo | 653f420 | 2020-07-22 21:47:28 +0200 | [diff] [blame] | 656 | type_ =_type_check( |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 657 | eval(self.__forward_code__, globalns, localns), |
Nina Zakharenko | 2d2d3b1 | 2018-05-16 12:27:03 -0400 | [diff] [blame] | 658 | "Forward references must evaluate to types.", |
wyfo | 653f420 | 2020-07-22 21:47:28 +0200 | [diff] [blame] | 659 | is_argument=self.__forward_is_argument__, |
| 660 | ) |
| 661 | self.__forward_value__ = _eval_type( |
| 662 | type_, globalns, localns, recursive_guard | {self.__forward_arg__} |
| 663 | ) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 664 | self.__forward_evaluated__ = True |
| 665 | return self.__forward_value__ |
| 666 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 667 | def __eq__(self, other): |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 668 | if not isinstance(other, ForwardRef): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 669 | return NotImplemented |
plokmijnuhby | e082e7c | 2019-09-13 20:40:54 +0100 | [diff] [blame] | 670 | if self.__forward_evaluated__ and other.__forward_evaluated__: |
| 671 | return (self.__forward_arg__ == other.__forward_arg__ and |
| 672 | self.__forward_value__ == other.__forward_value__) |
| 673 | return self.__forward_arg__ == other.__forward_arg__ |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 674 | |
| 675 | def __hash__(self): |
plokmijnuhby | e082e7c | 2019-09-13 20:40:54 +0100 | [diff] [blame] | 676 | return hash(self.__forward_arg__) |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 677 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 678 | def __repr__(self): |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 679 | return f'ForwardRef({self.__forward_arg__!r})' |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 680 | |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 681 | class _TypeVarLike: |
| 682 | """Mixin for TypeVar-like types (TypeVar and ParamSpec).""" |
| 683 | def __init__(self, bound, covariant, contravariant): |
| 684 | """Used to setup TypeVars and ParamSpec's bound, covariant and |
| 685 | contravariant attributes. |
| 686 | """ |
| 687 | if covariant and contravariant: |
| 688 | raise ValueError("Bivariant types are not supported.") |
| 689 | self.__covariant__ = bool(covariant) |
| 690 | self.__contravariant__ = bool(contravariant) |
| 691 | if bound: |
| 692 | self.__bound__ = _type_check(bound, "Bound must be a type.") |
| 693 | else: |
| 694 | self.__bound__ = None |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 695 | |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 696 | def __or__(self, right): |
| 697 | return Union[self, right] |
| 698 | |
Jelle Zijlstra | 9045919 | 2021-04-10 20:00:05 -0700 | [diff] [blame] | 699 | def __ror__(self, left): |
| 700 | return Union[left, self] |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 701 | |
| 702 | def __repr__(self): |
| 703 | if self.__covariant__: |
| 704 | prefix = '+' |
| 705 | elif self.__contravariant__: |
| 706 | prefix = '-' |
| 707 | else: |
| 708 | prefix = '~' |
| 709 | return prefix + self.__name__ |
| 710 | |
| 711 | def __reduce__(self): |
| 712 | return self.__name__ |
| 713 | |
| 714 | |
| 715 | class TypeVar( _Final, _Immutable, _TypeVarLike, _root=True): |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 716 | """Type variable. |
| 717 | |
| 718 | Usage:: |
| 719 | |
| 720 | T = TypeVar('T') # Can be anything |
| 721 | A = TypeVar('A', str, bytes) # Must be str or bytes |
| 722 | |
| 723 | Type variables exist primarily for the benefit of static type |
| 724 | checkers. They serve as the parameters for generic types as well |
| 725 | as for generic function definitions. See class Generic for more |
| 726 | information on generic types. Generic functions work as follows: |
| 727 | |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 728 | def repeat(x: T, n: int) -> List[T]: |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 729 | '''Return a list containing n references to x.''' |
| 730 | return [x]*n |
| 731 | |
| 732 | def longest(x: A, y: A) -> A: |
| 733 | '''Return the longest of two strings.''' |
| 734 | return x if len(x) >= len(y) else y |
| 735 | |
| 736 | The latter example's signature is essentially the overloading |
| 737 | of (str, str) -> str and (bytes, bytes) -> bytes. Also note |
| 738 | that if the arguments are instances of some subclass of str, |
| 739 | the return type is still plain str. |
| 740 | |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 741 | At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 742 | |
Guido van Rossum | efa798d | 2016-08-23 11:01:50 -0700 | [diff] [blame] | 743 | Type variables defined with covariant=True or contravariant=True |
João D. Ferreira | 86bfed3 | 2018-07-07 16:41:20 +0100 | [diff] [blame] | 744 | can be used to declare covariant or contravariant generic types. |
Guido van Rossum | efa798d | 2016-08-23 11:01:50 -0700 | [diff] [blame] | 745 | See PEP 484 for more details. By default generic types are invariant |
| 746 | in all type variables. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 747 | |
| 748 | Type variables can be introspected. e.g.: |
| 749 | |
| 750 | T.__name__ == 'T' |
| 751 | T.__constraints__ == () |
| 752 | T.__covariant__ == False |
| 753 | T.__contravariant__ = False |
| 754 | A.__constraints__ == (str, bytes) |
Ivan Levkivskyi | 8349403 | 2018-03-26 23:01:12 +0100 | [diff] [blame] | 755 | |
| 756 | Note that only type variables defined in global scope can be pickled. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 757 | """ |
| 758 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 759 | __slots__ = ('__name__', '__bound__', '__constraints__', |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 760 | '__covariant__', '__contravariant__', '__dict__') |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 761 | |
| 762 | def __init__(self, name, *constraints, bound=None, |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 763 | covariant=False, contravariant=False): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 764 | self.__name__ = name |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 765 | super().__init__(bound, covariant, contravariant) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 766 | if constraints and bound is not None: |
| 767 | raise TypeError("Constraints cannot be combined with bound=...") |
| 768 | if constraints and len(constraints) == 1: |
| 769 | raise TypeError("A single constraint is not allowed") |
| 770 | msg = "TypeVar(name, constraint, ...): constraints must be types." |
| 771 | self.__constraints__ = tuple(_type_check(t, msg) for t in constraints) |
HongWeipeng | a25a04f | 2020-04-21 04:01:53 +0800 | [diff] [blame] | 772 | try: |
| 773 | def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling |
| 774 | except (AttributeError, ValueError): |
| 775 | def_mod = None |
Serhiy Storchaka | 09f3221 | 2018-05-26 21:19:26 +0300 | [diff] [blame] | 776 | if def_mod != 'typing': |
| 777 | self.__module__ = def_mod |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 778 | |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 779 | |
Jelle Zijlstra | 5224336 | 2021-04-10 19:57:05 -0700 | [diff] [blame] | 780 | class ParamSpecArgs(_Final, _Immutable, _root=True): |
| 781 | """The args for a ParamSpec object. |
| 782 | |
| 783 | Given a ParamSpec object P, P.args is an instance of ParamSpecArgs. |
| 784 | |
| 785 | ParamSpecArgs objects have a reference back to their ParamSpec: |
| 786 | |
| 787 | P.args.__origin__ is P |
| 788 | |
| 789 | This type is meant for runtime introspection and has no special meaning to |
| 790 | static type checkers. |
| 791 | """ |
| 792 | def __init__(self, origin): |
| 793 | self.__origin__ = origin |
| 794 | |
| 795 | def __repr__(self): |
| 796 | return f"{self.__origin__.__name__}.args" |
| 797 | |
| 798 | |
| 799 | class ParamSpecKwargs(_Final, _Immutable, _root=True): |
| 800 | """The kwargs for a ParamSpec object. |
| 801 | |
| 802 | Given a ParamSpec object P, P.kwargs is an instance of ParamSpecKwargs. |
| 803 | |
| 804 | ParamSpecKwargs objects have a reference back to their ParamSpec: |
| 805 | |
| 806 | P.kwargs.__origin__ is P |
| 807 | |
| 808 | This type is meant for runtime introspection and has no special meaning to |
| 809 | static type checkers. |
| 810 | """ |
| 811 | def __init__(self, origin): |
| 812 | self.__origin__ = origin |
| 813 | |
| 814 | def __repr__(self): |
| 815 | return f"{self.__origin__.__name__}.kwargs" |
| 816 | |
| 817 | |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 818 | class ParamSpec(_Final, _Immutable, _TypeVarLike, _root=True): |
| 819 | """Parameter specification variable. |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 820 | |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 821 | Usage:: |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 822 | |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 823 | P = ParamSpec('P') |
| 824 | |
| 825 | Parameter specification variables exist primarily for the benefit of static |
| 826 | type checkers. They are used to forward the parameter types of one |
Ken Jin | 11276cd | 2021-01-02 08:45:50 +0800 | [diff] [blame] | 827 | callable to another callable, a pattern commonly found in higher order |
| 828 | functions and decorators. They are only valid when used in ``Concatenate``, |
| 829 | or s the first argument to ``Callable``, or as parameters for user-defined |
| 830 | Generics. See class Generic for more information on generic types. An |
| 831 | example for annotating a decorator:: |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 832 | |
| 833 | T = TypeVar('T') |
| 834 | P = ParamSpec('P') |
| 835 | |
| 836 | def add_logging(f: Callable[P, T]) -> Callable[P, T]: |
| 837 | '''A type-safe decorator to add logging to a function.''' |
| 838 | def inner(*args: P.args, **kwargs: P.kwargs) -> T: |
| 839 | logging.info(f'{f.__name__} was called') |
| 840 | return f(*args, **kwargs) |
| 841 | return inner |
| 842 | |
| 843 | @add_logging |
| 844 | def add_two(x: float, y: float) -> float: |
| 845 | '''Add two numbers together.''' |
| 846 | return x + y |
| 847 | |
| 848 | Parameter specification variables defined with covariant=True or |
| 849 | contravariant=True can be used to declare covariant or contravariant |
| 850 | generic types. These keyword arguments are valid, but their actual semantics |
| 851 | are yet to be decided. See PEP 612 for details. |
| 852 | |
| 853 | Parameter specification variables can be introspected. e.g.: |
| 854 | |
| 855 | P.__name__ == 'T' |
| 856 | P.__bound__ == None |
| 857 | P.__covariant__ == False |
| 858 | P.__contravariant__ == False |
| 859 | |
| 860 | Note that only parameter specification variables defined in global scope can |
| 861 | be pickled. |
| 862 | """ |
| 863 | |
| 864 | __slots__ = ('__name__', '__bound__', '__covariant__', '__contravariant__', |
| 865 | '__dict__') |
| 866 | |
Jelle Zijlstra | 5224336 | 2021-04-10 19:57:05 -0700 | [diff] [blame] | 867 | @property |
| 868 | def args(self): |
| 869 | return ParamSpecArgs(self) |
| 870 | |
| 871 | @property |
| 872 | def kwargs(self): |
| 873 | return ParamSpecKwargs(self) |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 874 | |
Ken Jin | ace008c | 2021-01-11 08:11:41 +0800 | [diff] [blame] | 875 | def __init__(self, name, *, bound=None, covariant=False, contravariant=False): |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 876 | self.__name__ = name |
| 877 | super().__init__(bound, covariant, contravariant) |
| 878 | try: |
| 879 | def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') |
| 880 | except (AttributeError, ValueError): |
| 881 | def_mod = None |
| 882 | if def_mod != 'typing': |
| 883 | self.__module__ = def_mod |
Ivan Levkivskyi | 8349403 | 2018-03-26 23:01:12 +0100 | [diff] [blame] | 884 | |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 885 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 886 | def _is_dunder(attr): |
| 887 | return attr.startswith('__') and attr.endswith('__') |
Guido van Rossum | 83ec302 | 2017-01-17 20:43:28 -0800 | [diff] [blame] | 888 | |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 889 | class _BaseGenericAlias(_Final, _root=True): |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 890 | """The central part of internal API. |
| 891 | |
| 892 | This represents a generic version of type 'origin' with type arguments 'params'. |
| 893 | There are two kind of these aliases: user defined and special. The special ones |
| 894 | are wrappers around builtin collections and ABCs in collections.abc. These must |
| 895 | have 'name' always set. If 'inst' is False, then the alias can't be instantiated, |
| 896 | this is used by e.g. typing.List and typing.Dict. |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 897 | """ |
Serhiy Storchaka | fcb2856 | 2020-05-10 11:53:16 +0300 | [diff] [blame] | 898 | def __init__(self, origin, *, inst=True, name=None): |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 899 | self._inst = inst |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 900 | self._name = name |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 901 | self.__origin__ = origin |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 902 | self.__slots__ = None # This is not documented. |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 903 | |
| 904 | def __call__(self, *args, **kwargs): |
| 905 | if not self._inst: |
| 906 | raise TypeError(f"Type {self._name} cannot be instantiated; " |
| 907 | f"use {self.__origin__.__name__}() instead") |
| 908 | result = self.__origin__(*args, **kwargs) |
| 909 | try: |
| 910 | result.__orig_class__ = self |
| 911 | except AttributeError: |
| 912 | pass |
| 913 | return result |
| 914 | |
| 915 | def __mro_entries__(self, bases): |
| 916 | res = [] |
| 917 | if self.__origin__ not in bases: |
| 918 | res.append(self.__origin__) |
| 919 | i = bases.index(self) |
| 920 | for b in bases[i+1:]: |
| 921 | if isinstance(b, _BaseGenericAlias) or issubclass(b, Generic): |
| 922 | break |
| 923 | else: |
| 924 | res.append(Generic) |
| 925 | return tuple(res) |
| 926 | |
| 927 | def __getattr__(self, attr): |
| 928 | # We are careful for copy and pickle. |
| 929 | # Also for simplicity we just don't relay all dunder names |
| 930 | if '__origin__' in self.__dict__ and not _is_dunder(attr): |
| 931 | return getattr(self.__origin__, attr) |
| 932 | raise AttributeError(attr) |
| 933 | |
| 934 | def __setattr__(self, attr, val): |
Serhiy Storchaka | fcb2856 | 2020-05-10 11:53:16 +0300 | [diff] [blame] | 935 | if _is_dunder(attr) or attr in ('_name', '_inst', '_nparams'): |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 936 | super().__setattr__(attr, val) |
| 937 | else: |
| 938 | setattr(self.__origin__, attr, val) |
| 939 | |
| 940 | def __instancecheck__(self, obj): |
| 941 | return self.__subclasscheck__(type(obj)) |
| 942 | |
| 943 | def __subclasscheck__(self, cls): |
| 944 | raise TypeError("Subscripted generics cannot be used with" |
| 945 | " class and instance checks") |
| 946 | |
| 947 | |
Serhiy Storchaka | fcb2856 | 2020-05-10 11:53:16 +0300 | [diff] [blame] | 948 | # Special typing constructs Union, Optional, Generic, Callable and Tuple |
| 949 | # use three special attributes for internal bookkeeping of generic types: |
| 950 | # * __parameters__ is a tuple of unique free type parameters of a generic |
| 951 | # type, for example, Dict[T, T].__parameters__ == (T,); |
| 952 | # * __origin__ keeps a reference to a type that was subscripted, |
| 953 | # e.g., Union[T, int].__origin__ == Union, or the non-generic version of |
| 954 | # the type. |
| 955 | # * __args__ is a tuple of all arguments used in subscripting, |
| 956 | # e.g., Dict[T, int].__args__ == (T, int). |
| 957 | |
| 958 | |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 959 | class _GenericAlias(_BaseGenericAlias, _root=True): |
Serhiy Storchaka | fcb2856 | 2020-05-10 11:53:16 +0300 | [diff] [blame] | 960 | def __init__(self, origin, params, *, inst=True, name=None): |
| 961 | super().__init__(origin, inst=inst, name=name) |
| 962 | if not isinstance(params, tuple): |
| 963 | params = (params,) |
| 964 | self.__args__ = tuple(... if a is _TypingEllipsis else |
| 965 | () if a is _TypingEmpty else |
| 966 | a for a in params) |
| 967 | self.__parameters__ = _collect_type_vars(params) |
| 968 | if not name: |
| 969 | self.__module__ = origin.__module__ |
| 970 | |
| 971 | def __eq__(self, other): |
| 972 | if not isinstance(other, _GenericAlias): |
| 973 | return NotImplemented |
| 974 | return (self.__origin__ == other.__origin__ |
| 975 | and self.__args__ == other.__args__) |
| 976 | |
| 977 | def __hash__(self): |
| 978 | return hash((self.__origin__, self.__args__)) |
| 979 | |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 980 | def __or__(self, right): |
| 981 | return Union[self, right] |
| 982 | |
| 983 | def __ror__(self, right): |
| 984 | return Union[self, right] |
| 985 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 986 | @_tp_cache |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 987 | def __getitem__(self, params): |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 988 | if self.__origin__ in (Generic, Protocol): |
| 989 | # Can't subscript Generic[...] or Protocol[...]. |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 990 | raise TypeError(f"Cannot subscript already-subscripted {self}") |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 991 | if not isinstance(params, tuple): |
| 992 | params = (params,) |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 993 | params = tuple(_type_convert(p) for p in params) |
| 994 | if any(isinstance(t, ParamSpec) for t in self.__parameters__): |
| 995 | params = _prepare_paramspec_params(self, params) |
Serhiy Storchaka | fcb2856 | 2020-05-10 11:53:16 +0300 | [diff] [blame] | 996 | _check_generic(self, params, len(self.__parameters__)) |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 997 | |
| 998 | subst = dict(zip(self.__parameters__, params)) |
| 999 | new_args = [] |
| 1000 | for arg in self.__args__: |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 1001 | if isinstance(arg, _TypeVarLike): |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 1002 | arg = subst[arg] |
Serhiy Storchaka | fcb2856 | 2020-05-10 11:53:16 +0300 | [diff] [blame] | 1003 | elif isinstance(arg, (_GenericAlias, GenericAlias)): |
Serhiy Storchaka | 0122d48 | 2020-05-10 13:39:40 +0300 | [diff] [blame] | 1004 | subparams = arg.__parameters__ |
| 1005 | if subparams: |
| 1006 | subargs = tuple(subst[x] for x in subparams) |
| 1007 | arg = arg[subargs] |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 1008 | # Required to flatten out the args for CallableGenericAlias |
| 1009 | if self.__origin__ == collections.abc.Callable and isinstance(arg, tuple): |
| 1010 | new_args.extend(arg) |
| 1011 | else: |
| 1012 | new_args.append(arg) |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 1013 | return self.copy_with(tuple(new_args)) |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 1014 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1015 | def copy_with(self, params): |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 1016 | return self.__class__(self.__origin__, params, name=self._name, inst=self._inst) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1017 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1018 | def __repr__(self): |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 1019 | if self._name: |
| 1020 | name = 'typing.' + self._name |
| 1021 | else: |
| 1022 | name = _type_repr(self.__origin__) |
| 1023 | args = ", ".join([_type_repr(a) for a in self.__args__]) |
| 1024 | return f'{name}[{args}]' |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1025 | |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 1026 | def __reduce__(self): |
| 1027 | if self._name: |
| 1028 | origin = globals()[self._name] |
| 1029 | else: |
| 1030 | origin = self.__origin__ |
| 1031 | args = tuple(self.__args__) |
| 1032 | if len(args) == 1 and not isinstance(args[0], tuple): |
| 1033 | args, = args |
| 1034 | return operator.getitem, (origin, args) |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1035 | |
| 1036 | def __mro_entries__(self, bases): |
| 1037 | if self._name: # generic version of an ABC or built-in class |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 1038 | return super().__mro_entries__(bases) |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1039 | if self.__origin__ is Generic: |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 1040 | if Protocol in bases: |
| 1041 | return () |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1042 | i = bases.index(self) |
| 1043 | for b in bases[i+1:]: |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 1044 | if isinstance(b, _BaseGenericAlias) and b is not self: |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1045 | return () |
| 1046 | return (self.__origin__,) |
| 1047 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1048 | |
Serhiy Storchaka | fcb2856 | 2020-05-10 11:53:16 +0300 | [diff] [blame] | 1049 | # _nparams is the number of accepted parameters, e.g. 0 for Hashable, |
| 1050 | # 1 for List and 2 for Dict. It may be -1 if variable number of |
| 1051 | # parameters are accepted (needs custom __getitem__). |
| 1052 | |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 1053 | class _SpecialGenericAlias(_BaseGenericAlias, _root=True): |
Serhiy Storchaka | fcb2856 | 2020-05-10 11:53:16 +0300 | [diff] [blame] | 1054 | def __init__(self, origin, nparams, *, inst=True, name=None): |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 1055 | if name is None: |
| 1056 | name = origin.__name__ |
Serhiy Storchaka | fcb2856 | 2020-05-10 11:53:16 +0300 | [diff] [blame] | 1057 | super().__init__(origin, inst=inst, name=name) |
| 1058 | self._nparams = nparams |
Serhiy Storchaka | 2fbc57a | 2020-05-10 15:14:27 +0300 | [diff] [blame] | 1059 | if origin.__module__ == 'builtins': |
| 1060 | self.__doc__ = f'A generic version of {origin.__qualname__}.' |
| 1061 | else: |
| 1062 | self.__doc__ = f'A generic version of {origin.__module__}.{origin.__qualname__}.' |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1063 | |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 1064 | @_tp_cache |
| 1065 | def __getitem__(self, params): |
| 1066 | if not isinstance(params, tuple): |
| 1067 | params = (params,) |
| 1068 | msg = "Parameters to generic types must be types." |
| 1069 | params = tuple(_type_check(p, msg) for p in params) |
Serhiy Storchaka | fcb2856 | 2020-05-10 11:53:16 +0300 | [diff] [blame] | 1070 | _check_generic(self, params, self._nparams) |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 1071 | return self.copy_with(params) |
| 1072 | |
| 1073 | def copy_with(self, params): |
| 1074 | return _GenericAlias(self.__origin__, params, |
| 1075 | name=self._name, inst=self._inst) |
| 1076 | |
| 1077 | def __repr__(self): |
| 1078 | return 'typing.' + self._name |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1079 | |
| 1080 | def __subclasscheck__(self, cls): |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 1081 | if isinstance(cls, _SpecialGenericAlias): |
| 1082 | return issubclass(cls.__origin__, self.__origin__) |
| 1083 | if not isinstance(cls, _GenericAlias): |
| 1084 | return issubclass(cls, self.__origin__) |
| 1085 | return super().__subclasscheck__(cls) |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1086 | |
Ivan Levkivskyi | 8349403 | 2018-03-26 23:01:12 +0100 | [diff] [blame] | 1087 | def __reduce__(self): |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 1088 | return self._name |
Ivan Levkivskyi | 8349403 | 2018-03-26 23:01:12 +0100 | [diff] [blame] | 1089 | |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 1090 | def __or__(self, right): |
| 1091 | return Union[self, right] |
| 1092 | |
| 1093 | def __ror__(self, right): |
| 1094 | return Union[self, right] |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1095 | |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 1096 | class _CallableGenericAlias(_GenericAlias, _root=True): |
| 1097 | def __repr__(self): |
| 1098 | assert self._name == 'Callable' |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 1099 | args = self.__args__ |
| 1100 | if len(args) == 2 and (args[0] is Ellipsis |
| 1101 | or isinstance(args[0], (ParamSpec, _ConcatenateGenericAlias))): |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 1102 | return super().__repr__() |
| 1103 | return (f'typing.Callable' |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 1104 | f'[[{", ".join([_type_repr(a) for a in args[:-1]])}], ' |
| 1105 | f'{_type_repr(args[-1])}]') |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 1106 | |
| 1107 | def __reduce__(self): |
| 1108 | args = self.__args__ |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 1109 | if not (len(args) == 2 and (args[0] is Ellipsis |
| 1110 | or isinstance(args[0], (ParamSpec, _ConcatenateGenericAlias)))): |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 1111 | args = list(args[:-1]), args[-1] |
| 1112 | return operator.getitem, (Callable, args) |
| 1113 | |
| 1114 | |
| 1115 | class _CallableType(_SpecialGenericAlias, _root=True): |
| 1116 | def copy_with(self, params): |
| 1117 | return _CallableGenericAlias(self.__origin__, params, |
| 1118 | name=self._name, inst=self._inst) |
| 1119 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1120 | def __getitem__(self, params): |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1121 | if not isinstance(params, tuple) or len(params) != 2: |
| 1122 | raise TypeError("Callable must be used as " |
| 1123 | "Callable[[arg, ...], result].") |
| 1124 | args, result = params |
kj | 463c7d3 | 2020-12-14 02:38:24 +0800 | [diff] [blame] | 1125 | # This relaxes what args can be on purpose to allow things like |
| 1126 | # PEP 612 ParamSpec. Responsibility for whether a user is using |
| 1127 | # Callable[...] properly is deferred to static type checkers. |
| 1128 | if isinstance(args, list): |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1129 | params = (tuple(args), result) |
kj | 463c7d3 | 2020-12-14 02:38:24 +0800 | [diff] [blame] | 1130 | else: |
| 1131 | params = (args, result) |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1132 | return self.__getitem_inner__(params) |
| 1133 | |
| 1134 | @_tp_cache |
| 1135 | def __getitem_inner__(self, params): |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 1136 | args, result = params |
| 1137 | msg = "Callable[args, result]: result must be a type." |
| 1138 | result = _type_check(result, msg) |
| 1139 | if args is Ellipsis: |
| 1140 | return self.copy_with((_TypingEllipsis, result)) |
kj | 463c7d3 | 2020-12-14 02:38:24 +0800 | [diff] [blame] | 1141 | if not isinstance(args, tuple): |
| 1142 | args = (args,) |
| 1143 | args = tuple(_type_convert(arg) for arg in args) |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 1144 | params = args + (result,) |
| 1145 | return self.copy_with(params) |
| 1146 | |
| 1147 | |
| 1148 | class _TupleType(_SpecialGenericAlias, _root=True): |
| 1149 | @_tp_cache |
| 1150 | def __getitem__(self, params): |
| 1151 | if params == (): |
| 1152 | return self.copy_with((_TypingEmpty,)) |
| 1153 | if not isinstance(params, tuple): |
| 1154 | params = (params,) |
| 1155 | if len(params) == 2 and params[1] is ...: |
| 1156 | msg = "Tuple[t, ...]: t must be a type." |
| 1157 | p = _type_check(params[0], msg) |
| 1158 | return self.copy_with((p, _TypingEllipsis)) |
| 1159 | msg = "Tuple[t0, t1, ...]: each t must be a type." |
| 1160 | params = tuple(_type_check(p, msg) for p in params) |
| 1161 | return self.copy_with(params) |
| 1162 | |
| 1163 | |
| 1164 | class _UnionGenericAlias(_GenericAlias, _root=True): |
| 1165 | def copy_with(self, params): |
| 1166 | return Union[params] |
| 1167 | |
| 1168 | def __eq__(self, other): |
| 1169 | if not isinstance(other, _UnionGenericAlias): |
| 1170 | return NotImplemented |
| 1171 | return set(self.__args__) == set(other.__args__) |
| 1172 | |
| 1173 | def __hash__(self): |
| 1174 | return hash(frozenset(self.__args__)) |
| 1175 | |
| 1176 | def __repr__(self): |
| 1177 | args = self.__args__ |
| 1178 | if len(args) == 2: |
| 1179 | if args[0] is type(None): |
| 1180 | return f'typing.Optional[{_type_repr(args[1])}]' |
| 1181 | elif args[1] is type(None): |
| 1182 | return f'typing.Optional[{_type_repr(args[0])}]' |
| 1183 | return super().__repr__() |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1184 | |
Maggie Moss | 1b4552c | 2020-09-09 13:23:24 -0700 | [diff] [blame] | 1185 | def __instancecheck__(self, obj): |
| 1186 | return self.__subclasscheck__(type(obj)) |
| 1187 | |
| 1188 | def __subclasscheck__(self, cls): |
| 1189 | for arg in self.__args__: |
| 1190 | if issubclass(cls, arg): |
| 1191 | return True |
| 1192 | |
| 1193 | |
Yurii Karabas | f03d318 | 2020-11-17 04:23:19 +0200 | [diff] [blame] | 1194 | def _value_and_type_iter(parameters): |
| 1195 | return ((p, type(p)) for p in parameters) |
| 1196 | |
| 1197 | |
| 1198 | class _LiteralGenericAlias(_GenericAlias, _root=True): |
| 1199 | |
| 1200 | def __eq__(self, other): |
| 1201 | if not isinstance(other, _LiteralGenericAlias): |
| 1202 | return NotImplemented |
| 1203 | |
| 1204 | return set(_value_and_type_iter(self.__args__)) == set(_value_and_type_iter(other.__args__)) |
| 1205 | |
| 1206 | def __hash__(self): |
Yurii Karabas | 1b54077 | 2020-11-19 18:17:38 +0200 | [diff] [blame] | 1207 | return hash(frozenset(_value_and_type_iter(self.__args__))) |
Yurii Karabas | f03d318 | 2020-11-17 04:23:19 +0200 | [diff] [blame] | 1208 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1209 | |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 1210 | class _ConcatenateGenericAlias(_GenericAlias, _root=True): |
| 1211 | pass |
| 1212 | |
| 1213 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1214 | class Generic: |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1215 | """Abstract base class for generic types. |
| 1216 | |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 1217 | A generic type is typically declared by inheriting from |
| 1218 | this class parameterized with one or more type variables. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1219 | For example, a generic mapping type might be defined as:: |
| 1220 | |
| 1221 | class Mapping(Generic[KT, VT]): |
| 1222 | def __getitem__(self, key: KT) -> VT: |
| 1223 | ... |
| 1224 | # Etc. |
| 1225 | |
| 1226 | This class can then be used as follows:: |
| 1227 | |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1228 | def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT: |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1229 | try: |
| 1230 | return mapping[key] |
| 1231 | except KeyError: |
| 1232 | return default |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1233 | """ |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 1234 | __slots__ = () |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 1235 | _is_protocol = False |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 1236 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1237 | @_tp_cache |
| 1238 | def __class_getitem__(cls, params): |
| 1239 | if not isinstance(params, tuple): |
| 1240 | params = (params,) |
| 1241 | if not params and cls is not Tuple: |
| 1242 | raise TypeError( |
| 1243 | f"Parameter list to {cls.__qualname__}[...] cannot be empty") |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 1244 | params = tuple(_type_convert(p) for p in params) |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 1245 | if cls in (Generic, Protocol): |
| 1246 | # Generic and Protocol can only be subscripted with unique type variables. |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 1247 | if not all(isinstance(p, _TypeVarLike) for p in params): |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1248 | raise TypeError( |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 1249 | f"Parameters to {cls.__name__}[...] must all be type variables " |
| 1250 | f"or parameter specification variables.") |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1251 | if len(set(params)) != len(params): |
| 1252 | raise TypeError( |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 1253 | f"Parameters to {cls.__name__}[...] must all be unique") |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1254 | else: |
| 1255 | # Subscripting a regular Generic subclass. |
kj | 73607be | 2020-12-24 12:33:48 +0800 | [diff] [blame] | 1256 | if any(isinstance(t, ParamSpec) for t in cls.__parameters__): |
| 1257 | params = _prepare_paramspec_params(cls, params) |
Serhiy Storchaka | fcb2856 | 2020-05-10 11:53:16 +0300 | [diff] [blame] | 1258 | _check_generic(cls, params, len(cls.__parameters__)) |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1259 | return _GenericAlias(cls, params) |
| 1260 | |
| 1261 | def __init_subclass__(cls, *args, **kwargs): |
Ivan Levkivskyi | ee566fe | 2018-04-04 17:00:15 +0100 | [diff] [blame] | 1262 | super().__init_subclass__(*args, **kwargs) |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1263 | tvars = [] |
| 1264 | if '__orig_bases__' in cls.__dict__: |
| 1265 | error = Generic in cls.__orig_bases__ |
| 1266 | else: |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 1267 | error = Generic in cls.__bases__ and cls.__name__ != 'Protocol' |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1268 | if error: |
| 1269 | raise TypeError("Cannot inherit from plain Generic") |
| 1270 | if '__orig_bases__' in cls.__dict__: |
| 1271 | tvars = _collect_type_vars(cls.__orig_bases__) |
| 1272 | # Look for Generic[T1, ..., Tn]. |
| 1273 | # If found, tvars must be a subset of it. |
| 1274 | # If not found, tvars is it. |
| 1275 | # Also check for and reject plain Generic, |
| 1276 | # and reject multiple Generic[...]. |
| 1277 | gvars = None |
| 1278 | for base in cls.__orig_bases__: |
| 1279 | if (isinstance(base, _GenericAlias) and |
| 1280 | base.__origin__ is Generic): |
| 1281 | if gvars is not None: |
| 1282 | raise TypeError( |
| 1283 | "Cannot inherit from Generic[...] multiple types.") |
| 1284 | gvars = base.__parameters__ |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 1285 | if gvars is not None: |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1286 | tvarset = set(tvars) |
| 1287 | gvarset = set(gvars) |
| 1288 | if not tvarset <= gvarset: |
| 1289 | s_vars = ', '.join(str(t) for t in tvars if t not in gvarset) |
| 1290 | s_args = ', '.join(str(g) for g in gvars) |
| 1291 | raise TypeError(f"Some type variables ({s_vars}) are" |
| 1292 | f" not listed in Generic[{s_args}]") |
| 1293 | tvars = gvars |
| 1294 | cls.__parameters__ = tuple(tvars) |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1295 | |
| 1296 | |
| 1297 | class _TypingEmpty: |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 1298 | """Internal placeholder for () or []. Used by TupleMeta and CallableMeta |
| 1299 | to allow empty list/tuple in specific places, without allowing them |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1300 | to sneak in where prohibited. |
| 1301 | """ |
| 1302 | |
| 1303 | |
| 1304 | class _TypingEllipsis: |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 1305 | """Internal placeholder for ... (ellipsis).""" |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1306 | |
| 1307 | |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 1308 | _TYPING_INTERNALS = ['__parameters__', '__orig_bases__', '__orig_class__', |
| 1309 | '_is_protocol', '_is_runtime_protocol'] |
| 1310 | |
| 1311 | _SPECIAL_NAMES = ['__abstractmethods__', '__annotations__', '__dict__', '__doc__', |
| 1312 | '__init__', '__module__', '__new__', '__slots__', |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 1313 | '__subclasshook__', '__weakref__', '__class_getitem__'] |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 1314 | |
| 1315 | # These special attributes will be not collected as protocol members. |
| 1316 | EXCLUDED_ATTRIBUTES = _TYPING_INTERNALS + _SPECIAL_NAMES + ['_MutableMapping__marker'] |
| 1317 | |
| 1318 | |
| 1319 | def _get_protocol_attrs(cls): |
| 1320 | """Collect protocol members from a protocol class objects. |
| 1321 | |
| 1322 | This includes names actually defined in the class dictionary, as well |
| 1323 | as names that appear in annotations. Special names (above) are skipped. |
| 1324 | """ |
| 1325 | attrs = set() |
| 1326 | for base in cls.__mro__[:-1]: # without object |
| 1327 | if base.__name__ in ('Protocol', 'Generic'): |
| 1328 | continue |
| 1329 | annotations = getattr(base, '__annotations__', {}) |
| 1330 | for attr in list(base.__dict__.keys()) + list(annotations.keys()): |
| 1331 | if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES: |
| 1332 | attrs.add(attr) |
| 1333 | return attrs |
| 1334 | |
| 1335 | |
| 1336 | def _is_callable_members_only(cls): |
| 1337 | # PEP 544 prohibits using issubclass() with protocols that have non-method members. |
| 1338 | return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls)) |
| 1339 | |
| 1340 | |
| 1341 | def _no_init(self, *args, **kwargs): |
| 1342 | if type(self)._is_protocol: |
| 1343 | raise TypeError('Protocols cannot be instantiated') |
| 1344 | |
| 1345 | |
Ross | c1af128 | 2020-12-29 11:55:28 +0000 | [diff] [blame] | 1346 | def _allow_reckless_class_checks(): |
Nickolena Fisher | cfaf4c0 | 2020-04-26 12:49:11 -0500 | [diff] [blame] | 1347 | """Allow instance and class checks for special stdlib modules. |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 1348 | |
| 1349 | The abc and functools modules indiscriminately call isinstance() and |
| 1350 | issubclass() on the whole MRO of a user class, which may contain protocols. |
| 1351 | """ |
| 1352 | try: |
| 1353 | return sys._getframe(3).f_globals['__name__'] in ['abc', 'functools'] |
| 1354 | except (AttributeError, ValueError): # For platforms without _getframe(). |
| 1355 | return True |
| 1356 | |
| 1357 | |
Victor Stinner | 0e95bbf | 2020-08-12 10:53:12 +0200 | [diff] [blame] | 1358 | _PROTO_ALLOWLIST = { |
Divij Rajkumar | 692a0dc | 2019-09-12 11:13:51 +0100 | [diff] [blame] | 1359 | 'collections.abc': [ |
| 1360 | 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable', |
| 1361 | 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible', |
| 1362 | ], |
| 1363 | 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'], |
| 1364 | } |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 1365 | |
| 1366 | |
| 1367 | class _ProtocolMeta(ABCMeta): |
| 1368 | # This metaclass is really unfortunate and exists only because of |
| 1369 | # the lack of __instancehook__. |
| 1370 | def __instancecheck__(cls, instance): |
| 1371 | # We need this method for situations where attributes are |
| 1372 | # assigned in __init__. |
| 1373 | if ((not getattr(cls, '_is_protocol', False) or |
| 1374 | _is_callable_members_only(cls)) and |
| 1375 | issubclass(instance.__class__, cls)): |
| 1376 | return True |
| 1377 | if cls._is_protocol: |
| 1378 | if all(hasattr(instance, attr) and |
| 1379 | # All *methods* can be blocked by setting them to None. |
| 1380 | (not callable(getattr(cls, attr, None)) or |
| 1381 | getattr(instance, attr) is not None) |
| 1382 | for attr in _get_protocol_attrs(cls)): |
| 1383 | return True |
| 1384 | return super().__instancecheck__(instance) |
| 1385 | |
| 1386 | |
| 1387 | class Protocol(Generic, metaclass=_ProtocolMeta): |
| 1388 | """Base class for protocol classes. |
| 1389 | |
| 1390 | Protocol classes are defined as:: |
| 1391 | |
| 1392 | class Proto(Protocol): |
| 1393 | def meth(self) -> int: |
| 1394 | ... |
| 1395 | |
| 1396 | Such classes are primarily used with static type checkers that recognize |
| 1397 | structural subtyping (static duck-typing), for example:: |
| 1398 | |
| 1399 | class C: |
| 1400 | def meth(self) -> int: |
| 1401 | return 0 |
| 1402 | |
| 1403 | def func(x: Proto) -> int: |
| 1404 | return x.meth() |
| 1405 | |
| 1406 | func(C()) # Passes static type check |
| 1407 | |
| 1408 | See PEP 544 for details. Protocol classes decorated with |
| 1409 | @typing.runtime_checkable act as simple-minded runtime protocols that check |
| 1410 | only the presence of given attributes, ignoring their type signatures. |
| 1411 | Protocol classes can be generic, they are defined as:: |
| 1412 | |
| 1413 | class GenProto(Protocol[T]): |
| 1414 | def meth(self) -> T: |
| 1415 | ... |
| 1416 | """ |
| 1417 | __slots__ = () |
| 1418 | _is_protocol = True |
| 1419 | _is_runtime_protocol = False |
| 1420 | |
| 1421 | def __init_subclass__(cls, *args, **kwargs): |
| 1422 | super().__init_subclass__(*args, **kwargs) |
| 1423 | |
| 1424 | # Determine if this is a protocol or a concrete subclass. |
| 1425 | if not cls.__dict__.get('_is_protocol', False): |
| 1426 | cls._is_protocol = any(b is Protocol for b in cls.__bases__) |
| 1427 | |
| 1428 | # Set (or override) the protocol subclass hook. |
| 1429 | def _proto_hook(other): |
| 1430 | if not cls.__dict__.get('_is_protocol', False): |
| 1431 | return NotImplemented |
| 1432 | |
| 1433 | # First, perform various sanity checks. |
| 1434 | if not getattr(cls, '_is_runtime_protocol', False): |
Ross | c1af128 | 2020-12-29 11:55:28 +0000 | [diff] [blame] | 1435 | if _allow_reckless_class_checks(): |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 1436 | return NotImplemented |
| 1437 | raise TypeError("Instance and class checks can only be used with" |
| 1438 | " @runtime_checkable protocols") |
| 1439 | if not _is_callable_members_only(cls): |
Ross | c1af128 | 2020-12-29 11:55:28 +0000 | [diff] [blame] | 1440 | if _allow_reckless_class_checks(): |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 1441 | return NotImplemented |
| 1442 | raise TypeError("Protocols with non-method members" |
| 1443 | " don't support issubclass()") |
| 1444 | if not isinstance(other, type): |
| 1445 | # Same error message as for issubclass(1, int). |
| 1446 | raise TypeError('issubclass() arg 1 must be a class') |
| 1447 | |
| 1448 | # Second, perform the actual structural compatibility check. |
| 1449 | for attr in _get_protocol_attrs(cls): |
| 1450 | for base in other.__mro__: |
| 1451 | # Check if the members appears in the class dictionary... |
| 1452 | if attr in base.__dict__: |
| 1453 | if base.__dict__[attr] is None: |
| 1454 | return NotImplemented |
| 1455 | break |
| 1456 | |
| 1457 | # ...or in annotations, if it is a sub-protocol. |
| 1458 | annotations = getattr(base, '__annotations__', {}) |
| 1459 | if (isinstance(annotations, collections.abc.Mapping) and |
| 1460 | attr in annotations and |
| 1461 | issubclass(other, Generic) and other._is_protocol): |
| 1462 | break |
| 1463 | else: |
| 1464 | return NotImplemented |
| 1465 | return True |
| 1466 | |
| 1467 | if '__subclasshook__' not in cls.__dict__: |
| 1468 | cls.__subclasshook__ = _proto_hook |
| 1469 | |
| 1470 | # We have nothing more to do for non-protocols... |
| 1471 | if not cls._is_protocol: |
| 1472 | return |
| 1473 | |
| 1474 | # ... otherwise check consistency of bases, and prohibit instantiation. |
| 1475 | for base in cls.__bases__: |
| 1476 | if not (base in (object, Generic) or |
Victor Stinner | 0e95bbf | 2020-08-12 10:53:12 +0200 | [diff] [blame] | 1477 | base.__module__ in _PROTO_ALLOWLIST and |
| 1478 | base.__name__ in _PROTO_ALLOWLIST[base.__module__] or |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 1479 | issubclass(base, Generic) and base._is_protocol): |
| 1480 | raise TypeError('Protocols can only inherit from other' |
| 1481 | ' protocols, got %r' % base) |
| 1482 | cls.__init__ = _no_init |
| 1483 | |
| 1484 | |
Jakub Stasiak | cf5b109 | 2020-02-05 02:10:19 +0100 | [diff] [blame] | 1485 | class _AnnotatedAlias(_GenericAlias, _root=True): |
| 1486 | """Runtime representation of an annotated type. |
| 1487 | |
| 1488 | At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't' |
| 1489 | with extra annotations. The alias behaves like a normal typing alias, |
| 1490 | instantiating is the same as instantiating the underlying type, binding |
| 1491 | it to types is also the same. |
| 1492 | """ |
| 1493 | def __init__(self, origin, metadata): |
| 1494 | if isinstance(origin, _AnnotatedAlias): |
| 1495 | metadata = origin.__metadata__ + metadata |
| 1496 | origin = origin.__origin__ |
| 1497 | super().__init__(origin, origin) |
| 1498 | self.__metadata__ = metadata |
| 1499 | |
| 1500 | def copy_with(self, params): |
| 1501 | assert len(params) == 1 |
| 1502 | new_type = params[0] |
| 1503 | return _AnnotatedAlias(new_type, self.__metadata__) |
| 1504 | |
| 1505 | def __repr__(self): |
| 1506 | return "typing.Annotated[{}, {}]".format( |
| 1507 | _type_repr(self.__origin__), |
| 1508 | ", ".join(repr(a) for a in self.__metadata__) |
| 1509 | ) |
| 1510 | |
| 1511 | def __reduce__(self): |
| 1512 | return operator.getitem, ( |
| 1513 | Annotated, (self.__origin__,) + self.__metadata__ |
| 1514 | ) |
| 1515 | |
| 1516 | def __eq__(self, other): |
| 1517 | if not isinstance(other, _AnnotatedAlias): |
| 1518 | return NotImplemented |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 1519 | return (self.__origin__ == other.__origin__ |
| 1520 | and self.__metadata__ == other.__metadata__) |
Jakub Stasiak | cf5b109 | 2020-02-05 02:10:19 +0100 | [diff] [blame] | 1521 | |
| 1522 | def __hash__(self): |
| 1523 | return hash((self.__origin__, self.__metadata__)) |
| 1524 | |
| 1525 | |
| 1526 | class Annotated: |
| 1527 | """Add context specific metadata to a type. |
| 1528 | |
| 1529 | Example: Annotated[int, runtime_check.Unsigned] indicates to the |
| 1530 | hypothetical runtime_check module that this type is an unsigned int. |
| 1531 | Every other consumer of this type can ignore this metadata and treat |
| 1532 | this type as int. |
| 1533 | |
| 1534 | The first argument to Annotated must be a valid type. |
| 1535 | |
| 1536 | Details: |
| 1537 | |
| 1538 | - It's an error to call `Annotated` with less than two arguments. |
| 1539 | - Nested Annotated are flattened:: |
| 1540 | |
| 1541 | Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3] |
| 1542 | |
| 1543 | - Instantiating an annotated type is equivalent to instantiating the |
| 1544 | underlying type:: |
| 1545 | |
| 1546 | Annotated[C, Ann1](5) == C(5) |
| 1547 | |
| 1548 | - Annotated can be used as a generic type alias:: |
| 1549 | |
| 1550 | Optimized = Annotated[T, runtime.Optimize()] |
| 1551 | Optimized[int] == Annotated[int, runtime.Optimize()] |
| 1552 | |
| 1553 | OptimizedList = Annotated[List[T], runtime.Optimize()] |
| 1554 | OptimizedList[int] == Annotated[List[int], runtime.Optimize()] |
| 1555 | """ |
| 1556 | |
| 1557 | __slots__ = () |
| 1558 | |
| 1559 | def __new__(cls, *args, **kwargs): |
| 1560 | raise TypeError("Type Annotated cannot be instantiated.") |
| 1561 | |
| 1562 | @_tp_cache |
| 1563 | def __class_getitem__(cls, params): |
| 1564 | if not isinstance(params, tuple) or len(params) < 2: |
| 1565 | raise TypeError("Annotated[...] should be used " |
| 1566 | "with at least two arguments (a type and an " |
| 1567 | "annotation).") |
| 1568 | msg = "Annotated[t, ...]: t must be a type." |
| 1569 | origin = _type_check(params[0], msg) |
| 1570 | metadata = tuple(params[1:]) |
| 1571 | return _AnnotatedAlias(origin, metadata) |
| 1572 | |
| 1573 | def __init_subclass__(cls, *args, **kwargs): |
| 1574 | raise TypeError( |
| 1575 | "Cannot subclass {}.Annotated".format(cls.__module__) |
| 1576 | ) |
| 1577 | |
| 1578 | |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 1579 | def runtime_checkable(cls): |
| 1580 | """Mark a protocol class as a runtime protocol. |
| 1581 | |
| 1582 | Such protocol can be used with isinstance() and issubclass(). |
| 1583 | Raise TypeError if applied to a non-protocol class. |
| 1584 | This allows a simple-minded structural check very similar to |
| 1585 | one trick ponies in collections.abc such as Iterable. |
| 1586 | For example:: |
| 1587 | |
| 1588 | @runtime_checkable |
| 1589 | class Closable(Protocol): |
| 1590 | def close(self): ... |
| 1591 | |
| 1592 | assert isinstance(open('/some/file'), Closable) |
| 1593 | |
| 1594 | Warning: this will check only the presence of the required methods, |
| 1595 | not their type signatures! |
| 1596 | """ |
| 1597 | if not issubclass(cls, Generic) or not cls._is_protocol: |
| 1598 | raise TypeError('@runtime_checkable can be only applied to protocol classes,' |
| 1599 | ' got %r' % cls) |
| 1600 | cls._is_runtime_protocol = True |
| 1601 | return cls |
| 1602 | |
| 1603 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1604 | def cast(typ, val): |
| 1605 | """Cast a value to a type. |
| 1606 | |
| 1607 | This returns the value unchanged. To the type checker this |
| 1608 | signals that the return value has the designated type, but at |
| 1609 | runtime we intentionally don't check anything (we want this |
| 1610 | to be as fast as possible). |
| 1611 | """ |
| 1612 | return val |
| 1613 | |
| 1614 | |
| 1615 | def _get_defaults(func): |
| 1616 | """Internal helper to extract the default arguments, by name.""" |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1617 | try: |
| 1618 | code = func.__code__ |
| 1619 | except AttributeError: |
| 1620 | # Some built-in functions don't have __code__, __defaults__, etc. |
| 1621 | return {} |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1622 | pos_count = code.co_argcount |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1623 | arg_names = code.co_varnames |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1624 | arg_names = arg_names[:pos_count] |
| 1625 | defaults = func.__defaults__ or () |
| 1626 | kwdefaults = func.__kwdefaults__ |
| 1627 | res = dict(kwdefaults) if kwdefaults else {} |
| 1628 | pos_offset = pos_count - len(defaults) |
| 1629 | for name, value in zip(arg_names[pos_offset:], defaults): |
| 1630 | assert name not in res |
| 1631 | res[name] = value |
| 1632 | return res |
| 1633 | |
| 1634 | |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 1635 | _allowed_types = (types.FunctionType, types.BuiltinFunctionType, |
| 1636 | types.MethodType, types.ModuleType, |
Ivan Levkivskyi | f06e021 | 2017-05-02 19:14:07 +0200 | [diff] [blame] | 1637 | WrapperDescriptorType, MethodWrapperType, MethodDescriptorType) |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 1638 | |
| 1639 | |
Jakub Stasiak | cf5b109 | 2020-02-05 02:10:19 +0100 | [diff] [blame] | 1640 | def get_type_hints(obj, globalns=None, localns=None, include_extras=False): |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1641 | """Return type hints for an object. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1642 | |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1643 | This is often the same as obj.__annotations__, but it handles |
Jakub Stasiak | cf5b109 | 2020-02-05 02:10:19 +0100 | [diff] [blame] | 1644 | forward references encoded as string literals, adds Optional[t] if a |
| 1645 | default value equal to None is set and recursively replaces all |
| 1646 | 'Annotated[T, ...]' with 'T' (unless 'include_extras=True'). |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1647 | |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1648 | The argument may be a module, class, method, or function. The annotations |
| 1649 | are returned as a dictionary. For classes, annotations include also |
| 1650 | inherited members. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1651 | |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1652 | TypeError is raised if the argument is not of a type that can contain |
| 1653 | annotations, and an empty dictionary is returned if no annotations are |
| 1654 | present. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1655 | |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1656 | BEWARE -- the behavior of globalns and localns is counterintuitive |
| 1657 | (unless you are familiar with how eval() and exec() work). The |
| 1658 | search order is locals first, then globals. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1659 | |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1660 | - If no dict arguments are passed, an attempt is made to use the |
Łukasz Langa | f350a26 | 2017-09-14 14:33:00 -0400 | [diff] [blame] | 1661 | globals from obj (or the respective module's globals for classes), |
| 1662 | and these are also used as the locals. If the object does not appear |
Ken Jin | 1b1f985 | 2021-04-27 01:31:21 +0800 | [diff] [blame] | 1663 | to have globals, an empty dictionary is used. For classes, the search |
| 1664 | order is globals first then locals. |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1665 | |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1666 | - If one dict argument is passed, it is used for both globals and |
| 1667 | locals. |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1668 | |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1669 | - If two dict arguments are passed, they specify globals and |
| 1670 | locals, respectively. |
| 1671 | """ |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1672 | |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1673 | if getattr(obj, '__no_type_check__', None): |
| 1674 | return {} |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1675 | # Classes require a special treatment. |
| 1676 | if isinstance(obj, type): |
| 1677 | hints = {} |
| 1678 | for base in reversed(obj.__mro__): |
Łukasz Langa | f350a26 | 2017-09-14 14:33:00 -0400 | [diff] [blame] | 1679 | if globalns is None: |
Karthikeyan Singaravelan | a9cf69d | 2021-04-12 23:47:25 +0530 | [diff] [blame] | 1680 | try: |
| 1681 | base_globals = sys.modules[base.__module__].__dict__ |
| 1682 | except KeyError: |
| 1683 | continue |
Łukasz Langa | f350a26 | 2017-09-14 14:33:00 -0400 | [diff] [blame] | 1684 | else: |
| 1685 | base_globals = globalns |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1686 | ann = base.__dict__.get('__annotations__', {}) |
larryhastings | 2f2b698 | 2021-04-29 20:09:08 -0700 | [diff] [blame] | 1687 | if isinstance(ann, types.GetSetDescriptorType): |
| 1688 | ann = {} |
Ken Jin | 852150d | 2021-04-13 01:23:12 +0800 | [diff] [blame] | 1689 | base_locals = dict(vars(base)) if localns is None else localns |
Ken Jin | 1b1f985 | 2021-04-27 01:31:21 +0800 | [diff] [blame] | 1690 | if localns is None and globalns is None: |
| 1691 | # This is surprising, but required. Before Python 3.10, |
| 1692 | # get_type_hints only evaluated the globalns of |
| 1693 | # a class. To maintain backwards compatibility, we reverse |
| 1694 | # the globalns and localns order so that eval() looks into |
| 1695 | # *base_globals* first rather than *base_locals*. |
| 1696 | # This only affects ForwardRefs. |
| 1697 | base_globals, base_locals = base_locals, base_globals |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1698 | for name, value in ann.items(): |
| 1699 | if value is None: |
| 1700 | value = type(None) |
| 1701 | if isinstance(value, str): |
Nina Zakharenko | 0e61dff | 2018-05-22 20:32:10 -0700 | [diff] [blame] | 1702 | value = ForwardRef(value, is_argument=False) |
Ken Jin | 852150d | 2021-04-13 01:23:12 +0800 | [diff] [blame] | 1703 | value = _eval_type(value, base_globals, base_locals) |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1704 | hints[name] = value |
Jakub Stasiak | cf5b109 | 2020-02-05 02:10:19 +0100 | [diff] [blame] | 1705 | return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()} |
Łukasz Langa | f350a26 | 2017-09-14 14:33:00 -0400 | [diff] [blame] | 1706 | |
| 1707 | if globalns is None: |
| 1708 | if isinstance(obj, types.ModuleType): |
| 1709 | globalns = obj.__dict__ |
| 1710 | else: |
benedwards14 | 0aca3a3 | 2019-11-21 17:24:58 +0000 | [diff] [blame] | 1711 | nsobj = obj |
| 1712 | # Find globalns for the unwrapped object. |
| 1713 | while hasattr(nsobj, '__wrapped__'): |
| 1714 | nsobj = nsobj.__wrapped__ |
| 1715 | globalns = getattr(nsobj, '__globals__', {}) |
Łukasz Langa | f350a26 | 2017-09-14 14:33:00 -0400 | [diff] [blame] | 1716 | if localns is None: |
| 1717 | localns = globalns |
| 1718 | elif localns is None: |
| 1719 | localns = globalns |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1720 | hints = getattr(obj, '__annotations__', None) |
| 1721 | if hints is None: |
| 1722 | # Return empty annotations for something that _could_ have them. |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 1723 | if isinstance(obj, _allowed_types): |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1724 | return {} |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1725 | else: |
| 1726 | raise TypeError('{!r} is not a module, class, method, ' |
| 1727 | 'or function.'.format(obj)) |
| 1728 | defaults = _get_defaults(obj) |
| 1729 | hints = dict(hints) |
| 1730 | for name, value in hints.items(): |
| 1731 | if value is None: |
| 1732 | value = type(None) |
| 1733 | if isinstance(value, str): |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1734 | value = ForwardRef(value) |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1735 | value = _eval_type(value, globalns, localns) |
| 1736 | if name in defaults and defaults[name] is None: |
| 1737 | value = Optional[value] |
| 1738 | hints[name] = value |
Jakub Stasiak | cf5b109 | 2020-02-05 02:10:19 +0100 | [diff] [blame] | 1739 | return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()} |
| 1740 | |
| 1741 | |
| 1742 | def _strip_annotations(t): |
| 1743 | """Strips the annotations from a given type. |
| 1744 | """ |
| 1745 | if isinstance(t, _AnnotatedAlias): |
| 1746 | return _strip_annotations(t.__origin__) |
| 1747 | if isinstance(t, _GenericAlias): |
| 1748 | stripped_args = tuple(_strip_annotations(a) for a in t.__args__) |
| 1749 | if stripped_args == t.__args__: |
| 1750 | return t |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 1751 | return t.copy_with(stripped_args) |
Serhiy Storchaka | 68b352a | 2020-04-26 21:21:08 +0300 | [diff] [blame] | 1752 | if isinstance(t, GenericAlias): |
| 1753 | stripped_args = tuple(_strip_annotations(a) for a in t.__args__) |
| 1754 | if stripped_args == t.__args__: |
| 1755 | return t |
| 1756 | return GenericAlias(t.__origin__, stripped_args) |
Jakub Stasiak | cf5b109 | 2020-02-05 02:10:19 +0100 | [diff] [blame] | 1757 | return t |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1758 | |
| 1759 | |
Ivan Levkivskyi | 4c23aff | 2019-05-31 00:10:07 +0100 | [diff] [blame] | 1760 | def get_origin(tp): |
| 1761 | """Get the unsubscripted version of a type. |
| 1762 | |
Jakub Stasiak | 38aaaaa | 2020-02-07 02:15:12 +0100 | [diff] [blame] | 1763 | This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar |
| 1764 | and Annotated. Return None for unsupported types. Examples:: |
Ivan Levkivskyi | 4c23aff | 2019-05-31 00:10:07 +0100 | [diff] [blame] | 1765 | |
| 1766 | get_origin(Literal[42]) is Literal |
| 1767 | get_origin(int) is None |
| 1768 | get_origin(ClassVar[int]) is ClassVar |
| 1769 | get_origin(Generic) is Generic |
| 1770 | get_origin(Generic[T]) is Generic |
| 1771 | get_origin(Union[T, int]) is Union |
| 1772 | get_origin(List[Tuple[T, T]][int]) == list |
Jelle Zijlstra | 5224336 | 2021-04-10 19:57:05 -0700 | [diff] [blame] | 1773 | get_origin(P.args) is P |
Ivan Levkivskyi | 4c23aff | 2019-05-31 00:10:07 +0100 | [diff] [blame] | 1774 | """ |
Jakub Stasiak | cf5b109 | 2020-02-05 02:10:19 +0100 | [diff] [blame] | 1775 | if isinstance(tp, _AnnotatedAlias): |
| 1776 | return Annotated |
Jelle Zijlstra | 5224336 | 2021-04-10 19:57:05 -0700 | [diff] [blame] | 1777 | if isinstance(tp, (_BaseGenericAlias, GenericAlias, |
| 1778 | ParamSpecArgs, ParamSpecKwargs)): |
Ivan Levkivskyi | 4c23aff | 2019-05-31 00:10:07 +0100 | [diff] [blame] | 1779 | return tp.__origin__ |
| 1780 | if tp is Generic: |
| 1781 | return Generic |
Ken Jin | efb1f09 | 2020-12-29 10:26:19 +0800 | [diff] [blame] | 1782 | if isinstance(tp, types.Union): |
| 1783 | return types.Union |
Ivan Levkivskyi | 4c23aff | 2019-05-31 00:10:07 +0100 | [diff] [blame] | 1784 | return None |
| 1785 | |
| 1786 | |
| 1787 | def get_args(tp): |
| 1788 | """Get type arguments with all substitutions performed. |
| 1789 | |
| 1790 | For unions, basic simplifications used by Union constructor are performed. |
| 1791 | Examples:: |
| 1792 | get_args(Dict[str, int]) == (str, int) |
| 1793 | get_args(int) == () |
| 1794 | get_args(Union[int, Union[T, int], str][int]) == (int, str) |
| 1795 | get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int]) |
| 1796 | get_args(Callable[[], T][int]) == ([], int) |
| 1797 | """ |
Jakub Stasiak | cf5b109 | 2020-02-05 02:10:19 +0100 | [diff] [blame] | 1798 | if isinstance(tp, _AnnotatedAlias): |
| 1799 | return (tp.__origin__,) + tp.__metadata__ |
Ken Jin | 4140f10 | 2020-12-29 04:06:19 +0800 | [diff] [blame] | 1800 | if isinstance(tp, (_GenericAlias, GenericAlias)): |
Ivan Levkivskyi | 4c23aff | 2019-05-31 00:10:07 +0100 | [diff] [blame] | 1801 | res = tp.__args__ |
Ken Jin | efb1f09 | 2020-12-29 10:26:19 +0800 | [diff] [blame] | 1802 | if (tp.__origin__ is collections.abc.Callable |
| 1803 | and not (res[0] is Ellipsis |
| 1804 | or isinstance(res[0], (ParamSpec, _ConcatenateGenericAlias)))): |
Ivan Levkivskyi | 4c23aff | 2019-05-31 00:10:07 +0100 | [diff] [blame] | 1805 | res = (list(res[:-1]), res[-1]) |
| 1806 | return res |
Ken Jin | efb1f09 | 2020-12-29 10:26:19 +0800 | [diff] [blame] | 1807 | if isinstance(tp, types.Union): |
| 1808 | return tp.__args__ |
Ivan Levkivskyi | 4c23aff | 2019-05-31 00:10:07 +0100 | [diff] [blame] | 1809 | return () |
| 1810 | |
| 1811 | |
Patrick Reader | 0705ec8 | 2020-09-16 05:58:32 +0100 | [diff] [blame] | 1812 | def is_typeddict(tp): |
| 1813 | """Check if an annotation is a TypedDict class |
| 1814 | |
| 1815 | For example:: |
| 1816 | class Film(TypedDict): |
| 1817 | title: str |
| 1818 | year: int |
| 1819 | |
| 1820 | is_typeddict(Film) # => True |
| 1821 | is_typeddict(Union[list, str]) # => False |
| 1822 | """ |
| 1823 | return isinstance(tp, _TypedDictMeta) |
| 1824 | |
| 1825 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1826 | def no_type_check(arg): |
| 1827 | """Decorator to indicate that annotations are not type hints. |
| 1828 | |
| 1829 | The argument must be a class or function; if it is a class, it |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1830 | applies recursively to all methods and classes defined in that class |
| 1831 | (but not to methods defined in its superclasses or subclasses). |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1832 | |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1833 | This mutates the function(s) or class(es) in place. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1834 | """ |
| 1835 | if isinstance(arg, type): |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1836 | arg_attrs = arg.__dict__.copy() |
| 1837 | for attr, val in arg.__dict__.items(): |
Ivan Levkivskyi | 65bc620 | 2017-09-14 01:25:15 +0200 | [diff] [blame] | 1838 | if val in arg.__bases__ + (arg,): |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1839 | arg_attrs.pop(attr) |
| 1840 | for obj in arg_attrs.values(): |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1841 | if isinstance(obj, types.FunctionType): |
| 1842 | obj.__no_type_check__ = True |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1843 | if isinstance(obj, type): |
| 1844 | no_type_check(obj) |
| 1845 | try: |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1846 | arg.__no_type_check__ = True |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 1847 | except TypeError: # built-in classes |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1848 | pass |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1849 | return arg |
| 1850 | |
| 1851 | |
| 1852 | def no_type_check_decorator(decorator): |
| 1853 | """Decorator to give another decorator the @no_type_check effect. |
| 1854 | |
| 1855 | This wraps the decorator with something that wraps the decorated |
| 1856 | function in @no_type_check. |
| 1857 | """ |
| 1858 | |
| 1859 | @functools.wraps(decorator) |
| 1860 | def wrapped_decorator(*args, **kwds): |
| 1861 | func = decorator(*args, **kwds) |
| 1862 | func = no_type_check(func) |
| 1863 | return func |
| 1864 | |
| 1865 | return wrapped_decorator |
| 1866 | |
| 1867 | |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1868 | def _overload_dummy(*args, **kwds): |
| 1869 | """Helper for @overload to raise when called.""" |
| 1870 | raise NotImplementedError( |
| 1871 | "You should not call an overloaded function. " |
| 1872 | "A series of @overload-decorated functions " |
| 1873 | "outside a stub module should always be followed " |
| 1874 | "by an implementation that is not @overload-ed.") |
| 1875 | |
| 1876 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1877 | def overload(func): |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1878 | """Decorator for overloaded functions/methods. |
| 1879 | |
| 1880 | In a stub file, place two or more stub definitions for the same |
| 1881 | function in a row, each decorated with @overload. For example: |
| 1882 | |
| 1883 | @overload |
| 1884 | def utf8(value: None) -> None: ... |
| 1885 | @overload |
| 1886 | def utf8(value: bytes) -> bytes: ... |
| 1887 | @overload |
| 1888 | def utf8(value: str) -> bytes: ... |
| 1889 | |
| 1890 | In a non-stub file (i.e. a regular .py file), do the same but |
| 1891 | follow it with an implementation. The implementation should *not* |
| 1892 | be decorated with @overload. For example: |
| 1893 | |
| 1894 | @overload |
| 1895 | def utf8(value: None) -> None: ... |
| 1896 | @overload |
| 1897 | def utf8(value: bytes) -> bytes: ... |
| 1898 | @overload |
| 1899 | def utf8(value: str) -> bytes: ... |
| 1900 | def utf8(value): |
| 1901 | # implementation goes here |
| 1902 | """ |
| 1903 | return _overload_dummy |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1904 | |
| 1905 | |
Ivan Levkivskyi | f367242 | 2019-05-26 09:37:07 +0100 | [diff] [blame] | 1906 | def final(f): |
| 1907 | """A decorator to indicate final methods and final classes. |
| 1908 | |
| 1909 | Use this decorator to indicate to type checkers that the decorated |
| 1910 | method cannot be overridden, and decorated class cannot be subclassed. |
| 1911 | For example: |
| 1912 | |
| 1913 | class Base: |
| 1914 | @final |
| 1915 | def done(self) -> None: |
| 1916 | ... |
| 1917 | class Sub(Base): |
| 1918 | def done(self) -> None: # Error reported by type checker |
| 1919 | ... |
| 1920 | |
| 1921 | @final |
| 1922 | class Leaf: |
| 1923 | ... |
| 1924 | class Other(Leaf): # Error reported by type checker |
| 1925 | ... |
| 1926 | |
| 1927 | There is no runtime checking of these properties. |
| 1928 | """ |
| 1929 | return f |
| 1930 | |
| 1931 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1932 | # Some unconstrained type variables. These are used by the container types. |
| 1933 | # (These are not for export.) |
| 1934 | T = TypeVar('T') # Any type. |
| 1935 | KT = TypeVar('KT') # Key type. |
| 1936 | VT = TypeVar('VT') # Value type. |
| 1937 | T_co = TypeVar('T_co', covariant=True) # Any type covariant containers. |
| 1938 | V_co = TypeVar('V_co', covariant=True) # Any type covariant containers. |
| 1939 | VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers. |
| 1940 | T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant. |
| 1941 | # Internal type variable used for Type[]. |
| 1942 | CT_co = TypeVar('CT_co', covariant=True, bound=type) |
| 1943 | |
| 1944 | # A useful type variable with constraints. This represents string types. |
| 1945 | # (This one *is* for export!) |
| 1946 | AnyStr = TypeVar('AnyStr', bytes, str) |
| 1947 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1948 | |
| 1949 | # Various ABCs mimicking those in collections.abc. |
Serhiy Storchaka | c1c7d8e | 2020-05-07 04:09:33 +0300 | [diff] [blame] | 1950 | _alias = _SpecialGenericAlias |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1951 | |
Serhiy Storchaka | fcb2856 | 2020-05-10 11:53:16 +0300 | [diff] [blame] | 1952 | Hashable = _alias(collections.abc.Hashable, 0) # Not generic. |
| 1953 | Awaitable = _alias(collections.abc.Awaitable, 1) |
| 1954 | Coroutine = _alias(collections.abc.Coroutine, 3) |
| 1955 | AsyncIterable = _alias(collections.abc.AsyncIterable, 1) |
| 1956 | AsyncIterator = _alias(collections.abc.AsyncIterator, 1) |
| 1957 | Iterable = _alias(collections.abc.Iterable, 1) |
| 1958 | Iterator = _alias(collections.abc.Iterator, 1) |
| 1959 | Reversible = _alias(collections.abc.Reversible, 1) |
| 1960 | Sized = _alias(collections.abc.Sized, 0) # Not generic. |
| 1961 | Container = _alias(collections.abc.Container, 1) |
| 1962 | Collection = _alias(collections.abc.Collection, 1) |
| 1963 | Callable = _CallableType(collections.abc.Callable, 2) |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1964 | Callable.__doc__ = \ |
| 1965 | """Callable type; Callable[[int], str] is a function of (int) -> str. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1966 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1967 | The subscription syntax must always be used with exactly two |
| 1968 | values: the argument list and the return type. The argument list |
| 1969 | must be a list of types or ellipsis; the return type must be a single type. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1970 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1971 | There is no syntax to indicate optional or keyword arguments, |
| 1972 | such function types are rarely used as callback types. |
| 1973 | """ |
Serhiy Storchaka | fcb2856 | 2020-05-10 11:53:16 +0300 | [diff] [blame] | 1974 | AbstractSet = _alias(collections.abc.Set, 1, name='AbstractSet') |
| 1975 | MutableSet = _alias(collections.abc.MutableSet, 1) |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1976 | # NOTE: Mapping is only covariant in the value type. |
Serhiy Storchaka | fcb2856 | 2020-05-10 11:53:16 +0300 | [diff] [blame] | 1977 | Mapping = _alias(collections.abc.Mapping, 2) |
| 1978 | MutableMapping = _alias(collections.abc.MutableMapping, 2) |
| 1979 | Sequence = _alias(collections.abc.Sequence, 1) |
| 1980 | MutableSequence = _alias(collections.abc.MutableSequence, 1) |
| 1981 | ByteString = _alias(collections.abc.ByteString, 0) # Not generic |
| 1982 | # Tuple accepts variable number of parameters. |
| 1983 | Tuple = _TupleType(tuple, -1, inst=False, name='Tuple') |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1984 | Tuple.__doc__ = \ |
| 1985 | """Tuple type; Tuple[X, Y] is the cross-product type of X and Y. |
Guido van Rossum | 62fe1bb | 2016-10-29 16:05:26 -0700 | [diff] [blame] | 1986 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1987 | Example: Tuple[T1, T2] is a tuple of two elements corresponding |
| 1988 | to type variables T1 and T2. Tuple[int, float, str] is a tuple |
| 1989 | of an int, a float and a string. |
Guido van Rossum | 62fe1bb | 2016-10-29 16:05:26 -0700 | [diff] [blame] | 1990 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 1991 | To specify a variable-length tuple of homogeneous type, use Tuple[T, ...]. |
| 1992 | """ |
Serhiy Storchaka | fcb2856 | 2020-05-10 11:53:16 +0300 | [diff] [blame] | 1993 | List = _alias(list, 1, inst=False, name='List') |
| 1994 | Deque = _alias(collections.deque, 1, name='Deque') |
| 1995 | Set = _alias(set, 1, inst=False, name='Set') |
| 1996 | FrozenSet = _alias(frozenset, 1, inst=False, name='FrozenSet') |
| 1997 | MappingView = _alias(collections.abc.MappingView, 1) |
| 1998 | KeysView = _alias(collections.abc.KeysView, 1) |
| 1999 | ItemsView = _alias(collections.abc.ItemsView, 2) |
| 2000 | ValuesView = _alias(collections.abc.ValuesView, 1) |
| 2001 | ContextManager = _alias(contextlib.AbstractContextManager, 1, name='ContextManager') |
| 2002 | AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, 1, name='AsyncContextManager') |
| 2003 | Dict = _alias(dict, 2, inst=False, name='Dict') |
| 2004 | DefaultDict = _alias(collections.defaultdict, 2, name='DefaultDict') |
| 2005 | OrderedDict = _alias(collections.OrderedDict, 2) |
| 2006 | Counter = _alias(collections.Counter, 1) |
| 2007 | ChainMap = _alias(collections.ChainMap, 2) |
| 2008 | Generator = _alias(collections.abc.Generator, 3) |
| 2009 | AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2) |
| 2010 | Type = _alias(type, 1, inst=False, name='Type') |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 2011 | Type.__doc__ = \ |
| 2012 | """A special construct usable to annotate class objects. |
Guido van Rossum | 62fe1bb | 2016-10-29 16:05:26 -0700 | [diff] [blame] | 2013 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 2014 | For example, suppose we have the following classes:: |
Guido van Rossum | 62fe1bb | 2016-10-29 16:05:26 -0700 | [diff] [blame] | 2015 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 2016 | class User: ... # Abstract base for User classes |
| 2017 | class BasicUser(User): ... |
| 2018 | class ProUser(User): ... |
| 2019 | class TeamUser(User): ... |
Guido van Rossum | f17c200 | 2015-12-03 17:31:24 -0800 | [diff] [blame] | 2020 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 2021 | And a function that takes a class argument that's a subclass of |
| 2022 | User and returns an instance of the corresponding class:: |
Guido van Rossum | f17c200 | 2015-12-03 17:31:24 -0800 | [diff] [blame] | 2023 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 2024 | U = TypeVar('U', bound=User) |
| 2025 | def new_user(user_class: Type[U]) -> U: |
| 2026 | user = user_class() |
| 2027 | # (Here we could write the user object to a database) |
| 2028 | return user |
Guido van Rossum | f17c200 | 2015-12-03 17:31:24 -0800 | [diff] [blame] | 2029 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 2030 | joe = new_user(BasicUser) |
Guido van Rossum | f17c200 | 2015-12-03 17:31:24 -0800 | [diff] [blame] | 2031 | |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 2032 | At this point the type checker knows that joe has type BasicUser. |
| 2033 | """ |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2034 | |
| 2035 | |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 2036 | @runtime_checkable |
| 2037 | class SupportsInt(Protocol): |
Serhiy Storchaka | 8252c52 | 2019-10-08 16:30:17 +0300 | [diff] [blame] | 2038 | """An ABC with one abstract method __int__.""" |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 2039 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2040 | |
| 2041 | @abstractmethod |
| 2042 | def __int__(self) -> int: |
| 2043 | pass |
| 2044 | |
| 2045 | |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 2046 | @runtime_checkable |
| 2047 | class SupportsFloat(Protocol): |
Serhiy Storchaka | 8252c52 | 2019-10-08 16:30:17 +0300 | [diff] [blame] | 2048 | """An ABC with one abstract method __float__.""" |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 2049 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2050 | |
| 2051 | @abstractmethod |
| 2052 | def __float__(self) -> float: |
| 2053 | pass |
| 2054 | |
| 2055 | |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 2056 | @runtime_checkable |
| 2057 | class SupportsComplex(Protocol): |
Serhiy Storchaka | 8252c52 | 2019-10-08 16:30:17 +0300 | [diff] [blame] | 2058 | """An ABC with one abstract method __complex__.""" |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 2059 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2060 | |
| 2061 | @abstractmethod |
| 2062 | def __complex__(self) -> complex: |
| 2063 | pass |
| 2064 | |
| 2065 | |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 2066 | @runtime_checkable |
| 2067 | class SupportsBytes(Protocol): |
Serhiy Storchaka | 8252c52 | 2019-10-08 16:30:17 +0300 | [diff] [blame] | 2068 | """An ABC with one abstract method __bytes__.""" |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 2069 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2070 | |
| 2071 | @abstractmethod |
| 2072 | def __bytes__(self) -> bytes: |
| 2073 | pass |
| 2074 | |
| 2075 | |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 2076 | @runtime_checkable |
| 2077 | class SupportsIndex(Protocol): |
Serhiy Storchaka | 8252c52 | 2019-10-08 16:30:17 +0300 | [diff] [blame] | 2078 | """An ABC with one abstract method __index__.""" |
Paul Dagnelie | 4c7a46e | 2019-05-22 07:23:01 -0700 | [diff] [blame] | 2079 | __slots__ = () |
| 2080 | |
| 2081 | @abstractmethod |
| 2082 | def __index__(self) -> int: |
| 2083 | pass |
| 2084 | |
| 2085 | |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 2086 | @runtime_checkable |
| 2087 | class SupportsAbs(Protocol[T_co]): |
Serhiy Storchaka | 8252c52 | 2019-10-08 16:30:17 +0300 | [diff] [blame] | 2088 | """An ABC with one abstract method __abs__ that is covariant in its return type.""" |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 2089 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2090 | |
| 2091 | @abstractmethod |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 2092 | def __abs__(self) -> T_co: |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2093 | pass |
| 2094 | |
| 2095 | |
Ivan Levkivskyi | 74d7f76 | 2019-05-28 08:40:15 +0100 | [diff] [blame] | 2096 | @runtime_checkable |
| 2097 | class SupportsRound(Protocol[T_co]): |
Serhiy Storchaka | 8252c52 | 2019-10-08 16:30:17 +0300 | [diff] [blame] | 2098 | """An ABC with one abstract method __round__ that is covariant in its return type.""" |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 2099 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2100 | |
| 2101 | @abstractmethod |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 2102 | def __round__(self, ndigits: int = 0) -> T_co: |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2103 | pass |
| 2104 | |
| 2105 | |
Serhiy Storchaka | a2ec069 | 2020-04-08 10:59:04 +0300 | [diff] [blame] | 2106 | def _make_nmtuple(name, types, module, defaults = ()): |
| 2107 | fields = [n for n, t in types] |
| 2108 | types = {n: _type_check(t, f"field {n} annotation must be a type") |
| 2109 | for n, t in types} |
| 2110 | nm_tpl = collections.namedtuple(name, fields, |
| 2111 | defaults=defaults, module=module) |
| 2112 | nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2113 | return nm_tpl |
| 2114 | |
| 2115 | |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 2116 | # attributes prohibited to set in NamedTuple class syntax |
Serhiy Storchaka | a2ec069 | 2020-04-08 10:59:04 +0300 | [diff] [blame] | 2117 | _prohibited = frozenset({'__new__', '__init__', '__slots__', '__getnewargs__', |
| 2118 | '_fields', '_field_defaults', |
| 2119 | '_make', '_replace', '_asdict', '_source'}) |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 2120 | |
Serhiy Storchaka | a2ec069 | 2020-04-08 10:59:04 +0300 | [diff] [blame] | 2121 | _special = frozenset({'__module__', '__name__', '__annotations__'}) |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 2122 | |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2123 | |
Guido van Rossum | 2f84144 | 2016-11-15 09:48:06 -0800 | [diff] [blame] | 2124 | class NamedTupleMeta(type): |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2125 | |
Guido van Rossum | 2f84144 | 2016-11-15 09:48:06 -0800 | [diff] [blame] | 2126 | def __new__(cls, typename, bases, ns): |
Serhiy Storchaka | a2ec069 | 2020-04-08 10:59:04 +0300 | [diff] [blame] | 2127 | assert bases[0] is _NamedTuple |
Guido van Rossum | 2f84144 | 2016-11-15 09:48:06 -0800 | [diff] [blame] | 2128 | types = ns.get('__annotations__', {}) |
Serhiy Storchaka | a2ec069 | 2020-04-08 10:59:04 +0300 | [diff] [blame] | 2129 | default_names = [] |
Guido van Rossum | 3c268be | 2017-01-18 08:03:50 -0800 | [diff] [blame] | 2130 | for field_name in types: |
| 2131 | if field_name in ns: |
Serhiy Storchaka | a2ec069 | 2020-04-08 10:59:04 +0300 | [diff] [blame] | 2132 | default_names.append(field_name) |
| 2133 | elif default_names: |
| 2134 | raise TypeError(f"Non-default namedtuple field {field_name} " |
| 2135 | f"cannot follow default field" |
| 2136 | f"{'s' if len(default_names) > 1 else ''} " |
| 2137 | f"{', '.join(default_names)}") |
| 2138 | nm_tpl = _make_nmtuple(typename, types.items(), |
| 2139 | defaults=[ns[n] for n in default_names], |
| 2140 | module=ns['__module__']) |
Guido van Rossum | 95919c0 | 2017-01-22 17:47:20 -0800 | [diff] [blame] | 2141 | # update from user namespace without overriding special namedtuple attributes |
| 2142 | for key in ns: |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 2143 | if key in _prohibited: |
| 2144 | raise AttributeError("Cannot overwrite NamedTuple attribute " + key) |
| 2145 | elif key not in _special and key not in nm_tpl._fields: |
Guido van Rossum | 95919c0 | 2017-01-22 17:47:20 -0800 | [diff] [blame] | 2146 | setattr(nm_tpl, key, ns[key]) |
Guido van Rossum | 3c268be | 2017-01-18 08:03:50 -0800 | [diff] [blame] | 2147 | return nm_tpl |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2148 | |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 2149 | |
Serhiy Storchaka | a2ec069 | 2020-04-08 10:59:04 +0300 | [diff] [blame] | 2150 | def NamedTuple(typename, fields=None, /, **kwargs): |
Guido van Rossum | 2f84144 | 2016-11-15 09:48:06 -0800 | [diff] [blame] | 2151 | """Typed version of namedtuple. |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2152 | |
Guido van Rossum | 2f84144 | 2016-11-15 09:48:06 -0800 | [diff] [blame] | 2153 | Usage in Python versions >= 3.6:: |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2154 | |
Guido van Rossum | 2f84144 | 2016-11-15 09:48:06 -0800 | [diff] [blame] | 2155 | class Employee(NamedTuple): |
| 2156 | name: str |
| 2157 | id: int |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2158 | |
Guido van Rossum | 2f84144 | 2016-11-15 09:48:06 -0800 | [diff] [blame] | 2159 | This is equivalent to:: |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2160 | |
Guido van Rossum | 2f84144 | 2016-11-15 09:48:06 -0800 | [diff] [blame] | 2161 | Employee = collections.namedtuple('Employee', ['name', 'id']) |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2162 | |
Raymond Hettinger | f7b57df | 2019-03-18 09:53:56 -0700 | [diff] [blame] | 2163 | The resulting class has an extra __annotations__ attribute, giving a |
| 2164 | dict that maps field names to types. (The field names are also in |
| 2165 | the _fields attribute, which is part of the namedtuple API.) |
| 2166 | Alternative equivalent keyword syntax is also accepted:: |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2167 | |
Guido van Rossum | 2f84144 | 2016-11-15 09:48:06 -0800 | [diff] [blame] | 2168 | Employee = NamedTuple('Employee', name=str, id=int) |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2169 | |
Guido van Rossum | 2f84144 | 2016-11-15 09:48:06 -0800 | [diff] [blame] | 2170 | In Python versions <= 3.5 use:: |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2171 | |
Guido van Rossum | 2f84144 | 2016-11-15 09:48:06 -0800 | [diff] [blame] | 2172 | Employee = NamedTuple('Employee', [('name', str), ('id', int)]) |
| 2173 | """ |
Serhiy Storchaka | a2ec069 | 2020-04-08 10:59:04 +0300 | [diff] [blame] | 2174 | if fields is None: |
| 2175 | fields = kwargs.items() |
| 2176 | elif kwargs: |
| 2177 | raise TypeError("Either list of fields or keywords" |
| 2178 | " can be provided to NamedTuple, not both") |
| 2179 | try: |
| 2180 | module = sys._getframe(1).f_globals.get('__name__', '__main__') |
| 2181 | except (AttributeError, ValueError): |
| 2182 | module = None |
| 2183 | return _make_nmtuple(typename, fields, module=module) |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2184 | |
Serhiy Storchaka | a2ec069 | 2020-04-08 10:59:04 +0300 | [diff] [blame] | 2185 | _NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {}) |
| 2186 | |
| 2187 | def _namedtuple_mro_entries(bases): |
| 2188 | if len(bases) > 1: |
| 2189 | raise TypeError("Multiple inheritance with NamedTuple is not supported") |
| 2190 | assert bases[0] is NamedTuple |
| 2191 | return (_NamedTuple,) |
| 2192 | |
| 2193 | NamedTuple.__mro_entries__ = _namedtuple_mro_entries |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2194 | |
| 2195 | |
Ivan Levkivskyi | 135c6a5 | 2019-05-26 09:39:24 +0100 | [diff] [blame] | 2196 | class _TypedDictMeta(type): |
| 2197 | def __new__(cls, name, bases, ns, total=True): |
| 2198 | """Create new typed dict class object. |
| 2199 | |
Serhiy Storchaka | f228bf2 | 2020-04-08 11:03:27 +0300 | [diff] [blame] | 2200 | This method is called when TypedDict is subclassed, |
| 2201 | or when TypedDict is instantiated. This way |
Ivan Levkivskyi | 135c6a5 | 2019-05-26 09:39:24 +0100 | [diff] [blame] | 2202 | TypedDict supports all three syntax forms described in its docstring. |
Serhiy Storchaka | f228bf2 | 2020-04-08 11:03:27 +0300 | [diff] [blame] | 2203 | Subclasses and instances of TypedDict return actual dictionaries. |
Ivan Levkivskyi | 135c6a5 | 2019-05-26 09:39:24 +0100 | [diff] [blame] | 2204 | """ |
Serhiy Storchaka | f228bf2 | 2020-04-08 11:03:27 +0300 | [diff] [blame] | 2205 | for base in bases: |
| 2206 | if type(base) is not _TypedDictMeta: |
| 2207 | raise TypeError('cannot inherit from both a TypedDict type ' |
| 2208 | 'and a non-TypedDict base class') |
| 2209 | tp_dict = type.__new__(_TypedDictMeta, name, (dict,), ns) |
Ivan Levkivskyi | 135c6a5 | 2019-05-26 09:39:24 +0100 | [diff] [blame] | 2210 | |
Vlad Emelianov | 10e87e5 | 2020-02-13 20:53:29 +0100 | [diff] [blame] | 2211 | annotations = {} |
| 2212 | own_annotations = ns.get('__annotations__', {}) |
| 2213 | own_annotation_keys = set(own_annotations.keys()) |
Ivan Levkivskyi | 135c6a5 | 2019-05-26 09:39:24 +0100 | [diff] [blame] | 2214 | msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type" |
Vlad Emelianov | 10e87e5 | 2020-02-13 20:53:29 +0100 | [diff] [blame] | 2215 | own_annotations = { |
| 2216 | n: _type_check(tp, msg) for n, tp in own_annotations.items() |
| 2217 | } |
| 2218 | required_keys = set() |
| 2219 | optional_keys = set() |
Zac Hatfield-Dodds | 665ad3d | 2019-11-24 21:48:48 +1100 | [diff] [blame] | 2220 | |
Ivan Levkivskyi | 135c6a5 | 2019-05-26 09:39:24 +0100 | [diff] [blame] | 2221 | for base in bases: |
Vlad Emelianov | 10e87e5 | 2020-02-13 20:53:29 +0100 | [diff] [blame] | 2222 | annotations.update(base.__dict__.get('__annotations__', {})) |
| 2223 | required_keys.update(base.__dict__.get('__required_keys__', ())) |
| 2224 | optional_keys.update(base.__dict__.get('__optional_keys__', ())) |
Zac Hatfield-Dodds | 665ad3d | 2019-11-24 21:48:48 +1100 | [diff] [blame] | 2225 | |
Vlad Emelianov | 10e87e5 | 2020-02-13 20:53:29 +0100 | [diff] [blame] | 2226 | annotations.update(own_annotations) |
| 2227 | if total: |
| 2228 | required_keys.update(own_annotation_keys) |
| 2229 | else: |
| 2230 | optional_keys.update(own_annotation_keys) |
| 2231 | |
| 2232 | tp_dict.__annotations__ = annotations |
| 2233 | tp_dict.__required_keys__ = frozenset(required_keys) |
| 2234 | tp_dict.__optional_keys__ = frozenset(optional_keys) |
Ivan Levkivskyi | 135c6a5 | 2019-05-26 09:39:24 +0100 | [diff] [blame] | 2235 | if not hasattr(tp_dict, '__total__'): |
| 2236 | tp_dict.__total__ = total |
| 2237 | return tp_dict |
| 2238 | |
Serhiy Storchaka | f228bf2 | 2020-04-08 11:03:27 +0300 | [diff] [blame] | 2239 | __call__ = dict # static method |
| 2240 | |
| 2241 | def __subclasscheck__(cls, other): |
| 2242 | # Typed dicts are only for static structural subtyping. |
| 2243 | raise TypeError('TypedDict does not support instance and class checks') |
| 2244 | |
| 2245 | __instancecheck__ = __subclasscheck__ |
Ivan Levkivskyi | 135c6a5 | 2019-05-26 09:39:24 +0100 | [diff] [blame] | 2246 | |
| 2247 | |
Serhiy Storchaka | f228bf2 | 2020-04-08 11:03:27 +0300 | [diff] [blame] | 2248 | def TypedDict(typename, fields=None, /, *, total=True, **kwargs): |
Ivan Levkivskyi | 135c6a5 | 2019-05-26 09:39:24 +0100 | [diff] [blame] | 2249 | """A simple typed namespace. At runtime it is equivalent to a plain dict. |
| 2250 | |
| 2251 | TypedDict creates a dictionary type that expects all of its |
| 2252 | instances to have a certain set of keys, where each key is |
| 2253 | associated with a value of a consistent type. This expectation |
| 2254 | is not checked at runtime but is only enforced by type checkers. |
| 2255 | Usage:: |
| 2256 | |
| 2257 | class Point2D(TypedDict): |
| 2258 | x: int |
| 2259 | y: int |
| 2260 | label: str |
| 2261 | |
| 2262 | a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK |
| 2263 | b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check |
| 2264 | |
| 2265 | assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first') |
| 2266 | |
Zac Hatfield-Dodds | 665ad3d | 2019-11-24 21:48:48 +1100 | [diff] [blame] | 2267 | The type info can be accessed via the Point2D.__annotations__ dict, and |
| 2268 | the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets. |
| 2269 | TypedDict supports two additional equivalent forms:: |
Ivan Levkivskyi | 135c6a5 | 2019-05-26 09:39:24 +0100 | [diff] [blame] | 2270 | |
| 2271 | Point2D = TypedDict('Point2D', x=int, y=int, label=str) |
| 2272 | Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str}) |
| 2273 | |
ananthan-123 | ab6423f | 2020-02-19 10:03:05 +0530 | [diff] [blame] | 2274 | By default, all keys must be present in a TypedDict. It is possible |
| 2275 | to override this by specifying totality. |
| 2276 | Usage:: |
| 2277 | |
| 2278 | class point2D(TypedDict, total=False): |
| 2279 | x: int |
| 2280 | y: int |
| 2281 | |
| 2282 | This means that a point2D TypedDict can have any of the keys omitted.A type |
| 2283 | checker is only expected to support a literal False or True as the value of |
| 2284 | the total argument. True is the default, and makes all items defined in the |
| 2285 | class body be required. |
| 2286 | |
Ivan Levkivskyi | 135c6a5 | 2019-05-26 09:39:24 +0100 | [diff] [blame] | 2287 | The class syntax is only supported in Python 3.6+, while two other |
| 2288 | syntax forms work for Python 2.7 and 3.2+ |
| 2289 | """ |
Serhiy Storchaka | f228bf2 | 2020-04-08 11:03:27 +0300 | [diff] [blame] | 2290 | if fields is None: |
| 2291 | fields = kwargs |
| 2292 | elif kwargs: |
| 2293 | raise TypeError("TypedDict takes either a dict or keyword arguments," |
| 2294 | " but not both") |
| 2295 | |
Alex Grönholm | 67b769f | 2020-12-10 23:49:05 +0200 | [diff] [blame] | 2296 | ns = {'__annotations__': dict(fields)} |
Serhiy Storchaka | f228bf2 | 2020-04-08 11:03:27 +0300 | [diff] [blame] | 2297 | try: |
| 2298 | # Setting correct module is necessary to make typed dict classes pickleable. |
| 2299 | ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__') |
| 2300 | except (AttributeError, ValueError): |
| 2301 | pass |
| 2302 | |
Alex Grönholm | 67b769f | 2020-12-10 23:49:05 +0200 | [diff] [blame] | 2303 | return _TypedDictMeta(typename, (), ns, total=total) |
Serhiy Storchaka | f228bf2 | 2020-04-08 11:03:27 +0300 | [diff] [blame] | 2304 | |
| 2305 | _TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {}) |
| 2306 | TypedDict.__mro_entries__ = lambda bases: (_TypedDict,) |
Ivan Levkivskyi | 135c6a5 | 2019-05-26 09:39:24 +0100 | [diff] [blame] | 2307 | |
| 2308 | |
Guido van Rossum | 91185fe | 2016-06-08 11:19:11 -0700 | [diff] [blame] | 2309 | def NewType(name, tp): |
| 2310 | """NewType creates simple unique types with almost zero |
| 2311 | runtime overhead. NewType(name, tp) is considered a subtype of tp |
| 2312 | by static type checkers. At runtime, NewType(name, tp) returns |
| 2313 | a dummy function that simply returns its argument. Usage:: |
| 2314 | |
| 2315 | UserId = NewType('UserId', int) |
| 2316 | |
| 2317 | def name_by_id(user_id: UserId) -> str: |
| 2318 | ... |
| 2319 | |
| 2320 | UserId('user') # Fails type check |
| 2321 | |
| 2322 | name_by_id(42) # Fails type check |
| 2323 | name_by_id(UserId(42)) # OK |
| 2324 | |
| 2325 | num = UserId(5) + 1 # type: int |
| 2326 | """ |
| 2327 | |
| 2328 | def new_type(x): |
| 2329 | return x |
| 2330 | |
| 2331 | new_type.__name__ = name |
| 2332 | new_type.__supertype__ = tp |
| 2333 | return new_type |
| 2334 | |
| 2335 | |
Guido van Rossum | 0e0563c | 2016-04-05 14:54:25 -0700 | [diff] [blame] | 2336 | # Python-version-specific alias (Python 2: unicode; Python 3: str) |
| 2337 | Text = str |
| 2338 | |
| 2339 | |
Guido van Rossum | 91185fe | 2016-06-08 11:19:11 -0700 | [diff] [blame] | 2340 | # Constant that's True when type checking, but False here. |
| 2341 | TYPE_CHECKING = False |
| 2342 | |
| 2343 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2344 | class IO(Generic[AnyStr]): |
| 2345 | """Generic base class for TextIO and BinaryIO. |
| 2346 | |
| 2347 | This is an abstract, generic version of the return of open(). |
| 2348 | |
| 2349 | NOTE: This does not distinguish between the different possible |
| 2350 | classes (text vs. binary, read vs. write vs. read/write, |
| 2351 | append-only, unbuffered). The TextIO and BinaryIO subclasses |
| 2352 | below capture the distinctions between text vs. binary, which is |
| 2353 | pervasive in the interface; however we currently do not offer a |
| 2354 | way to track the other distinctions in the type system. |
| 2355 | """ |
| 2356 | |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 2357 | __slots__ = () |
| 2358 | |
HongWeipeng | 6ce03ec | 2019-09-27 15:54:26 +0800 | [diff] [blame] | 2359 | @property |
| 2360 | @abstractmethod |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2361 | def mode(self) -> str: |
| 2362 | pass |
| 2363 | |
HongWeipeng | 6ce03ec | 2019-09-27 15:54:26 +0800 | [diff] [blame] | 2364 | @property |
| 2365 | @abstractmethod |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2366 | def name(self) -> str: |
| 2367 | pass |
| 2368 | |
| 2369 | @abstractmethod |
| 2370 | def close(self) -> None: |
| 2371 | pass |
| 2372 | |
Shantanu | 2e6569b | 2020-01-29 18:52:36 -0800 | [diff] [blame] | 2373 | @property |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2374 | @abstractmethod |
| 2375 | def closed(self) -> bool: |
| 2376 | pass |
| 2377 | |
| 2378 | @abstractmethod |
| 2379 | def fileno(self) -> int: |
| 2380 | pass |
| 2381 | |
| 2382 | @abstractmethod |
| 2383 | def flush(self) -> None: |
| 2384 | pass |
| 2385 | |
| 2386 | @abstractmethod |
| 2387 | def isatty(self) -> bool: |
| 2388 | pass |
| 2389 | |
| 2390 | @abstractmethod |
| 2391 | def read(self, n: int = -1) -> AnyStr: |
| 2392 | pass |
| 2393 | |
| 2394 | @abstractmethod |
| 2395 | def readable(self) -> bool: |
| 2396 | pass |
| 2397 | |
| 2398 | @abstractmethod |
| 2399 | def readline(self, limit: int = -1) -> AnyStr: |
| 2400 | pass |
| 2401 | |
| 2402 | @abstractmethod |
| 2403 | def readlines(self, hint: int = -1) -> List[AnyStr]: |
| 2404 | pass |
| 2405 | |
| 2406 | @abstractmethod |
| 2407 | def seek(self, offset: int, whence: int = 0) -> int: |
| 2408 | pass |
| 2409 | |
| 2410 | @abstractmethod |
| 2411 | def seekable(self) -> bool: |
| 2412 | pass |
| 2413 | |
| 2414 | @abstractmethod |
| 2415 | def tell(self) -> int: |
| 2416 | pass |
| 2417 | |
| 2418 | @abstractmethod |
| 2419 | def truncate(self, size: int = None) -> int: |
| 2420 | pass |
| 2421 | |
| 2422 | @abstractmethod |
| 2423 | def writable(self) -> bool: |
| 2424 | pass |
| 2425 | |
| 2426 | @abstractmethod |
| 2427 | def write(self, s: AnyStr) -> int: |
| 2428 | pass |
| 2429 | |
| 2430 | @abstractmethod |
| 2431 | def writelines(self, lines: List[AnyStr]) -> None: |
| 2432 | pass |
| 2433 | |
| 2434 | @abstractmethod |
| 2435 | def __enter__(self) -> 'IO[AnyStr]': |
| 2436 | pass |
| 2437 | |
| 2438 | @abstractmethod |
| 2439 | def __exit__(self, type, value, traceback) -> None: |
| 2440 | pass |
| 2441 | |
| 2442 | |
| 2443 | class BinaryIO(IO[bytes]): |
| 2444 | """Typed version of the return of open() in binary mode.""" |
| 2445 | |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 2446 | __slots__ = () |
| 2447 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2448 | @abstractmethod |
| 2449 | def write(self, s: Union[bytes, bytearray]) -> int: |
| 2450 | pass |
| 2451 | |
| 2452 | @abstractmethod |
| 2453 | def __enter__(self) -> 'BinaryIO': |
| 2454 | pass |
| 2455 | |
| 2456 | |
| 2457 | class TextIO(IO[str]): |
| 2458 | """Typed version of the return of open() in text mode.""" |
| 2459 | |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 2460 | __slots__ = () |
| 2461 | |
HongWeipeng | 6ce03ec | 2019-09-27 15:54:26 +0800 | [diff] [blame] | 2462 | @property |
| 2463 | @abstractmethod |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2464 | def buffer(self) -> BinaryIO: |
| 2465 | pass |
| 2466 | |
HongWeipeng | 6ce03ec | 2019-09-27 15:54:26 +0800 | [diff] [blame] | 2467 | @property |
| 2468 | @abstractmethod |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2469 | def encoding(self) -> str: |
| 2470 | pass |
| 2471 | |
HongWeipeng | 6ce03ec | 2019-09-27 15:54:26 +0800 | [diff] [blame] | 2472 | @property |
| 2473 | @abstractmethod |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 2474 | def errors(self) -> Optional[str]: |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2475 | pass |
| 2476 | |
HongWeipeng | 6ce03ec | 2019-09-27 15:54:26 +0800 | [diff] [blame] | 2477 | @property |
| 2478 | @abstractmethod |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2479 | def line_buffering(self) -> bool: |
| 2480 | pass |
| 2481 | |
HongWeipeng | 6ce03ec | 2019-09-27 15:54:26 +0800 | [diff] [blame] | 2482 | @property |
| 2483 | @abstractmethod |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2484 | def newlines(self) -> Any: |
| 2485 | pass |
| 2486 | |
| 2487 | @abstractmethod |
| 2488 | def __enter__(self) -> 'TextIO': |
| 2489 | pass |
| 2490 | |
| 2491 | |
| 2492 | class io: |
| 2493 | """Wrapper namespace for IO generic classes.""" |
| 2494 | |
| 2495 | __all__ = ['IO', 'TextIO', 'BinaryIO'] |
| 2496 | IO = IO |
| 2497 | TextIO = TextIO |
| 2498 | BinaryIO = BinaryIO |
| 2499 | |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 2500 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2501 | io.__name__ = __name__ + '.io' |
| 2502 | sys.modules[io.__name__] = io |
| 2503 | |
Serhiy Storchaka | fcb2856 | 2020-05-10 11:53:16 +0300 | [diff] [blame] | 2504 | Pattern = _alias(stdlib_re.Pattern, 1) |
| 2505 | Match = _alias(stdlib_re.Match, 1) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2506 | |
| 2507 | class re: |
| 2508 | """Wrapper namespace for re type aliases.""" |
| 2509 | |
| 2510 | __all__ = ['Pattern', 'Match'] |
| 2511 | Pattern = Pattern |
| 2512 | Match = Match |
| 2513 | |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 2514 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2515 | re.__name__ = __name__ + '.re' |
| 2516 | sys.modules[re.__name__] = re |