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