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