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