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