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