blob: a24c01f0e3b9e364f259d8d9ee72099ba10e0992 [file] [log] [blame]
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001"""
2The typing module: Support for gradual typing as defined by PEP 484.
3
4At large scale, the structure of the module is following:
Tim McNamara5265b3a2018-09-01 20:56:58 +12005* Imports and exports, all public names should be explicitly added to __all__.
Ivan Levkivskyid911e402018-01-20 11:23:59 +00006* Internal helper functions: these should never be used in code outside this module.
kj73607be2020-12-24 12:33:48 +08007* _SpecialForm and its instances (special forms):
8 Any, NoReturn, ClassVar, Union, Optional, Concatenate
9* Classes whose instances can be type arguments in addition to types:
10 ForwardRef, TypeVar and ParamSpec
Ivan Levkivskyid911e402018-01-20 11:23:59 +000011* The core of internal generics API: _GenericAlias and _VariadicGenericAlias, the latter is
12 currently only used by Tuple and Callable. All subscripted types like X[int], Union[int, str],
13 etc., are instances of either of these classes.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +010014* The public counterpart of the generics API consists of two classes: Generic and Protocol.
Ivan Levkivskyid911e402018-01-20 11:23:59 +000015* Public helper functions: get_type_hints, overload, cast, no_type_check,
16 no_type_check_decorator.
17* Generic aliases for collections.abc ABCs and few additional protocols.
ananthan-123ab6423f2020-02-19 10:03:05 +053018* Special types: NewType, NamedTuple, TypedDict.
Ivan Levkivskyid911e402018-01-20 11:23:59 +000019* Wrapper submodules for re and io related types.
20"""
21
HongWeipeng6ce03ec2019-09-27 15:54:26 +080022from abc import abstractmethod, ABCMeta
Batuhan Taskaya044a1042020-10-06 23:03:02 +030023import ast
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070024import collections
Ivan Levkivskyid911e402018-01-20 11:23:59 +000025import collections.abc
Brett Cannonf3ad0422016-04-15 10:51:30 -070026import contextlib
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070027import functools
Serhiy Storchaka09f32212018-05-26 21:19:26 +030028import operator
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070029import re as stdlib_re # Avoid confusion with the re we export.
30import sys
31import types
Guido van Rossum48b069a2020-04-07 09:50:06 -070032from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType, GenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070033
34# Please keep __all__ alphabetized within each category.
35__all__ = [
36 # Super-special typing primitives.
Jakub Stasiakcf5b1092020-02-05 02:10:19 +010037 'Annotated',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070038 'Any',
39 'Callable',
Guido van Rossum0a6976d2016-09-11 15:34:56 -070040 'ClassVar',
kj73607be2020-12-24 12:33:48 +080041 'Concatenate',
Ivan Levkivskyif3672422019-05-26 09:37:07 +010042 'Final',
Anthony Sottiled30da5d2019-05-29 11:19:38 -070043 'ForwardRef',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070044 'Generic',
Ivan Levkivskyib891c462019-05-26 09:37:48 +010045 'Literal',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070046 'Optional',
kj73607be2020-12-24 12:33:48 +080047 'ParamSpec',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +010048 'Protocol',
Guido van Rossumeb9aca32016-05-24 16:38:22 -070049 'Tuple',
50 'Type',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070051 'TypeVar',
52 'Union',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070053
54 # ABCs (from collections.abc).
55 'AbstractSet', # collections.abc.Set.
56 'ByteString',
57 'Container',
Ivan Levkivskyi29fda8d2017-06-10 21:57:56 +020058 'ContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070059 'Hashable',
60 'ItemsView',
61 'Iterable',
62 'Iterator',
63 'KeysView',
64 'Mapping',
65 'MappingView',
66 'MutableMapping',
67 'MutableSequence',
68 'MutableSet',
69 'Sequence',
70 'Sized',
71 'ValuesView',
Ivan Levkivskyid911e402018-01-20 11:23:59 +000072 'Awaitable',
73 'AsyncIterator',
74 'AsyncIterable',
75 'Coroutine',
76 'Collection',
77 'AsyncGenerator',
78 'AsyncContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070079
80 # Structural checks, a.k.a. protocols.
81 'Reversible',
82 'SupportsAbs',
Ivan Levkivskyif06e0212017-05-02 19:14:07 +020083 'SupportsBytes',
84 'SupportsComplex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070085 'SupportsFloat',
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -070086 'SupportsIndex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070087 'SupportsInt',
88 'SupportsRound',
89
90 # Concrete collection types.
Anthony Sottiled30da5d2019-05-29 11:19:38 -070091 'ChainMap',
Ivan Levkivskyib692dc82017-02-13 22:50:14 +010092 'Counter',
Raymond Hettinger80490522017-01-16 22:42:37 -080093 'Deque',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070094 'Dict',
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070095 'DefaultDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070096 'List',
Anthony Sottiled30da5d2019-05-29 11:19:38 -070097 'OrderedDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070098 'Set',
Guido van Rossumefa798d2016-08-23 11:01:50 -070099 'FrozenSet',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700100 'NamedTuple', # Not really a type.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +0100101 'TypedDict', # Not really a type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700102 'Generator',
103
104 # One-off things.
105 'AnyStr',
106 'cast',
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100107 'final',
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +0100108 'get_args',
109 'get_origin',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700110 'get_type_hints',
Patrick Reader0705ec82020-09-16 05:58:32 +0100111 'is_typeddict',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700112 'NewType',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700113 'no_type_check',
114 'no_type_check_decorator',
aetracht45738202018-03-19 14:41:32 -0400115 'NoReturn',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700116 'overload',
Jelle Zijlstra52243362021-04-10 19:57:05 -0700117 'ParamSpecArgs',
118 'ParamSpecKwargs',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100119 'runtime_checkable',
Guido van Rossum0e0563c2016-04-05 14:54:25 -0700120 'Text',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700121 'TYPE_CHECKING',
Mikhail Golubev4f3c2502020-10-08 00:44:31 +0300122 'TypeAlias',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700123]
124
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700125# The pseudo-submodules 're' and 'io' are part of the public
126# namespace, but excluded from __all__ because they might stomp on
127# legitimate imports of those modules.
128
kj463c7d32020-12-14 02:38:24 +0800129
130def _type_convert(arg):
131 """For converting None to type(None), and strings to ForwardRef."""
132 if arg is None:
133 return type(None)
134 if isinstance(arg, str):
135 return ForwardRef(arg)
136 return arg
137
138
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700139def _type_check(arg, msg, is_argument=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000140 """Check that the argument is a type, and return it (internal helper).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700141
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000142 As a special case, accept None and return type(None) instead. Also wrap strings
143 into ForwardRef instances. Consider several corner cases, for example plain
144 special forms like Union are not valid, while Union[int, str] is OK, etc.
145 The msg argument is a human-readable error message, e.g::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700146
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000147 "Union[arg, ...]: arg should be a type."
Guido van Rossum4cefe742016-09-27 15:20:12 -0700148
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000149 We append the repr() of the actual value (truncated to 100 chars).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700150 """
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100151 invalid_generic_forms = (Generic, Protocol)
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700152 if is_argument:
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100153 invalid_generic_forms = invalid_generic_forms + (ClassVar, Final)
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400154
kj463c7d32020-12-14 02:38:24 +0800155 arg = _type_convert(arg)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000156 if (isinstance(arg, _GenericAlias) and
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400157 arg.__origin__ in invalid_generic_forms):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000158 raise TypeError(f"{arg} is not valid as type argument")
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300159 if arg in (Any, NoReturn):
160 return arg
161 if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000162 raise TypeError(f"Plain {arg} is not valid as type argument")
kj73607be2020-12-24 12:33:48 +0800163 if isinstance(arg, (type, TypeVar, ForwardRef, types.Union, ParamSpec)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000164 return arg
165 if not callable(arg):
166 raise TypeError(f"{msg} Got {arg!r:.100}.")
167 return arg
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700168
169
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000170def _type_repr(obj):
171 """Return the repr() of an object, special-casing types (internal helper).
172
173 If obj is a type, we return a shorter version than the default
174 type.__repr__, based on the module and qualified name, which is
175 typically enough to uniquely identify a type. For everything
176 else, we fall back on repr(obj).
177 """
kj1f7dfb22020-11-02 02:13:38 +0800178 if isinstance(obj, types.GenericAlias):
179 return repr(obj)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000180 if isinstance(obj, type):
181 if obj.__module__ == 'builtins':
182 return obj.__qualname__
183 return f'{obj.__module__}.{obj.__qualname__}'
184 if obj is ...:
185 return('...')
186 if isinstance(obj, types.FunctionType):
187 return obj.__name__
188 return repr(obj)
189
190
191def _collect_type_vars(types):
kj73607be2020-12-24 12:33:48 +0800192 """Collect all type variable-like variables contained
193 in types in order of first appearance (lexicographic order). For example::
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000194
195 _collect_type_vars((T, List[S, T])) == (T, S)
196 """
197 tvars = []
198 for t in types:
kj73607be2020-12-24 12:33:48 +0800199 if isinstance(t, _TypeVarLike) and t not in tvars:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000200 tvars.append(t)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300201 if isinstance(t, (_GenericAlias, GenericAlias)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000202 tvars.extend([t for t in t.__parameters__ if t not in tvars])
203 return tuple(tvars)
204
205
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300206def _check_generic(cls, parameters, elen):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000207 """Check correct count for parameters of a generic cls (internal helper).
208 This gives a nice error message in case of count mismatch.
209 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300210 if not elen:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000211 raise TypeError(f"{cls} is not a generic class")
212 alen = len(parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000213 if alen != elen:
214 raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
215 f" actual {alen}, expected {elen}")
216
kj73607be2020-12-24 12:33:48 +0800217def _prepare_paramspec_params(cls, params):
218 """Prepares the parameters for a Generic containing ParamSpec
219 variables (internal helper).
220 """
221 # Special case where Z[[int, str, bool]] == Z[int, str, bool] in PEP 612.
222 if len(cls.__parameters__) == 1 and len(params) > 1:
223 return (params,)
224 else:
225 _params = []
226 # Convert lists to tuples to help other libraries cache the results.
227 for p, tvar in zip(params, cls.__parameters__):
228 if isinstance(tvar, ParamSpec) and isinstance(p, list):
229 p = tuple(p)
230 _params.append(p)
231 return tuple(_params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000232
Yurii Karabasf03d3182020-11-17 04:23:19 +0200233def _deduplicate(params):
234 # Weed out strict duplicates, preserving the first of each occurrence.
235 all_params = set(params)
236 if len(all_params) < len(params):
237 new_params = []
238 for t in params:
239 if t in all_params:
240 new_params.append(t)
241 all_params.remove(t)
242 params = new_params
243 assert not all_params, all_params
244 return params
245
246
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000247def _remove_dups_flatten(parameters):
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700248 """An internal helper for Union creation and substitution: flatten Unions
249 among parameters, then remove duplicates.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000250 """
251 # Flatten out Union[Union[...], ...].
252 params = []
253 for p in parameters:
Maggie Moss1b4552c2020-09-09 13:23:24 -0700254 if isinstance(p, (_UnionGenericAlias, types.Union)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000255 params.extend(p.__args__)
256 elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
257 params.extend(p[1:])
258 else:
259 params.append(p)
Yurii Karabasf03d3182020-11-17 04:23:19 +0200260
261 return tuple(_deduplicate(params))
262
263
264def _flatten_literal_params(parameters):
265 """An internal helper for Literal creation: flatten Literals among parameters"""
266 params = []
267 for p in parameters:
268 if isinstance(p, _LiteralGenericAlias):
269 params.extend(p.__args__)
270 else:
271 params.append(p)
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700272 return tuple(params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000273
274
275_cleanups = []
276
277
Yurii Karabasf03d3182020-11-17 04:23:19 +0200278def _tp_cache(func=None, /, *, typed=False):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000279 """Internal wrapper caching __getitem__ of generic types with a fallback to
280 original function for non-hashable arguments.
281 """
Yurii Karabasf03d3182020-11-17 04:23:19 +0200282 def decorator(func):
283 cached = functools.lru_cache(typed=typed)(func)
284 _cleanups.append(cached.cache_clear)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000285
Yurii Karabasf03d3182020-11-17 04:23:19 +0200286 @functools.wraps(func)
287 def inner(*args, **kwds):
288 try:
289 return cached(*args, **kwds)
290 except TypeError:
291 pass # All real errors (not unhashable args) are raised below.
292 return func(*args, **kwds)
293 return inner
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000294
Yurii Karabasf03d3182020-11-17 04:23:19 +0200295 if func is not None:
296 return decorator(func)
297
298 return decorator
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000299
wyfo653f4202020-07-22 21:47:28 +0200300def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
Graham Bleaney84ef33c2020-09-08 18:41:10 -0400301 """Evaluate all forward references in the given type t.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000302 For use of globalns and localns see the docstring for get_type_hints().
wyfo653f4202020-07-22 21:47:28 +0200303 recursive_guard is used to prevent prevent infinite recursion
304 with recursive ForwardRef.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000305 """
306 if isinstance(t, ForwardRef):
wyfo653f4202020-07-22 21:47:28 +0200307 return t._evaluate(globalns, localns, recursive_guard)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300308 if isinstance(t, (_GenericAlias, GenericAlias)):
wyfo653f4202020-07-22 21:47:28 +0200309 ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000310 if ev_args == t.__args__:
311 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300312 if isinstance(t, GenericAlias):
313 return GenericAlias(t.__origin__, ev_args)
314 else:
315 return t.copy_with(ev_args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000316 return t
317
318
319class _Final:
320 """Mixin to prohibit subclassing"""
Guido van Rossum4cefe742016-09-27 15:20:12 -0700321
Guido van Rossum83ec3022017-01-17 20:43:28 -0800322 __slots__ = ('__weakref__',)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700323
Serhiy Storchaka2085bd02019-06-01 11:00:15 +0300324 def __init_subclass__(self, /, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000325 if '_root' not in kwds:
326 raise TypeError("Cannot subclass special typing classes")
327
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100328class _Immutable:
329 """Mixin to indicate that object should not be copied."""
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300330 __slots__ = ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000331
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100332 def __copy__(self):
333 return self
334
335 def __deepcopy__(self, memo):
336 return self
337
338
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300339# Internal indicator of special typing constructs.
340# See __doc__ instance attribute for specific docs.
341class _SpecialForm(_Final, _root=True):
342 __slots__ = ('_name', '__doc__', '_getitem')
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000343
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300344 def __init__(self, getitem):
345 self._getitem = getitem
346 self._name = getitem.__name__
347 self.__doc__ = getitem.__doc__
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000348
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300349 def __mro_entries__(self, bases):
350 raise TypeError(f"Cannot subclass {self!r}")
Guido van Rossum4cefe742016-09-27 15:20:12 -0700351
352 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000353 return 'typing.' + self._name
354
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100355 def __reduce__(self):
356 return self._name
Guido van Rossum4cefe742016-09-27 15:20:12 -0700357
358 def __call__(self, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000359 raise TypeError(f"Cannot instantiate {self!r}")
360
361 def __instancecheck__(self, obj):
362 raise TypeError(f"{self} cannot be used with isinstance()")
363
364 def __subclasscheck__(self, cls):
365 raise TypeError(f"{self} cannot be used with issubclass()")
366
367 @_tp_cache
368 def __getitem__(self, parameters):
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300369 return self._getitem(self, parameters)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700370
Yurii Karabasf03d3182020-11-17 04:23:19 +0200371
372class _LiteralSpecialForm(_SpecialForm, _root=True):
373 @_tp_cache(typed=True)
374 def __getitem__(self, parameters):
375 return self._getitem(self, parameters)
376
377
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300378@_SpecialForm
379def Any(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000380 """Special type indicating an unconstrained type.
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700381
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000382 - Any is compatible with every type.
383 - Any assumed to have all methods.
384 - All values assumed to be instances of Any.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700385
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000386 Note that all the above statements are true from the point of view of
387 static type checkers. At runtime, Any should not be used with instance
388 or class checks.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300389 """
390 raise TypeError(f"{self} is not subscriptable")
Guido van Rossumd70fe632015-08-05 12:11:06 +0200391
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300392@_SpecialForm
393def NoReturn(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000394 """Special type indicating functions that never return.
395 Example::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700396
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000397 from typing import NoReturn
398
399 def stop() -> NoReturn:
400 raise Exception('no way')
401
402 This type is invalid in other positions, e.g., ``List[NoReturn]``
403 will fail in static type checkers.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300404 """
405 raise TypeError(f"{self} is not subscriptable")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000406
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300407@_SpecialForm
408def ClassVar(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000409 """Special type construct to mark class variables.
410
411 An annotation wrapped in ClassVar indicates that a given
412 attribute is intended to be used as a class variable and
413 should not be set on instances of that class. Usage::
414
415 class Starship:
416 stats: ClassVar[Dict[str, int]] = {} # class variable
417 damage: int = 10 # instance variable
418
419 ClassVar accepts only types and cannot be further subscribed.
420
421 Note that ClassVar is not a class itself, and should not
422 be used with isinstance() or issubclass().
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300423 """
424 item = _type_check(parameters, f'{self} accepts only single type.')
425 return _GenericAlias(self, (item,))
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000426
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300427@_SpecialForm
428def Final(self, parameters):
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100429 """Special typing construct to indicate final names to type checkers.
430
431 A final name cannot be re-assigned or overridden in a subclass.
432 For example:
433
434 MAX_SIZE: Final = 9000
435 MAX_SIZE += 1 # Error reported by type checker
436
437 class Connection:
438 TIMEOUT: Final[int] = 10
439
440 class FastConnector(Connection):
441 TIMEOUT = 1 # Error reported by type checker
442
443 There is no runtime checking of these properties.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300444 """
445 item = _type_check(parameters, f'{self} accepts only single type.')
446 return _GenericAlias(self, (item,))
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100447
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300448@_SpecialForm
449def Union(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000450 """Union type; Union[X, Y] means either X or Y.
451
452 To define a union, use e.g. Union[int, str]. Details:
453 - The arguments must be types and there must be at least one.
454 - None as an argument is a special case and is replaced by
455 type(None).
456 - Unions of unions are flattened, e.g.::
457
458 Union[Union[int, str], float] == Union[int, str, float]
459
460 - Unions of a single argument vanish, e.g.::
461
462 Union[int] == int # The constructor actually returns int
463
464 - Redundant arguments are skipped, e.g.::
465
466 Union[int, str, int] == Union[int, str]
467
468 - When comparing unions, the argument order is ignored, e.g.::
469
470 Union[int, str] == Union[str, int]
471
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000472 - You cannot subclass or instantiate a union.
473 - You can use Optional[X] as a shorthand for Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300474 """
475 if parameters == ():
476 raise TypeError("Cannot take a Union of no types.")
477 if not isinstance(parameters, tuple):
478 parameters = (parameters,)
479 msg = "Union[arg, ...]: each arg must be a type."
480 parameters = tuple(_type_check(p, msg) for p in parameters)
481 parameters = _remove_dups_flatten(parameters)
482 if len(parameters) == 1:
483 return parameters[0]
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300484 return _UnionGenericAlias(self, parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000485
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300486@_SpecialForm
487def Optional(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000488 """Optional type.
489
490 Optional[X] is equivalent to Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300491 """
492 arg = _type_check(parameters, f"{self} requires a single type.")
493 return Union[arg, type(None)]
Guido van Rossumb7dedc82016-10-29 12:44:29 -0700494
Yurii Karabasf03d3182020-11-17 04:23:19 +0200495@_LiteralSpecialForm
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300496def Literal(self, parameters):
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100497 """Special typing form to define literal types (a.k.a. value types).
498
499 This form can be used to indicate to type checkers that the corresponding
500 variable or function parameter has a value equivalent to the provided
501 literal (or one of several literals):
502
503 def validate_simple(data: Any) -> Literal[True]: # always returns True
504 ...
505
506 MODE = Literal['r', 'rb', 'w', 'wb']
507 def open_helper(file: str, mode: MODE) -> str:
508 ...
509
510 open_helper('/some/path', 'r') # Passes type check
511 open_helper('/other/path', 'typo') # Error in type checker
512
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300513 Literal[...] cannot be subclassed. At runtime, an arbitrary value
514 is allowed as type argument to Literal[...], but type checkers may
515 impose restrictions.
516 """
517 # There is no '_type_check' call because arguments to Literal[...] are
518 # values, not types.
Yurii Karabasf03d3182020-11-17 04:23:19 +0200519 if not isinstance(parameters, tuple):
520 parameters = (parameters,)
521
522 parameters = _flatten_literal_params(parameters)
523
524 try:
525 parameters = tuple(p for p, _ in _deduplicate(list(_value_and_type_iter(parameters))))
526 except TypeError: # unhashable parameters
527 pass
528
529 return _LiteralGenericAlias(self, parameters)
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100530
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700531
Mikhail Golubev4f3c2502020-10-08 00:44:31 +0300532@_SpecialForm
533def TypeAlias(self, parameters):
534 """Special marker indicating that an assignment should
535 be recognized as a proper type alias definition by type
536 checkers.
537
538 For example::
539
540 Predicate: TypeAlias = Callable[..., bool]
541
542 It's invalid when used anywhere except as in the example above.
543 """
544 raise TypeError(f"{self} is not subscriptable")
545
546
kj73607be2020-12-24 12:33:48 +0800547@_SpecialForm
548def Concatenate(self, parameters):
Ken Jin11276cd2021-01-02 08:45:50 +0800549 """Used in conjunction with ``ParamSpec`` and ``Callable`` to represent a
550 higher order function which adds, removes or transforms parameters of a
551 callable.
kj73607be2020-12-24 12:33:48 +0800552
553 For example::
554
555 Callable[Concatenate[int, P], int]
556
557 See PEP 612 for detailed information.
558 """
559 if parameters == ():
560 raise TypeError("Cannot take a Concatenate of no types.")
561 if not isinstance(parameters, tuple):
562 parameters = (parameters,)
563 if not isinstance(parameters[-1], ParamSpec):
564 raise TypeError("The last parameter to Concatenate should be a "
565 "ParamSpec variable.")
566 msg = "Concatenate[arg, ...]: each arg must be a type."
567 parameters = tuple(_type_check(p, msg) for p in parameters)
568 return _ConcatenateGenericAlias(self, parameters)
569
570
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000571class ForwardRef(_Final, _root=True):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800572 """Internal wrapper to hold a forward reference."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700573
Guido van Rossum4cefe742016-09-27 15:20:12 -0700574 __slots__ = ('__forward_arg__', '__forward_code__',
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400575 '__forward_evaluated__', '__forward_value__',
576 '__forward_is_argument__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700577
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700578 def __init__(self, arg, is_argument=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700579 if not isinstance(arg, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000580 raise TypeError(f"Forward reference must be a string -- got {arg!r}")
Batuhan Taskaya044a1042020-10-06 23:03:02 +0300581
582 # Double-stringified forward references is a result of activating
583 # the 'annotations' future by default. This way, we eliminate them in
584 # the runtime.
585 if arg.startswith(("'", '\"')) and arg.endswith(("'", '"')):
586 arg = arg[1:-1]
587
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700588 try:
589 code = compile(arg, '<string>', 'eval')
590 except SyntaxError:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000591 raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700592 self.__forward_arg__ = arg
593 self.__forward_code__ = code
594 self.__forward_evaluated__ = False
595 self.__forward_value__ = None
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400596 self.__forward_is_argument__ = is_argument
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700597
wyfo653f4202020-07-22 21:47:28 +0200598 def _evaluate(self, globalns, localns, recursive_guard):
599 if self.__forward_arg__ in recursive_guard:
600 return self
Guido van Rossumdad17902016-11-10 08:29:18 -0800601 if not self.__forward_evaluated__ or localns is not globalns:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700602 if globalns is None and localns is None:
603 globalns = localns = {}
604 elif globalns is None:
605 globalns = localns
606 elif localns is None:
607 localns = globalns
wyfo653f4202020-07-22 21:47:28 +0200608 type_ =_type_check(
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700609 eval(self.__forward_code__, globalns, localns),
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400610 "Forward references must evaluate to types.",
wyfo653f4202020-07-22 21:47:28 +0200611 is_argument=self.__forward_is_argument__,
612 )
613 self.__forward_value__ = _eval_type(
614 type_, globalns, localns, recursive_guard | {self.__forward_arg__}
615 )
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700616 self.__forward_evaluated__ = True
617 return self.__forward_value__
618
Guido van Rossum4cefe742016-09-27 15:20:12 -0700619 def __eq__(self, other):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000620 if not isinstance(other, ForwardRef):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700621 return NotImplemented
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100622 if self.__forward_evaluated__ and other.__forward_evaluated__:
623 return (self.__forward_arg__ == other.__forward_arg__ and
624 self.__forward_value__ == other.__forward_value__)
625 return self.__forward_arg__ == other.__forward_arg__
Guido van Rossum4cefe742016-09-27 15:20:12 -0700626
627 def __hash__(self):
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100628 return hash(self.__forward_arg__)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700629
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700630 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000631 return f'ForwardRef({self.__forward_arg__!r})'
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700632
kj73607be2020-12-24 12:33:48 +0800633class _TypeVarLike:
634 """Mixin for TypeVar-like types (TypeVar and ParamSpec)."""
635 def __init__(self, bound, covariant, contravariant):
636 """Used to setup TypeVars and ParamSpec's bound, covariant and
637 contravariant attributes.
638 """
639 if covariant and contravariant:
640 raise ValueError("Bivariant types are not supported.")
641 self.__covariant__ = bool(covariant)
642 self.__contravariant__ = bool(contravariant)
643 if bound:
644 self.__bound__ = _type_check(bound, "Bound must be a type.")
645 else:
646 self.__bound__ = None
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700647
kj73607be2020-12-24 12:33:48 +0800648 def __or__(self, right):
649 return Union[self, right]
650
Jelle Zijlstra90459192021-04-10 20:00:05 -0700651 def __ror__(self, left):
652 return Union[left, self]
kj73607be2020-12-24 12:33:48 +0800653
654 def __repr__(self):
655 if self.__covariant__:
656 prefix = '+'
657 elif self.__contravariant__:
658 prefix = '-'
659 else:
660 prefix = '~'
661 return prefix + self.__name__
662
663 def __reduce__(self):
664 return self.__name__
665
666
667class TypeVar( _Final, _Immutable, _TypeVarLike, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700668 """Type variable.
669
670 Usage::
671
672 T = TypeVar('T') # Can be anything
673 A = TypeVar('A', str, bytes) # Must be str or bytes
674
675 Type variables exist primarily for the benefit of static type
676 checkers. They serve as the parameters for generic types as well
677 as for generic function definitions. See class Generic for more
678 information on generic types. Generic functions work as follows:
679
Guido van Rossumb24569a2016-11-20 18:01:29 -0800680 def repeat(x: T, n: int) -> List[T]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700681 '''Return a list containing n references to x.'''
682 return [x]*n
683
684 def longest(x: A, y: A) -> A:
685 '''Return the longest of two strings.'''
686 return x if len(x) >= len(y) else y
687
688 The latter example's signature is essentially the overloading
689 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
690 that if the arguments are instances of some subclass of str,
691 the return type is still plain str.
692
Guido van Rossumb24569a2016-11-20 18:01:29 -0800693 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700694
Guido van Rossumefa798d2016-08-23 11:01:50 -0700695 Type variables defined with covariant=True or contravariant=True
João D. Ferreira86bfed32018-07-07 16:41:20 +0100696 can be used to declare covariant or contravariant generic types.
Guido van Rossumefa798d2016-08-23 11:01:50 -0700697 See PEP 484 for more details. By default generic types are invariant
698 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700699
700 Type variables can be introspected. e.g.:
701
702 T.__name__ == 'T'
703 T.__constraints__ == ()
704 T.__covariant__ == False
705 T.__contravariant__ = False
706 A.__constraints__ == (str, bytes)
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100707
708 Note that only type variables defined in global scope can be pickled.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700709 """
710
Guido van Rossum4cefe742016-09-27 15:20:12 -0700711 __slots__ = ('__name__', '__bound__', '__constraints__',
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300712 '__covariant__', '__contravariant__', '__dict__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700713
714 def __init__(self, name, *constraints, bound=None,
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800715 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700716 self.__name__ = name
kj73607be2020-12-24 12:33:48 +0800717 super().__init__(bound, covariant, contravariant)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700718 if constraints and bound is not None:
719 raise TypeError("Constraints cannot be combined with bound=...")
720 if constraints and len(constraints) == 1:
721 raise TypeError("A single constraint is not allowed")
722 msg = "TypeVar(name, constraint, ...): constraints must be types."
723 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
HongWeipenga25a04f2020-04-21 04:01:53 +0800724 try:
725 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling
726 except (AttributeError, ValueError):
727 def_mod = None
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300728 if def_mod != 'typing':
729 self.__module__ = def_mod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700730
Maggie Moss1b4552c2020-09-09 13:23:24 -0700731
Jelle Zijlstra52243362021-04-10 19:57:05 -0700732class ParamSpecArgs(_Final, _Immutable, _root=True):
733 """The args for a ParamSpec object.
734
735 Given a ParamSpec object P, P.args is an instance of ParamSpecArgs.
736
737 ParamSpecArgs objects have a reference back to their ParamSpec:
738
739 P.args.__origin__ is P
740
741 This type is meant for runtime introspection and has no special meaning to
742 static type checkers.
743 """
744 def __init__(self, origin):
745 self.__origin__ = origin
746
747 def __repr__(self):
748 return f"{self.__origin__.__name__}.args"
749
750
751class ParamSpecKwargs(_Final, _Immutable, _root=True):
752 """The kwargs for a ParamSpec object.
753
754 Given a ParamSpec object P, P.kwargs is an instance of ParamSpecKwargs.
755
756 ParamSpecKwargs objects have a reference back to their ParamSpec:
757
758 P.kwargs.__origin__ is P
759
760 This type is meant for runtime introspection and has no special meaning to
761 static type checkers.
762 """
763 def __init__(self, origin):
764 self.__origin__ = origin
765
766 def __repr__(self):
767 return f"{self.__origin__.__name__}.kwargs"
768
769
kj73607be2020-12-24 12:33:48 +0800770class ParamSpec(_Final, _Immutable, _TypeVarLike, _root=True):
771 """Parameter specification variable.
Maggie Moss1b4552c2020-09-09 13:23:24 -0700772
kj73607be2020-12-24 12:33:48 +0800773 Usage::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700774
kj73607be2020-12-24 12:33:48 +0800775 P = ParamSpec('P')
776
777 Parameter specification variables exist primarily for the benefit of static
778 type checkers. They are used to forward the parameter types of one
Ken Jin11276cd2021-01-02 08:45:50 +0800779 callable to another callable, a pattern commonly found in higher order
780 functions and decorators. They are only valid when used in ``Concatenate``,
781 or s the first argument to ``Callable``, or as parameters for user-defined
782 Generics. See class Generic for more information on generic types. An
783 example for annotating a decorator::
kj73607be2020-12-24 12:33:48 +0800784
785 T = TypeVar('T')
786 P = ParamSpec('P')
787
788 def add_logging(f: Callable[P, T]) -> Callable[P, T]:
789 '''A type-safe decorator to add logging to a function.'''
790 def inner(*args: P.args, **kwargs: P.kwargs) -> T:
791 logging.info(f'{f.__name__} was called')
792 return f(*args, **kwargs)
793 return inner
794
795 @add_logging
796 def add_two(x: float, y: float) -> float:
797 '''Add two numbers together.'''
798 return x + y
799
800 Parameter specification variables defined with covariant=True or
801 contravariant=True can be used to declare covariant or contravariant
802 generic types. These keyword arguments are valid, but their actual semantics
803 are yet to be decided. See PEP 612 for details.
804
805 Parameter specification variables can be introspected. e.g.:
806
807 P.__name__ == 'T'
808 P.__bound__ == None
809 P.__covariant__ == False
810 P.__contravariant__ == False
811
812 Note that only parameter specification variables defined in global scope can
813 be pickled.
814 """
815
816 __slots__ = ('__name__', '__bound__', '__covariant__', '__contravariant__',
817 '__dict__')
818
Jelle Zijlstra52243362021-04-10 19:57:05 -0700819 @property
820 def args(self):
821 return ParamSpecArgs(self)
822
823 @property
824 def kwargs(self):
825 return ParamSpecKwargs(self)
kj73607be2020-12-24 12:33:48 +0800826
Ken Jinace008c2021-01-11 08:11:41 +0800827 def __init__(self, name, *, bound=None, covariant=False, contravariant=False):
kj73607be2020-12-24 12:33:48 +0800828 self.__name__ = name
829 super().__init__(bound, covariant, contravariant)
830 try:
831 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__')
832 except (AttributeError, ValueError):
833 def_mod = None
834 if def_mod != 'typing':
835 self.__module__ = def_mod
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100836
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700837
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000838def _is_dunder(attr):
839 return attr.startswith('__') and attr.endswith('__')
Guido van Rossum83ec3022017-01-17 20:43:28 -0800840
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300841class _BaseGenericAlias(_Final, _root=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000842 """The central part of internal API.
843
844 This represents a generic version of type 'origin' with type arguments 'params'.
845 There are two kind of these aliases: user defined and special. The special ones
846 are wrappers around builtin collections and ABCs in collections.abc. These must
847 have 'name' always set. If 'inst' is False, then the alias can't be instantiated,
848 this is used by e.g. typing.List and typing.Dict.
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700849 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300850 def __init__(self, origin, *, inst=True, name=None):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000851 self._inst = inst
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000852 self._name = name
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700853 self.__origin__ = origin
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000854 self.__slots__ = None # This is not documented.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300855
856 def __call__(self, *args, **kwargs):
857 if not self._inst:
858 raise TypeError(f"Type {self._name} cannot be instantiated; "
859 f"use {self.__origin__.__name__}() instead")
860 result = self.__origin__(*args, **kwargs)
861 try:
862 result.__orig_class__ = self
863 except AttributeError:
864 pass
865 return result
866
867 def __mro_entries__(self, bases):
868 res = []
869 if self.__origin__ not in bases:
870 res.append(self.__origin__)
871 i = bases.index(self)
872 for b in bases[i+1:]:
873 if isinstance(b, _BaseGenericAlias) or issubclass(b, Generic):
874 break
875 else:
876 res.append(Generic)
877 return tuple(res)
878
879 def __getattr__(self, attr):
880 # We are careful for copy and pickle.
881 # Also for simplicity we just don't relay all dunder names
882 if '__origin__' in self.__dict__ and not _is_dunder(attr):
883 return getattr(self.__origin__, attr)
884 raise AttributeError(attr)
885
886 def __setattr__(self, attr, val):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300887 if _is_dunder(attr) or attr in ('_name', '_inst', '_nparams'):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300888 super().__setattr__(attr, val)
889 else:
890 setattr(self.__origin__, attr, val)
891
892 def __instancecheck__(self, obj):
893 return self.__subclasscheck__(type(obj))
894
895 def __subclasscheck__(self, cls):
896 raise TypeError("Subscripted generics cannot be used with"
897 " class and instance checks")
898
899
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300900# Special typing constructs Union, Optional, Generic, Callable and Tuple
901# use three special attributes for internal bookkeeping of generic types:
902# * __parameters__ is a tuple of unique free type parameters of a generic
903# type, for example, Dict[T, T].__parameters__ == (T,);
904# * __origin__ keeps a reference to a type that was subscripted,
905# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
906# the type.
907# * __args__ is a tuple of all arguments used in subscripting,
908# e.g., Dict[T, int].__args__ == (T, int).
909
910
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300911class _GenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300912 def __init__(self, origin, params, *, inst=True, name=None):
913 super().__init__(origin, inst=inst, name=name)
914 if not isinstance(params, tuple):
915 params = (params,)
916 self.__args__ = tuple(... if a is _TypingEllipsis else
917 () if a is _TypingEmpty else
918 a for a in params)
919 self.__parameters__ = _collect_type_vars(params)
920 if not name:
921 self.__module__ = origin.__module__
922
923 def __eq__(self, other):
924 if not isinstance(other, _GenericAlias):
925 return NotImplemented
926 return (self.__origin__ == other.__origin__
927 and self.__args__ == other.__args__)
928
929 def __hash__(self):
930 return hash((self.__origin__, self.__args__))
931
Maggie Moss1b4552c2020-09-09 13:23:24 -0700932 def __or__(self, right):
933 return Union[self, right]
934
935 def __ror__(self, right):
936 return Union[self, right]
937
Guido van Rossum4cefe742016-09-27 15:20:12 -0700938 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700939 def __getitem__(self, params):
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100940 if self.__origin__ in (Generic, Protocol):
941 # Can't subscript Generic[...] or Protocol[...].
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000942 raise TypeError(f"Cannot subscript already-subscripted {self}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700943 if not isinstance(params, tuple):
944 params = (params,)
kj73607be2020-12-24 12:33:48 +0800945 params = tuple(_type_convert(p) for p in params)
946 if any(isinstance(t, ParamSpec) for t in self.__parameters__):
947 params = _prepare_paramspec_params(self, params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300948 _check_generic(self, params, len(self.__parameters__))
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300949
950 subst = dict(zip(self.__parameters__, params))
951 new_args = []
952 for arg in self.__args__:
kj73607be2020-12-24 12:33:48 +0800953 if isinstance(arg, _TypeVarLike):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300954 arg = subst[arg]
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300955 elif isinstance(arg, (_GenericAlias, GenericAlias)):
Serhiy Storchaka0122d482020-05-10 13:39:40 +0300956 subparams = arg.__parameters__
957 if subparams:
958 subargs = tuple(subst[x] for x in subparams)
959 arg = arg[subargs]
kj73607be2020-12-24 12:33:48 +0800960 # Required to flatten out the args for CallableGenericAlias
961 if self.__origin__ == collections.abc.Callable and isinstance(arg, tuple):
962 new_args.extend(arg)
963 else:
964 new_args.append(arg)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300965 return self.copy_with(tuple(new_args))
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100966
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000967 def copy_with(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300968 return self.__class__(self.__origin__, params, name=self._name, inst=self._inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700969
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000970 def __repr__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300971 if self._name:
972 name = 'typing.' + self._name
973 else:
974 name = _type_repr(self.__origin__)
975 args = ", ".join([_type_repr(a) for a in self.__args__])
976 return f'{name}[{args}]'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000977
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300978 def __reduce__(self):
979 if self._name:
980 origin = globals()[self._name]
981 else:
982 origin = self.__origin__
983 args = tuple(self.__args__)
984 if len(args) == 1 and not isinstance(args[0], tuple):
985 args, = args
986 return operator.getitem, (origin, args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000987
988 def __mro_entries__(self, bases):
989 if self._name: # generic version of an ABC or built-in class
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300990 return super().__mro_entries__(bases)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000991 if self.__origin__ is Generic:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100992 if Protocol in bases:
993 return ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000994 i = bases.index(self)
995 for b in bases[i+1:]:
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300996 if isinstance(b, _BaseGenericAlias) and b is not self:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000997 return ()
998 return (self.__origin__,)
999
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001000
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001001# _nparams is the number of accepted parameters, e.g. 0 for Hashable,
1002# 1 for List and 2 for Dict. It may be -1 if variable number of
1003# parameters are accepted (needs custom __getitem__).
1004
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001005class _SpecialGenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001006 def __init__(self, origin, nparams, *, inst=True, name=None):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001007 if name is None:
1008 name = origin.__name__
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001009 super().__init__(origin, inst=inst, name=name)
1010 self._nparams = nparams
Serhiy Storchaka2fbc57a2020-05-10 15:14:27 +03001011 if origin.__module__ == 'builtins':
1012 self.__doc__ = f'A generic version of {origin.__qualname__}.'
1013 else:
1014 self.__doc__ = f'A generic version of {origin.__module__}.{origin.__qualname__}.'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001015
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001016 @_tp_cache
1017 def __getitem__(self, params):
1018 if not isinstance(params, tuple):
1019 params = (params,)
1020 msg = "Parameters to generic types must be types."
1021 params = tuple(_type_check(p, msg) for p in params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001022 _check_generic(self, params, self._nparams)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001023 return self.copy_with(params)
1024
1025 def copy_with(self, params):
1026 return _GenericAlias(self.__origin__, params,
1027 name=self._name, inst=self._inst)
1028
1029 def __repr__(self):
1030 return 'typing.' + self._name
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001031
1032 def __subclasscheck__(self, cls):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001033 if isinstance(cls, _SpecialGenericAlias):
1034 return issubclass(cls.__origin__, self.__origin__)
1035 if not isinstance(cls, _GenericAlias):
1036 return issubclass(cls, self.__origin__)
1037 return super().__subclasscheck__(cls)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001038
Ivan Levkivskyi83494032018-03-26 23:01:12 +01001039 def __reduce__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001040 return self._name
Ivan Levkivskyi83494032018-03-26 23:01:12 +01001041
Maggie Moss1b4552c2020-09-09 13:23:24 -07001042 def __or__(self, right):
1043 return Union[self, right]
1044
1045 def __ror__(self, right):
1046 return Union[self, right]
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001047
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001048class _CallableGenericAlias(_GenericAlias, _root=True):
1049 def __repr__(self):
1050 assert self._name == 'Callable'
kj73607be2020-12-24 12:33:48 +08001051 args = self.__args__
1052 if len(args) == 2 and (args[0] is Ellipsis
1053 or isinstance(args[0], (ParamSpec, _ConcatenateGenericAlias))):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001054 return super().__repr__()
1055 return (f'typing.Callable'
kj73607be2020-12-24 12:33:48 +08001056 f'[[{", ".join([_type_repr(a) for a in args[:-1]])}], '
1057 f'{_type_repr(args[-1])}]')
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001058
1059 def __reduce__(self):
1060 args = self.__args__
kj73607be2020-12-24 12:33:48 +08001061 if not (len(args) == 2 and (args[0] is Ellipsis
1062 or isinstance(args[0], (ParamSpec, _ConcatenateGenericAlias)))):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001063 args = list(args[:-1]), args[-1]
1064 return operator.getitem, (Callable, args)
1065
1066
1067class _CallableType(_SpecialGenericAlias, _root=True):
1068 def copy_with(self, params):
1069 return _CallableGenericAlias(self.__origin__, params,
1070 name=self._name, inst=self._inst)
1071
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001072 def __getitem__(self, params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001073 if not isinstance(params, tuple) or len(params) != 2:
1074 raise TypeError("Callable must be used as "
1075 "Callable[[arg, ...], result].")
1076 args, result = params
kj463c7d32020-12-14 02:38:24 +08001077 # This relaxes what args can be on purpose to allow things like
1078 # PEP 612 ParamSpec. Responsibility for whether a user is using
1079 # Callable[...] properly is deferred to static type checkers.
1080 if isinstance(args, list):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001081 params = (tuple(args), result)
kj463c7d32020-12-14 02:38:24 +08001082 else:
1083 params = (args, result)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001084 return self.__getitem_inner__(params)
1085
1086 @_tp_cache
1087 def __getitem_inner__(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001088 args, result = params
1089 msg = "Callable[args, result]: result must be a type."
1090 result = _type_check(result, msg)
1091 if args is Ellipsis:
1092 return self.copy_with((_TypingEllipsis, result))
kj463c7d32020-12-14 02:38:24 +08001093 if not isinstance(args, tuple):
1094 args = (args,)
1095 args = tuple(_type_convert(arg) for arg in args)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001096 params = args + (result,)
1097 return self.copy_with(params)
1098
1099
1100class _TupleType(_SpecialGenericAlias, _root=True):
1101 @_tp_cache
1102 def __getitem__(self, params):
1103 if params == ():
1104 return self.copy_with((_TypingEmpty,))
1105 if not isinstance(params, tuple):
1106 params = (params,)
1107 if len(params) == 2 and params[1] is ...:
1108 msg = "Tuple[t, ...]: t must be a type."
1109 p = _type_check(params[0], msg)
1110 return self.copy_with((p, _TypingEllipsis))
1111 msg = "Tuple[t0, t1, ...]: each t must be a type."
1112 params = tuple(_type_check(p, msg) for p in params)
1113 return self.copy_with(params)
1114
1115
1116class _UnionGenericAlias(_GenericAlias, _root=True):
1117 def copy_with(self, params):
1118 return Union[params]
1119
1120 def __eq__(self, other):
1121 if not isinstance(other, _UnionGenericAlias):
1122 return NotImplemented
1123 return set(self.__args__) == set(other.__args__)
1124
1125 def __hash__(self):
1126 return hash(frozenset(self.__args__))
1127
1128 def __repr__(self):
1129 args = self.__args__
1130 if len(args) == 2:
1131 if args[0] is type(None):
1132 return f'typing.Optional[{_type_repr(args[1])}]'
1133 elif args[1] is type(None):
1134 return f'typing.Optional[{_type_repr(args[0])}]'
1135 return super().__repr__()
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001136
Maggie Moss1b4552c2020-09-09 13:23:24 -07001137 def __instancecheck__(self, obj):
1138 return self.__subclasscheck__(type(obj))
1139
1140 def __subclasscheck__(self, cls):
1141 for arg in self.__args__:
1142 if issubclass(cls, arg):
1143 return True
1144
1145
Yurii Karabasf03d3182020-11-17 04:23:19 +02001146def _value_and_type_iter(parameters):
1147 return ((p, type(p)) for p in parameters)
1148
1149
1150class _LiteralGenericAlias(_GenericAlias, _root=True):
1151
1152 def __eq__(self, other):
1153 if not isinstance(other, _LiteralGenericAlias):
1154 return NotImplemented
1155
1156 return set(_value_and_type_iter(self.__args__)) == set(_value_and_type_iter(other.__args__))
1157
1158 def __hash__(self):
Yurii Karabas1b540772020-11-19 18:17:38 +02001159 return hash(frozenset(_value_and_type_iter(self.__args__)))
Yurii Karabasf03d3182020-11-17 04:23:19 +02001160
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001161
kj73607be2020-12-24 12:33:48 +08001162class _ConcatenateGenericAlias(_GenericAlias, _root=True):
1163 pass
1164
1165
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001166class Generic:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001167 """Abstract base class for generic types.
1168
Guido van Rossumb24569a2016-11-20 18:01:29 -08001169 A generic type is typically declared by inheriting from
1170 this class parameterized with one or more type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001171 For example, a generic mapping type might be defined as::
1172
1173 class Mapping(Generic[KT, VT]):
1174 def __getitem__(self, key: KT) -> VT:
1175 ...
1176 # Etc.
1177
1178 This class can then be used as follows::
1179
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001180 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001181 try:
1182 return mapping[key]
1183 except KeyError:
1184 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001185 """
Guido van Rossumd70fe632015-08-05 12:11:06 +02001186 __slots__ = ()
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001187 _is_protocol = False
Guido van Rossumd70fe632015-08-05 12:11:06 +02001188
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001189 @_tp_cache
1190 def __class_getitem__(cls, params):
1191 if not isinstance(params, tuple):
1192 params = (params,)
1193 if not params and cls is not Tuple:
1194 raise TypeError(
1195 f"Parameter list to {cls.__qualname__}[...] cannot be empty")
kj73607be2020-12-24 12:33:48 +08001196 params = tuple(_type_convert(p) for p in params)
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001197 if cls in (Generic, Protocol):
1198 # Generic and Protocol can only be subscripted with unique type variables.
kj73607be2020-12-24 12:33:48 +08001199 if not all(isinstance(p, _TypeVarLike) for p in params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001200 raise TypeError(
kj73607be2020-12-24 12:33:48 +08001201 f"Parameters to {cls.__name__}[...] must all be type variables "
1202 f"or parameter specification variables.")
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001203 if len(set(params)) != len(params):
1204 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001205 f"Parameters to {cls.__name__}[...] must all be unique")
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001206 else:
1207 # Subscripting a regular Generic subclass.
kj73607be2020-12-24 12:33:48 +08001208 if any(isinstance(t, ParamSpec) for t in cls.__parameters__):
1209 params = _prepare_paramspec_params(cls, params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001210 _check_generic(cls, params, len(cls.__parameters__))
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001211 return _GenericAlias(cls, params)
1212
1213 def __init_subclass__(cls, *args, **kwargs):
Ivan Levkivskyiee566fe2018-04-04 17:00:15 +01001214 super().__init_subclass__(*args, **kwargs)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001215 tvars = []
1216 if '__orig_bases__' in cls.__dict__:
1217 error = Generic in cls.__orig_bases__
1218 else:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001219 error = Generic in cls.__bases__ and cls.__name__ != 'Protocol'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001220 if error:
1221 raise TypeError("Cannot inherit from plain Generic")
1222 if '__orig_bases__' in cls.__dict__:
1223 tvars = _collect_type_vars(cls.__orig_bases__)
1224 # Look for Generic[T1, ..., Tn].
1225 # If found, tvars must be a subset of it.
1226 # If not found, tvars is it.
1227 # Also check for and reject plain Generic,
1228 # and reject multiple Generic[...].
1229 gvars = None
1230 for base in cls.__orig_bases__:
1231 if (isinstance(base, _GenericAlias) and
1232 base.__origin__ is Generic):
1233 if gvars is not None:
1234 raise TypeError(
1235 "Cannot inherit from Generic[...] multiple types.")
1236 gvars = base.__parameters__
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001237 if gvars is not None:
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001238 tvarset = set(tvars)
1239 gvarset = set(gvars)
1240 if not tvarset <= gvarset:
1241 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
1242 s_args = ', '.join(str(g) for g in gvars)
1243 raise TypeError(f"Some type variables ({s_vars}) are"
1244 f" not listed in Generic[{s_args}]")
1245 tvars = gvars
1246 cls.__parameters__ = tuple(tvars)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001247
1248
1249class _TypingEmpty:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001250 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
1251 to allow empty list/tuple in specific places, without allowing them
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001252 to sneak in where prohibited.
1253 """
1254
1255
1256class _TypingEllipsis:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001257 """Internal placeholder for ... (ellipsis)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001258
1259
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001260_TYPING_INTERNALS = ['__parameters__', '__orig_bases__', '__orig_class__',
1261 '_is_protocol', '_is_runtime_protocol']
1262
1263_SPECIAL_NAMES = ['__abstractmethods__', '__annotations__', '__dict__', '__doc__',
1264 '__init__', '__module__', '__new__', '__slots__',
Guido van Rossum48b069a2020-04-07 09:50:06 -07001265 '__subclasshook__', '__weakref__', '__class_getitem__']
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001266
1267# These special attributes will be not collected as protocol members.
1268EXCLUDED_ATTRIBUTES = _TYPING_INTERNALS + _SPECIAL_NAMES + ['_MutableMapping__marker']
1269
1270
1271def _get_protocol_attrs(cls):
1272 """Collect protocol members from a protocol class objects.
1273
1274 This includes names actually defined in the class dictionary, as well
1275 as names that appear in annotations. Special names (above) are skipped.
1276 """
1277 attrs = set()
1278 for base in cls.__mro__[:-1]: # without object
1279 if base.__name__ in ('Protocol', 'Generic'):
1280 continue
1281 annotations = getattr(base, '__annotations__', {})
1282 for attr in list(base.__dict__.keys()) + list(annotations.keys()):
1283 if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
1284 attrs.add(attr)
1285 return attrs
1286
1287
1288def _is_callable_members_only(cls):
1289 # PEP 544 prohibits using issubclass() with protocols that have non-method members.
1290 return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls))
1291
1292
1293def _no_init(self, *args, **kwargs):
1294 if type(self)._is_protocol:
1295 raise TypeError('Protocols cannot be instantiated')
1296
1297
Rossc1af1282020-12-29 11:55:28 +00001298def _allow_reckless_class_checks():
Nickolena Fishercfaf4c02020-04-26 12:49:11 -05001299 """Allow instance and class checks for special stdlib modules.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001300
1301 The abc and functools modules indiscriminately call isinstance() and
1302 issubclass() on the whole MRO of a user class, which may contain protocols.
1303 """
1304 try:
1305 return sys._getframe(3).f_globals['__name__'] in ['abc', 'functools']
1306 except (AttributeError, ValueError): # For platforms without _getframe().
1307 return True
1308
1309
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001310_PROTO_ALLOWLIST = {
Divij Rajkumar692a0dc2019-09-12 11:13:51 +01001311 'collections.abc': [
1312 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
1313 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
1314 ],
1315 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
1316}
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001317
1318
1319class _ProtocolMeta(ABCMeta):
1320 # This metaclass is really unfortunate and exists only because of
1321 # the lack of __instancehook__.
1322 def __instancecheck__(cls, instance):
1323 # We need this method for situations where attributes are
1324 # assigned in __init__.
1325 if ((not getattr(cls, '_is_protocol', False) or
1326 _is_callable_members_only(cls)) and
1327 issubclass(instance.__class__, cls)):
1328 return True
1329 if cls._is_protocol:
1330 if all(hasattr(instance, attr) and
1331 # All *methods* can be blocked by setting them to None.
1332 (not callable(getattr(cls, attr, None)) or
1333 getattr(instance, attr) is not None)
1334 for attr in _get_protocol_attrs(cls)):
1335 return True
1336 return super().__instancecheck__(instance)
1337
1338
1339class Protocol(Generic, metaclass=_ProtocolMeta):
1340 """Base class for protocol classes.
1341
1342 Protocol classes are defined as::
1343
1344 class Proto(Protocol):
1345 def meth(self) -> int:
1346 ...
1347
1348 Such classes are primarily used with static type checkers that recognize
1349 structural subtyping (static duck-typing), for example::
1350
1351 class C:
1352 def meth(self) -> int:
1353 return 0
1354
1355 def func(x: Proto) -> int:
1356 return x.meth()
1357
1358 func(C()) # Passes static type check
1359
1360 See PEP 544 for details. Protocol classes decorated with
1361 @typing.runtime_checkable act as simple-minded runtime protocols that check
1362 only the presence of given attributes, ignoring their type signatures.
1363 Protocol classes can be generic, they are defined as::
1364
1365 class GenProto(Protocol[T]):
1366 def meth(self) -> T:
1367 ...
1368 """
1369 __slots__ = ()
1370 _is_protocol = True
1371 _is_runtime_protocol = False
1372
1373 def __init_subclass__(cls, *args, **kwargs):
1374 super().__init_subclass__(*args, **kwargs)
1375
1376 # Determine if this is a protocol or a concrete subclass.
1377 if not cls.__dict__.get('_is_protocol', False):
1378 cls._is_protocol = any(b is Protocol for b in cls.__bases__)
1379
1380 # Set (or override) the protocol subclass hook.
1381 def _proto_hook(other):
1382 if not cls.__dict__.get('_is_protocol', False):
1383 return NotImplemented
1384
1385 # First, perform various sanity checks.
1386 if not getattr(cls, '_is_runtime_protocol', False):
Rossc1af1282020-12-29 11:55:28 +00001387 if _allow_reckless_class_checks():
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001388 return NotImplemented
1389 raise TypeError("Instance and class checks can only be used with"
1390 " @runtime_checkable protocols")
1391 if not _is_callable_members_only(cls):
Rossc1af1282020-12-29 11:55:28 +00001392 if _allow_reckless_class_checks():
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001393 return NotImplemented
1394 raise TypeError("Protocols with non-method members"
1395 " don't support issubclass()")
1396 if not isinstance(other, type):
1397 # Same error message as for issubclass(1, int).
1398 raise TypeError('issubclass() arg 1 must be a class')
1399
1400 # Second, perform the actual structural compatibility check.
1401 for attr in _get_protocol_attrs(cls):
1402 for base in other.__mro__:
1403 # Check if the members appears in the class dictionary...
1404 if attr in base.__dict__:
1405 if base.__dict__[attr] is None:
1406 return NotImplemented
1407 break
1408
1409 # ...or in annotations, if it is a sub-protocol.
1410 annotations = getattr(base, '__annotations__', {})
1411 if (isinstance(annotations, collections.abc.Mapping) and
1412 attr in annotations and
1413 issubclass(other, Generic) and other._is_protocol):
1414 break
1415 else:
1416 return NotImplemented
1417 return True
1418
1419 if '__subclasshook__' not in cls.__dict__:
1420 cls.__subclasshook__ = _proto_hook
1421
1422 # We have nothing more to do for non-protocols...
1423 if not cls._is_protocol:
1424 return
1425
1426 # ... otherwise check consistency of bases, and prohibit instantiation.
1427 for base in cls.__bases__:
1428 if not (base in (object, Generic) or
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001429 base.__module__ in _PROTO_ALLOWLIST and
1430 base.__name__ in _PROTO_ALLOWLIST[base.__module__] or
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001431 issubclass(base, Generic) and base._is_protocol):
1432 raise TypeError('Protocols can only inherit from other'
1433 ' protocols, got %r' % base)
1434 cls.__init__ = _no_init
1435
1436
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001437class _AnnotatedAlias(_GenericAlias, _root=True):
1438 """Runtime representation of an annotated type.
1439
1440 At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't'
1441 with extra annotations. The alias behaves like a normal typing alias,
1442 instantiating is the same as instantiating the underlying type, binding
1443 it to types is also the same.
1444 """
1445 def __init__(self, origin, metadata):
1446 if isinstance(origin, _AnnotatedAlias):
1447 metadata = origin.__metadata__ + metadata
1448 origin = origin.__origin__
1449 super().__init__(origin, origin)
1450 self.__metadata__ = metadata
1451
1452 def copy_with(self, params):
1453 assert len(params) == 1
1454 new_type = params[0]
1455 return _AnnotatedAlias(new_type, self.__metadata__)
1456
1457 def __repr__(self):
1458 return "typing.Annotated[{}, {}]".format(
1459 _type_repr(self.__origin__),
1460 ", ".join(repr(a) for a in self.__metadata__)
1461 )
1462
1463 def __reduce__(self):
1464 return operator.getitem, (
1465 Annotated, (self.__origin__,) + self.__metadata__
1466 )
1467
1468 def __eq__(self, other):
1469 if not isinstance(other, _AnnotatedAlias):
1470 return NotImplemented
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001471 return (self.__origin__ == other.__origin__
1472 and self.__metadata__ == other.__metadata__)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001473
1474 def __hash__(self):
1475 return hash((self.__origin__, self.__metadata__))
1476
1477
1478class Annotated:
1479 """Add context specific metadata to a type.
1480
1481 Example: Annotated[int, runtime_check.Unsigned] indicates to the
1482 hypothetical runtime_check module that this type is an unsigned int.
1483 Every other consumer of this type can ignore this metadata and treat
1484 this type as int.
1485
1486 The first argument to Annotated must be a valid type.
1487
1488 Details:
1489
1490 - It's an error to call `Annotated` with less than two arguments.
1491 - Nested Annotated are flattened::
1492
1493 Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
1494
1495 - Instantiating an annotated type is equivalent to instantiating the
1496 underlying type::
1497
1498 Annotated[C, Ann1](5) == C(5)
1499
1500 - Annotated can be used as a generic type alias::
1501
1502 Optimized = Annotated[T, runtime.Optimize()]
1503 Optimized[int] == Annotated[int, runtime.Optimize()]
1504
1505 OptimizedList = Annotated[List[T], runtime.Optimize()]
1506 OptimizedList[int] == Annotated[List[int], runtime.Optimize()]
1507 """
1508
1509 __slots__ = ()
1510
1511 def __new__(cls, *args, **kwargs):
1512 raise TypeError("Type Annotated cannot be instantiated.")
1513
1514 @_tp_cache
1515 def __class_getitem__(cls, params):
1516 if not isinstance(params, tuple) or len(params) < 2:
1517 raise TypeError("Annotated[...] should be used "
1518 "with at least two arguments (a type and an "
1519 "annotation).")
1520 msg = "Annotated[t, ...]: t must be a type."
1521 origin = _type_check(params[0], msg)
1522 metadata = tuple(params[1:])
1523 return _AnnotatedAlias(origin, metadata)
1524
1525 def __init_subclass__(cls, *args, **kwargs):
1526 raise TypeError(
1527 "Cannot subclass {}.Annotated".format(cls.__module__)
1528 )
1529
1530
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001531def runtime_checkable(cls):
1532 """Mark a protocol class as a runtime protocol.
1533
1534 Such protocol can be used with isinstance() and issubclass().
1535 Raise TypeError if applied to a non-protocol class.
1536 This allows a simple-minded structural check very similar to
1537 one trick ponies in collections.abc such as Iterable.
1538 For example::
1539
1540 @runtime_checkable
1541 class Closable(Protocol):
1542 def close(self): ...
1543
1544 assert isinstance(open('/some/file'), Closable)
1545
1546 Warning: this will check only the presence of the required methods,
1547 not their type signatures!
1548 """
1549 if not issubclass(cls, Generic) or not cls._is_protocol:
1550 raise TypeError('@runtime_checkable can be only applied to protocol classes,'
1551 ' got %r' % cls)
1552 cls._is_runtime_protocol = True
1553 return cls
1554
1555
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001556def cast(typ, val):
1557 """Cast a value to a type.
1558
1559 This returns the value unchanged. To the type checker this
1560 signals that the return value has the designated type, but at
1561 runtime we intentionally don't check anything (we want this
1562 to be as fast as possible).
1563 """
1564 return val
1565
1566
1567def _get_defaults(func):
1568 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -08001569 try:
1570 code = func.__code__
1571 except AttributeError:
1572 # Some built-in functions don't have __code__, __defaults__, etc.
1573 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001574 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001575 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001576 arg_names = arg_names[:pos_count]
1577 defaults = func.__defaults__ or ()
1578 kwdefaults = func.__kwdefaults__
1579 res = dict(kwdefaults) if kwdefaults else {}
1580 pos_offset = pos_count - len(defaults)
1581 for name, value in zip(arg_names[pos_offset:], defaults):
1582 assert name not in res
1583 res[name] = value
1584 return res
1585
1586
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001587_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
1588 types.MethodType, types.ModuleType,
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02001589 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001590
1591
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001592def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
Guido van Rossum991d14f2016-11-09 13:12:51 -08001593 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001594
Guido van Rossum991d14f2016-11-09 13:12:51 -08001595 This is often the same as obj.__annotations__, but it handles
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001596 forward references encoded as string literals, adds Optional[t] if a
1597 default value equal to None is set and recursively replaces all
1598 'Annotated[T, ...]' with 'T' (unless 'include_extras=True').
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001599
Guido van Rossum991d14f2016-11-09 13:12:51 -08001600 The argument may be a module, class, method, or function. The annotations
1601 are returned as a dictionary. For classes, annotations include also
1602 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001603
Guido van Rossum991d14f2016-11-09 13:12:51 -08001604 TypeError is raised if the argument is not of a type that can contain
1605 annotations, and an empty dictionary is returned if no annotations are
1606 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001607
Guido van Rossum991d14f2016-11-09 13:12:51 -08001608 BEWARE -- the behavior of globalns and localns is counterintuitive
1609 (unless you are familiar with how eval() and exec() work). The
1610 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001611
Guido van Rossum991d14f2016-11-09 13:12:51 -08001612 - If no dict arguments are passed, an attempt is made to use the
Łukasz Langaf350a262017-09-14 14:33:00 -04001613 globals from obj (or the respective module's globals for classes),
1614 and these are also used as the locals. If the object does not appear
1615 to have globals, an empty dictionary is used.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001616
Guido van Rossum991d14f2016-11-09 13:12:51 -08001617 - If one dict argument is passed, it is used for both globals and
1618 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001619
Guido van Rossum991d14f2016-11-09 13:12:51 -08001620 - If two dict arguments are passed, they specify globals and
1621 locals, respectively.
1622 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001623
Guido van Rossum991d14f2016-11-09 13:12:51 -08001624 if getattr(obj, '__no_type_check__', None):
1625 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001626 # Classes require a special treatment.
1627 if isinstance(obj, type):
1628 hints = {}
1629 for base in reversed(obj.__mro__):
Łukasz Langaf350a262017-09-14 14:33:00 -04001630 if globalns is None:
1631 base_globals = sys.modules[base.__module__].__dict__
1632 else:
1633 base_globals = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001634 ann = base.__dict__.get('__annotations__', {})
1635 for name, value in ann.items():
1636 if value is None:
1637 value = type(None)
1638 if isinstance(value, str):
Nina Zakharenko0e61dff2018-05-22 20:32:10 -07001639 value = ForwardRef(value, is_argument=False)
Łukasz Langaf350a262017-09-14 14:33:00 -04001640 value = _eval_type(value, base_globals, localns)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001641 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001642 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
Łukasz Langaf350a262017-09-14 14:33:00 -04001643
1644 if globalns is None:
1645 if isinstance(obj, types.ModuleType):
1646 globalns = obj.__dict__
1647 else:
benedwards140aca3a32019-11-21 17:24:58 +00001648 nsobj = obj
1649 # Find globalns for the unwrapped object.
1650 while hasattr(nsobj, '__wrapped__'):
1651 nsobj = nsobj.__wrapped__
1652 globalns = getattr(nsobj, '__globals__', {})
Łukasz Langaf350a262017-09-14 14:33:00 -04001653 if localns is None:
1654 localns = globalns
1655 elif localns is None:
1656 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001657 hints = getattr(obj, '__annotations__', None)
1658 if hints is None:
1659 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001660 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001661 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001662 else:
1663 raise TypeError('{!r} is not a module, class, method, '
1664 'or function.'.format(obj))
1665 defaults = _get_defaults(obj)
1666 hints = dict(hints)
1667 for name, value in hints.items():
1668 if value is None:
1669 value = type(None)
1670 if isinstance(value, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001671 value = ForwardRef(value)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001672 value = _eval_type(value, globalns, localns)
1673 if name in defaults and defaults[name] is None:
1674 value = Optional[value]
1675 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001676 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
1677
1678
1679def _strip_annotations(t):
1680 """Strips the annotations from a given type.
1681 """
1682 if isinstance(t, _AnnotatedAlias):
1683 return _strip_annotations(t.__origin__)
1684 if isinstance(t, _GenericAlias):
1685 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1686 if stripped_args == t.__args__:
1687 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001688 return t.copy_with(stripped_args)
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001689 if isinstance(t, GenericAlias):
1690 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1691 if stripped_args == t.__args__:
1692 return t
1693 return GenericAlias(t.__origin__, stripped_args)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001694 return t
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001695
1696
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001697def get_origin(tp):
1698 """Get the unsubscripted version of a type.
1699
Jakub Stasiak38aaaaa2020-02-07 02:15:12 +01001700 This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
1701 and Annotated. Return None for unsupported types. Examples::
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001702
1703 get_origin(Literal[42]) is Literal
1704 get_origin(int) is None
1705 get_origin(ClassVar[int]) is ClassVar
1706 get_origin(Generic) is Generic
1707 get_origin(Generic[T]) is Generic
1708 get_origin(Union[T, int]) is Union
1709 get_origin(List[Tuple[T, T]][int]) == list
Jelle Zijlstra52243362021-04-10 19:57:05 -07001710 get_origin(P.args) is P
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001711 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001712 if isinstance(tp, _AnnotatedAlias):
1713 return Annotated
Jelle Zijlstra52243362021-04-10 19:57:05 -07001714 if isinstance(tp, (_BaseGenericAlias, GenericAlias,
1715 ParamSpecArgs, ParamSpecKwargs)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001716 return tp.__origin__
1717 if tp is Generic:
1718 return Generic
Ken Jinefb1f092020-12-29 10:26:19 +08001719 if isinstance(tp, types.Union):
1720 return types.Union
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001721 return None
1722
1723
1724def get_args(tp):
1725 """Get type arguments with all substitutions performed.
1726
1727 For unions, basic simplifications used by Union constructor are performed.
1728 Examples::
1729 get_args(Dict[str, int]) == (str, int)
1730 get_args(int) == ()
1731 get_args(Union[int, Union[T, int], str][int]) == (int, str)
1732 get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
1733 get_args(Callable[[], T][int]) == ([], int)
1734 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001735 if isinstance(tp, _AnnotatedAlias):
1736 return (tp.__origin__,) + tp.__metadata__
Ken Jin4140f102020-12-29 04:06:19 +08001737 if isinstance(tp, (_GenericAlias, GenericAlias)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001738 res = tp.__args__
Ken Jinefb1f092020-12-29 10:26:19 +08001739 if (tp.__origin__ is collections.abc.Callable
1740 and not (res[0] is Ellipsis
1741 or isinstance(res[0], (ParamSpec, _ConcatenateGenericAlias)))):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001742 res = (list(res[:-1]), res[-1])
1743 return res
Ken Jinefb1f092020-12-29 10:26:19 +08001744 if isinstance(tp, types.Union):
1745 return tp.__args__
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001746 return ()
1747
1748
Patrick Reader0705ec82020-09-16 05:58:32 +01001749def is_typeddict(tp):
1750 """Check if an annotation is a TypedDict class
1751
1752 For example::
1753 class Film(TypedDict):
1754 title: str
1755 year: int
1756
1757 is_typeddict(Film) # => True
1758 is_typeddict(Union[list, str]) # => False
1759 """
1760 return isinstance(tp, _TypedDictMeta)
1761
1762
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001763def no_type_check(arg):
1764 """Decorator to indicate that annotations are not type hints.
1765
1766 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001767 applies recursively to all methods and classes defined in that class
1768 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001769
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001770 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001771 """
1772 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001773 arg_attrs = arg.__dict__.copy()
1774 for attr, val in arg.__dict__.items():
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001775 if val in arg.__bases__ + (arg,):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001776 arg_attrs.pop(attr)
1777 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001778 if isinstance(obj, types.FunctionType):
1779 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001780 if isinstance(obj, type):
1781 no_type_check(obj)
1782 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001783 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001784 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001785 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001786 return arg
1787
1788
1789def no_type_check_decorator(decorator):
1790 """Decorator to give another decorator the @no_type_check effect.
1791
1792 This wraps the decorator with something that wraps the decorated
1793 function in @no_type_check.
1794 """
1795
1796 @functools.wraps(decorator)
1797 def wrapped_decorator(*args, **kwds):
1798 func = decorator(*args, **kwds)
1799 func = no_type_check(func)
1800 return func
1801
1802 return wrapped_decorator
1803
1804
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001805def _overload_dummy(*args, **kwds):
1806 """Helper for @overload to raise when called."""
1807 raise NotImplementedError(
1808 "You should not call an overloaded function. "
1809 "A series of @overload-decorated functions "
1810 "outside a stub module should always be followed "
1811 "by an implementation that is not @overload-ed.")
1812
1813
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001814def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001815 """Decorator for overloaded functions/methods.
1816
1817 In a stub file, place two or more stub definitions for the same
1818 function in a row, each decorated with @overload. For example:
1819
1820 @overload
1821 def utf8(value: None) -> None: ...
1822 @overload
1823 def utf8(value: bytes) -> bytes: ...
1824 @overload
1825 def utf8(value: str) -> bytes: ...
1826
1827 In a non-stub file (i.e. a regular .py file), do the same but
1828 follow it with an implementation. The implementation should *not*
1829 be decorated with @overload. For example:
1830
1831 @overload
1832 def utf8(value: None) -> None: ...
1833 @overload
1834 def utf8(value: bytes) -> bytes: ...
1835 @overload
1836 def utf8(value: str) -> bytes: ...
1837 def utf8(value):
1838 # implementation goes here
1839 """
1840 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001841
1842
Ivan Levkivskyif3672422019-05-26 09:37:07 +01001843def final(f):
1844 """A decorator to indicate final methods and final classes.
1845
1846 Use this decorator to indicate to type checkers that the decorated
1847 method cannot be overridden, and decorated class cannot be subclassed.
1848 For example:
1849
1850 class Base:
1851 @final
1852 def done(self) -> None:
1853 ...
1854 class Sub(Base):
1855 def done(self) -> None: # Error reported by type checker
1856 ...
1857
1858 @final
1859 class Leaf:
1860 ...
1861 class Other(Leaf): # Error reported by type checker
1862 ...
1863
1864 There is no runtime checking of these properties.
1865 """
1866 return f
1867
1868
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001869# Some unconstrained type variables. These are used by the container types.
1870# (These are not for export.)
1871T = TypeVar('T') # Any type.
1872KT = TypeVar('KT') # Key type.
1873VT = TypeVar('VT') # Value type.
1874T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
1875V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
1876VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
1877T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
1878# Internal type variable used for Type[].
1879CT_co = TypeVar('CT_co', covariant=True, bound=type)
1880
1881# A useful type variable with constraints. This represents string types.
1882# (This one *is* for export!)
1883AnyStr = TypeVar('AnyStr', bytes, str)
1884
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001885
1886# Various ABCs mimicking those in collections.abc.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001887_alias = _SpecialGenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001888
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001889Hashable = _alias(collections.abc.Hashable, 0) # Not generic.
1890Awaitable = _alias(collections.abc.Awaitable, 1)
1891Coroutine = _alias(collections.abc.Coroutine, 3)
1892AsyncIterable = _alias(collections.abc.AsyncIterable, 1)
1893AsyncIterator = _alias(collections.abc.AsyncIterator, 1)
1894Iterable = _alias(collections.abc.Iterable, 1)
1895Iterator = _alias(collections.abc.Iterator, 1)
1896Reversible = _alias(collections.abc.Reversible, 1)
1897Sized = _alias(collections.abc.Sized, 0) # Not generic.
1898Container = _alias(collections.abc.Container, 1)
1899Collection = _alias(collections.abc.Collection, 1)
1900Callable = _CallableType(collections.abc.Callable, 2)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001901Callable.__doc__ = \
1902 """Callable type; Callable[[int], str] is a function of (int) -> str.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001903
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001904 The subscription syntax must always be used with exactly two
1905 values: the argument list and the return type. The argument list
1906 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001907
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001908 There is no syntax to indicate optional or keyword arguments,
1909 such function types are rarely used as callback types.
1910 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001911AbstractSet = _alias(collections.abc.Set, 1, name='AbstractSet')
1912MutableSet = _alias(collections.abc.MutableSet, 1)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001913# NOTE: Mapping is only covariant in the value type.
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001914Mapping = _alias(collections.abc.Mapping, 2)
1915MutableMapping = _alias(collections.abc.MutableMapping, 2)
1916Sequence = _alias(collections.abc.Sequence, 1)
1917MutableSequence = _alias(collections.abc.MutableSequence, 1)
1918ByteString = _alias(collections.abc.ByteString, 0) # Not generic
1919# Tuple accepts variable number of parameters.
1920Tuple = _TupleType(tuple, -1, inst=False, name='Tuple')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001921Tuple.__doc__ = \
1922 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001923
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001924 Example: Tuple[T1, T2] is a tuple of two elements corresponding
1925 to type variables T1 and T2. Tuple[int, float, str] is a tuple
1926 of an int, a float and a string.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001927
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001928 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
1929 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001930List = _alias(list, 1, inst=False, name='List')
1931Deque = _alias(collections.deque, 1, name='Deque')
1932Set = _alias(set, 1, inst=False, name='Set')
1933FrozenSet = _alias(frozenset, 1, inst=False, name='FrozenSet')
1934MappingView = _alias(collections.abc.MappingView, 1)
1935KeysView = _alias(collections.abc.KeysView, 1)
1936ItemsView = _alias(collections.abc.ItemsView, 2)
1937ValuesView = _alias(collections.abc.ValuesView, 1)
1938ContextManager = _alias(contextlib.AbstractContextManager, 1, name='ContextManager')
1939AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, 1, name='AsyncContextManager')
1940Dict = _alias(dict, 2, inst=False, name='Dict')
1941DefaultDict = _alias(collections.defaultdict, 2, name='DefaultDict')
1942OrderedDict = _alias(collections.OrderedDict, 2)
1943Counter = _alias(collections.Counter, 1)
1944ChainMap = _alias(collections.ChainMap, 2)
1945Generator = _alias(collections.abc.Generator, 3)
1946AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2)
1947Type = _alias(type, 1, inst=False, name='Type')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001948Type.__doc__ = \
1949 """A special construct usable to annotate class objects.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001950
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001951 For example, suppose we have the following classes::
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001952
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001953 class User: ... # Abstract base for User classes
1954 class BasicUser(User): ...
1955 class ProUser(User): ...
1956 class TeamUser(User): ...
Guido van Rossumf17c2002015-12-03 17:31:24 -08001957
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001958 And a function that takes a class argument that's a subclass of
1959 User and returns an instance of the corresponding class::
Guido van Rossumf17c2002015-12-03 17:31:24 -08001960
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001961 U = TypeVar('U', bound=User)
1962 def new_user(user_class: Type[U]) -> U:
1963 user = user_class()
1964 # (Here we could write the user object to a database)
1965 return user
Guido van Rossumf17c2002015-12-03 17:31:24 -08001966
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001967 joe = new_user(BasicUser)
Guido van Rossumf17c2002015-12-03 17:31:24 -08001968
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001969 At this point the type checker knows that joe has type BasicUser.
1970 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001971
1972
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001973@runtime_checkable
1974class SupportsInt(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001975 """An ABC with one abstract method __int__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001976 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001977
1978 @abstractmethod
1979 def __int__(self) -> int:
1980 pass
1981
1982
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001983@runtime_checkable
1984class SupportsFloat(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001985 """An ABC with one abstract method __float__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001986 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001987
1988 @abstractmethod
1989 def __float__(self) -> float:
1990 pass
1991
1992
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001993@runtime_checkable
1994class SupportsComplex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001995 """An ABC with one abstract method __complex__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001996 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001997
1998 @abstractmethod
1999 def __complex__(self) -> complex:
2000 pass
2001
2002
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002003@runtime_checkable
2004class SupportsBytes(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002005 """An ABC with one abstract method __bytes__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002006 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002007
2008 @abstractmethod
2009 def __bytes__(self) -> bytes:
2010 pass
2011
2012
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002013@runtime_checkable
2014class SupportsIndex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002015 """An ABC with one abstract method __index__."""
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -07002016 __slots__ = ()
2017
2018 @abstractmethod
2019 def __index__(self) -> int:
2020 pass
2021
2022
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002023@runtime_checkable
2024class SupportsAbs(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002025 """An ABC with one abstract method __abs__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002026 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002027
2028 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02002029 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002030 pass
2031
2032
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002033@runtime_checkable
2034class SupportsRound(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002035 """An ABC with one abstract method __round__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002036 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002037
2038 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02002039 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002040 pass
2041
2042
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002043def _make_nmtuple(name, types, module, defaults = ()):
2044 fields = [n for n, t in types]
2045 types = {n: _type_check(t, f"field {n} annotation must be a type")
2046 for n, t in types}
2047 nm_tpl = collections.namedtuple(name, fields,
2048 defaults=defaults, module=module)
2049 nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002050 return nm_tpl
2051
2052
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002053# attributes prohibited to set in NamedTuple class syntax
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002054_prohibited = frozenset({'__new__', '__init__', '__slots__', '__getnewargs__',
2055 '_fields', '_field_defaults',
2056 '_make', '_replace', '_asdict', '_source'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002057
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002058_special = frozenset({'__module__', '__name__', '__annotations__'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002059
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002060
Guido van Rossum2f841442016-11-15 09:48:06 -08002061class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002062
Guido van Rossum2f841442016-11-15 09:48:06 -08002063 def __new__(cls, typename, bases, ns):
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002064 assert bases[0] is _NamedTuple
Guido van Rossum2f841442016-11-15 09:48:06 -08002065 types = ns.get('__annotations__', {})
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002066 default_names = []
Guido van Rossum3c268be2017-01-18 08:03:50 -08002067 for field_name in types:
2068 if field_name in ns:
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002069 default_names.append(field_name)
2070 elif default_names:
2071 raise TypeError(f"Non-default namedtuple field {field_name} "
2072 f"cannot follow default field"
2073 f"{'s' if len(default_names) > 1 else ''} "
2074 f"{', '.join(default_names)}")
2075 nm_tpl = _make_nmtuple(typename, types.items(),
2076 defaults=[ns[n] for n in default_names],
2077 module=ns['__module__'])
Guido van Rossum95919c02017-01-22 17:47:20 -08002078 # update from user namespace without overriding special namedtuple attributes
2079 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002080 if key in _prohibited:
2081 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
2082 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08002083 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08002084 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002085
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002086
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002087def NamedTuple(typename, fields=None, /, **kwargs):
Guido van Rossum2f841442016-11-15 09:48:06 -08002088 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002089
Guido van Rossum2f841442016-11-15 09:48:06 -08002090 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002091
Guido van Rossum2f841442016-11-15 09:48:06 -08002092 class Employee(NamedTuple):
2093 name: str
2094 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002095
Guido van Rossum2f841442016-11-15 09:48:06 -08002096 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002097
Guido van Rossum2f841442016-11-15 09:48:06 -08002098 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002099
Raymond Hettingerf7b57df2019-03-18 09:53:56 -07002100 The resulting class has an extra __annotations__ attribute, giving a
2101 dict that maps field names to types. (The field names are also in
2102 the _fields attribute, which is part of the namedtuple API.)
2103 Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002104
Guido van Rossum2f841442016-11-15 09:48:06 -08002105 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002106
Guido van Rossum2f841442016-11-15 09:48:06 -08002107 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002108
Guido van Rossum2f841442016-11-15 09:48:06 -08002109 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
2110 """
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002111 if fields is None:
2112 fields = kwargs.items()
2113 elif kwargs:
2114 raise TypeError("Either list of fields or keywords"
2115 " can be provided to NamedTuple, not both")
2116 try:
2117 module = sys._getframe(1).f_globals.get('__name__', '__main__')
2118 except (AttributeError, ValueError):
2119 module = None
2120 return _make_nmtuple(typename, fields, module=module)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002121
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002122_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})
2123
2124def _namedtuple_mro_entries(bases):
2125 if len(bases) > 1:
2126 raise TypeError("Multiple inheritance with NamedTuple is not supported")
2127 assert bases[0] is NamedTuple
2128 return (_NamedTuple,)
2129
2130NamedTuple.__mro_entries__ = _namedtuple_mro_entries
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002131
2132
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002133class _TypedDictMeta(type):
2134 def __new__(cls, name, bases, ns, total=True):
2135 """Create new typed dict class object.
2136
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002137 This method is called when TypedDict is subclassed,
2138 or when TypedDict is instantiated. This way
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002139 TypedDict supports all three syntax forms described in its docstring.
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002140 Subclasses and instances of TypedDict return actual dictionaries.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002141 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002142 for base in bases:
2143 if type(base) is not _TypedDictMeta:
2144 raise TypeError('cannot inherit from both a TypedDict type '
2145 'and a non-TypedDict base class')
2146 tp_dict = type.__new__(_TypedDictMeta, name, (dict,), ns)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002147
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002148 annotations = {}
2149 own_annotations = ns.get('__annotations__', {})
2150 own_annotation_keys = set(own_annotations.keys())
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002151 msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002152 own_annotations = {
2153 n: _type_check(tp, msg) for n, tp in own_annotations.items()
2154 }
2155 required_keys = set()
2156 optional_keys = set()
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002157
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002158 for base in bases:
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002159 annotations.update(base.__dict__.get('__annotations__', {}))
2160 required_keys.update(base.__dict__.get('__required_keys__', ()))
2161 optional_keys.update(base.__dict__.get('__optional_keys__', ()))
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002162
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002163 annotations.update(own_annotations)
2164 if total:
2165 required_keys.update(own_annotation_keys)
2166 else:
2167 optional_keys.update(own_annotation_keys)
2168
2169 tp_dict.__annotations__ = annotations
2170 tp_dict.__required_keys__ = frozenset(required_keys)
2171 tp_dict.__optional_keys__ = frozenset(optional_keys)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002172 if not hasattr(tp_dict, '__total__'):
2173 tp_dict.__total__ = total
2174 return tp_dict
2175
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002176 __call__ = dict # static method
2177
2178 def __subclasscheck__(cls, other):
2179 # Typed dicts are only for static structural subtyping.
2180 raise TypeError('TypedDict does not support instance and class checks')
2181
2182 __instancecheck__ = __subclasscheck__
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002183
2184
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002185def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002186 """A simple typed namespace. At runtime it is equivalent to a plain dict.
2187
2188 TypedDict creates a dictionary type that expects all of its
2189 instances to have a certain set of keys, where each key is
2190 associated with a value of a consistent type. This expectation
2191 is not checked at runtime but is only enforced by type checkers.
2192 Usage::
2193
2194 class Point2D(TypedDict):
2195 x: int
2196 y: int
2197 label: str
2198
2199 a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
2200 b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
2201
2202 assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
2203
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002204 The type info can be accessed via the Point2D.__annotations__ dict, and
2205 the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
2206 TypedDict supports two additional equivalent forms::
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002207
2208 Point2D = TypedDict('Point2D', x=int, y=int, label=str)
2209 Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
2210
ananthan-123ab6423f2020-02-19 10:03:05 +05302211 By default, all keys must be present in a TypedDict. It is possible
2212 to override this by specifying totality.
2213 Usage::
2214
2215 class point2D(TypedDict, total=False):
2216 x: int
2217 y: int
2218
2219 This means that a point2D TypedDict can have any of the keys omitted.A type
2220 checker is only expected to support a literal False or True as the value of
2221 the total argument. True is the default, and makes all items defined in the
2222 class body be required.
2223
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002224 The class syntax is only supported in Python 3.6+, while two other
2225 syntax forms work for Python 2.7 and 3.2+
2226 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002227 if fields is None:
2228 fields = kwargs
2229 elif kwargs:
2230 raise TypeError("TypedDict takes either a dict or keyword arguments,"
2231 " but not both")
2232
Alex Grönholm67b769f2020-12-10 23:49:05 +02002233 ns = {'__annotations__': dict(fields)}
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002234 try:
2235 # Setting correct module is necessary to make typed dict classes pickleable.
2236 ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
2237 except (AttributeError, ValueError):
2238 pass
2239
Alex Grönholm67b769f2020-12-10 23:49:05 +02002240 return _TypedDictMeta(typename, (), ns, total=total)
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002241
2242_TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {})
2243TypedDict.__mro_entries__ = lambda bases: (_TypedDict,)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002244
2245
Guido van Rossum91185fe2016-06-08 11:19:11 -07002246def NewType(name, tp):
2247 """NewType creates simple unique types with almost zero
2248 runtime overhead. NewType(name, tp) is considered a subtype of tp
2249 by static type checkers. At runtime, NewType(name, tp) returns
2250 a dummy function that simply returns its argument. Usage::
2251
2252 UserId = NewType('UserId', int)
2253
2254 def name_by_id(user_id: UserId) -> str:
2255 ...
2256
2257 UserId('user') # Fails type check
2258
2259 name_by_id(42) # Fails type check
2260 name_by_id(UserId(42)) # OK
2261
2262 num = UserId(5) + 1 # type: int
2263 """
2264
2265 def new_type(x):
2266 return x
2267
2268 new_type.__name__ = name
2269 new_type.__supertype__ = tp
2270 return new_type
2271
2272
Guido van Rossum0e0563c2016-04-05 14:54:25 -07002273# Python-version-specific alias (Python 2: unicode; Python 3: str)
2274Text = str
2275
2276
Guido van Rossum91185fe2016-06-08 11:19:11 -07002277# Constant that's True when type checking, but False here.
2278TYPE_CHECKING = False
2279
2280
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002281class IO(Generic[AnyStr]):
2282 """Generic base class for TextIO and BinaryIO.
2283
2284 This is an abstract, generic version of the return of open().
2285
2286 NOTE: This does not distinguish between the different possible
2287 classes (text vs. binary, read vs. write vs. read/write,
2288 append-only, unbuffered). The TextIO and BinaryIO subclasses
2289 below capture the distinctions between text vs. binary, which is
2290 pervasive in the interface; however we currently do not offer a
2291 way to track the other distinctions in the type system.
2292 """
2293
Guido van Rossumd70fe632015-08-05 12:11:06 +02002294 __slots__ = ()
2295
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002296 @property
2297 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002298 def mode(self) -> str:
2299 pass
2300
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002301 @property
2302 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002303 def name(self) -> str:
2304 pass
2305
2306 @abstractmethod
2307 def close(self) -> None:
2308 pass
2309
Shantanu2e6569b2020-01-29 18:52:36 -08002310 @property
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002311 @abstractmethod
2312 def closed(self) -> bool:
2313 pass
2314
2315 @abstractmethod
2316 def fileno(self) -> int:
2317 pass
2318
2319 @abstractmethod
2320 def flush(self) -> None:
2321 pass
2322
2323 @abstractmethod
2324 def isatty(self) -> bool:
2325 pass
2326
2327 @abstractmethod
2328 def read(self, n: int = -1) -> AnyStr:
2329 pass
2330
2331 @abstractmethod
2332 def readable(self) -> bool:
2333 pass
2334
2335 @abstractmethod
2336 def readline(self, limit: int = -1) -> AnyStr:
2337 pass
2338
2339 @abstractmethod
2340 def readlines(self, hint: int = -1) -> List[AnyStr]:
2341 pass
2342
2343 @abstractmethod
2344 def seek(self, offset: int, whence: int = 0) -> int:
2345 pass
2346
2347 @abstractmethod
2348 def seekable(self) -> bool:
2349 pass
2350
2351 @abstractmethod
2352 def tell(self) -> int:
2353 pass
2354
2355 @abstractmethod
2356 def truncate(self, size: int = None) -> int:
2357 pass
2358
2359 @abstractmethod
2360 def writable(self) -> bool:
2361 pass
2362
2363 @abstractmethod
2364 def write(self, s: AnyStr) -> int:
2365 pass
2366
2367 @abstractmethod
2368 def writelines(self, lines: List[AnyStr]) -> None:
2369 pass
2370
2371 @abstractmethod
2372 def __enter__(self) -> 'IO[AnyStr]':
2373 pass
2374
2375 @abstractmethod
2376 def __exit__(self, type, value, traceback) -> None:
2377 pass
2378
2379
2380class BinaryIO(IO[bytes]):
2381 """Typed version of the return of open() in binary mode."""
2382
Guido van Rossumd70fe632015-08-05 12:11:06 +02002383 __slots__ = ()
2384
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002385 @abstractmethod
2386 def write(self, s: Union[bytes, bytearray]) -> int:
2387 pass
2388
2389 @abstractmethod
2390 def __enter__(self) -> 'BinaryIO':
2391 pass
2392
2393
2394class TextIO(IO[str]):
2395 """Typed version of the return of open() in text mode."""
2396
Guido van Rossumd70fe632015-08-05 12:11:06 +02002397 __slots__ = ()
2398
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002399 @property
2400 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002401 def buffer(self) -> BinaryIO:
2402 pass
2403
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002404 @property
2405 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002406 def encoding(self) -> str:
2407 pass
2408
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002409 @property
2410 @abstractmethod
Guido van Rossum991d14f2016-11-09 13:12:51 -08002411 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002412 pass
2413
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002414 @property
2415 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002416 def line_buffering(self) -> bool:
2417 pass
2418
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002419 @property
2420 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002421 def newlines(self) -> Any:
2422 pass
2423
2424 @abstractmethod
2425 def __enter__(self) -> 'TextIO':
2426 pass
2427
2428
2429class io:
2430 """Wrapper namespace for IO generic classes."""
2431
2432 __all__ = ['IO', 'TextIO', 'BinaryIO']
2433 IO = IO
2434 TextIO = TextIO
2435 BinaryIO = BinaryIO
2436
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002437
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002438io.__name__ = __name__ + '.io'
2439sys.modules[io.__name__] = io
2440
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002441Pattern = _alias(stdlib_re.Pattern, 1)
2442Match = _alias(stdlib_re.Match, 1)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002443
2444class re:
2445 """Wrapper namespace for re type aliases."""
2446
2447 __all__ = ['Pattern', 'Match']
2448 Pattern = Pattern
2449 Match = Match
2450
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002451
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002452re.__name__ = __name__ + '.re'
2453sys.modules[re.__name__] = re