blob: 9a3a6a7725a932cde3a1b4da2a084f7177fbe460 [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
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070023import collections
Ivan Levkivskyid911e402018-01-20 11:23:59 +000024import collections.abc
Brett Cannonf3ad0422016-04-15 10:51:30 -070025import contextlib
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070026import functools
Serhiy Storchaka09f32212018-05-26 21:19:26 +030027import operator
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070028import re as stdlib_re # Avoid confusion with the re we export.
29import sys
30import types
Guido van Rossum48b069a2020-04-07 09:50:06 -070031from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType, GenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070032
33# Please keep __all__ alphabetized within each category.
34__all__ = [
35 # Super-special typing primitives.
Jakub Stasiakcf5b1092020-02-05 02:10:19 +010036 'Annotated',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070037 'Any',
38 'Callable',
Guido van Rossum0a6976d2016-09-11 15:34:56 -070039 'ClassVar',
kj73607be2020-12-24 12:33:48 +080040 'Concatenate',
Ivan Levkivskyif3672422019-05-26 09:37:07 +010041 'Final',
Anthony Sottiled30da5d2019-05-29 11:19:38 -070042 'ForwardRef',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070043 'Generic',
Ivan Levkivskyib891c462019-05-26 09:37:48 +010044 'Literal',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070045 'Optional',
kj73607be2020-12-24 12:33:48 +080046 'ParamSpec',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +010047 'Protocol',
Guido van Rossumeb9aca32016-05-24 16:38:22 -070048 'Tuple',
49 'Type',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070050 'TypeVar',
51 'Union',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070052
53 # ABCs (from collections.abc).
54 'AbstractSet', # collections.abc.Set.
55 'ByteString',
56 'Container',
Ivan Levkivskyi29fda8d2017-06-10 21:57:56 +020057 'ContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070058 'Hashable',
59 'ItemsView',
60 'Iterable',
61 'Iterator',
62 'KeysView',
63 'Mapping',
64 'MappingView',
65 'MutableMapping',
66 'MutableSequence',
67 'MutableSet',
68 'Sequence',
69 'Sized',
70 'ValuesView',
Ivan Levkivskyid911e402018-01-20 11:23:59 +000071 'Awaitable',
72 'AsyncIterator',
73 'AsyncIterable',
74 'Coroutine',
75 'Collection',
76 'AsyncGenerator',
77 'AsyncContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070078
79 # Structural checks, a.k.a. protocols.
80 'Reversible',
81 'SupportsAbs',
Ivan Levkivskyif06e0212017-05-02 19:14:07 +020082 'SupportsBytes',
83 'SupportsComplex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070084 'SupportsFloat',
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -070085 'SupportsIndex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070086 'SupportsInt',
87 'SupportsRound',
88
89 # Concrete collection types.
Anthony Sottiled30da5d2019-05-29 11:19:38 -070090 'ChainMap',
Ivan Levkivskyib692dc82017-02-13 22:50:14 +010091 'Counter',
Raymond Hettinger80490522017-01-16 22:42:37 -080092 'Deque',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070093 'Dict',
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070094 'DefaultDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070095 'List',
Anthony Sottiled30da5d2019-05-29 11:19:38 -070096 'OrderedDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070097 'Set',
Guido van Rossumefa798d2016-08-23 11:01:50 -070098 'FrozenSet',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070099 'NamedTuple', # Not really a type.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +0100100 'TypedDict', # Not really a type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700101 'Generator',
102
103 # One-off things.
104 'AnyStr',
105 'cast',
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100106 'final',
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +0100107 'get_args',
108 'get_origin',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700109 'get_type_hints',
Patrick Reader0705ec82020-09-16 05:58:32 +0100110 'is_typeddict',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700111 'NewType',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700112 'no_type_check',
113 'no_type_check_decorator',
aetracht45738202018-03-19 14:41:32 -0400114 'NoReturn',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700115 'overload',
Jelle Zijlstra52243362021-04-10 19:57:05 -0700116 'ParamSpecArgs',
117 'ParamSpecKwargs',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100118 'runtime_checkable',
Guido van Rossum0e0563c2016-04-05 14:54:25 -0700119 'Text',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700120 'TYPE_CHECKING',
Mikhail Golubev4f3c2502020-10-08 00:44:31 +0300121 'TypeAlias',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700122]
123
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700124# The pseudo-submodules 're' and 'io' are part of the public
125# namespace, but excluded from __all__ because they might stomp on
126# legitimate imports of those modules.
127
kj463c7d32020-12-14 02:38:24 +0800128
129def _type_convert(arg):
130 """For converting None to type(None), and strings to ForwardRef."""
131 if arg is None:
132 return type(None)
133 if isinstance(arg, str):
134 return ForwardRef(arg)
135 return arg
136
137
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700138def _type_check(arg, msg, is_argument=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000139 """Check that the argument is a type, and return it (internal helper).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700140
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000141 As a special case, accept None and return type(None) instead. Also wrap strings
142 into ForwardRef instances. Consider several corner cases, for example plain
143 special forms like Union are not valid, while Union[int, str] is OK, etc.
144 The msg argument is a human-readable error message, e.g::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700145
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000146 "Union[arg, ...]: arg should be a type."
Guido van Rossum4cefe742016-09-27 15:20:12 -0700147
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000148 We append the repr() of the actual value (truncated to 100 chars).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700149 """
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100150 invalid_generic_forms = (Generic, Protocol)
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700151 if is_argument:
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100152 invalid_generic_forms = invalid_generic_forms + (ClassVar, Final)
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400153
kj463c7d32020-12-14 02:38:24 +0800154 arg = _type_convert(arg)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000155 if (isinstance(arg, _GenericAlias) and
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400156 arg.__origin__ in invalid_generic_forms):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000157 raise TypeError(f"{arg} is not valid as type argument")
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300158 if arg in (Any, NoReturn):
159 return arg
160 if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000161 raise TypeError(f"Plain {arg} is not valid as type argument")
kj73607be2020-12-24 12:33:48 +0800162 if isinstance(arg, (type, TypeVar, ForwardRef, types.Union, ParamSpec)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000163 return arg
164 if not callable(arg):
165 raise TypeError(f"{msg} Got {arg!r:.100}.")
166 return arg
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700167
168
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000169def _type_repr(obj):
170 """Return the repr() of an object, special-casing types (internal helper).
171
172 If obj is a type, we return a shorter version than the default
173 type.__repr__, based on the module and qualified name, which is
174 typically enough to uniquely identify a type. For everything
175 else, we fall back on repr(obj).
176 """
kj1f7dfb22020-11-02 02:13:38 +0800177 if isinstance(obj, types.GenericAlias):
178 return repr(obj)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000179 if isinstance(obj, type):
180 if obj.__module__ == 'builtins':
181 return obj.__qualname__
182 return f'{obj.__module__}.{obj.__qualname__}'
183 if obj is ...:
184 return('...')
185 if isinstance(obj, types.FunctionType):
186 return obj.__name__
187 return repr(obj)
188
189
190def _collect_type_vars(types):
kj73607be2020-12-24 12:33:48 +0800191 """Collect all type variable-like variables contained
192 in types in order of first appearance (lexicographic order). For example::
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000193
194 _collect_type_vars((T, List[S, T])) == (T, S)
195 """
196 tvars = []
197 for t in types:
kj73607be2020-12-24 12:33:48 +0800198 if isinstance(t, _TypeVarLike) and t not in tvars:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000199 tvars.append(t)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300200 if isinstance(t, (_GenericAlias, GenericAlias)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000201 tvars.extend([t for t in t.__parameters__ if t not in tvars])
202 return tuple(tvars)
203
204
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300205def _check_generic(cls, parameters, elen):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000206 """Check correct count for parameters of a generic cls (internal helper).
207 This gives a nice error message in case of count mismatch.
208 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300209 if not elen:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000210 raise TypeError(f"{cls} is not a generic class")
211 alen = len(parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000212 if alen != elen:
213 raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
214 f" actual {alen}, expected {elen}")
215
kj73607be2020-12-24 12:33:48 +0800216def _prepare_paramspec_params(cls, params):
217 """Prepares the parameters for a Generic containing ParamSpec
218 variables (internal helper).
219 """
220 # Special case where Z[[int, str, bool]] == Z[int, str, bool] in PEP 612.
221 if len(cls.__parameters__) == 1 and len(params) > 1:
222 return (params,)
223 else:
224 _params = []
225 # Convert lists to tuples to help other libraries cache the results.
226 for p, tvar in zip(params, cls.__parameters__):
227 if isinstance(tvar, ParamSpec) and isinstance(p, list):
228 p = tuple(p)
229 _params.append(p)
230 return tuple(_params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000231
Yurii Karabasf03d3182020-11-17 04:23:19 +0200232def _deduplicate(params):
233 # Weed out strict duplicates, preserving the first of each occurrence.
234 all_params = set(params)
235 if len(all_params) < len(params):
236 new_params = []
237 for t in params:
238 if t in all_params:
239 new_params.append(t)
240 all_params.remove(t)
241 params = new_params
242 assert not all_params, all_params
243 return params
244
245
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000246def _remove_dups_flatten(parameters):
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700247 """An internal helper for Union creation and substitution: flatten Unions
248 among parameters, then remove duplicates.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000249 """
250 # Flatten out Union[Union[...], ...].
251 params = []
252 for p in parameters:
Maggie Moss1b4552c2020-09-09 13:23:24 -0700253 if isinstance(p, (_UnionGenericAlias, types.Union)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000254 params.extend(p.__args__)
255 elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
256 params.extend(p[1:])
257 else:
258 params.append(p)
Yurii Karabasf03d3182020-11-17 04:23:19 +0200259
260 return tuple(_deduplicate(params))
261
262
263def _flatten_literal_params(parameters):
264 """An internal helper for Literal creation: flatten Literals among parameters"""
265 params = []
266 for p in parameters:
267 if isinstance(p, _LiteralGenericAlias):
268 params.extend(p.__args__)
269 else:
270 params.append(p)
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700271 return tuple(params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000272
273
274_cleanups = []
275
276
Yurii Karabasf03d3182020-11-17 04:23:19 +0200277def _tp_cache(func=None, /, *, typed=False):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000278 """Internal wrapper caching __getitem__ of generic types with a fallback to
279 original function for non-hashable arguments.
280 """
Yurii Karabasf03d3182020-11-17 04:23:19 +0200281 def decorator(func):
282 cached = functools.lru_cache(typed=typed)(func)
283 _cleanups.append(cached.cache_clear)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000284
Yurii Karabasf03d3182020-11-17 04:23:19 +0200285 @functools.wraps(func)
286 def inner(*args, **kwds):
287 try:
288 return cached(*args, **kwds)
289 except TypeError:
290 pass # All real errors (not unhashable args) are raised below.
291 return func(*args, **kwds)
292 return inner
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000293
Yurii Karabasf03d3182020-11-17 04:23:19 +0200294 if func is not None:
295 return decorator(func)
296
297 return decorator
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000298
wyfo653f4202020-07-22 21:47:28 +0200299def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
Graham Bleaney84ef33c2020-09-08 18:41:10 -0400300 """Evaluate all forward references in the given type t.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000301 For use of globalns and localns see the docstring for get_type_hints().
wyfo653f4202020-07-22 21:47:28 +0200302 recursive_guard is used to prevent prevent infinite recursion
303 with recursive ForwardRef.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000304 """
305 if isinstance(t, ForwardRef):
wyfo653f4202020-07-22 21:47:28 +0200306 return t._evaluate(globalns, localns, recursive_guard)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300307 if isinstance(t, (_GenericAlias, GenericAlias)):
wyfo653f4202020-07-22 21:47:28 +0200308 ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000309 if ev_args == t.__args__:
310 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300311 if isinstance(t, GenericAlias):
312 return GenericAlias(t.__origin__, ev_args)
313 else:
314 return t.copy_with(ev_args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000315 return t
316
317
318class _Final:
319 """Mixin to prohibit subclassing"""
Guido van Rossum4cefe742016-09-27 15:20:12 -0700320
Guido van Rossum83ec3022017-01-17 20:43:28 -0800321 __slots__ = ('__weakref__',)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700322
Serhiy Storchaka2085bd02019-06-01 11:00:15 +0300323 def __init_subclass__(self, /, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000324 if '_root' not in kwds:
325 raise TypeError("Cannot subclass special typing classes")
326
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100327class _Immutable:
328 """Mixin to indicate that object should not be copied."""
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300329 __slots__ = ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000330
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100331 def __copy__(self):
332 return self
333
334 def __deepcopy__(self, memo):
335 return self
336
337
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300338# Internal indicator of special typing constructs.
339# See __doc__ instance attribute for specific docs.
340class _SpecialForm(_Final, _root=True):
341 __slots__ = ('_name', '__doc__', '_getitem')
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000342
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300343 def __init__(self, getitem):
344 self._getitem = getitem
345 self._name = getitem.__name__
346 self.__doc__ = getitem.__doc__
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000347
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300348 def __mro_entries__(self, bases):
349 raise TypeError(f"Cannot subclass {self!r}")
Guido van Rossum4cefe742016-09-27 15:20:12 -0700350
351 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000352 return 'typing.' + self._name
353
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100354 def __reduce__(self):
355 return self._name
Guido van Rossum4cefe742016-09-27 15:20:12 -0700356
357 def __call__(self, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000358 raise TypeError(f"Cannot instantiate {self!r}")
359
360 def __instancecheck__(self, obj):
361 raise TypeError(f"{self} cannot be used with isinstance()")
362
363 def __subclasscheck__(self, cls):
364 raise TypeError(f"{self} cannot be used with issubclass()")
365
366 @_tp_cache
367 def __getitem__(self, parameters):
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300368 return self._getitem(self, parameters)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700369
Yurii Karabasf03d3182020-11-17 04:23:19 +0200370
371class _LiteralSpecialForm(_SpecialForm, _root=True):
372 @_tp_cache(typed=True)
373 def __getitem__(self, parameters):
374 return self._getitem(self, parameters)
375
376
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300377@_SpecialForm
378def Any(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000379 """Special type indicating an unconstrained type.
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700380
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000381 - Any is compatible with every type.
382 - Any assumed to have all methods.
383 - All values assumed to be instances of Any.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700384
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000385 Note that all the above statements are true from the point of view of
386 static type checkers. At runtime, Any should not be used with instance
387 or class checks.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300388 """
389 raise TypeError(f"{self} is not subscriptable")
Guido van Rossumd70fe632015-08-05 12:11:06 +0200390
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300391@_SpecialForm
392def NoReturn(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000393 """Special type indicating functions that never return.
394 Example::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700395
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000396 from typing import NoReturn
397
398 def stop() -> NoReturn:
399 raise Exception('no way')
400
401 This type is invalid in other positions, e.g., ``List[NoReturn]``
402 will fail in static type checkers.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300403 """
404 raise TypeError(f"{self} is not subscriptable")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000405
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300406@_SpecialForm
407def ClassVar(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000408 """Special type construct to mark class variables.
409
410 An annotation wrapped in ClassVar indicates that a given
411 attribute is intended to be used as a class variable and
412 should not be set on instances of that class. Usage::
413
414 class Starship:
415 stats: ClassVar[Dict[str, int]] = {} # class variable
416 damage: int = 10 # instance variable
417
418 ClassVar accepts only types and cannot be further subscribed.
419
420 Note that ClassVar is not a class itself, and should not
421 be used with isinstance() or issubclass().
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300422 """
423 item = _type_check(parameters, f'{self} accepts only single type.')
424 return _GenericAlias(self, (item,))
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000425
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300426@_SpecialForm
427def Final(self, parameters):
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100428 """Special typing construct to indicate final names to type checkers.
429
430 A final name cannot be re-assigned or overridden in a subclass.
431 For example:
432
433 MAX_SIZE: Final = 9000
434 MAX_SIZE += 1 # Error reported by type checker
435
436 class Connection:
437 TIMEOUT: Final[int] = 10
438
439 class FastConnector(Connection):
440 TIMEOUT = 1 # Error reported by type checker
441
442 There is no runtime checking of these properties.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300443 """
444 item = _type_check(parameters, f'{self} accepts only single type.')
445 return _GenericAlias(self, (item,))
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100446
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300447@_SpecialForm
448def Union(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000449 """Union type; Union[X, Y] means either X or Y.
450
451 To define a union, use e.g. Union[int, str]. Details:
452 - The arguments must be types and there must be at least one.
453 - None as an argument is a special case and is replaced by
454 type(None).
455 - Unions of unions are flattened, e.g.::
456
457 Union[Union[int, str], float] == Union[int, str, float]
458
459 - Unions of a single argument vanish, e.g.::
460
461 Union[int] == int # The constructor actually returns int
462
463 - Redundant arguments are skipped, e.g.::
464
465 Union[int, str, int] == Union[int, str]
466
467 - When comparing unions, the argument order is ignored, e.g.::
468
469 Union[int, str] == Union[str, int]
470
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000471 - You cannot subclass or instantiate a union.
472 - You can use Optional[X] as a shorthand for Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300473 """
474 if parameters == ():
475 raise TypeError("Cannot take a Union of no types.")
476 if not isinstance(parameters, tuple):
477 parameters = (parameters,)
478 msg = "Union[arg, ...]: each arg must be a type."
479 parameters = tuple(_type_check(p, msg) for p in parameters)
480 parameters = _remove_dups_flatten(parameters)
481 if len(parameters) == 1:
482 return parameters[0]
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300483 return _UnionGenericAlias(self, parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000484
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300485@_SpecialForm
486def Optional(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000487 """Optional type.
488
489 Optional[X] is equivalent to Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300490 """
491 arg = _type_check(parameters, f"{self} requires a single type.")
492 return Union[arg, type(None)]
Guido van Rossumb7dedc82016-10-29 12:44:29 -0700493
Yurii Karabasf03d3182020-11-17 04:23:19 +0200494@_LiteralSpecialForm
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300495def Literal(self, parameters):
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100496 """Special typing form to define literal types (a.k.a. value types).
497
498 This form can be used to indicate to type checkers that the corresponding
499 variable or function parameter has a value equivalent to the provided
500 literal (or one of several literals):
501
502 def validate_simple(data: Any) -> Literal[True]: # always returns True
503 ...
504
505 MODE = Literal['r', 'rb', 'w', 'wb']
506 def open_helper(file: str, mode: MODE) -> str:
507 ...
508
509 open_helper('/some/path', 'r') # Passes type check
510 open_helper('/other/path', 'typo') # Error in type checker
511
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300512 Literal[...] cannot be subclassed. At runtime, an arbitrary value
513 is allowed as type argument to Literal[...], but type checkers may
514 impose restrictions.
515 """
516 # There is no '_type_check' call because arguments to Literal[...] are
517 # values, not types.
Yurii Karabasf03d3182020-11-17 04:23:19 +0200518 if not isinstance(parameters, tuple):
519 parameters = (parameters,)
520
521 parameters = _flatten_literal_params(parameters)
522
523 try:
524 parameters = tuple(p for p, _ in _deduplicate(list(_value_and_type_iter(parameters))))
525 except TypeError: # unhashable parameters
526 pass
527
528 return _LiteralGenericAlias(self, parameters)
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100529
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700530
Mikhail Golubev4f3c2502020-10-08 00:44:31 +0300531@_SpecialForm
532def TypeAlias(self, parameters):
533 """Special marker indicating that an assignment should
534 be recognized as a proper type alias definition by type
535 checkers.
536
537 For example::
538
539 Predicate: TypeAlias = Callable[..., bool]
540
541 It's invalid when used anywhere except as in the example above.
542 """
543 raise TypeError(f"{self} is not subscriptable")
544
545
kj73607be2020-12-24 12:33:48 +0800546@_SpecialForm
547def Concatenate(self, parameters):
Ken Jin11276cd2021-01-02 08:45:50 +0800548 """Used in conjunction with ``ParamSpec`` and ``Callable`` to represent a
549 higher order function which adds, removes or transforms parameters of a
550 callable.
kj73607be2020-12-24 12:33:48 +0800551
552 For example::
553
554 Callable[Concatenate[int, P], int]
555
556 See PEP 612 for detailed information.
557 """
558 if parameters == ():
559 raise TypeError("Cannot take a Concatenate of no types.")
560 if not isinstance(parameters, tuple):
561 parameters = (parameters,)
562 if not isinstance(parameters[-1], ParamSpec):
563 raise TypeError("The last parameter to Concatenate should be a "
564 "ParamSpec variable.")
565 msg = "Concatenate[arg, ...]: each arg must be a type."
566 parameters = tuple(_type_check(p, msg) for p in parameters)
567 return _ConcatenateGenericAlias(self, parameters)
568
569
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000570class ForwardRef(_Final, _root=True):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800571 """Internal wrapper to hold a forward reference."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700572
Guido van Rossum4cefe742016-09-27 15:20:12 -0700573 __slots__ = ('__forward_arg__', '__forward_code__',
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400574 '__forward_evaluated__', '__forward_value__',
575 '__forward_is_argument__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700576
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700577 def __init__(self, arg, is_argument=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700578 if not isinstance(arg, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000579 raise TypeError(f"Forward reference must be a string -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700580 try:
581 code = compile(arg, '<string>', 'eval')
582 except SyntaxError:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000583 raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700584 self.__forward_arg__ = arg
585 self.__forward_code__ = code
586 self.__forward_evaluated__ = False
587 self.__forward_value__ = None
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400588 self.__forward_is_argument__ = is_argument
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700589
wyfo653f4202020-07-22 21:47:28 +0200590 def _evaluate(self, globalns, localns, recursive_guard):
591 if self.__forward_arg__ in recursive_guard:
592 return self
Guido van Rossumdad17902016-11-10 08:29:18 -0800593 if not self.__forward_evaluated__ or localns is not globalns:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700594 if globalns is None and localns is None:
595 globalns = localns = {}
596 elif globalns is None:
597 globalns = localns
598 elif localns is None:
599 localns = globalns
wyfo653f4202020-07-22 21:47:28 +0200600 type_ =_type_check(
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700601 eval(self.__forward_code__, globalns, localns),
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400602 "Forward references must evaluate to types.",
wyfo653f4202020-07-22 21:47:28 +0200603 is_argument=self.__forward_is_argument__,
604 )
605 self.__forward_value__ = _eval_type(
606 type_, globalns, localns, recursive_guard | {self.__forward_arg__}
607 )
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700608 self.__forward_evaluated__ = True
609 return self.__forward_value__
610
Guido van Rossum4cefe742016-09-27 15:20:12 -0700611 def __eq__(self, other):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000612 if not isinstance(other, ForwardRef):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700613 return NotImplemented
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100614 if self.__forward_evaluated__ and other.__forward_evaluated__:
615 return (self.__forward_arg__ == other.__forward_arg__ and
616 self.__forward_value__ == other.__forward_value__)
617 return self.__forward_arg__ == other.__forward_arg__
Guido van Rossum4cefe742016-09-27 15:20:12 -0700618
619 def __hash__(self):
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100620 return hash(self.__forward_arg__)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700621
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700622 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000623 return f'ForwardRef({self.__forward_arg__!r})'
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700624
kj73607be2020-12-24 12:33:48 +0800625class _TypeVarLike:
626 """Mixin for TypeVar-like types (TypeVar and ParamSpec)."""
627 def __init__(self, bound, covariant, contravariant):
628 """Used to setup TypeVars and ParamSpec's bound, covariant and
629 contravariant attributes.
630 """
631 if covariant and contravariant:
632 raise ValueError("Bivariant types are not supported.")
633 self.__covariant__ = bool(covariant)
634 self.__contravariant__ = bool(contravariant)
635 if bound:
636 self.__bound__ = _type_check(bound, "Bound must be a type.")
637 else:
638 self.__bound__ = None
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700639
kj73607be2020-12-24 12:33:48 +0800640 def __or__(self, right):
641 return Union[self, right]
642
Jelle Zijlstra90459192021-04-10 20:00:05 -0700643 def __ror__(self, left):
644 return Union[left, self]
kj73607be2020-12-24 12:33:48 +0800645
646 def __repr__(self):
647 if self.__covariant__:
648 prefix = '+'
649 elif self.__contravariant__:
650 prefix = '-'
651 else:
652 prefix = '~'
653 return prefix + self.__name__
654
655 def __reduce__(self):
656 return self.__name__
657
658
659class TypeVar( _Final, _Immutable, _TypeVarLike, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700660 """Type variable.
661
662 Usage::
663
664 T = TypeVar('T') # Can be anything
665 A = TypeVar('A', str, bytes) # Must be str or bytes
666
667 Type variables exist primarily for the benefit of static type
668 checkers. They serve as the parameters for generic types as well
669 as for generic function definitions. See class Generic for more
670 information on generic types. Generic functions work as follows:
671
Guido van Rossumb24569a2016-11-20 18:01:29 -0800672 def repeat(x: T, n: int) -> List[T]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700673 '''Return a list containing n references to x.'''
674 return [x]*n
675
676 def longest(x: A, y: A) -> A:
677 '''Return the longest of two strings.'''
678 return x if len(x) >= len(y) else y
679
680 The latter example's signature is essentially the overloading
681 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
682 that if the arguments are instances of some subclass of str,
683 the return type is still plain str.
684
Guido van Rossumb24569a2016-11-20 18:01:29 -0800685 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700686
Guido van Rossumefa798d2016-08-23 11:01:50 -0700687 Type variables defined with covariant=True or contravariant=True
João D. Ferreira86bfed32018-07-07 16:41:20 +0100688 can be used to declare covariant or contravariant generic types.
Guido van Rossumefa798d2016-08-23 11:01:50 -0700689 See PEP 484 for more details. By default generic types are invariant
690 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700691
692 Type variables can be introspected. e.g.:
693
694 T.__name__ == 'T'
695 T.__constraints__ == ()
696 T.__covariant__ == False
697 T.__contravariant__ = False
698 A.__constraints__ == (str, bytes)
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100699
700 Note that only type variables defined in global scope can be pickled.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700701 """
702
Guido van Rossum4cefe742016-09-27 15:20:12 -0700703 __slots__ = ('__name__', '__bound__', '__constraints__',
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300704 '__covariant__', '__contravariant__', '__dict__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700705
706 def __init__(self, name, *constraints, bound=None,
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800707 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700708 self.__name__ = name
kj73607be2020-12-24 12:33:48 +0800709 super().__init__(bound, covariant, contravariant)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700710 if constraints and bound is not None:
711 raise TypeError("Constraints cannot be combined with bound=...")
712 if constraints and len(constraints) == 1:
713 raise TypeError("A single constraint is not allowed")
714 msg = "TypeVar(name, constraint, ...): constraints must be types."
715 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
HongWeipenga25a04f2020-04-21 04:01:53 +0800716 try:
717 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling
718 except (AttributeError, ValueError):
719 def_mod = None
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300720 if def_mod != 'typing':
721 self.__module__ = def_mod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700722
Maggie Moss1b4552c2020-09-09 13:23:24 -0700723
Jelle Zijlstra52243362021-04-10 19:57:05 -0700724class ParamSpecArgs(_Final, _Immutable, _root=True):
725 """The args for a ParamSpec object.
726
727 Given a ParamSpec object P, P.args is an instance of ParamSpecArgs.
728
729 ParamSpecArgs objects have a reference back to their ParamSpec:
730
731 P.args.__origin__ is P
732
733 This type is meant for runtime introspection and has no special meaning to
734 static type checkers.
735 """
736 def __init__(self, origin):
737 self.__origin__ = origin
738
739 def __repr__(self):
740 return f"{self.__origin__.__name__}.args"
741
742
743class ParamSpecKwargs(_Final, _Immutable, _root=True):
744 """The kwargs for a ParamSpec object.
745
746 Given a ParamSpec object P, P.kwargs is an instance of ParamSpecKwargs.
747
748 ParamSpecKwargs objects have a reference back to their ParamSpec:
749
750 P.kwargs.__origin__ is P
751
752 This type is meant for runtime introspection and has no special meaning to
753 static type checkers.
754 """
755 def __init__(self, origin):
756 self.__origin__ = origin
757
758 def __repr__(self):
759 return f"{self.__origin__.__name__}.kwargs"
760
761
kj73607be2020-12-24 12:33:48 +0800762class ParamSpec(_Final, _Immutable, _TypeVarLike, _root=True):
763 """Parameter specification variable.
Maggie Moss1b4552c2020-09-09 13:23:24 -0700764
kj73607be2020-12-24 12:33:48 +0800765 Usage::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700766
kj73607be2020-12-24 12:33:48 +0800767 P = ParamSpec('P')
768
769 Parameter specification variables exist primarily for the benefit of static
770 type checkers. They are used to forward the parameter types of one
Ken Jin11276cd2021-01-02 08:45:50 +0800771 callable to another callable, a pattern commonly found in higher order
772 functions and decorators. They are only valid when used in ``Concatenate``,
773 or s the first argument to ``Callable``, or as parameters for user-defined
774 Generics. See class Generic for more information on generic types. An
775 example for annotating a decorator::
kj73607be2020-12-24 12:33:48 +0800776
777 T = TypeVar('T')
778 P = ParamSpec('P')
779
780 def add_logging(f: Callable[P, T]) -> Callable[P, T]:
781 '''A type-safe decorator to add logging to a function.'''
782 def inner(*args: P.args, **kwargs: P.kwargs) -> T:
783 logging.info(f'{f.__name__} was called')
784 return f(*args, **kwargs)
785 return inner
786
787 @add_logging
788 def add_two(x: float, y: float) -> float:
789 '''Add two numbers together.'''
790 return x + y
791
792 Parameter specification variables defined with covariant=True or
793 contravariant=True can be used to declare covariant or contravariant
794 generic types. These keyword arguments are valid, but their actual semantics
795 are yet to be decided. See PEP 612 for details.
796
797 Parameter specification variables can be introspected. e.g.:
798
799 P.__name__ == 'T'
800 P.__bound__ == None
801 P.__covariant__ == False
802 P.__contravariant__ == False
803
804 Note that only parameter specification variables defined in global scope can
805 be pickled.
806 """
807
808 __slots__ = ('__name__', '__bound__', '__covariant__', '__contravariant__',
809 '__dict__')
810
Jelle Zijlstra52243362021-04-10 19:57:05 -0700811 @property
812 def args(self):
813 return ParamSpecArgs(self)
814
815 @property
816 def kwargs(self):
817 return ParamSpecKwargs(self)
kj73607be2020-12-24 12:33:48 +0800818
Ken Jinace008c2021-01-11 08:11:41 +0800819 def __init__(self, name, *, bound=None, covariant=False, contravariant=False):
kj73607be2020-12-24 12:33:48 +0800820 self.__name__ = name
821 super().__init__(bound, covariant, contravariant)
822 try:
823 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__')
824 except (AttributeError, ValueError):
825 def_mod = None
826 if def_mod != 'typing':
827 self.__module__ = def_mod
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100828
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700829
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000830def _is_dunder(attr):
831 return attr.startswith('__') and attr.endswith('__')
Guido van Rossum83ec3022017-01-17 20:43:28 -0800832
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300833class _BaseGenericAlias(_Final, _root=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000834 """The central part of internal API.
835
836 This represents a generic version of type 'origin' with type arguments 'params'.
837 There are two kind of these aliases: user defined and special. The special ones
838 are wrappers around builtin collections and ABCs in collections.abc. These must
839 have 'name' always set. If 'inst' is False, then the alias can't be instantiated,
840 this is used by e.g. typing.List and typing.Dict.
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700841 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300842 def __init__(self, origin, *, inst=True, name=None):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000843 self._inst = inst
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000844 self._name = name
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700845 self.__origin__ = origin
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000846 self.__slots__ = None # This is not documented.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300847
848 def __call__(self, *args, **kwargs):
849 if not self._inst:
850 raise TypeError(f"Type {self._name} cannot be instantiated; "
851 f"use {self.__origin__.__name__}() instead")
852 result = self.__origin__(*args, **kwargs)
853 try:
854 result.__orig_class__ = self
855 except AttributeError:
856 pass
857 return result
858
859 def __mro_entries__(self, bases):
860 res = []
861 if self.__origin__ not in bases:
862 res.append(self.__origin__)
863 i = bases.index(self)
864 for b in bases[i+1:]:
865 if isinstance(b, _BaseGenericAlias) or issubclass(b, Generic):
866 break
867 else:
868 res.append(Generic)
869 return tuple(res)
870
871 def __getattr__(self, attr):
872 # We are careful for copy and pickle.
873 # Also for simplicity we just don't relay all dunder names
874 if '__origin__' in self.__dict__ and not _is_dunder(attr):
875 return getattr(self.__origin__, attr)
876 raise AttributeError(attr)
877
878 def __setattr__(self, attr, val):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300879 if _is_dunder(attr) or attr in ('_name', '_inst', '_nparams'):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300880 super().__setattr__(attr, val)
881 else:
882 setattr(self.__origin__, attr, val)
883
884 def __instancecheck__(self, obj):
885 return self.__subclasscheck__(type(obj))
886
887 def __subclasscheck__(self, cls):
888 raise TypeError("Subscripted generics cannot be used with"
889 " class and instance checks")
890
891
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300892# Special typing constructs Union, Optional, Generic, Callable and Tuple
893# use three special attributes for internal bookkeeping of generic types:
894# * __parameters__ is a tuple of unique free type parameters of a generic
895# type, for example, Dict[T, T].__parameters__ == (T,);
896# * __origin__ keeps a reference to a type that was subscripted,
897# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
898# the type.
899# * __args__ is a tuple of all arguments used in subscripting,
900# e.g., Dict[T, int].__args__ == (T, int).
901
902
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300903class _GenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300904 def __init__(self, origin, params, *, inst=True, name=None):
905 super().__init__(origin, inst=inst, name=name)
906 if not isinstance(params, tuple):
907 params = (params,)
908 self.__args__ = tuple(... if a is _TypingEllipsis else
909 () if a is _TypingEmpty else
910 a for a in params)
911 self.__parameters__ = _collect_type_vars(params)
912 if not name:
913 self.__module__ = origin.__module__
914
915 def __eq__(self, other):
916 if not isinstance(other, _GenericAlias):
917 return NotImplemented
918 return (self.__origin__ == other.__origin__
919 and self.__args__ == other.__args__)
920
921 def __hash__(self):
922 return hash((self.__origin__, self.__args__))
923
Maggie Moss1b4552c2020-09-09 13:23:24 -0700924 def __or__(self, right):
925 return Union[self, right]
926
927 def __ror__(self, right):
928 return Union[self, right]
929
Guido van Rossum4cefe742016-09-27 15:20:12 -0700930 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700931 def __getitem__(self, params):
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100932 if self.__origin__ in (Generic, Protocol):
933 # Can't subscript Generic[...] or Protocol[...].
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000934 raise TypeError(f"Cannot subscript already-subscripted {self}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700935 if not isinstance(params, tuple):
936 params = (params,)
kj73607be2020-12-24 12:33:48 +0800937 params = tuple(_type_convert(p) for p in params)
938 if any(isinstance(t, ParamSpec) for t in self.__parameters__):
939 params = _prepare_paramspec_params(self, params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300940 _check_generic(self, params, len(self.__parameters__))
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300941
942 subst = dict(zip(self.__parameters__, params))
943 new_args = []
944 for arg in self.__args__:
kj73607be2020-12-24 12:33:48 +0800945 if isinstance(arg, _TypeVarLike):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300946 arg = subst[arg]
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300947 elif isinstance(arg, (_GenericAlias, GenericAlias)):
Serhiy Storchaka0122d482020-05-10 13:39:40 +0300948 subparams = arg.__parameters__
949 if subparams:
950 subargs = tuple(subst[x] for x in subparams)
951 arg = arg[subargs]
kj73607be2020-12-24 12:33:48 +0800952 # Required to flatten out the args for CallableGenericAlias
953 if self.__origin__ == collections.abc.Callable and isinstance(arg, tuple):
954 new_args.extend(arg)
955 else:
956 new_args.append(arg)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300957 return self.copy_with(tuple(new_args))
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100958
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000959 def copy_with(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300960 return self.__class__(self.__origin__, params, name=self._name, inst=self._inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700961
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000962 def __repr__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300963 if self._name:
964 name = 'typing.' + self._name
965 else:
966 name = _type_repr(self.__origin__)
967 args = ", ".join([_type_repr(a) for a in self.__args__])
968 return f'{name}[{args}]'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000969
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300970 def __reduce__(self):
971 if self._name:
972 origin = globals()[self._name]
973 else:
974 origin = self.__origin__
975 args = tuple(self.__args__)
976 if len(args) == 1 and not isinstance(args[0], tuple):
977 args, = args
978 return operator.getitem, (origin, args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000979
980 def __mro_entries__(self, bases):
981 if self._name: # generic version of an ABC or built-in class
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300982 return super().__mro_entries__(bases)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000983 if self.__origin__ is Generic:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100984 if Protocol in bases:
985 return ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000986 i = bases.index(self)
987 for b in bases[i+1:]:
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300988 if isinstance(b, _BaseGenericAlias) and b is not self:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000989 return ()
990 return (self.__origin__,)
991
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000992
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300993# _nparams is the number of accepted parameters, e.g. 0 for Hashable,
994# 1 for List and 2 for Dict. It may be -1 if variable number of
995# parameters are accepted (needs custom __getitem__).
996
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300997class _SpecialGenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300998 def __init__(self, origin, nparams, *, inst=True, name=None):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300999 if name is None:
1000 name = origin.__name__
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001001 super().__init__(origin, inst=inst, name=name)
1002 self._nparams = nparams
Serhiy Storchaka2fbc57a2020-05-10 15:14:27 +03001003 if origin.__module__ == 'builtins':
1004 self.__doc__ = f'A generic version of {origin.__qualname__}.'
1005 else:
1006 self.__doc__ = f'A generic version of {origin.__module__}.{origin.__qualname__}.'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001007
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001008 @_tp_cache
1009 def __getitem__(self, params):
1010 if not isinstance(params, tuple):
1011 params = (params,)
1012 msg = "Parameters to generic types must be types."
1013 params = tuple(_type_check(p, msg) for p in params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001014 _check_generic(self, params, self._nparams)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001015 return self.copy_with(params)
1016
1017 def copy_with(self, params):
1018 return _GenericAlias(self.__origin__, params,
1019 name=self._name, inst=self._inst)
1020
1021 def __repr__(self):
1022 return 'typing.' + self._name
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001023
1024 def __subclasscheck__(self, cls):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001025 if isinstance(cls, _SpecialGenericAlias):
1026 return issubclass(cls.__origin__, self.__origin__)
1027 if not isinstance(cls, _GenericAlias):
1028 return issubclass(cls, self.__origin__)
1029 return super().__subclasscheck__(cls)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001030
Ivan Levkivskyi83494032018-03-26 23:01:12 +01001031 def __reduce__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001032 return self._name
Ivan Levkivskyi83494032018-03-26 23:01:12 +01001033
Maggie Moss1b4552c2020-09-09 13:23:24 -07001034 def __or__(self, right):
1035 return Union[self, right]
1036
1037 def __ror__(self, right):
1038 return Union[self, right]
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001039
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001040class _CallableGenericAlias(_GenericAlias, _root=True):
1041 def __repr__(self):
1042 assert self._name == 'Callable'
kj73607be2020-12-24 12:33:48 +08001043 args = self.__args__
1044 if len(args) == 2 and (args[0] is Ellipsis
1045 or isinstance(args[0], (ParamSpec, _ConcatenateGenericAlias))):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001046 return super().__repr__()
1047 return (f'typing.Callable'
kj73607be2020-12-24 12:33:48 +08001048 f'[[{", ".join([_type_repr(a) for a in args[:-1]])}], '
1049 f'{_type_repr(args[-1])}]')
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001050
1051 def __reduce__(self):
1052 args = self.__args__
kj73607be2020-12-24 12:33:48 +08001053 if not (len(args) == 2 and (args[0] is Ellipsis
1054 or isinstance(args[0], (ParamSpec, _ConcatenateGenericAlias)))):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001055 args = list(args[:-1]), args[-1]
1056 return operator.getitem, (Callable, args)
1057
1058
1059class _CallableType(_SpecialGenericAlias, _root=True):
1060 def copy_with(self, params):
1061 return _CallableGenericAlias(self.__origin__, params,
1062 name=self._name, inst=self._inst)
1063
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001064 def __getitem__(self, params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001065 if not isinstance(params, tuple) or len(params) != 2:
1066 raise TypeError("Callable must be used as "
1067 "Callable[[arg, ...], result].")
1068 args, result = params
kj463c7d32020-12-14 02:38:24 +08001069 # This relaxes what args can be on purpose to allow things like
1070 # PEP 612 ParamSpec. Responsibility for whether a user is using
1071 # Callable[...] properly is deferred to static type checkers.
1072 if isinstance(args, list):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001073 params = (tuple(args), result)
kj463c7d32020-12-14 02:38:24 +08001074 else:
1075 params = (args, result)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001076 return self.__getitem_inner__(params)
1077
1078 @_tp_cache
1079 def __getitem_inner__(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001080 args, result = params
1081 msg = "Callable[args, result]: result must be a type."
1082 result = _type_check(result, msg)
1083 if args is Ellipsis:
1084 return self.copy_with((_TypingEllipsis, result))
kj463c7d32020-12-14 02:38:24 +08001085 if not isinstance(args, tuple):
1086 args = (args,)
1087 args = tuple(_type_convert(arg) for arg in args)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001088 params = args + (result,)
1089 return self.copy_with(params)
1090
1091
1092class _TupleType(_SpecialGenericAlias, _root=True):
1093 @_tp_cache
1094 def __getitem__(self, params):
1095 if params == ():
1096 return self.copy_with((_TypingEmpty,))
1097 if not isinstance(params, tuple):
1098 params = (params,)
1099 if len(params) == 2 and params[1] is ...:
1100 msg = "Tuple[t, ...]: t must be a type."
1101 p = _type_check(params[0], msg)
1102 return self.copy_with((p, _TypingEllipsis))
1103 msg = "Tuple[t0, t1, ...]: each t must be a type."
1104 params = tuple(_type_check(p, msg) for p in params)
1105 return self.copy_with(params)
1106
1107
1108class _UnionGenericAlias(_GenericAlias, _root=True):
1109 def copy_with(self, params):
1110 return Union[params]
1111
1112 def __eq__(self, other):
1113 if not isinstance(other, _UnionGenericAlias):
1114 return NotImplemented
1115 return set(self.__args__) == set(other.__args__)
1116
1117 def __hash__(self):
1118 return hash(frozenset(self.__args__))
1119
1120 def __repr__(self):
1121 args = self.__args__
1122 if len(args) == 2:
1123 if args[0] is type(None):
1124 return f'typing.Optional[{_type_repr(args[1])}]'
1125 elif args[1] is type(None):
1126 return f'typing.Optional[{_type_repr(args[0])}]'
1127 return super().__repr__()
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001128
Maggie Moss1b4552c2020-09-09 13:23:24 -07001129 def __instancecheck__(self, obj):
1130 return self.__subclasscheck__(type(obj))
1131
1132 def __subclasscheck__(self, cls):
1133 for arg in self.__args__:
1134 if issubclass(cls, arg):
1135 return True
1136
1137
Yurii Karabasf03d3182020-11-17 04:23:19 +02001138def _value_and_type_iter(parameters):
1139 return ((p, type(p)) for p in parameters)
1140
1141
1142class _LiteralGenericAlias(_GenericAlias, _root=True):
1143
1144 def __eq__(self, other):
1145 if not isinstance(other, _LiteralGenericAlias):
1146 return NotImplemented
1147
1148 return set(_value_and_type_iter(self.__args__)) == set(_value_and_type_iter(other.__args__))
1149
1150 def __hash__(self):
Yurii Karabas1b540772020-11-19 18:17:38 +02001151 return hash(frozenset(_value_and_type_iter(self.__args__)))
Yurii Karabasf03d3182020-11-17 04:23:19 +02001152
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001153
kj73607be2020-12-24 12:33:48 +08001154class _ConcatenateGenericAlias(_GenericAlias, _root=True):
1155 pass
1156
1157
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001158class Generic:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001159 """Abstract base class for generic types.
1160
Guido van Rossumb24569a2016-11-20 18:01:29 -08001161 A generic type is typically declared by inheriting from
1162 this class parameterized with one or more type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001163 For example, a generic mapping type might be defined as::
1164
1165 class Mapping(Generic[KT, VT]):
1166 def __getitem__(self, key: KT) -> VT:
1167 ...
1168 # Etc.
1169
1170 This class can then be used as follows::
1171
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001172 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001173 try:
1174 return mapping[key]
1175 except KeyError:
1176 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001177 """
Guido van Rossumd70fe632015-08-05 12:11:06 +02001178 __slots__ = ()
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001179 _is_protocol = False
Guido van Rossumd70fe632015-08-05 12:11:06 +02001180
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001181 @_tp_cache
1182 def __class_getitem__(cls, params):
1183 if not isinstance(params, tuple):
1184 params = (params,)
1185 if not params and cls is not Tuple:
1186 raise TypeError(
1187 f"Parameter list to {cls.__qualname__}[...] cannot be empty")
kj73607be2020-12-24 12:33:48 +08001188 params = tuple(_type_convert(p) for p in params)
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001189 if cls in (Generic, Protocol):
1190 # Generic and Protocol can only be subscripted with unique type variables.
kj73607be2020-12-24 12:33:48 +08001191 if not all(isinstance(p, _TypeVarLike) for p in params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001192 raise TypeError(
kj73607be2020-12-24 12:33:48 +08001193 f"Parameters to {cls.__name__}[...] must all be type variables "
1194 f"or parameter specification variables.")
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001195 if len(set(params)) != len(params):
1196 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001197 f"Parameters to {cls.__name__}[...] must all be unique")
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001198 else:
1199 # Subscripting a regular Generic subclass.
kj73607be2020-12-24 12:33:48 +08001200 if any(isinstance(t, ParamSpec) for t in cls.__parameters__):
1201 params = _prepare_paramspec_params(cls, params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001202 _check_generic(cls, params, len(cls.__parameters__))
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001203 return _GenericAlias(cls, params)
1204
1205 def __init_subclass__(cls, *args, **kwargs):
Ivan Levkivskyiee566fe2018-04-04 17:00:15 +01001206 super().__init_subclass__(*args, **kwargs)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001207 tvars = []
1208 if '__orig_bases__' in cls.__dict__:
1209 error = Generic in cls.__orig_bases__
1210 else:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001211 error = Generic in cls.__bases__ and cls.__name__ != 'Protocol'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001212 if error:
1213 raise TypeError("Cannot inherit from plain Generic")
1214 if '__orig_bases__' in cls.__dict__:
1215 tvars = _collect_type_vars(cls.__orig_bases__)
1216 # Look for Generic[T1, ..., Tn].
1217 # If found, tvars must be a subset of it.
1218 # If not found, tvars is it.
1219 # Also check for and reject plain Generic,
1220 # and reject multiple Generic[...].
1221 gvars = None
1222 for base in cls.__orig_bases__:
1223 if (isinstance(base, _GenericAlias) and
1224 base.__origin__ is Generic):
1225 if gvars is not None:
1226 raise TypeError(
1227 "Cannot inherit from Generic[...] multiple types.")
1228 gvars = base.__parameters__
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001229 if gvars is not None:
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001230 tvarset = set(tvars)
1231 gvarset = set(gvars)
1232 if not tvarset <= gvarset:
1233 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
1234 s_args = ', '.join(str(g) for g in gvars)
1235 raise TypeError(f"Some type variables ({s_vars}) are"
1236 f" not listed in Generic[{s_args}]")
1237 tvars = gvars
1238 cls.__parameters__ = tuple(tvars)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001239
1240
1241class _TypingEmpty:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001242 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
1243 to allow empty list/tuple in specific places, without allowing them
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001244 to sneak in where prohibited.
1245 """
1246
1247
1248class _TypingEllipsis:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001249 """Internal placeholder for ... (ellipsis)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001250
1251
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001252_TYPING_INTERNALS = ['__parameters__', '__orig_bases__', '__orig_class__',
1253 '_is_protocol', '_is_runtime_protocol']
1254
1255_SPECIAL_NAMES = ['__abstractmethods__', '__annotations__', '__dict__', '__doc__',
1256 '__init__', '__module__', '__new__', '__slots__',
Guido van Rossum48b069a2020-04-07 09:50:06 -07001257 '__subclasshook__', '__weakref__', '__class_getitem__']
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001258
1259# These special attributes will be not collected as protocol members.
1260EXCLUDED_ATTRIBUTES = _TYPING_INTERNALS + _SPECIAL_NAMES + ['_MutableMapping__marker']
1261
1262
1263def _get_protocol_attrs(cls):
1264 """Collect protocol members from a protocol class objects.
1265
1266 This includes names actually defined in the class dictionary, as well
1267 as names that appear in annotations. Special names (above) are skipped.
1268 """
1269 attrs = set()
1270 for base in cls.__mro__[:-1]: # without object
1271 if base.__name__ in ('Protocol', 'Generic'):
1272 continue
1273 annotations = getattr(base, '__annotations__', {})
1274 for attr in list(base.__dict__.keys()) + list(annotations.keys()):
1275 if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
1276 attrs.add(attr)
1277 return attrs
1278
1279
1280def _is_callable_members_only(cls):
1281 # PEP 544 prohibits using issubclass() with protocols that have non-method members.
1282 return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls))
1283
1284
1285def _no_init(self, *args, **kwargs):
1286 if type(self)._is_protocol:
1287 raise TypeError('Protocols cannot be instantiated')
1288
1289
Rossc1af1282020-12-29 11:55:28 +00001290def _allow_reckless_class_checks():
Nickolena Fishercfaf4c02020-04-26 12:49:11 -05001291 """Allow instance and class checks for special stdlib modules.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001292
1293 The abc and functools modules indiscriminately call isinstance() and
1294 issubclass() on the whole MRO of a user class, which may contain protocols.
1295 """
1296 try:
1297 return sys._getframe(3).f_globals['__name__'] in ['abc', 'functools']
1298 except (AttributeError, ValueError): # For platforms without _getframe().
1299 return True
1300
1301
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001302_PROTO_ALLOWLIST = {
Divij Rajkumar692a0dc2019-09-12 11:13:51 +01001303 'collections.abc': [
1304 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
1305 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
1306 ],
1307 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
1308}
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001309
1310
1311class _ProtocolMeta(ABCMeta):
1312 # This metaclass is really unfortunate and exists only because of
1313 # the lack of __instancehook__.
1314 def __instancecheck__(cls, instance):
1315 # We need this method for situations where attributes are
1316 # assigned in __init__.
1317 if ((not getattr(cls, '_is_protocol', False) or
1318 _is_callable_members_only(cls)) and
1319 issubclass(instance.__class__, cls)):
1320 return True
1321 if cls._is_protocol:
1322 if all(hasattr(instance, attr) and
1323 # All *methods* can be blocked by setting them to None.
1324 (not callable(getattr(cls, attr, None)) or
1325 getattr(instance, attr) is not None)
1326 for attr in _get_protocol_attrs(cls)):
1327 return True
1328 return super().__instancecheck__(instance)
1329
1330
1331class Protocol(Generic, metaclass=_ProtocolMeta):
1332 """Base class for protocol classes.
1333
1334 Protocol classes are defined as::
1335
1336 class Proto(Protocol):
1337 def meth(self) -> int:
1338 ...
1339
1340 Such classes are primarily used with static type checkers that recognize
1341 structural subtyping (static duck-typing), for example::
1342
1343 class C:
1344 def meth(self) -> int:
1345 return 0
1346
1347 def func(x: Proto) -> int:
1348 return x.meth()
1349
1350 func(C()) # Passes static type check
1351
1352 See PEP 544 for details. Protocol classes decorated with
1353 @typing.runtime_checkable act as simple-minded runtime protocols that check
1354 only the presence of given attributes, ignoring their type signatures.
1355 Protocol classes can be generic, they are defined as::
1356
1357 class GenProto(Protocol[T]):
1358 def meth(self) -> T:
1359 ...
1360 """
1361 __slots__ = ()
1362 _is_protocol = True
1363 _is_runtime_protocol = False
1364
1365 def __init_subclass__(cls, *args, **kwargs):
1366 super().__init_subclass__(*args, **kwargs)
1367
1368 # Determine if this is a protocol or a concrete subclass.
1369 if not cls.__dict__.get('_is_protocol', False):
1370 cls._is_protocol = any(b is Protocol for b in cls.__bases__)
1371
1372 # Set (or override) the protocol subclass hook.
1373 def _proto_hook(other):
1374 if not cls.__dict__.get('_is_protocol', False):
1375 return NotImplemented
1376
1377 # First, perform various sanity checks.
1378 if not getattr(cls, '_is_runtime_protocol', False):
Rossc1af1282020-12-29 11:55:28 +00001379 if _allow_reckless_class_checks():
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001380 return NotImplemented
1381 raise TypeError("Instance and class checks can only be used with"
1382 " @runtime_checkable protocols")
1383 if not _is_callable_members_only(cls):
Rossc1af1282020-12-29 11:55:28 +00001384 if _allow_reckless_class_checks():
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001385 return NotImplemented
1386 raise TypeError("Protocols with non-method members"
1387 " don't support issubclass()")
1388 if not isinstance(other, type):
1389 # Same error message as for issubclass(1, int).
1390 raise TypeError('issubclass() arg 1 must be a class')
1391
1392 # Second, perform the actual structural compatibility check.
1393 for attr in _get_protocol_attrs(cls):
1394 for base in other.__mro__:
1395 # Check if the members appears in the class dictionary...
1396 if attr in base.__dict__:
1397 if base.__dict__[attr] is None:
1398 return NotImplemented
1399 break
1400
1401 # ...or in annotations, if it is a sub-protocol.
1402 annotations = getattr(base, '__annotations__', {})
1403 if (isinstance(annotations, collections.abc.Mapping) and
1404 attr in annotations and
1405 issubclass(other, Generic) and other._is_protocol):
1406 break
1407 else:
1408 return NotImplemented
1409 return True
1410
1411 if '__subclasshook__' not in cls.__dict__:
1412 cls.__subclasshook__ = _proto_hook
1413
1414 # We have nothing more to do for non-protocols...
1415 if not cls._is_protocol:
1416 return
1417
1418 # ... otherwise check consistency of bases, and prohibit instantiation.
1419 for base in cls.__bases__:
1420 if not (base in (object, Generic) or
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001421 base.__module__ in _PROTO_ALLOWLIST and
1422 base.__name__ in _PROTO_ALLOWLIST[base.__module__] or
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001423 issubclass(base, Generic) and base._is_protocol):
1424 raise TypeError('Protocols can only inherit from other'
1425 ' protocols, got %r' % base)
1426 cls.__init__ = _no_init
1427
1428
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001429class _AnnotatedAlias(_GenericAlias, _root=True):
1430 """Runtime representation of an annotated type.
1431
1432 At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't'
1433 with extra annotations. The alias behaves like a normal typing alias,
1434 instantiating is the same as instantiating the underlying type, binding
1435 it to types is also the same.
1436 """
1437 def __init__(self, origin, metadata):
1438 if isinstance(origin, _AnnotatedAlias):
1439 metadata = origin.__metadata__ + metadata
1440 origin = origin.__origin__
1441 super().__init__(origin, origin)
1442 self.__metadata__ = metadata
1443
1444 def copy_with(self, params):
1445 assert len(params) == 1
1446 new_type = params[0]
1447 return _AnnotatedAlias(new_type, self.__metadata__)
1448
1449 def __repr__(self):
1450 return "typing.Annotated[{}, {}]".format(
1451 _type_repr(self.__origin__),
1452 ", ".join(repr(a) for a in self.__metadata__)
1453 )
1454
1455 def __reduce__(self):
1456 return operator.getitem, (
1457 Annotated, (self.__origin__,) + self.__metadata__
1458 )
1459
1460 def __eq__(self, other):
1461 if not isinstance(other, _AnnotatedAlias):
1462 return NotImplemented
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001463 return (self.__origin__ == other.__origin__
1464 and self.__metadata__ == other.__metadata__)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001465
1466 def __hash__(self):
1467 return hash((self.__origin__, self.__metadata__))
1468
1469
1470class Annotated:
1471 """Add context specific metadata to a type.
1472
1473 Example: Annotated[int, runtime_check.Unsigned] indicates to the
1474 hypothetical runtime_check module that this type is an unsigned int.
1475 Every other consumer of this type can ignore this metadata and treat
1476 this type as int.
1477
1478 The first argument to Annotated must be a valid type.
1479
1480 Details:
1481
1482 - It's an error to call `Annotated` with less than two arguments.
1483 - Nested Annotated are flattened::
1484
1485 Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
1486
1487 - Instantiating an annotated type is equivalent to instantiating the
1488 underlying type::
1489
1490 Annotated[C, Ann1](5) == C(5)
1491
1492 - Annotated can be used as a generic type alias::
1493
1494 Optimized = Annotated[T, runtime.Optimize()]
1495 Optimized[int] == Annotated[int, runtime.Optimize()]
1496
1497 OptimizedList = Annotated[List[T], runtime.Optimize()]
1498 OptimizedList[int] == Annotated[List[int], runtime.Optimize()]
1499 """
1500
1501 __slots__ = ()
1502
1503 def __new__(cls, *args, **kwargs):
1504 raise TypeError("Type Annotated cannot be instantiated.")
1505
1506 @_tp_cache
1507 def __class_getitem__(cls, params):
1508 if not isinstance(params, tuple) or len(params) < 2:
1509 raise TypeError("Annotated[...] should be used "
1510 "with at least two arguments (a type and an "
1511 "annotation).")
1512 msg = "Annotated[t, ...]: t must be a type."
1513 origin = _type_check(params[0], msg)
1514 metadata = tuple(params[1:])
1515 return _AnnotatedAlias(origin, metadata)
1516
1517 def __init_subclass__(cls, *args, **kwargs):
1518 raise TypeError(
1519 "Cannot subclass {}.Annotated".format(cls.__module__)
1520 )
1521
1522
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001523def runtime_checkable(cls):
1524 """Mark a protocol class as a runtime protocol.
1525
1526 Such protocol can be used with isinstance() and issubclass().
1527 Raise TypeError if applied to a non-protocol class.
1528 This allows a simple-minded structural check very similar to
1529 one trick ponies in collections.abc such as Iterable.
1530 For example::
1531
1532 @runtime_checkable
1533 class Closable(Protocol):
1534 def close(self): ...
1535
1536 assert isinstance(open('/some/file'), Closable)
1537
1538 Warning: this will check only the presence of the required methods,
1539 not their type signatures!
1540 """
1541 if not issubclass(cls, Generic) or not cls._is_protocol:
1542 raise TypeError('@runtime_checkable can be only applied to protocol classes,'
1543 ' got %r' % cls)
1544 cls._is_runtime_protocol = True
1545 return cls
1546
1547
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001548def cast(typ, val):
1549 """Cast a value to a type.
1550
1551 This returns the value unchanged. To the type checker this
1552 signals that the return value has the designated type, but at
1553 runtime we intentionally don't check anything (we want this
1554 to be as fast as possible).
1555 """
1556 return val
1557
1558
1559def _get_defaults(func):
1560 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -08001561 try:
1562 code = func.__code__
1563 except AttributeError:
1564 # Some built-in functions don't have __code__, __defaults__, etc.
1565 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001566 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001567 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001568 arg_names = arg_names[:pos_count]
1569 defaults = func.__defaults__ or ()
1570 kwdefaults = func.__kwdefaults__
1571 res = dict(kwdefaults) if kwdefaults else {}
1572 pos_offset = pos_count - len(defaults)
1573 for name, value in zip(arg_names[pos_offset:], defaults):
1574 assert name not in res
1575 res[name] = value
1576 return res
1577
1578
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001579_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
1580 types.MethodType, types.ModuleType,
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02001581 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001582
1583
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001584def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
Guido van Rossum991d14f2016-11-09 13:12:51 -08001585 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001586
Guido van Rossum991d14f2016-11-09 13:12:51 -08001587 This is often the same as obj.__annotations__, but it handles
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001588 forward references encoded as string literals, adds Optional[t] if a
1589 default value equal to None is set and recursively replaces all
1590 'Annotated[T, ...]' with 'T' (unless 'include_extras=True').
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001591
Guido van Rossum991d14f2016-11-09 13:12:51 -08001592 The argument may be a module, class, method, or function. The annotations
1593 are returned as a dictionary. For classes, annotations include also
1594 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001595
Guido van Rossum991d14f2016-11-09 13:12:51 -08001596 TypeError is raised if the argument is not of a type that can contain
1597 annotations, and an empty dictionary is returned if no annotations are
1598 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001599
Guido van Rossum991d14f2016-11-09 13:12:51 -08001600 BEWARE -- the behavior of globalns and localns is counterintuitive
1601 (unless you are familiar with how eval() and exec() work). The
1602 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001603
Guido van Rossum991d14f2016-11-09 13:12:51 -08001604 - If no dict arguments are passed, an attempt is made to use the
Łukasz Langaf350a262017-09-14 14:33:00 -04001605 globals from obj (or the respective module's globals for classes),
1606 and these are also used as the locals. If the object does not appear
1607 to have globals, an empty dictionary is used.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001608
Guido van Rossum991d14f2016-11-09 13:12:51 -08001609 - If one dict argument is passed, it is used for both globals and
1610 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001611
Guido van Rossum991d14f2016-11-09 13:12:51 -08001612 - If two dict arguments are passed, they specify globals and
1613 locals, respectively.
1614 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001615
Guido van Rossum991d14f2016-11-09 13:12:51 -08001616 if getattr(obj, '__no_type_check__', None):
1617 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001618 # Classes require a special treatment.
1619 if isinstance(obj, type):
1620 hints = {}
1621 for base in reversed(obj.__mro__):
Łukasz Langaf350a262017-09-14 14:33:00 -04001622 if globalns is None:
Karthikeyan Singaravelana9cf69d2021-04-12 23:47:25 +05301623 try:
1624 base_globals = sys.modules[base.__module__].__dict__
1625 except KeyError:
1626 continue
Łukasz Langaf350a262017-09-14 14:33:00 -04001627 else:
1628 base_globals = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001629 ann = base.__dict__.get('__annotations__', {})
Ken Jin852150d2021-04-13 01:23:12 +08001630 base_locals = dict(vars(base)) if localns is None else localns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001631 for name, value in ann.items():
1632 if value is None:
1633 value = type(None)
1634 if isinstance(value, str):
Nina Zakharenko0e61dff2018-05-22 20:32:10 -07001635 value = ForwardRef(value, is_argument=False)
Ken Jin852150d2021-04-13 01:23:12 +08001636 value = _eval_type(value, base_globals, base_locals)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001637 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001638 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
Łukasz Langaf350a262017-09-14 14:33:00 -04001639
1640 if globalns is None:
1641 if isinstance(obj, types.ModuleType):
1642 globalns = obj.__dict__
1643 else:
benedwards140aca3a32019-11-21 17:24:58 +00001644 nsobj = obj
1645 # Find globalns for the unwrapped object.
1646 while hasattr(nsobj, '__wrapped__'):
1647 nsobj = nsobj.__wrapped__
1648 globalns = getattr(nsobj, '__globals__', {})
Łukasz Langaf350a262017-09-14 14:33:00 -04001649 if localns is None:
1650 localns = globalns
1651 elif localns is None:
1652 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001653 hints = getattr(obj, '__annotations__', None)
1654 if hints is None:
1655 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001656 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001657 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001658 else:
1659 raise TypeError('{!r} is not a module, class, method, '
1660 'or function.'.format(obj))
1661 defaults = _get_defaults(obj)
1662 hints = dict(hints)
1663 for name, value in hints.items():
1664 if value is None:
1665 value = type(None)
1666 if isinstance(value, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001667 value = ForwardRef(value)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001668 value = _eval_type(value, globalns, localns)
1669 if name in defaults and defaults[name] is None:
1670 value = Optional[value]
1671 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001672 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
1673
1674
1675def _strip_annotations(t):
1676 """Strips the annotations from a given type.
1677 """
1678 if isinstance(t, _AnnotatedAlias):
1679 return _strip_annotations(t.__origin__)
1680 if isinstance(t, _GenericAlias):
1681 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1682 if stripped_args == t.__args__:
1683 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001684 return t.copy_with(stripped_args)
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001685 if isinstance(t, GenericAlias):
1686 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1687 if stripped_args == t.__args__:
1688 return t
1689 return GenericAlias(t.__origin__, stripped_args)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001690 return t
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001691
1692
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001693def get_origin(tp):
1694 """Get the unsubscripted version of a type.
1695
Jakub Stasiak38aaaaa2020-02-07 02:15:12 +01001696 This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
1697 and Annotated. Return None for unsupported types. Examples::
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001698
1699 get_origin(Literal[42]) is Literal
1700 get_origin(int) is None
1701 get_origin(ClassVar[int]) is ClassVar
1702 get_origin(Generic) is Generic
1703 get_origin(Generic[T]) is Generic
1704 get_origin(Union[T, int]) is Union
1705 get_origin(List[Tuple[T, T]][int]) == list
Jelle Zijlstra52243362021-04-10 19:57:05 -07001706 get_origin(P.args) is P
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001707 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001708 if isinstance(tp, _AnnotatedAlias):
1709 return Annotated
Jelle Zijlstra52243362021-04-10 19:57:05 -07001710 if isinstance(tp, (_BaseGenericAlias, GenericAlias,
1711 ParamSpecArgs, ParamSpecKwargs)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001712 return tp.__origin__
1713 if tp is Generic:
1714 return Generic
Ken Jinefb1f092020-12-29 10:26:19 +08001715 if isinstance(tp, types.Union):
1716 return types.Union
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001717 return None
1718
1719
1720def get_args(tp):
1721 """Get type arguments with all substitutions performed.
1722
1723 For unions, basic simplifications used by Union constructor are performed.
1724 Examples::
1725 get_args(Dict[str, int]) == (str, int)
1726 get_args(int) == ()
1727 get_args(Union[int, Union[T, int], str][int]) == (int, str)
1728 get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
1729 get_args(Callable[[], T][int]) == ([], int)
1730 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001731 if isinstance(tp, _AnnotatedAlias):
1732 return (tp.__origin__,) + tp.__metadata__
Ken Jin4140f102020-12-29 04:06:19 +08001733 if isinstance(tp, (_GenericAlias, GenericAlias)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001734 res = tp.__args__
Ken Jinefb1f092020-12-29 10:26:19 +08001735 if (tp.__origin__ is collections.abc.Callable
1736 and not (res[0] is Ellipsis
1737 or isinstance(res[0], (ParamSpec, _ConcatenateGenericAlias)))):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001738 res = (list(res[:-1]), res[-1])
1739 return res
Ken Jinefb1f092020-12-29 10:26:19 +08001740 if isinstance(tp, types.Union):
1741 return tp.__args__
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001742 return ()
1743
1744
Patrick Reader0705ec82020-09-16 05:58:32 +01001745def is_typeddict(tp):
1746 """Check if an annotation is a TypedDict class
1747
1748 For example::
1749 class Film(TypedDict):
1750 title: str
1751 year: int
1752
1753 is_typeddict(Film) # => True
1754 is_typeddict(Union[list, str]) # => False
1755 """
1756 return isinstance(tp, _TypedDictMeta)
1757
1758
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001759def no_type_check(arg):
1760 """Decorator to indicate that annotations are not type hints.
1761
1762 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001763 applies recursively to all methods and classes defined in that class
1764 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001765
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001766 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001767 """
1768 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001769 arg_attrs = arg.__dict__.copy()
1770 for attr, val in arg.__dict__.items():
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001771 if val in arg.__bases__ + (arg,):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001772 arg_attrs.pop(attr)
1773 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001774 if isinstance(obj, types.FunctionType):
1775 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001776 if isinstance(obj, type):
1777 no_type_check(obj)
1778 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001779 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001780 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001781 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001782 return arg
1783
1784
1785def no_type_check_decorator(decorator):
1786 """Decorator to give another decorator the @no_type_check effect.
1787
1788 This wraps the decorator with something that wraps the decorated
1789 function in @no_type_check.
1790 """
1791
1792 @functools.wraps(decorator)
1793 def wrapped_decorator(*args, **kwds):
1794 func = decorator(*args, **kwds)
1795 func = no_type_check(func)
1796 return func
1797
1798 return wrapped_decorator
1799
1800
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001801def _overload_dummy(*args, **kwds):
1802 """Helper for @overload to raise when called."""
1803 raise NotImplementedError(
1804 "You should not call an overloaded function. "
1805 "A series of @overload-decorated functions "
1806 "outside a stub module should always be followed "
1807 "by an implementation that is not @overload-ed.")
1808
1809
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001810def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001811 """Decorator for overloaded functions/methods.
1812
1813 In a stub file, place two or more stub definitions for the same
1814 function in a row, each decorated with @overload. For example:
1815
1816 @overload
1817 def utf8(value: None) -> None: ...
1818 @overload
1819 def utf8(value: bytes) -> bytes: ...
1820 @overload
1821 def utf8(value: str) -> bytes: ...
1822
1823 In a non-stub file (i.e. a regular .py file), do the same but
1824 follow it with an implementation. The implementation should *not*
1825 be decorated with @overload. For example:
1826
1827 @overload
1828 def utf8(value: None) -> None: ...
1829 @overload
1830 def utf8(value: bytes) -> bytes: ...
1831 @overload
1832 def utf8(value: str) -> bytes: ...
1833 def utf8(value):
1834 # implementation goes here
1835 """
1836 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001837
1838
Ivan Levkivskyif3672422019-05-26 09:37:07 +01001839def final(f):
1840 """A decorator to indicate final methods and final classes.
1841
1842 Use this decorator to indicate to type checkers that the decorated
1843 method cannot be overridden, and decorated class cannot be subclassed.
1844 For example:
1845
1846 class Base:
1847 @final
1848 def done(self) -> None:
1849 ...
1850 class Sub(Base):
1851 def done(self) -> None: # Error reported by type checker
1852 ...
1853
1854 @final
1855 class Leaf:
1856 ...
1857 class Other(Leaf): # Error reported by type checker
1858 ...
1859
1860 There is no runtime checking of these properties.
1861 """
1862 return f
1863
1864
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001865# Some unconstrained type variables. These are used by the container types.
1866# (These are not for export.)
1867T = TypeVar('T') # Any type.
1868KT = TypeVar('KT') # Key type.
1869VT = TypeVar('VT') # Value type.
1870T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
1871V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
1872VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
1873T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
1874# Internal type variable used for Type[].
1875CT_co = TypeVar('CT_co', covariant=True, bound=type)
1876
1877# A useful type variable with constraints. This represents string types.
1878# (This one *is* for export!)
1879AnyStr = TypeVar('AnyStr', bytes, str)
1880
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001881
1882# Various ABCs mimicking those in collections.abc.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001883_alias = _SpecialGenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001884
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001885Hashable = _alias(collections.abc.Hashable, 0) # Not generic.
1886Awaitable = _alias(collections.abc.Awaitable, 1)
1887Coroutine = _alias(collections.abc.Coroutine, 3)
1888AsyncIterable = _alias(collections.abc.AsyncIterable, 1)
1889AsyncIterator = _alias(collections.abc.AsyncIterator, 1)
1890Iterable = _alias(collections.abc.Iterable, 1)
1891Iterator = _alias(collections.abc.Iterator, 1)
1892Reversible = _alias(collections.abc.Reversible, 1)
1893Sized = _alias(collections.abc.Sized, 0) # Not generic.
1894Container = _alias(collections.abc.Container, 1)
1895Collection = _alias(collections.abc.Collection, 1)
1896Callable = _CallableType(collections.abc.Callable, 2)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001897Callable.__doc__ = \
1898 """Callable type; Callable[[int], str] is a function of (int) -> str.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001899
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001900 The subscription syntax must always be used with exactly two
1901 values: the argument list and the return type. The argument list
1902 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001903
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001904 There is no syntax to indicate optional or keyword arguments,
1905 such function types are rarely used as callback types.
1906 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001907AbstractSet = _alias(collections.abc.Set, 1, name='AbstractSet')
1908MutableSet = _alias(collections.abc.MutableSet, 1)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001909# NOTE: Mapping is only covariant in the value type.
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001910Mapping = _alias(collections.abc.Mapping, 2)
1911MutableMapping = _alias(collections.abc.MutableMapping, 2)
1912Sequence = _alias(collections.abc.Sequence, 1)
1913MutableSequence = _alias(collections.abc.MutableSequence, 1)
1914ByteString = _alias(collections.abc.ByteString, 0) # Not generic
1915# Tuple accepts variable number of parameters.
1916Tuple = _TupleType(tuple, -1, inst=False, name='Tuple')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001917Tuple.__doc__ = \
1918 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001919
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001920 Example: Tuple[T1, T2] is a tuple of two elements corresponding
1921 to type variables T1 and T2. Tuple[int, float, str] is a tuple
1922 of an int, a float and a string.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001923
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001924 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
1925 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001926List = _alias(list, 1, inst=False, name='List')
1927Deque = _alias(collections.deque, 1, name='Deque')
1928Set = _alias(set, 1, inst=False, name='Set')
1929FrozenSet = _alias(frozenset, 1, inst=False, name='FrozenSet')
1930MappingView = _alias(collections.abc.MappingView, 1)
1931KeysView = _alias(collections.abc.KeysView, 1)
1932ItemsView = _alias(collections.abc.ItemsView, 2)
1933ValuesView = _alias(collections.abc.ValuesView, 1)
1934ContextManager = _alias(contextlib.AbstractContextManager, 1, name='ContextManager')
1935AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, 1, name='AsyncContextManager')
1936Dict = _alias(dict, 2, inst=False, name='Dict')
1937DefaultDict = _alias(collections.defaultdict, 2, name='DefaultDict')
1938OrderedDict = _alias(collections.OrderedDict, 2)
1939Counter = _alias(collections.Counter, 1)
1940ChainMap = _alias(collections.ChainMap, 2)
1941Generator = _alias(collections.abc.Generator, 3)
1942AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2)
1943Type = _alias(type, 1, inst=False, name='Type')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001944Type.__doc__ = \
1945 """A special construct usable to annotate class objects.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001946
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001947 For example, suppose we have the following classes::
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001948
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001949 class User: ... # Abstract base for User classes
1950 class BasicUser(User): ...
1951 class ProUser(User): ...
1952 class TeamUser(User): ...
Guido van Rossumf17c2002015-12-03 17:31:24 -08001953
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001954 And a function that takes a class argument that's a subclass of
1955 User and returns an instance of the corresponding class::
Guido van Rossumf17c2002015-12-03 17:31:24 -08001956
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001957 U = TypeVar('U', bound=User)
1958 def new_user(user_class: Type[U]) -> U:
1959 user = user_class()
1960 # (Here we could write the user object to a database)
1961 return user
Guido van Rossumf17c2002015-12-03 17:31:24 -08001962
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001963 joe = new_user(BasicUser)
Guido van Rossumf17c2002015-12-03 17:31:24 -08001964
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001965 At this point the type checker knows that joe has type BasicUser.
1966 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001967
1968
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001969@runtime_checkable
1970class SupportsInt(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001971 """An ABC with one abstract method __int__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001972 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001973
1974 @abstractmethod
1975 def __int__(self) -> int:
1976 pass
1977
1978
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001979@runtime_checkable
1980class SupportsFloat(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001981 """An ABC with one abstract method __float__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001982 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001983
1984 @abstractmethod
1985 def __float__(self) -> float:
1986 pass
1987
1988
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001989@runtime_checkable
1990class SupportsComplex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001991 """An ABC with one abstract method __complex__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001992 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001993
1994 @abstractmethod
1995 def __complex__(self) -> complex:
1996 pass
1997
1998
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001999@runtime_checkable
2000class SupportsBytes(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002001 """An ABC with one abstract method __bytes__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002002 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002003
2004 @abstractmethod
2005 def __bytes__(self) -> bytes:
2006 pass
2007
2008
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002009@runtime_checkable
2010class SupportsIndex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002011 """An ABC with one abstract method __index__."""
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -07002012 __slots__ = ()
2013
2014 @abstractmethod
2015 def __index__(self) -> int:
2016 pass
2017
2018
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002019@runtime_checkable
2020class SupportsAbs(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002021 """An ABC with one abstract method __abs__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002022 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002023
2024 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02002025 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002026 pass
2027
2028
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002029@runtime_checkable
2030class SupportsRound(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002031 """An ABC with one abstract method __round__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002032 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002033
2034 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02002035 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002036 pass
2037
2038
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002039def _make_nmtuple(name, types, module, defaults = ()):
2040 fields = [n for n, t in types]
2041 types = {n: _type_check(t, f"field {n} annotation must be a type")
2042 for n, t in types}
2043 nm_tpl = collections.namedtuple(name, fields,
2044 defaults=defaults, module=module)
2045 nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002046 return nm_tpl
2047
2048
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002049# attributes prohibited to set in NamedTuple class syntax
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002050_prohibited = frozenset({'__new__', '__init__', '__slots__', '__getnewargs__',
2051 '_fields', '_field_defaults',
2052 '_make', '_replace', '_asdict', '_source'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002053
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002054_special = frozenset({'__module__', '__name__', '__annotations__'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002055
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002056
Guido van Rossum2f841442016-11-15 09:48:06 -08002057class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002058
Guido van Rossum2f841442016-11-15 09:48:06 -08002059 def __new__(cls, typename, bases, ns):
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002060 assert bases[0] is _NamedTuple
Guido van Rossum2f841442016-11-15 09:48:06 -08002061 types = ns.get('__annotations__', {})
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002062 default_names = []
Guido van Rossum3c268be2017-01-18 08:03:50 -08002063 for field_name in types:
2064 if field_name in ns:
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002065 default_names.append(field_name)
2066 elif default_names:
2067 raise TypeError(f"Non-default namedtuple field {field_name} "
2068 f"cannot follow default field"
2069 f"{'s' if len(default_names) > 1 else ''} "
2070 f"{', '.join(default_names)}")
2071 nm_tpl = _make_nmtuple(typename, types.items(),
2072 defaults=[ns[n] for n in default_names],
2073 module=ns['__module__'])
Guido van Rossum95919c02017-01-22 17:47:20 -08002074 # update from user namespace without overriding special namedtuple attributes
2075 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002076 if key in _prohibited:
2077 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
2078 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08002079 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08002080 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002081
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002082
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002083def NamedTuple(typename, fields=None, /, **kwargs):
Guido van Rossum2f841442016-11-15 09:48:06 -08002084 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002085
Guido van Rossum2f841442016-11-15 09:48:06 -08002086 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002087
Guido van Rossum2f841442016-11-15 09:48:06 -08002088 class Employee(NamedTuple):
2089 name: str
2090 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002091
Guido van Rossum2f841442016-11-15 09:48:06 -08002092 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002093
Guido van Rossum2f841442016-11-15 09:48:06 -08002094 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002095
Raymond Hettingerf7b57df2019-03-18 09:53:56 -07002096 The resulting class has an extra __annotations__ attribute, giving a
2097 dict that maps field names to types. (The field names are also in
2098 the _fields attribute, which is part of the namedtuple API.)
2099 Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002100
Guido van Rossum2f841442016-11-15 09:48:06 -08002101 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002102
Guido van Rossum2f841442016-11-15 09:48:06 -08002103 In Python versions <= 3.5 use::
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)])
2106 """
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002107 if fields is None:
2108 fields = kwargs.items()
2109 elif kwargs:
2110 raise TypeError("Either list of fields or keywords"
2111 " can be provided to NamedTuple, not both")
2112 try:
2113 module = sys._getframe(1).f_globals.get('__name__', '__main__')
2114 except (AttributeError, ValueError):
2115 module = None
2116 return _make_nmtuple(typename, fields, module=module)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002117
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002118_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})
2119
2120def _namedtuple_mro_entries(bases):
2121 if len(bases) > 1:
2122 raise TypeError("Multiple inheritance with NamedTuple is not supported")
2123 assert bases[0] is NamedTuple
2124 return (_NamedTuple,)
2125
2126NamedTuple.__mro_entries__ = _namedtuple_mro_entries
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002127
2128
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002129class _TypedDictMeta(type):
2130 def __new__(cls, name, bases, ns, total=True):
2131 """Create new typed dict class object.
2132
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002133 This method is called when TypedDict is subclassed,
2134 or when TypedDict is instantiated. This way
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002135 TypedDict supports all three syntax forms described in its docstring.
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002136 Subclasses and instances of TypedDict return actual dictionaries.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002137 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002138 for base in bases:
2139 if type(base) is not _TypedDictMeta:
2140 raise TypeError('cannot inherit from both a TypedDict type '
2141 'and a non-TypedDict base class')
2142 tp_dict = type.__new__(_TypedDictMeta, name, (dict,), ns)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002143
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002144 annotations = {}
2145 own_annotations = ns.get('__annotations__', {})
2146 own_annotation_keys = set(own_annotations.keys())
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002147 msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002148 own_annotations = {
2149 n: _type_check(tp, msg) for n, tp in own_annotations.items()
2150 }
2151 required_keys = set()
2152 optional_keys = set()
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002153
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002154 for base in bases:
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002155 annotations.update(base.__dict__.get('__annotations__', {}))
2156 required_keys.update(base.__dict__.get('__required_keys__', ()))
2157 optional_keys.update(base.__dict__.get('__optional_keys__', ()))
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002158
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002159 annotations.update(own_annotations)
2160 if total:
2161 required_keys.update(own_annotation_keys)
2162 else:
2163 optional_keys.update(own_annotation_keys)
2164
2165 tp_dict.__annotations__ = annotations
2166 tp_dict.__required_keys__ = frozenset(required_keys)
2167 tp_dict.__optional_keys__ = frozenset(optional_keys)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002168 if not hasattr(tp_dict, '__total__'):
2169 tp_dict.__total__ = total
2170 return tp_dict
2171
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002172 __call__ = dict # static method
2173
2174 def __subclasscheck__(cls, other):
2175 # Typed dicts are only for static structural subtyping.
2176 raise TypeError('TypedDict does not support instance and class checks')
2177
2178 __instancecheck__ = __subclasscheck__
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002179
2180
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002181def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002182 """A simple typed namespace. At runtime it is equivalent to a plain dict.
2183
2184 TypedDict creates a dictionary type that expects all of its
2185 instances to have a certain set of keys, where each key is
2186 associated with a value of a consistent type. This expectation
2187 is not checked at runtime but is only enforced by type checkers.
2188 Usage::
2189
2190 class Point2D(TypedDict):
2191 x: int
2192 y: int
2193 label: str
2194
2195 a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
2196 b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
2197
2198 assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
2199
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002200 The type info can be accessed via the Point2D.__annotations__ dict, and
2201 the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
2202 TypedDict supports two additional equivalent forms::
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002203
2204 Point2D = TypedDict('Point2D', x=int, y=int, label=str)
2205 Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
2206
ananthan-123ab6423f2020-02-19 10:03:05 +05302207 By default, all keys must be present in a TypedDict. It is possible
2208 to override this by specifying totality.
2209 Usage::
2210
2211 class point2D(TypedDict, total=False):
2212 x: int
2213 y: int
2214
2215 This means that a point2D TypedDict can have any of the keys omitted.A type
2216 checker is only expected to support a literal False or True as the value of
2217 the total argument. True is the default, and makes all items defined in the
2218 class body be required.
2219
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002220 The class syntax is only supported in Python 3.6+, while two other
2221 syntax forms work for Python 2.7 and 3.2+
2222 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002223 if fields is None:
2224 fields = kwargs
2225 elif kwargs:
2226 raise TypeError("TypedDict takes either a dict or keyword arguments,"
2227 " but not both")
2228
Alex Grönholm67b769f2020-12-10 23:49:05 +02002229 ns = {'__annotations__': dict(fields)}
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002230 try:
2231 # Setting correct module is necessary to make typed dict classes pickleable.
2232 ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
2233 except (AttributeError, ValueError):
2234 pass
2235
Alex Grönholm67b769f2020-12-10 23:49:05 +02002236 return _TypedDictMeta(typename, (), ns, total=total)
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002237
2238_TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {})
2239TypedDict.__mro_entries__ = lambda bases: (_TypedDict,)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002240
2241
Guido van Rossum91185fe2016-06-08 11:19:11 -07002242def NewType(name, tp):
2243 """NewType creates simple unique types with almost zero
2244 runtime overhead. NewType(name, tp) is considered a subtype of tp
2245 by static type checkers. At runtime, NewType(name, tp) returns
2246 a dummy function that simply returns its argument. Usage::
2247
2248 UserId = NewType('UserId', int)
2249
2250 def name_by_id(user_id: UserId) -> str:
2251 ...
2252
2253 UserId('user') # Fails type check
2254
2255 name_by_id(42) # Fails type check
2256 name_by_id(UserId(42)) # OK
2257
2258 num = UserId(5) + 1 # type: int
2259 """
2260
2261 def new_type(x):
2262 return x
2263
2264 new_type.__name__ = name
2265 new_type.__supertype__ = tp
2266 return new_type
2267
2268
Guido van Rossum0e0563c2016-04-05 14:54:25 -07002269# Python-version-specific alias (Python 2: unicode; Python 3: str)
2270Text = str
2271
2272
Guido van Rossum91185fe2016-06-08 11:19:11 -07002273# Constant that's True when type checking, but False here.
2274TYPE_CHECKING = False
2275
2276
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002277class IO(Generic[AnyStr]):
2278 """Generic base class for TextIO and BinaryIO.
2279
2280 This is an abstract, generic version of the return of open().
2281
2282 NOTE: This does not distinguish between the different possible
2283 classes (text vs. binary, read vs. write vs. read/write,
2284 append-only, unbuffered). The TextIO and BinaryIO subclasses
2285 below capture the distinctions between text vs. binary, which is
2286 pervasive in the interface; however we currently do not offer a
2287 way to track the other distinctions in the type system.
2288 """
2289
Guido van Rossumd70fe632015-08-05 12:11:06 +02002290 __slots__ = ()
2291
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002292 @property
2293 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002294 def mode(self) -> str:
2295 pass
2296
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002297 @property
2298 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002299 def name(self) -> str:
2300 pass
2301
2302 @abstractmethod
2303 def close(self) -> None:
2304 pass
2305
Shantanu2e6569b2020-01-29 18:52:36 -08002306 @property
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002307 @abstractmethod
2308 def closed(self) -> bool:
2309 pass
2310
2311 @abstractmethod
2312 def fileno(self) -> int:
2313 pass
2314
2315 @abstractmethod
2316 def flush(self) -> None:
2317 pass
2318
2319 @abstractmethod
2320 def isatty(self) -> bool:
2321 pass
2322
2323 @abstractmethod
2324 def read(self, n: int = -1) -> AnyStr:
2325 pass
2326
2327 @abstractmethod
2328 def readable(self) -> bool:
2329 pass
2330
2331 @abstractmethod
2332 def readline(self, limit: int = -1) -> AnyStr:
2333 pass
2334
2335 @abstractmethod
2336 def readlines(self, hint: int = -1) -> List[AnyStr]:
2337 pass
2338
2339 @abstractmethod
2340 def seek(self, offset: int, whence: int = 0) -> int:
2341 pass
2342
2343 @abstractmethod
2344 def seekable(self) -> bool:
2345 pass
2346
2347 @abstractmethod
2348 def tell(self) -> int:
2349 pass
2350
2351 @abstractmethod
2352 def truncate(self, size: int = None) -> int:
2353 pass
2354
2355 @abstractmethod
2356 def writable(self) -> bool:
2357 pass
2358
2359 @abstractmethod
2360 def write(self, s: AnyStr) -> int:
2361 pass
2362
2363 @abstractmethod
2364 def writelines(self, lines: List[AnyStr]) -> None:
2365 pass
2366
2367 @abstractmethod
2368 def __enter__(self) -> 'IO[AnyStr]':
2369 pass
2370
2371 @abstractmethod
2372 def __exit__(self, type, value, traceback) -> None:
2373 pass
2374
2375
2376class BinaryIO(IO[bytes]):
2377 """Typed version of the return of open() in binary mode."""
2378
Guido van Rossumd70fe632015-08-05 12:11:06 +02002379 __slots__ = ()
2380
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002381 @abstractmethod
2382 def write(self, s: Union[bytes, bytearray]) -> int:
2383 pass
2384
2385 @abstractmethod
2386 def __enter__(self) -> 'BinaryIO':
2387 pass
2388
2389
2390class TextIO(IO[str]):
2391 """Typed version of the return of open() in text mode."""
2392
Guido van Rossumd70fe632015-08-05 12:11:06 +02002393 __slots__ = ()
2394
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002395 @property
2396 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002397 def buffer(self) -> BinaryIO:
2398 pass
2399
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002400 @property
2401 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002402 def encoding(self) -> str:
2403 pass
2404
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002405 @property
2406 @abstractmethod
Guido van Rossum991d14f2016-11-09 13:12:51 -08002407 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002408 pass
2409
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002410 @property
2411 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002412 def line_buffering(self) -> bool:
2413 pass
2414
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002415 @property
2416 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002417 def newlines(self) -> Any:
2418 pass
2419
2420 @abstractmethod
2421 def __enter__(self) -> 'TextIO':
2422 pass
2423
2424
2425class io:
2426 """Wrapper namespace for IO generic classes."""
2427
2428 __all__ = ['IO', 'TextIO', 'BinaryIO']
2429 IO = IO
2430 TextIO = TextIO
2431 BinaryIO = BinaryIO
2432
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002433
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002434io.__name__ = __name__ + '.io'
2435sys.modules[io.__name__] = io
2436
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002437Pattern = _alias(stdlib_re.Pattern, 1)
2438Match = _alias(stdlib_re.Match, 1)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002439
2440class re:
2441 """Wrapper namespace for re type aliases."""
2442
2443 __all__ = ['Pattern', 'Match']
2444 Pattern = Pattern
2445 Match = Match
2446
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002447
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002448re.__name__ = __name__ + '.re'
2449sys.modules[re.__name__] = re