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