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