blob: 1606de91ac66881a4bcfc1ecd2eda1c08eb62e8b [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',
Miss Islington (bot)e1bcc882021-05-04 02:51:33 -0700102
103 # Other concrete types.
104 'BinaryIO',
105 'IO',
106 'Match',
107 'Pattern',
108 'TextIO',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700109
110 # One-off things.
111 'AnyStr',
112 'cast',
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100113 'final',
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +0100114 'get_args',
115 'get_origin',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700116 'get_type_hints',
Patrick Reader0705ec82020-09-16 05:58:32 +0100117 'is_typeddict',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700118 'NewType',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700119 'no_type_check',
120 'no_type_check_decorator',
aetracht45738202018-03-19 14:41:32 -0400121 'NoReturn',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700122 'overload',
Jelle Zijlstra52243362021-04-10 19:57:05 -0700123 'ParamSpecArgs',
124 'ParamSpecKwargs',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100125 'runtime_checkable',
Guido van Rossum0e0563c2016-04-05 14:54:25 -0700126 'Text',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700127 'TYPE_CHECKING',
Mikhail Golubev4f3c2502020-10-08 00:44:31 +0300128 'TypeAlias',
Ken Jin05ab4b62021-04-27 22:31:04 +0800129 'TypeGuard',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700130]
131
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700132# The pseudo-submodules 're' and 'io' are part of the public
133# namespace, but excluded from __all__ because they might stomp on
134# legitimate imports of those modules.
135
kj463c7d32020-12-14 02:38:24 +0800136
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700137def _type_convert(arg, module=None):
kj463c7d32020-12-14 02:38:24 +0800138 """For converting None to type(None), and strings to ForwardRef."""
139 if arg is None:
140 return type(None)
141 if isinstance(arg, str):
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700142 return ForwardRef(arg, module=module)
kj463c7d32020-12-14 02:38:24 +0800143 return arg
144
145
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700146def _type_check(arg, msg, is_argument=True, module=None):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000147 """Check that the argument is a type, and return it (internal helper).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700148
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000149 As a special case, accept None and return type(None) instead. Also wrap strings
150 into ForwardRef instances. Consider several corner cases, for example plain
151 special forms like Union are not valid, while Union[int, str] is OK, etc.
152 The msg argument is a human-readable error message, e.g::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700153
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000154 "Union[arg, ...]: arg should be a type."
Guido van Rossum4cefe742016-09-27 15:20:12 -0700155
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000156 We append the repr() of the actual value (truncated to 100 chars).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700157 """
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100158 invalid_generic_forms = (Generic, Protocol)
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700159 if is_argument:
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100160 invalid_generic_forms = invalid_generic_forms + (ClassVar, Final)
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400161
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700162 arg = _type_convert(arg, module=module)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000163 if (isinstance(arg, _GenericAlias) and
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400164 arg.__origin__ in invalid_generic_forms):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000165 raise TypeError(f"{arg} is not valid as type argument")
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300166 if arg in (Any, NoReturn):
167 return arg
168 if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000169 raise TypeError(f"Plain {arg} is not valid as type argument")
kj73607be2020-12-24 12:33:48 +0800170 if isinstance(arg, (type, TypeVar, ForwardRef, types.Union, ParamSpec)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000171 return arg
172 if not callable(arg):
173 raise TypeError(f"{msg} Got {arg!r:.100}.")
174 return arg
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700175
176
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000177def _type_repr(obj):
178 """Return the repr() of an object, special-casing types (internal helper).
179
180 If obj is a type, we return a shorter version than the default
181 type.__repr__, based on the module and qualified name, which is
182 typically enough to uniquely identify a type. For everything
183 else, we fall back on repr(obj).
184 """
kj1f7dfb22020-11-02 02:13:38 +0800185 if isinstance(obj, types.GenericAlias):
186 return repr(obj)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000187 if isinstance(obj, type):
188 if obj.__module__ == 'builtins':
189 return obj.__qualname__
190 return f'{obj.__module__}.{obj.__qualname__}'
191 if obj is ...:
192 return('...')
193 if isinstance(obj, types.FunctionType):
194 return obj.__name__
195 return repr(obj)
196
197
Ken Jina2721642021-07-19 22:22:59 +0800198def _collect_type_vars(types_, typevar_types=None):
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700199 """Collect all type variable contained
kj73607be2020-12-24 12:33:48 +0800200 in types in order of first appearance (lexicographic order). For example::
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000201
202 _collect_type_vars((T, List[S, T])) == (T, S)
203 """
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700204 if typevar_types is None:
205 typevar_types = TypeVar
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000206 tvars = []
Ken Jina2721642021-07-19 22:22:59 +0800207 for t in types_:
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700208 if isinstance(t, typevar_types) and t not in tvars:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000209 tvars.append(t)
Ken Jina2721642021-07-19 22:22:59 +0800210 if isinstance(t, (_GenericAlias, GenericAlias, types.Union)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000211 tvars.extend([t for t in t.__parameters__ if t not in tvars])
212 return tuple(tvars)
213
214
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300215def _check_generic(cls, parameters, elen):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000216 """Check correct count for parameters of a generic cls (internal helper).
217 This gives a nice error message in case of count mismatch.
218 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300219 if not elen:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000220 raise TypeError(f"{cls} is not a generic class")
221 alen = len(parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000222 if alen != elen:
223 raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
224 f" actual {alen}, expected {elen}")
225
kj73607be2020-12-24 12:33:48 +0800226def _prepare_paramspec_params(cls, params):
227 """Prepares the parameters for a Generic containing ParamSpec
228 variables (internal helper).
229 """
230 # Special case where Z[[int, str, bool]] == Z[int, str, bool] in PEP 612.
231 if len(cls.__parameters__) == 1 and len(params) > 1:
232 return (params,)
233 else:
234 _params = []
235 # Convert lists to tuples to help other libraries cache the results.
236 for p, tvar in zip(params, cls.__parameters__):
237 if isinstance(tvar, ParamSpec) and isinstance(p, list):
238 p = tuple(p)
239 _params.append(p)
240 return tuple(_params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000241
Yurii Karabasf03d3182020-11-17 04:23:19 +0200242def _deduplicate(params):
243 # Weed out strict duplicates, preserving the first of each occurrence.
244 all_params = set(params)
245 if len(all_params) < len(params):
246 new_params = []
247 for t in params:
248 if t in all_params:
249 new_params.append(t)
250 all_params.remove(t)
251 params = new_params
252 assert not all_params, all_params
253 return params
254
255
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000256def _remove_dups_flatten(parameters):
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700257 """An internal helper for Union creation and substitution: flatten Unions
258 among parameters, then remove duplicates.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000259 """
260 # Flatten out Union[Union[...], ...].
261 params = []
262 for p in parameters:
Maggie Moss1b4552c2020-09-09 13:23:24 -0700263 if isinstance(p, (_UnionGenericAlias, types.Union)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000264 params.extend(p.__args__)
265 elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
266 params.extend(p[1:])
267 else:
268 params.append(p)
Yurii Karabasf03d3182020-11-17 04:23:19 +0200269
270 return tuple(_deduplicate(params))
271
272
273def _flatten_literal_params(parameters):
274 """An internal helper for Literal creation: flatten Literals among parameters"""
275 params = []
276 for p in parameters:
277 if isinstance(p, _LiteralGenericAlias):
278 params.extend(p.__args__)
279 else:
280 params.append(p)
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700281 return tuple(params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000282
283
284_cleanups = []
285
286
Yurii Karabasf03d3182020-11-17 04:23:19 +0200287def _tp_cache(func=None, /, *, typed=False):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000288 """Internal wrapper caching __getitem__ of generic types with a fallback to
289 original function for non-hashable arguments.
290 """
Yurii Karabasf03d3182020-11-17 04:23:19 +0200291 def decorator(func):
292 cached = functools.lru_cache(typed=typed)(func)
293 _cleanups.append(cached.cache_clear)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000294
Yurii Karabasf03d3182020-11-17 04:23:19 +0200295 @functools.wraps(func)
296 def inner(*args, **kwds):
297 try:
298 return cached(*args, **kwds)
299 except TypeError:
300 pass # All real errors (not unhashable args) are raised below.
301 return func(*args, **kwds)
302 return inner
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000303
Yurii Karabasf03d3182020-11-17 04:23:19 +0200304 if func is not None:
305 return decorator(func)
306
307 return decorator
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000308
wyfo653f4202020-07-22 21:47:28 +0200309def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
Graham Bleaney84ef33c2020-09-08 18:41:10 -0400310 """Evaluate all forward references in the given type t.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000311 For use of globalns and localns see the docstring for get_type_hints().
wyfo653f4202020-07-22 21:47:28 +0200312 recursive_guard is used to prevent prevent infinite recursion
313 with recursive ForwardRef.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000314 """
315 if isinstance(t, ForwardRef):
wyfo653f4202020-07-22 21:47:28 +0200316 return t._evaluate(globalns, localns, recursive_guard)
Ken Jina2721642021-07-19 22:22:59 +0800317 if isinstance(t, (_GenericAlias, GenericAlias, types.Union)):
wyfo653f4202020-07-22 21:47:28 +0200318 ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000319 if ev_args == t.__args__:
320 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300321 if isinstance(t, GenericAlias):
322 return GenericAlias(t.__origin__, ev_args)
Ken Jina2721642021-07-19 22:22:59 +0800323 if isinstance(t, types.Union):
324 return functools.reduce(operator.or_, ev_args)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300325 else:
326 return t.copy_with(ev_args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000327 return t
328
329
330class _Final:
331 """Mixin to prohibit subclassing"""
Guido van Rossum4cefe742016-09-27 15:20:12 -0700332
Guido van Rossum83ec3022017-01-17 20:43:28 -0800333 __slots__ = ('__weakref__',)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700334
Serhiy Storchaka2085bd02019-06-01 11:00:15 +0300335 def __init_subclass__(self, /, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000336 if '_root' not in kwds:
337 raise TypeError("Cannot subclass special typing classes")
338
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100339class _Immutable:
340 """Mixin to indicate that object should not be copied."""
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300341 __slots__ = ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000342
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100343 def __copy__(self):
344 return self
345
346 def __deepcopy__(self, memo):
347 return self
348
349
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300350# Internal indicator of special typing constructs.
351# See __doc__ instance attribute for specific docs.
352class _SpecialForm(_Final, _root=True):
353 __slots__ = ('_name', '__doc__', '_getitem')
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000354
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300355 def __init__(self, getitem):
356 self._getitem = getitem
357 self._name = getitem.__name__
358 self.__doc__ = getitem.__doc__
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000359
Miss Islington (bot)c895f2b2021-07-19 10:57:27 -0700360 def __getattr__(self, item):
361 if item in {'__name__', '__qualname__'}:
362 return self._name
363
364 raise AttributeError(item)
365
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300366 def __mro_entries__(self, bases):
367 raise TypeError(f"Cannot subclass {self!r}")
Guido van Rossum4cefe742016-09-27 15:20:12 -0700368
369 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000370 return 'typing.' + self._name
371
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100372 def __reduce__(self):
373 return self._name
Guido van Rossum4cefe742016-09-27 15:20:12 -0700374
375 def __call__(self, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000376 raise TypeError(f"Cannot instantiate {self!r}")
377
378 def __instancecheck__(self, obj):
379 raise TypeError(f"{self} cannot be used with isinstance()")
380
381 def __subclasscheck__(self, cls):
382 raise TypeError(f"{self} cannot be used with issubclass()")
383
384 @_tp_cache
385 def __getitem__(self, parameters):
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300386 return self._getitem(self, parameters)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700387
Yurii Karabasf03d3182020-11-17 04:23:19 +0200388
389class _LiteralSpecialForm(_SpecialForm, _root=True):
390 @_tp_cache(typed=True)
391 def __getitem__(self, parameters):
392 return self._getitem(self, parameters)
393
394
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300395@_SpecialForm
396def Any(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000397 """Special type indicating an unconstrained type.
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700398
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000399 - Any is compatible with every type.
400 - Any assumed to have all methods.
401 - All values assumed to be instances of Any.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700402
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000403 Note that all the above statements are true from the point of view of
404 static type checkers. At runtime, Any should not be used with instance
405 or class checks.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300406 """
407 raise TypeError(f"{self} is not subscriptable")
Guido van Rossumd70fe632015-08-05 12:11:06 +0200408
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300409@_SpecialForm
410def NoReturn(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000411 """Special type indicating functions that never return.
412 Example::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700413
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000414 from typing import NoReturn
415
416 def stop() -> NoReturn:
417 raise Exception('no way')
418
419 This type is invalid in other positions, e.g., ``List[NoReturn]``
420 will fail in static type checkers.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300421 """
422 raise TypeError(f"{self} is not subscriptable")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000423
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300424@_SpecialForm
425def ClassVar(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000426 """Special type construct to mark class variables.
427
428 An annotation wrapped in ClassVar indicates that a given
429 attribute is intended to be used as a class variable and
430 should not be set on instances of that class. Usage::
431
432 class Starship:
433 stats: ClassVar[Dict[str, int]] = {} # class variable
434 damage: int = 10 # instance variable
435
436 ClassVar accepts only types and cannot be further subscribed.
437
438 Note that ClassVar is not a class itself, and should not
439 be used with isinstance() or issubclass().
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300440 """
441 item = _type_check(parameters, f'{self} accepts only single type.')
442 return _GenericAlias(self, (item,))
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000443
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300444@_SpecialForm
445def Final(self, parameters):
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100446 """Special typing construct to indicate final names to type checkers.
447
448 A final name cannot be re-assigned or overridden in a subclass.
449 For example:
450
451 MAX_SIZE: Final = 9000
452 MAX_SIZE += 1 # Error reported by type checker
453
454 class Connection:
455 TIMEOUT: Final[int] = 10
456
457 class FastConnector(Connection):
458 TIMEOUT = 1 # Error reported by type checker
459
460 There is no runtime checking of these properties.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300461 """
462 item = _type_check(parameters, f'{self} accepts only single type.')
463 return _GenericAlias(self, (item,))
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100464
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300465@_SpecialForm
466def Union(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000467 """Union type; Union[X, Y] means either X or Y.
468
469 To define a union, use e.g. Union[int, str]. Details:
470 - The arguments must be types and there must be at least one.
471 - None as an argument is a special case and is replaced by
472 type(None).
473 - Unions of unions are flattened, e.g.::
474
475 Union[Union[int, str], float] == Union[int, str, float]
476
477 - Unions of a single argument vanish, e.g.::
478
479 Union[int] == int # The constructor actually returns int
480
481 - Redundant arguments are skipped, e.g.::
482
483 Union[int, str, int] == Union[int, str]
484
485 - When comparing unions, the argument order is ignored, e.g.::
486
487 Union[int, str] == Union[str, int]
488
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000489 - You cannot subclass or instantiate a union.
490 - You can use Optional[X] as a shorthand for Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300491 """
492 if parameters == ():
493 raise TypeError("Cannot take a Union of no types.")
494 if not isinstance(parameters, tuple):
495 parameters = (parameters,)
496 msg = "Union[arg, ...]: each arg must be a type."
497 parameters = tuple(_type_check(p, msg) for p in parameters)
498 parameters = _remove_dups_flatten(parameters)
499 if len(parameters) == 1:
500 return parameters[0]
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300501 return _UnionGenericAlias(self, parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000502
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300503@_SpecialForm
504def Optional(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000505 """Optional type.
506
507 Optional[X] is equivalent to Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300508 """
509 arg = _type_check(parameters, f"{self} requires a single type.")
510 return Union[arg, type(None)]
Guido van Rossumb7dedc82016-10-29 12:44:29 -0700511
Yurii Karabasf03d3182020-11-17 04:23:19 +0200512@_LiteralSpecialForm
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300513def Literal(self, parameters):
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100514 """Special typing form to define literal types (a.k.a. value types).
515
516 This form can be used to indicate to type checkers that the corresponding
517 variable or function parameter has a value equivalent to the provided
518 literal (or one of several literals):
519
520 def validate_simple(data: Any) -> Literal[True]: # always returns True
521 ...
522
523 MODE = Literal['r', 'rb', 'w', 'wb']
524 def open_helper(file: str, mode: MODE) -> str:
525 ...
526
527 open_helper('/some/path', 'r') # Passes type check
528 open_helper('/other/path', 'typo') # Error in type checker
529
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300530 Literal[...] cannot be subclassed. At runtime, an arbitrary value
531 is allowed as type argument to Literal[...], but type checkers may
532 impose restrictions.
533 """
534 # There is no '_type_check' call because arguments to Literal[...] are
535 # values, not types.
Yurii Karabasf03d3182020-11-17 04:23:19 +0200536 if not isinstance(parameters, tuple):
537 parameters = (parameters,)
538
539 parameters = _flatten_literal_params(parameters)
540
541 try:
542 parameters = tuple(p for p, _ in _deduplicate(list(_value_and_type_iter(parameters))))
543 except TypeError: # unhashable parameters
544 pass
545
546 return _LiteralGenericAlias(self, parameters)
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100547
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700548
Mikhail Golubev4f3c2502020-10-08 00:44:31 +0300549@_SpecialForm
550def TypeAlias(self, parameters):
551 """Special marker indicating that an assignment should
552 be recognized as a proper type alias definition by type
553 checkers.
554
555 For example::
556
557 Predicate: TypeAlias = Callable[..., bool]
558
559 It's invalid when used anywhere except as in the example above.
560 """
561 raise TypeError(f"{self} is not subscriptable")
562
563
kj73607be2020-12-24 12:33:48 +0800564@_SpecialForm
565def Concatenate(self, parameters):
Ken Jin11276cd2021-01-02 08:45:50 +0800566 """Used in conjunction with ``ParamSpec`` and ``Callable`` to represent a
567 higher order function which adds, removes or transforms parameters of a
568 callable.
kj73607be2020-12-24 12:33:48 +0800569
570 For example::
571
572 Callable[Concatenate[int, P], int]
573
574 See PEP 612 for detailed information.
575 """
576 if parameters == ():
577 raise TypeError("Cannot take a Concatenate of no types.")
578 if not isinstance(parameters, tuple):
579 parameters = (parameters,)
580 if not isinstance(parameters[-1], ParamSpec):
581 raise TypeError("The last parameter to Concatenate should be a "
582 "ParamSpec variable.")
583 msg = "Concatenate[arg, ...]: each arg must be a type."
584 parameters = tuple(_type_check(p, msg) for p in parameters)
585 return _ConcatenateGenericAlias(self, parameters)
586
587
Ken Jin05ab4b62021-04-27 22:31:04 +0800588@_SpecialForm
589def TypeGuard(self, parameters):
590 """Special typing form used to annotate the return type of a user-defined
591 type guard function. ``TypeGuard`` only accepts a single type argument.
592 At runtime, functions marked this way should return a boolean.
593
594 ``TypeGuard`` aims to benefit *type narrowing* -- a technique used by static
595 type checkers to determine a more precise type of an expression within a
596 program's code flow. Usually type narrowing is done by analyzing
597 conditional code flow and applying the narrowing to a block of code. The
598 conditional expression here is sometimes referred to as a "type guard".
599
600 Sometimes it would be convenient to use a user-defined boolean function
601 as a type guard. Such a function should use ``TypeGuard[...]`` as its
602 return type to alert static type checkers to this intention.
603
604 Using ``-> TypeGuard`` tells the static type checker that for a given
605 function:
606
607 1. The return value is a boolean.
608 2. If the return value is ``True``, the type of its argument
609 is the type inside ``TypeGuard``.
610
611 For example::
612
613 def is_str(val: Union[str, float]):
614 # "isinstance" type guard
615 if isinstance(val, str):
616 # Type of ``val`` is narrowed to ``str``
617 ...
618 else:
619 # Else, type of ``val`` is narrowed to ``float``.
620 ...
621
622 Strict type narrowing is not enforced -- ``TypeB`` need not be a narrower
623 form of ``TypeA`` (it can even be a wider form) and this may lead to
624 type-unsafe results. The main reason is to allow for things like
625 narrowing ``List[object]`` to ``List[str]`` even though the latter is not
626 a subtype of the former, since ``List`` is invariant. The responsibility of
627 writing type-safe type guards is left to the user.
628
629 ``TypeGuard`` also works with type variables. For more information, see
630 PEP 647 (User-Defined Type Guards).
631 """
632 item = _type_check(parameters, f'{self} accepts only single type.')
633 return _GenericAlias(self, (item,))
634
635
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000636class ForwardRef(_Final, _root=True):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800637 """Internal wrapper to hold a forward reference."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700638
Guido van Rossum4cefe742016-09-27 15:20:12 -0700639 __slots__ = ('__forward_arg__', '__forward_code__',
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400640 '__forward_evaluated__', '__forward_value__',
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700641 '__forward_is_argument__', '__forward_module__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700642
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700643 def __init__(self, arg, is_argument=True, module=None):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700644 if not isinstance(arg, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000645 raise TypeError(f"Forward reference must be a string -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700646 try:
647 code = compile(arg, '<string>', 'eval')
648 except SyntaxError:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000649 raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700650 self.__forward_arg__ = arg
651 self.__forward_code__ = code
652 self.__forward_evaluated__ = False
653 self.__forward_value__ = None
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400654 self.__forward_is_argument__ = is_argument
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700655 self.__forward_module__ = module
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700656
wyfo653f4202020-07-22 21:47:28 +0200657 def _evaluate(self, globalns, localns, recursive_guard):
658 if self.__forward_arg__ in recursive_guard:
659 return self
Guido van Rossumdad17902016-11-10 08:29:18 -0800660 if not self.__forward_evaluated__ or localns is not globalns:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700661 if globalns is None and localns is None:
662 globalns = localns = {}
663 elif globalns is None:
664 globalns = localns
665 elif localns is None:
666 localns = globalns
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700667 if self.__forward_module__ is not None:
668 globalns = getattr(
669 sys.modules.get(self.__forward_module__, None), '__dict__', globalns
670 )
wyfo653f4202020-07-22 21:47:28 +0200671 type_ =_type_check(
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700672 eval(self.__forward_code__, globalns, localns),
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400673 "Forward references must evaluate to types.",
wyfo653f4202020-07-22 21:47:28 +0200674 is_argument=self.__forward_is_argument__,
675 )
676 self.__forward_value__ = _eval_type(
677 type_, globalns, localns, recursive_guard | {self.__forward_arg__}
678 )
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700679 self.__forward_evaluated__ = True
680 return self.__forward_value__
681
Guido van Rossum4cefe742016-09-27 15:20:12 -0700682 def __eq__(self, other):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000683 if not isinstance(other, ForwardRef):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700684 return NotImplemented
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100685 if self.__forward_evaluated__ and other.__forward_evaluated__:
686 return (self.__forward_arg__ == other.__forward_arg__ and
687 self.__forward_value__ == other.__forward_value__)
688 return self.__forward_arg__ == other.__forward_arg__
Guido van Rossum4cefe742016-09-27 15:20:12 -0700689
690 def __hash__(self):
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100691 return hash(self.__forward_arg__)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700692
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700693 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000694 return f'ForwardRef({self.__forward_arg__!r})'
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700695
kj73607be2020-12-24 12:33:48 +0800696class _TypeVarLike:
697 """Mixin for TypeVar-like types (TypeVar and ParamSpec)."""
698 def __init__(self, bound, covariant, contravariant):
699 """Used to setup TypeVars and ParamSpec's bound, covariant and
700 contravariant attributes.
701 """
702 if covariant and contravariant:
703 raise ValueError("Bivariant types are not supported.")
704 self.__covariant__ = bool(covariant)
705 self.__contravariant__ = bool(contravariant)
706 if bound:
707 self.__bound__ = _type_check(bound, "Bound must be a type.")
708 else:
709 self.__bound__ = None
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700710
kj73607be2020-12-24 12:33:48 +0800711 def __or__(self, right):
712 return Union[self, right]
713
Jelle Zijlstra90459192021-04-10 20:00:05 -0700714 def __ror__(self, left):
715 return Union[left, self]
kj73607be2020-12-24 12:33:48 +0800716
717 def __repr__(self):
718 if self.__covariant__:
719 prefix = '+'
720 elif self.__contravariant__:
721 prefix = '-'
722 else:
723 prefix = '~'
724 return prefix + self.__name__
725
726 def __reduce__(self):
727 return self.__name__
728
729
730class TypeVar( _Final, _Immutable, _TypeVarLike, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700731 """Type variable.
732
733 Usage::
734
735 T = TypeVar('T') # Can be anything
736 A = TypeVar('A', str, bytes) # Must be str or bytes
737
738 Type variables exist primarily for the benefit of static type
739 checkers. They serve as the parameters for generic types as well
740 as for generic function definitions. See class Generic for more
741 information on generic types. Generic functions work as follows:
742
Guido van Rossumb24569a2016-11-20 18:01:29 -0800743 def repeat(x: T, n: int) -> List[T]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700744 '''Return a list containing n references to x.'''
745 return [x]*n
746
747 def longest(x: A, y: A) -> A:
748 '''Return the longest of two strings.'''
749 return x if len(x) >= len(y) else y
750
751 The latter example's signature is essentially the overloading
752 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
753 that if the arguments are instances of some subclass of str,
754 the return type is still plain str.
755
Guido van Rossumb24569a2016-11-20 18:01:29 -0800756 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700757
Guido van Rossumefa798d2016-08-23 11:01:50 -0700758 Type variables defined with covariant=True or contravariant=True
João D. Ferreira86bfed32018-07-07 16:41:20 +0100759 can be used to declare covariant or contravariant generic types.
Guido van Rossumefa798d2016-08-23 11:01:50 -0700760 See PEP 484 for more details. By default generic types are invariant
761 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700762
763 Type variables can be introspected. e.g.:
764
765 T.__name__ == 'T'
766 T.__constraints__ == ()
767 T.__covariant__ == False
768 T.__contravariant__ = False
769 A.__constraints__ == (str, bytes)
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100770
771 Note that only type variables defined in global scope can be pickled.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700772 """
773
Guido van Rossum4cefe742016-09-27 15:20:12 -0700774 __slots__ = ('__name__', '__bound__', '__constraints__',
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300775 '__covariant__', '__contravariant__', '__dict__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700776
777 def __init__(self, name, *constraints, bound=None,
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800778 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700779 self.__name__ = name
kj73607be2020-12-24 12:33:48 +0800780 super().__init__(bound, covariant, contravariant)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700781 if constraints and bound is not None:
782 raise TypeError("Constraints cannot be combined with bound=...")
783 if constraints and len(constraints) == 1:
784 raise TypeError("A single constraint is not allowed")
785 msg = "TypeVar(name, constraint, ...): constraints must be types."
786 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
HongWeipenga25a04f2020-04-21 04:01:53 +0800787 try:
788 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling
789 except (AttributeError, ValueError):
790 def_mod = None
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300791 if def_mod != 'typing':
792 self.__module__ = def_mod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700793
Maggie Moss1b4552c2020-09-09 13:23:24 -0700794
Jelle Zijlstra52243362021-04-10 19:57:05 -0700795class ParamSpecArgs(_Final, _Immutable, _root=True):
796 """The args for a ParamSpec object.
797
798 Given a ParamSpec object P, P.args is an instance of ParamSpecArgs.
799
800 ParamSpecArgs objects have a reference back to their ParamSpec:
801
802 P.args.__origin__ is P
803
804 This type is meant for runtime introspection and has no special meaning to
805 static type checkers.
806 """
807 def __init__(self, origin):
808 self.__origin__ = origin
809
810 def __repr__(self):
811 return f"{self.__origin__.__name__}.args"
812
813
814class ParamSpecKwargs(_Final, _Immutable, _root=True):
815 """The kwargs for a ParamSpec object.
816
817 Given a ParamSpec object P, P.kwargs is an instance of ParamSpecKwargs.
818
819 ParamSpecKwargs objects have a reference back to their ParamSpec:
820
821 P.kwargs.__origin__ is P
822
823 This type is meant for runtime introspection and has no special meaning to
824 static type checkers.
825 """
826 def __init__(self, origin):
827 self.__origin__ = origin
828
829 def __repr__(self):
830 return f"{self.__origin__.__name__}.kwargs"
831
832
kj73607be2020-12-24 12:33:48 +0800833class ParamSpec(_Final, _Immutable, _TypeVarLike, _root=True):
834 """Parameter specification variable.
Maggie Moss1b4552c2020-09-09 13:23:24 -0700835
kj73607be2020-12-24 12:33:48 +0800836 Usage::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700837
kj73607be2020-12-24 12:33:48 +0800838 P = ParamSpec('P')
839
840 Parameter specification variables exist primarily for the benefit of static
841 type checkers. They are used to forward the parameter types of one
Ken Jin11276cd2021-01-02 08:45:50 +0800842 callable to another callable, a pattern commonly found in higher order
843 functions and decorators. They are only valid when used in ``Concatenate``,
844 or s the first argument to ``Callable``, or as parameters for user-defined
845 Generics. See class Generic for more information on generic types. An
846 example for annotating a decorator::
kj73607be2020-12-24 12:33:48 +0800847
848 T = TypeVar('T')
849 P = ParamSpec('P')
850
851 def add_logging(f: Callable[P, T]) -> Callable[P, T]:
852 '''A type-safe decorator to add logging to a function.'''
853 def inner(*args: P.args, **kwargs: P.kwargs) -> T:
854 logging.info(f'{f.__name__} was called')
855 return f(*args, **kwargs)
856 return inner
857
858 @add_logging
859 def add_two(x: float, y: float) -> float:
860 '''Add two numbers together.'''
861 return x + y
862
863 Parameter specification variables defined with covariant=True or
864 contravariant=True can be used to declare covariant or contravariant
865 generic types. These keyword arguments are valid, but their actual semantics
866 are yet to be decided. See PEP 612 for details.
867
868 Parameter specification variables can be introspected. e.g.:
869
870 P.__name__ == 'T'
871 P.__bound__ == None
872 P.__covariant__ == False
873 P.__contravariant__ == False
874
875 Note that only parameter specification variables defined in global scope can
876 be pickled.
877 """
878
879 __slots__ = ('__name__', '__bound__', '__covariant__', '__contravariant__',
880 '__dict__')
881
Jelle Zijlstra52243362021-04-10 19:57:05 -0700882 @property
883 def args(self):
884 return ParamSpecArgs(self)
885
886 @property
887 def kwargs(self):
888 return ParamSpecKwargs(self)
kj73607be2020-12-24 12:33:48 +0800889
Ken Jinace008c2021-01-11 08:11:41 +0800890 def __init__(self, name, *, bound=None, covariant=False, contravariant=False):
kj73607be2020-12-24 12:33:48 +0800891 self.__name__ = name
892 super().__init__(bound, covariant, contravariant)
893 try:
894 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__')
895 except (AttributeError, ValueError):
896 def_mod = None
897 if def_mod != 'typing':
898 self.__module__ = def_mod
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100899
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700900
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000901def _is_dunder(attr):
902 return attr.startswith('__') and attr.endswith('__')
Guido van Rossum83ec3022017-01-17 20:43:28 -0800903
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300904class _BaseGenericAlias(_Final, _root=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000905 """The central part of internal API.
906
907 This represents a generic version of type 'origin' with type arguments 'params'.
908 There are two kind of these aliases: user defined and special. The special ones
909 are wrappers around builtin collections and ABCs in collections.abc. These must
910 have 'name' always set. If 'inst' is False, then the alias can't be instantiated,
911 this is used by e.g. typing.List and typing.Dict.
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700912 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300913 def __init__(self, origin, *, inst=True, name=None):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000914 self._inst = inst
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000915 self._name = name
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700916 self.__origin__ = origin
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000917 self.__slots__ = None # This is not documented.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300918
919 def __call__(self, *args, **kwargs):
920 if not self._inst:
921 raise TypeError(f"Type {self._name} cannot be instantiated; "
922 f"use {self.__origin__.__name__}() instead")
923 result = self.__origin__(*args, **kwargs)
924 try:
925 result.__orig_class__ = self
926 except AttributeError:
927 pass
928 return result
929
930 def __mro_entries__(self, bases):
931 res = []
932 if self.__origin__ not in bases:
933 res.append(self.__origin__)
934 i = bases.index(self)
935 for b in bases[i+1:]:
936 if isinstance(b, _BaseGenericAlias) or issubclass(b, Generic):
937 break
938 else:
939 res.append(Generic)
940 return tuple(res)
941
942 def __getattr__(self, attr):
Miss Islington (bot)c895f2b2021-07-19 10:57:27 -0700943 if attr in {'__name__', '__qualname__'}:
944 return self._name
945
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300946 # We are careful for copy and pickle.
947 # Also for simplicity we just don't relay all dunder names
948 if '__origin__' in self.__dict__ and not _is_dunder(attr):
949 return getattr(self.__origin__, attr)
950 raise AttributeError(attr)
951
952 def __setattr__(self, attr, val):
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700953 if _is_dunder(attr) or attr in {'_name', '_inst', '_nparams',
954 '_typevar_types', '_paramspec_tvars'}:
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300955 super().__setattr__(attr, val)
956 else:
957 setattr(self.__origin__, attr, val)
958
959 def __instancecheck__(self, obj):
960 return self.__subclasscheck__(type(obj))
961
962 def __subclasscheck__(self, cls):
963 raise TypeError("Subscripted generics cannot be used with"
964 " class and instance checks")
965
966
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300967# Special typing constructs Union, Optional, Generic, Callable and Tuple
968# use three special attributes for internal bookkeeping of generic types:
969# * __parameters__ is a tuple of unique free type parameters of a generic
970# type, for example, Dict[T, T].__parameters__ == (T,);
971# * __origin__ keeps a reference to a type that was subscripted,
972# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
973# the type.
974# * __args__ is a tuple of all arguments used in subscripting,
975# e.g., Dict[T, int].__args__ == (T, int).
976
977
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300978class _GenericAlias(_BaseGenericAlias, _root=True):
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700979 def __init__(self, origin, params, *, inst=True, name=None,
980 _typevar_types=TypeVar,
981 _paramspec_tvars=False):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300982 super().__init__(origin, inst=inst, name=name)
983 if not isinstance(params, tuple):
984 params = (params,)
985 self.__args__ = tuple(... if a is _TypingEllipsis else
986 () if a is _TypingEmpty else
987 a for a in params)
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700988 self.__parameters__ = _collect_type_vars(params, typevar_types=_typevar_types)
989 self._typevar_types = _typevar_types
990 self._paramspec_tvars = _paramspec_tvars
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300991 if not name:
992 self.__module__ = origin.__module__
993
994 def __eq__(self, other):
995 if not isinstance(other, _GenericAlias):
996 return NotImplemented
997 return (self.__origin__ == other.__origin__
998 and self.__args__ == other.__args__)
999
1000 def __hash__(self):
1001 return hash((self.__origin__, self.__args__))
1002
Maggie Moss1b4552c2020-09-09 13:23:24 -07001003 def __or__(self, right):
1004 return Union[self, right]
1005
Serhiy Storchaka80844d12021-07-16 16:42:04 +03001006 def __ror__(self, left):
1007 return Union[left, self]
Maggie Moss1b4552c2020-09-09 13:23:24 -07001008
Guido van Rossum4cefe742016-09-27 15:20:12 -07001009 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001010 def __getitem__(self, params):
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001011 if self.__origin__ in (Generic, Protocol):
1012 # Can't subscript Generic[...] or Protocol[...].
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001013 raise TypeError(f"Cannot subscript already-subscripted {self}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001014 if not isinstance(params, tuple):
1015 params = (params,)
kj73607be2020-12-24 12:33:48 +08001016 params = tuple(_type_convert(p) for p in params)
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001017 if self._paramspec_tvars:
1018 if any(isinstance(t, ParamSpec) for t in self.__parameters__):
1019 params = _prepare_paramspec_params(self, params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001020 _check_generic(self, params, len(self.__parameters__))
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001021
1022 subst = dict(zip(self.__parameters__, params))
1023 new_args = []
1024 for arg in self.__args__:
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001025 if isinstance(arg, self._typevar_types):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001026 arg = subst[arg]
Ken Jina2721642021-07-19 22:22:59 +08001027 elif isinstance(arg, (_GenericAlias, GenericAlias, types.Union)):
Serhiy Storchaka0122d482020-05-10 13:39:40 +03001028 subparams = arg.__parameters__
1029 if subparams:
1030 subargs = tuple(subst[x] for x in subparams)
1031 arg = arg[subargs]
kj73607be2020-12-24 12:33:48 +08001032 # Required to flatten out the args for CallableGenericAlias
1033 if self.__origin__ == collections.abc.Callable and isinstance(arg, tuple):
1034 new_args.extend(arg)
1035 else:
1036 new_args.append(arg)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001037 return self.copy_with(tuple(new_args))
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001038
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001039 def copy_with(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001040 return self.__class__(self.__origin__, params, name=self._name, inst=self._inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001041
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001042 def __repr__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001043 if self._name:
1044 name = 'typing.' + self._name
1045 else:
1046 name = _type_repr(self.__origin__)
1047 args = ", ".join([_type_repr(a) for a in self.__args__])
1048 return f'{name}[{args}]'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001049
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001050 def __reduce__(self):
1051 if self._name:
1052 origin = globals()[self._name]
1053 else:
1054 origin = self.__origin__
1055 args = tuple(self.__args__)
1056 if len(args) == 1 and not isinstance(args[0], tuple):
1057 args, = args
1058 return operator.getitem, (origin, args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001059
1060 def __mro_entries__(self, bases):
1061 if self._name: # generic version of an ABC or built-in class
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001062 return super().__mro_entries__(bases)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001063 if self.__origin__ is Generic:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001064 if Protocol in bases:
1065 return ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001066 i = bases.index(self)
1067 for b in bases[i+1:]:
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001068 if isinstance(b, _BaseGenericAlias) and b is not self:
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001069 return ()
1070 return (self.__origin__,)
1071
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001072
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001073# _nparams is the number of accepted parameters, e.g. 0 for Hashable,
1074# 1 for List and 2 for Dict. It may be -1 if variable number of
1075# parameters are accepted (needs custom __getitem__).
1076
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001077class _SpecialGenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001078 def __init__(self, origin, nparams, *, inst=True, name=None):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001079 if name is None:
1080 name = origin.__name__
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001081 super().__init__(origin, inst=inst, name=name)
1082 self._nparams = nparams
Serhiy Storchaka2fbc57a2020-05-10 15:14:27 +03001083 if origin.__module__ == 'builtins':
1084 self.__doc__ = f'A generic version of {origin.__qualname__}.'
1085 else:
1086 self.__doc__ = f'A generic version of {origin.__module__}.{origin.__qualname__}.'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001087
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001088 @_tp_cache
1089 def __getitem__(self, params):
1090 if not isinstance(params, tuple):
1091 params = (params,)
1092 msg = "Parameters to generic types must be types."
1093 params = tuple(_type_check(p, msg) for p in params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001094 _check_generic(self, params, self._nparams)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001095 return self.copy_with(params)
1096
1097 def copy_with(self, params):
1098 return _GenericAlias(self.__origin__, params,
1099 name=self._name, inst=self._inst)
1100
1101 def __repr__(self):
1102 return 'typing.' + self._name
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001103
1104 def __subclasscheck__(self, cls):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001105 if isinstance(cls, _SpecialGenericAlias):
1106 return issubclass(cls.__origin__, self.__origin__)
1107 if not isinstance(cls, _GenericAlias):
1108 return issubclass(cls, self.__origin__)
1109 return super().__subclasscheck__(cls)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001110
Ivan Levkivskyi83494032018-03-26 23:01:12 +01001111 def __reduce__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001112 return self._name
Ivan Levkivskyi83494032018-03-26 23:01:12 +01001113
Maggie Moss1b4552c2020-09-09 13:23:24 -07001114 def __or__(self, right):
1115 return Union[self, right]
1116
Serhiy Storchaka80844d12021-07-16 16:42:04 +03001117 def __ror__(self, left):
1118 return Union[left, self]
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001119
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001120class _CallableGenericAlias(_GenericAlias, _root=True):
1121 def __repr__(self):
1122 assert self._name == 'Callable'
kj73607be2020-12-24 12:33:48 +08001123 args = self.__args__
1124 if len(args) == 2 and (args[0] is Ellipsis
1125 or isinstance(args[0], (ParamSpec, _ConcatenateGenericAlias))):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001126 return super().__repr__()
1127 return (f'typing.Callable'
kj73607be2020-12-24 12:33:48 +08001128 f'[[{", ".join([_type_repr(a) for a in args[:-1]])}], '
1129 f'{_type_repr(args[-1])}]')
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001130
1131 def __reduce__(self):
1132 args = self.__args__
kj73607be2020-12-24 12:33:48 +08001133 if not (len(args) == 2 and (args[0] is Ellipsis
1134 or isinstance(args[0], (ParamSpec, _ConcatenateGenericAlias)))):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001135 args = list(args[:-1]), args[-1]
1136 return operator.getitem, (Callable, args)
1137
1138
1139class _CallableType(_SpecialGenericAlias, _root=True):
1140 def copy_with(self, params):
1141 return _CallableGenericAlias(self.__origin__, params,
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001142 name=self._name, inst=self._inst,
1143 _typevar_types=(TypeVar, ParamSpec),
1144 _paramspec_tvars=True)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001145
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001146 def __getitem__(self, params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001147 if not isinstance(params, tuple) or len(params) != 2:
1148 raise TypeError("Callable must be used as "
1149 "Callable[[arg, ...], result].")
1150 args, result = params
kj463c7d32020-12-14 02:38:24 +08001151 # This relaxes what args can be on purpose to allow things like
1152 # PEP 612 ParamSpec. Responsibility for whether a user is using
1153 # Callable[...] properly is deferred to static type checkers.
1154 if isinstance(args, list):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001155 params = (tuple(args), result)
kj463c7d32020-12-14 02:38:24 +08001156 else:
1157 params = (args, result)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001158 return self.__getitem_inner__(params)
1159
1160 @_tp_cache
1161 def __getitem_inner__(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001162 args, result = params
1163 msg = "Callable[args, result]: result must be a type."
1164 result = _type_check(result, msg)
1165 if args is Ellipsis:
1166 return self.copy_with((_TypingEllipsis, result))
kj463c7d32020-12-14 02:38:24 +08001167 if not isinstance(args, tuple):
1168 args = (args,)
1169 args = tuple(_type_convert(arg) for arg in args)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001170 params = args + (result,)
1171 return self.copy_with(params)
1172
1173
1174class _TupleType(_SpecialGenericAlias, _root=True):
1175 @_tp_cache
1176 def __getitem__(self, params):
1177 if params == ():
1178 return self.copy_with((_TypingEmpty,))
1179 if not isinstance(params, tuple):
1180 params = (params,)
1181 if len(params) == 2 and params[1] is ...:
1182 msg = "Tuple[t, ...]: t must be a type."
1183 p = _type_check(params[0], msg)
1184 return self.copy_with((p, _TypingEllipsis))
1185 msg = "Tuple[t0, t1, ...]: each t must be a type."
1186 params = tuple(_type_check(p, msg) for p in params)
1187 return self.copy_with(params)
1188
1189
1190class _UnionGenericAlias(_GenericAlias, _root=True):
1191 def copy_with(self, params):
1192 return Union[params]
1193
1194 def __eq__(self, other):
Miss Islington (bot)03aad302021-07-17 14:10:21 -07001195 if not isinstance(other, (_UnionGenericAlias, types.Union)):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001196 return NotImplemented
1197 return set(self.__args__) == set(other.__args__)
1198
1199 def __hash__(self):
1200 return hash(frozenset(self.__args__))
1201
1202 def __repr__(self):
1203 args = self.__args__
1204 if len(args) == 2:
1205 if args[0] is type(None):
1206 return f'typing.Optional[{_type_repr(args[1])}]'
1207 elif args[1] is type(None):
1208 return f'typing.Optional[{_type_repr(args[0])}]'
1209 return super().__repr__()
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001210
Maggie Moss1b4552c2020-09-09 13:23:24 -07001211 def __instancecheck__(self, obj):
1212 return self.__subclasscheck__(type(obj))
1213
1214 def __subclasscheck__(self, cls):
1215 for arg in self.__args__:
1216 if issubclass(cls, arg):
1217 return True
1218
1219
Yurii Karabasf03d3182020-11-17 04:23:19 +02001220def _value_and_type_iter(parameters):
1221 return ((p, type(p)) for p in parameters)
1222
1223
1224class _LiteralGenericAlias(_GenericAlias, _root=True):
1225
1226 def __eq__(self, other):
1227 if not isinstance(other, _LiteralGenericAlias):
1228 return NotImplemented
1229
1230 return set(_value_and_type_iter(self.__args__)) == set(_value_and_type_iter(other.__args__))
1231
1232 def __hash__(self):
Yurii Karabas1b540772020-11-19 18:17:38 +02001233 return hash(frozenset(_value_and_type_iter(self.__args__)))
Yurii Karabasf03d3182020-11-17 04:23:19 +02001234
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001235
kj73607be2020-12-24 12:33:48 +08001236class _ConcatenateGenericAlias(_GenericAlias, _root=True):
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001237 def __init__(self, *args, **kwargs):
1238 super().__init__(*args, **kwargs,
1239 _typevar_types=(TypeVar, ParamSpec),
1240 _paramspec_tvars=True)
kj73607be2020-12-24 12:33:48 +08001241
1242
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001243class Generic:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001244 """Abstract base class for generic types.
1245
Guido van Rossumb24569a2016-11-20 18:01:29 -08001246 A generic type is typically declared by inheriting from
1247 this class parameterized with one or more type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001248 For example, a generic mapping type might be defined as::
1249
1250 class Mapping(Generic[KT, VT]):
1251 def __getitem__(self, key: KT) -> VT:
1252 ...
1253 # Etc.
1254
1255 This class can then be used as follows::
1256
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001257 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001258 try:
1259 return mapping[key]
1260 except KeyError:
1261 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001262 """
Guido van Rossumd70fe632015-08-05 12:11:06 +02001263 __slots__ = ()
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001264 _is_protocol = False
Guido van Rossumd70fe632015-08-05 12:11:06 +02001265
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001266 @_tp_cache
1267 def __class_getitem__(cls, params):
1268 if not isinstance(params, tuple):
1269 params = (params,)
1270 if not params and cls is not Tuple:
1271 raise TypeError(
1272 f"Parameter list to {cls.__qualname__}[...] cannot be empty")
kj73607be2020-12-24 12:33:48 +08001273 params = tuple(_type_convert(p) for p in params)
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001274 if cls in (Generic, Protocol):
1275 # Generic and Protocol can only be subscripted with unique type variables.
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001276 if not all(isinstance(p, (TypeVar, ParamSpec)) for p in params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001277 raise TypeError(
kj73607be2020-12-24 12:33:48 +08001278 f"Parameters to {cls.__name__}[...] must all be type variables "
1279 f"or parameter specification variables.")
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001280 if len(set(params)) != len(params):
1281 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001282 f"Parameters to {cls.__name__}[...] must all be unique")
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001283 else:
1284 # Subscripting a regular Generic subclass.
kj73607be2020-12-24 12:33:48 +08001285 if any(isinstance(t, ParamSpec) for t in cls.__parameters__):
1286 params = _prepare_paramspec_params(cls, params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001287 _check_generic(cls, params, len(cls.__parameters__))
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001288 return _GenericAlias(cls, params,
1289 _typevar_types=(TypeVar, ParamSpec),
1290 _paramspec_tvars=True)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001291
1292 def __init_subclass__(cls, *args, **kwargs):
Ivan Levkivskyiee566fe2018-04-04 17:00:15 +01001293 super().__init_subclass__(*args, **kwargs)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001294 tvars = []
1295 if '__orig_bases__' in cls.__dict__:
1296 error = Generic in cls.__orig_bases__
1297 else:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001298 error = Generic in cls.__bases__ and cls.__name__ != 'Protocol'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001299 if error:
1300 raise TypeError("Cannot inherit from plain Generic")
1301 if '__orig_bases__' in cls.__dict__:
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001302 tvars = _collect_type_vars(cls.__orig_bases__, (TypeVar, ParamSpec))
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001303 # Look for Generic[T1, ..., Tn].
1304 # If found, tvars must be a subset of it.
1305 # If not found, tvars is it.
1306 # Also check for and reject plain Generic,
1307 # and reject multiple Generic[...].
1308 gvars = None
1309 for base in cls.__orig_bases__:
1310 if (isinstance(base, _GenericAlias) and
1311 base.__origin__ is Generic):
1312 if gvars is not None:
1313 raise TypeError(
1314 "Cannot inherit from Generic[...] multiple types.")
1315 gvars = base.__parameters__
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001316 if gvars is not None:
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001317 tvarset = set(tvars)
1318 gvarset = set(gvars)
1319 if not tvarset <= gvarset:
1320 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
1321 s_args = ', '.join(str(g) for g in gvars)
1322 raise TypeError(f"Some type variables ({s_vars}) are"
1323 f" not listed in Generic[{s_args}]")
1324 tvars = gvars
1325 cls.__parameters__ = tuple(tvars)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001326
1327
1328class _TypingEmpty:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001329 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
1330 to allow empty list/tuple in specific places, without allowing them
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001331 to sneak in where prohibited.
1332 """
1333
1334
1335class _TypingEllipsis:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001336 """Internal placeholder for ... (ellipsis)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001337
1338
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001339_TYPING_INTERNALS = ['__parameters__', '__orig_bases__', '__orig_class__',
1340 '_is_protocol', '_is_runtime_protocol']
1341
1342_SPECIAL_NAMES = ['__abstractmethods__', '__annotations__', '__dict__', '__doc__',
1343 '__init__', '__module__', '__new__', '__slots__',
Guido van Rossum48b069a2020-04-07 09:50:06 -07001344 '__subclasshook__', '__weakref__', '__class_getitem__']
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001345
1346# These special attributes will be not collected as protocol members.
1347EXCLUDED_ATTRIBUTES = _TYPING_INTERNALS + _SPECIAL_NAMES + ['_MutableMapping__marker']
1348
1349
1350def _get_protocol_attrs(cls):
1351 """Collect protocol members from a protocol class objects.
1352
1353 This includes names actually defined in the class dictionary, as well
1354 as names that appear in annotations. Special names (above) are skipped.
1355 """
1356 attrs = set()
1357 for base in cls.__mro__[:-1]: # without object
1358 if base.__name__ in ('Protocol', 'Generic'):
1359 continue
1360 annotations = getattr(base, '__annotations__', {})
1361 for attr in list(base.__dict__.keys()) + list(annotations.keys()):
1362 if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
1363 attrs.add(attr)
1364 return attrs
1365
1366
1367def _is_callable_members_only(cls):
1368 # PEP 544 prohibits using issubclass() with protocols that have non-method members.
1369 return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls))
1370
1371
1372def _no_init(self, *args, **kwargs):
1373 if type(self)._is_protocol:
1374 raise TypeError('Protocols cannot be instantiated')
1375
Miss Islington (bot)c2f33df2021-07-20 08:24:57 -07001376def _callee(depth=2, default=None):
1377 try:
1378 return sys._getframe(depth).f_globals['__name__']
1379 except (AttributeError, ValueError): # For platforms without _getframe()
1380 return default
1381
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001382
Miss Islington (bot)a2d94a02021-05-12 09:09:04 -07001383def _allow_reckless_class_checks(depth=3):
Nickolena Fishercfaf4c02020-04-26 12:49:11 -05001384 """Allow instance and class checks for special stdlib modules.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001385
1386 The abc and functools modules indiscriminately call isinstance() and
1387 issubclass() on the whole MRO of a user class, which may contain protocols.
1388 """
1389 try:
Miss Islington (bot)a2d94a02021-05-12 09:09:04 -07001390 return sys._getframe(depth).f_globals['__name__'] in ['abc', 'functools']
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001391 except (AttributeError, ValueError): # For platforms without _getframe().
1392 return True
1393
1394
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001395_PROTO_ALLOWLIST = {
Divij Rajkumar692a0dc2019-09-12 11:13:51 +01001396 'collections.abc': [
1397 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
1398 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
1399 ],
1400 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
1401}
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001402
1403
1404class _ProtocolMeta(ABCMeta):
1405 # This metaclass is really unfortunate and exists only because of
1406 # the lack of __instancehook__.
1407 def __instancecheck__(cls, instance):
1408 # We need this method for situations where attributes are
1409 # assigned in __init__.
Miss Islington (bot)a2d94a02021-05-12 09:09:04 -07001410 if (
1411 getattr(cls, '_is_protocol', False) and
1412 not getattr(cls, '_is_runtime_protocol', False) and
1413 not _allow_reckless_class_checks(depth=2)
1414 ):
1415 raise TypeError("Instance and class checks can only be used with"
1416 " @runtime_checkable protocols")
1417
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001418 if ((not getattr(cls, '_is_protocol', False) or
1419 _is_callable_members_only(cls)) and
1420 issubclass(instance.__class__, cls)):
1421 return True
1422 if cls._is_protocol:
1423 if all(hasattr(instance, attr) and
1424 # All *methods* can be blocked by setting them to None.
1425 (not callable(getattr(cls, attr, None)) or
1426 getattr(instance, attr) is not None)
1427 for attr in _get_protocol_attrs(cls)):
1428 return True
1429 return super().__instancecheck__(instance)
1430
1431
1432class Protocol(Generic, metaclass=_ProtocolMeta):
1433 """Base class for protocol classes.
1434
1435 Protocol classes are defined as::
1436
1437 class Proto(Protocol):
1438 def meth(self) -> int:
1439 ...
1440
1441 Such classes are primarily used with static type checkers that recognize
1442 structural subtyping (static duck-typing), for example::
1443
1444 class C:
1445 def meth(self) -> int:
1446 return 0
1447
1448 def func(x: Proto) -> int:
1449 return x.meth()
1450
1451 func(C()) # Passes static type check
1452
1453 See PEP 544 for details. Protocol classes decorated with
1454 @typing.runtime_checkable act as simple-minded runtime protocols that check
1455 only the presence of given attributes, ignoring their type signatures.
1456 Protocol classes can be generic, they are defined as::
1457
1458 class GenProto(Protocol[T]):
1459 def meth(self) -> T:
1460 ...
1461 """
1462 __slots__ = ()
1463 _is_protocol = True
1464 _is_runtime_protocol = False
1465
1466 def __init_subclass__(cls, *args, **kwargs):
1467 super().__init_subclass__(*args, **kwargs)
1468
1469 # Determine if this is a protocol or a concrete subclass.
1470 if not cls.__dict__.get('_is_protocol', False):
1471 cls._is_protocol = any(b is Protocol for b in cls.__bases__)
1472
1473 # Set (or override) the protocol subclass hook.
1474 def _proto_hook(other):
1475 if not cls.__dict__.get('_is_protocol', False):
1476 return NotImplemented
1477
1478 # First, perform various sanity checks.
1479 if not getattr(cls, '_is_runtime_protocol', False):
Rossc1af1282020-12-29 11:55:28 +00001480 if _allow_reckless_class_checks():
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001481 return NotImplemented
1482 raise TypeError("Instance and class checks can only be used with"
1483 " @runtime_checkable protocols")
1484 if not _is_callable_members_only(cls):
Rossc1af1282020-12-29 11:55:28 +00001485 if _allow_reckless_class_checks():
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001486 return NotImplemented
1487 raise TypeError("Protocols with non-method members"
1488 " don't support issubclass()")
1489 if not isinstance(other, type):
1490 # Same error message as for issubclass(1, int).
1491 raise TypeError('issubclass() arg 1 must be a class')
1492
1493 # Second, perform the actual structural compatibility check.
1494 for attr in _get_protocol_attrs(cls):
1495 for base in other.__mro__:
1496 # Check if the members appears in the class dictionary...
1497 if attr in base.__dict__:
1498 if base.__dict__[attr] is None:
1499 return NotImplemented
1500 break
1501
1502 # ...or in annotations, if it is a sub-protocol.
1503 annotations = getattr(base, '__annotations__', {})
1504 if (isinstance(annotations, collections.abc.Mapping) and
1505 attr in annotations and
1506 issubclass(other, Generic) and other._is_protocol):
1507 break
1508 else:
1509 return NotImplemented
1510 return True
1511
1512 if '__subclasshook__' not in cls.__dict__:
1513 cls.__subclasshook__ = _proto_hook
1514
1515 # We have nothing more to do for non-protocols...
1516 if not cls._is_protocol:
1517 return
1518
1519 # ... otherwise check consistency of bases, and prohibit instantiation.
1520 for base in cls.__bases__:
1521 if not (base in (object, Generic) or
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001522 base.__module__ in _PROTO_ALLOWLIST and
1523 base.__name__ in _PROTO_ALLOWLIST[base.__module__] or
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001524 issubclass(base, Generic) and base._is_protocol):
1525 raise TypeError('Protocols can only inherit from other'
1526 ' protocols, got %r' % base)
1527 cls.__init__ = _no_init
1528
1529
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001530class _AnnotatedAlias(_GenericAlias, _root=True):
1531 """Runtime representation of an annotated type.
1532
1533 At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't'
1534 with extra annotations. The alias behaves like a normal typing alias,
1535 instantiating is the same as instantiating the underlying type, binding
1536 it to types is also the same.
1537 """
1538 def __init__(self, origin, metadata):
1539 if isinstance(origin, _AnnotatedAlias):
1540 metadata = origin.__metadata__ + metadata
1541 origin = origin.__origin__
1542 super().__init__(origin, origin)
1543 self.__metadata__ = metadata
1544
1545 def copy_with(self, params):
1546 assert len(params) == 1
1547 new_type = params[0]
1548 return _AnnotatedAlias(new_type, self.__metadata__)
1549
1550 def __repr__(self):
1551 return "typing.Annotated[{}, {}]".format(
1552 _type_repr(self.__origin__),
1553 ", ".join(repr(a) for a in self.__metadata__)
1554 )
1555
1556 def __reduce__(self):
1557 return operator.getitem, (
1558 Annotated, (self.__origin__,) + self.__metadata__
1559 )
1560
1561 def __eq__(self, other):
1562 if not isinstance(other, _AnnotatedAlias):
1563 return NotImplemented
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001564 return (self.__origin__ == other.__origin__
1565 and self.__metadata__ == other.__metadata__)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001566
1567 def __hash__(self):
1568 return hash((self.__origin__, self.__metadata__))
1569
1570
1571class Annotated:
1572 """Add context specific metadata to a type.
1573
1574 Example: Annotated[int, runtime_check.Unsigned] indicates to the
1575 hypothetical runtime_check module that this type is an unsigned int.
1576 Every other consumer of this type can ignore this metadata and treat
1577 this type as int.
1578
1579 The first argument to Annotated must be a valid type.
1580
1581 Details:
1582
1583 - It's an error to call `Annotated` with less than two arguments.
1584 - Nested Annotated are flattened::
1585
1586 Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
1587
1588 - Instantiating an annotated type is equivalent to instantiating the
1589 underlying type::
1590
1591 Annotated[C, Ann1](5) == C(5)
1592
1593 - Annotated can be used as a generic type alias::
1594
1595 Optimized = Annotated[T, runtime.Optimize()]
1596 Optimized[int] == Annotated[int, runtime.Optimize()]
1597
1598 OptimizedList = Annotated[List[T], runtime.Optimize()]
1599 OptimizedList[int] == Annotated[List[int], runtime.Optimize()]
1600 """
1601
1602 __slots__ = ()
1603
1604 def __new__(cls, *args, **kwargs):
1605 raise TypeError("Type Annotated cannot be instantiated.")
1606
1607 @_tp_cache
1608 def __class_getitem__(cls, params):
1609 if not isinstance(params, tuple) or len(params) < 2:
1610 raise TypeError("Annotated[...] should be used "
1611 "with at least two arguments (a type and an "
1612 "annotation).")
1613 msg = "Annotated[t, ...]: t must be a type."
1614 origin = _type_check(params[0], msg)
1615 metadata = tuple(params[1:])
1616 return _AnnotatedAlias(origin, metadata)
1617
1618 def __init_subclass__(cls, *args, **kwargs):
1619 raise TypeError(
1620 "Cannot subclass {}.Annotated".format(cls.__module__)
1621 )
1622
1623
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001624def runtime_checkable(cls):
1625 """Mark a protocol class as a runtime protocol.
1626
1627 Such protocol can be used with isinstance() and issubclass().
1628 Raise TypeError if applied to a non-protocol class.
1629 This allows a simple-minded structural check very similar to
1630 one trick ponies in collections.abc such as Iterable.
1631 For example::
1632
1633 @runtime_checkable
1634 class Closable(Protocol):
1635 def close(self): ...
1636
1637 assert isinstance(open('/some/file'), Closable)
1638
1639 Warning: this will check only the presence of the required methods,
1640 not their type signatures!
1641 """
1642 if not issubclass(cls, Generic) or not cls._is_protocol:
1643 raise TypeError('@runtime_checkable can be only applied to protocol classes,'
1644 ' got %r' % cls)
1645 cls._is_runtime_protocol = True
1646 return cls
1647
1648
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001649def cast(typ, val):
1650 """Cast a value to a type.
1651
1652 This returns the value unchanged. To the type checker this
1653 signals that the return value has the designated type, but at
1654 runtime we intentionally don't check anything (we want this
1655 to be as fast as possible).
1656 """
1657 return val
1658
1659
1660def _get_defaults(func):
1661 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -08001662 try:
1663 code = func.__code__
1664 except AttributeError:
1665 # Some built-in functions don't have __code__, __defaults__, etc.
1666 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001667 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001668 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001669 arg_names = arg_names[:pos_count]
1670 defaults = func.__defaults__ or ()
1671 kwdefaults = func.__kwdefaults__
1672 res = dict(kwdefaults) if kwdefaults else {}
1673 pos_offset = pos_count - len(defaults)
1674 for name, value in zip(arg_names[pos_offset:], defaults):
1675 assert name not in res
1676 res[name] = value
1677 return res
1678
1679
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001680_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
1681 types.MethodType, types.ModuleType,
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02001682 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001683
1684
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001685def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
Guido van Rossum991d14f2016-11-09 13:12:51 -08001686 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001687
Guido van Rossum991d14f2016-11-09 13:12:51 -08001688 This is often the same as obj.__annotations__, but it handles
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001689 forward references encoded as string literals, adds Optional[t] if a
1690 default value equal to None is set and recursively replaces all
1691 'Annotated[T, ...]' with 'T' (unless 'include_extras=True').
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001692
Guido van Rossum991d14f2016-11-09 13:12:51 -08001693 The argument may be a module, class, method, or function. The annotations
1694 are returned as a dictionary. For classes, annotations include also
1695 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001696
Guido van Rossum991d14f2016-11-09 13:12:51 -08001697 TypeError is raised if the argument is not of a type that can contain
1698 annotations, and an empty dictionary is returned if no annotations are
1699 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001700
Guido van Rossum991d14f2016-11-09 13:12:51 -08001701 BEWARE -- the behavior of globalns and localns is counterintuitive
1702 (unless you are familiar with how eval() and exec() work). The
1703 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001704
Guido van Rossum991d14f2016-11-09 13:12:51 -08001705 - If no dict arguments are passed, an attempt is made to use the
Łukasz Langaf350a262017-09-14 14:33:00 -04001706 globals from obj (or the respective module's globals for classes),
1707 and these are also used as the locals. If the object does not appear
Ken Jin1b1f9852021-04-27 01:31:21 +08001708 to have globals, an empty dictionary is used. For classes, the search
1709 order is globals first then locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001710
Guido van Rossum991d14f2016-11-09 13:12:51 -08001711 - If one dict argument is passed, it is used for both globals and
1712 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001713
Guido van Rossum991d14f2016-11-09 13:12:51 -08001714 - If two dict arguments are passed, they specify globals and
1715 locals, respectively.
1716 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001717
Guido van Rossum991d14f2016-11-09 13:12:51 -08001718 if getattr(obj, '__no_type_check__', None):
1719 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001720 # Classes require a special treatment.
1721 if isinstance(obj, type):
1722 hints = {}
1723 for base in reversed(obj.__mro__):
Łukasz Langaf350a262017-09-14 14:33:00 -04001724 if globalns is None:
Miss Islington (bot)3df23b52021-06-26 16:52:28 -07001725 base_globals = getattr(sys.modules.get(base.__module__, None), '__dict__', {})
Łukasz Langaf350a262017-09-14 14:33:00 -04001726 else:
1727 base_globals = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001728 ann = base.__dict__.get('__annotations__', {})
larryhastings2f2b6982021-04-29 20:09:08 -07001729 if isinstance(ann, types.GetSetDescriptorType):
1730 ann = {}
Ken Jin852150d2021-04-13 01:23:12 +08001731 base_locals = dict(vars(base)) if localns is None else localns
Ken Jin1b1f9852021-04-27 01:31:21 +08001732 if localns is None and globalns is None:
1733 # This is surprising, but required. Before Python 3.10,
1734 # get_type_hints only evaluated the globalns of
1735 # a class. To maintain backwards compatibility, we reverse
1736 # the globalns and localns order so that eval() looks into
1737 # *base_globals* first rather than *base_locals*.
1738 # This only affects ForwardRefs.
1739 base_globals, base_locals = base_locals, base_globals
Guido van Rossum991d14f2016-11-09 13:12:51 -08001740 for name, value in ann.items():
1741 if value is None:
1742 value = type(None)
1743 if isinstance(value, str):
Nina Zakharenko0e61dff2018-05-22 20:32:10 -07001744 value = ForwardRef(value, is_argument=False)
Ken Jin852150d2021-04-13 01:23:12 +08001745 value = _eval_type(value, base_globals, base_locals)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001746 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001747 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
Łukasz Langaf350a262017-09-14 14:33:00 -04001748
1749 if globalns is None:
1750 if isinstance(obj, types.ModuleType):
1751 globalns = obj.__dict__
1752 else:
benedwards140aca3a32019-11-21 17:24:58 +00001753 nsobj = obj
1754 # Find globalns for the unwrapped object.
1755 while hasattr(nsobj, '__wrapped__'):
1756 nsobj = nsobj.__wrapped__
1757 globalns = getattr(nsobj, '__globals__', {})
Łukasz Langaf350a262017-09-14 14:33:00 -04001758 if localns is None:
1759 localns = globalns
1760 elif localns is None:
1761 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001762 hints = getattr(obj, '__annotations__', None)
1763 if hints is None:
1764 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001765 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001766 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001767 else:
1768 raise TypeError('{!r} is not a module, class, method, '
1769 'or function.'.format(obj))
1770 defaults = _get_defaults(obj)
1771 hints = dict(hints)
1772 for name, value in hints.items():
1773 if value is None:
1774 value = type(None)
1775 if isinstance(value, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001776 value = ForwardRef(value)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001777 value = _eval_type(value, globalns, localns)
1778 if name in defaults and defaults[name] is None:
1779 value = Optional[value]
1780 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001781 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
1782
1783
1784def _strip_annotations(t):
1785 """Strips the annotations from a given type.
1786 """
1787 if isinstance(t, _AnnotatedAlias):
1788 return _strip_annotations(t.__origin__)
1789 if isinstance(t, _GenericAlias):
1790 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1791 if stripped_args == t.__args__:
1792 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001793 return t.copy_with(stripped_args)
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001794 if isinstance(t, GenericAlias):
1795 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1796 if stripped_args == t.__args__:
1797 return t
1798 return GenericAlias(t.__origin__, stripped_args)
Ken Jina2721642021-07-19 22:22:59 +08001799 if isinstance(t, types.Union):
1800 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1801 if stripped_args == t.__args__:
1802 return t
1803 return functools.reduce(operator.or_, stripped_args)
1804
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001805 return t
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001806
1807
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001808def get_origin(tp):
1809 """Get the unsubscripted version of a type.
1810
Jakub Stasiak38aaaaa2020-02-07 02:15:12 +01001811 This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
1812 and Annotated. Return None for unsupported types. Examples::
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001813
1814 get_origin(Literal[42]) is Literal
1815 get_origin(int) is None
1816 get_origin(ClassVar[int]) is ClassVar
1817 get_origin(Generic) is Generic
1818 get_origin(Generic[T]) is Generic
1819 get_origin(Union[T, int]) is Union
1820 get_origin(List[Tuple[T, T]][int]) == list
Jelle Zijlstra52243362021-04-10 19:57:05 -07001821 get_origin(P.args) is P
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001822 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001823 if isinstance(tp, _AnnotatedAlias):
1824 return Annotated
Jelle Zijlstra52243362021-04-10 19:57:05 -07001825 if isinstance(tp, (_BaseGenericAlias, GenericAlias,
1826 ParamSpecArgs, ParamSpecKwargs)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001827 return tp.__origin__
1828 if tp is Generic:
1829 return Generic
Ken Jinefb1f092020-12-29 10:26:19 +08001830 if isinstance(tp, types.Union):
1831 return types.Union
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001832 return None
1833
1834
1835def get_args(tp):
1836 """Get type arguments with all substitutions performed.
1837
1838 For unions, basic simplifications used by Union constructor are performed.
1839 Examples::
1840 get_args(Dict[str, int]) == (str, int)
1841 get_args(int) == ()
1842 get_args(Union[int, Union[T, int], str][int]) == (int, str)
1843 get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
1844 get_args(Callable[[], T][int]) == ([], int)
1845 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001846 if isinstance(tp, _AnnotatedAlias):
1847 return (tp.__origin__,) + tp.__metadata__
Ken Jin4140f102020-12-29 04:06:19 +08001848 if isinstance(tp, (_GenericAlias, GenericAlias)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001849 res = tp.__args__
Ken Jinefb1f092020-12-29 10:26:19 +08001850 if (tp.__origin__ is collections.abc.Callable
1851 and not (res[0] is Ellipsis
1852 or isinstance(res[0], (ParamSpec, _ConcatenateGenericAlias)))):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001853 res = (list(res[:-1]), res[-1])
1854 return res
Ken Jinefb1f092020-12-29 10:26:19 +08001855 if isinstance(tp, types.Union):
1856 return tp.__args__
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001857 return ()
1858
1859
Patrick Reader0705ec82020-09-16 05:58:32 +01001860def is_typeddict(tp):
1861 """Check if an annotation is a TypedDict class
1862
1863 For example::
1864 class Film(TypedDict):
1865 title: str
1866 year: int
1867
1868 is_typeddict(Film) # => True
1869 is_typeddict(Union[list, str]) # => False
1870 """
1871 return isinstance(tp, _TypedDictMeta)
1872
1873
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001874def no_type_check(arg):
1875 """Decorator to indicate that annotations are not type hints.
1876
1877 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001878 applies recursively to all methods and classes defined in that class
1879 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001880
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001881 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001882 """
1883 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001884 arg_attrs = arg.__dict__.copy()
1885 for attr, val in arg.__dict__.items():
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001886 if val in arg.__bases__ + (arg,):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001887 arg_attrs.pop(attr)
1888 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001889 if isinstance(obj, types.FunctionType):
1890 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001891 if isinstance(obj, type):
1892 no_type_check(obj)
1893 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001894 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001895 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001896 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001897 return arg
1898
1899
1900def no_type_check_decorator(decorator):
1901 """Decorator to give another decorator the @no_type_check effect.
1902
1903 This wraps the decorator with something that wraps the decorated
1904 function in @no_type_check.
1905 """
1906
1907 @functools.wraps(decorator)
1908 def wrapped_decorator(*args, **kwds):
1909 func = decorator(*args, **kwds)
1910 func = no_type_check(func)
1911 return func
1912
1913 return wrapped_decorator
1914
1915
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001916def _overload_dummy(*args, **kwds):
1917 """Helper for @overload to raise when called."""
1918 raise NotImplementedError(
1919 "You should not call an overloaded function. "
1920 "A series of @overload-decorated functions "
1921 "outside a stub module should always be followed "
1922 "by an implementation that is not @overload-ed.")
1923
1924
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001925def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001926 """Decorator for overloaded functions/methods.
1927
1928 In a stub file, place two or more stub definitions for the same
1929 function in a row, each decorated with @overload. For example:
1930
1931 @overload
1932 def utf8(value: None) -> None: ...
1933 @overload
1934 def utf8(value: bytes) -> bytes: ...
1935 @overload
1936 def utf8(value: str) -> bytes: ...
1937
1938 In a non-stub file (i.e. a regular .py file), do the same but
1939 follow it with an implementation. The implementation should *not*
1940 be decorated with @overload. For example:
1941
1942 @overload
1943 def utf8(value: None) -> None: ...
1944 @overload
1945 def utf8(value: bytes) -> bytes: ...
1946 @overload
1947 def utf8(value: str) -> bytes: ...
1948 def utf8(value):
1949 # implementation goes here
1950 """
1951 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001952
1953
Ivan Levkivskyif3672422019-05-26 09:37:07 +01001954def final(f):
1955 """A decorator to indicate final methods and final classes.
1956
1957 Use this decorator to indicate to type checkers that the decorated
1958 method cannot be overridden, and decorated class cannot be subclassed.
1959 For example:
1960
1961 class Base:
1962 @final
1963 def done(self) -> None:
1964 ...
1965 class Sub(Base):
1966 def done(self) -> None: # Error reported by type checker
1967 ...
1968
1969 @final
1970 class Leaf:
1971 ...
1972 class Other(Leaf): # Error reported by type checker
1973 ...
1974
1975 There is no runtime checking of these properties.
1976 """
1977 return f
1978
1979
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001980# Some unconstrained type variables. These are used by the container types.
1981# (These are not for export.)
1982T = TypeVar('T') # Any type.
1983KT = TypeVar('KT') # Key type.
1984VT = TypeVar('VT') # Value type.
1985T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
1986V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
1987VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
1988T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
1989# Internal type variable used for Type[].
1990CT_co = TypeVar('CT_co', covariant=True, bound=type)
1991
1992# A useful type variable with constraints. This represents string types.
1993# (This one *is* for export!)
1994AnyStr = TypeVar('AnyStr', bytes, str)
1995
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001996
1997# Various ABCs mimicking those in collections.abc.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001998_alias = _SpecialGenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001999
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002000Hashable = _alias(collections.abc.Hashable, 0) # Not generic.
2001Awaitable = _alias(collections.abc.Awaitable, 1)
2002Coroutine = _alias(collections.abc.Coroutine, 3)
2003AsyncIterable = _alias(collections.abc.AsyncIterable, 1)
2004AsyncIterator = _alias(collections.abc.AsyncIterator, 1)
2005Iterable = _alias(collections.abc.Iterable, 1)
2006Iterator = _alias(collections.abc.Iterator, 1)
2007Reversible = _alias(collections.abc.Reversible, 1)
2008Sized = _alias(collections.abc.Sized, 0) # Not generic.
2009Container = _alias(collections.abc.Container, 1)
2010Collection = _alias(collections.abc.Collection, 1)
2011Callable = _CallableType(collections.abc.Callable, 2)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002012Callable.__doc__ = \
2013 """Callable type; Callable[[int], str] is a function of (int) -> str.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002014
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002015 The subscription syntax must always be used with exactly two
2016 values: the argument list and the return type. The argument list
2017 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002018
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002019 There is no syntax to indicate optional or keyword arguments,
2020 such function types are rarely used as callback types.
2021 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002022AbstractSet = _alias(collections.abc.Set, 1, name='AbstractSet')
2023MutableSet = _alias(collections.abc.MutableSet, 1)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002024# NOTE: Mapping is only covariant in the value type.
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002025Mapping = _alias(collections.abc.Mapping, 2)
2026MutableMapping = _alias(collections.abc.MutableMapping, 2)
2027Sequence = _alias(collections.abc.Sequence, 1)
2028MutableSequence = _alias(collections.abc.MutableSequence, 1)
2029ByteString = _alias(collections.abc.ByteString, 0) # Not generic
2030# Tuple accepts variable number of parameters.
2031Tuple = _TupleType(tuple, -1, inst=False, name='Tuple')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002032Tuple.__doc__ = \
2033 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07002034
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002035 Example: Tuple[T1, T2] is a tuple of two elements corresponding
2036 to type variables T1 and T2. Tuple[int, float, str] is a tuple
2037 of an int, a float and a string.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07002038
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002039 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
2040 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002041List = _alias(list, 1, inst=False, name='List')
2042Deque = _alias(collections.deque, 1, name='Deque')
2043Set = _alias(set, 1, inst=False, name='Set')
2044FrozenSet = _alias(frozenset, 1, inst=False, name='FrozenSet')
2045MappingView = _alias(collections.abc.MappingView, 1)
2046KeysView = _alias(collections.abc.KeysView, 1)
2047ItemsView = _alias(collections.abc.ItemsView, 2)
2048ValuesView = _alias(collections.abc.ValuesView, 1)
2049ContextManager = _alias(contextlib.AbstractContextManager, 1, name='ContextManager')
2050AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, 1, name='AsyncContextManager')
2051Dict = _alias(dict, 2, inst=False, name='Dict')
2052DefaultDict = _alias(collections.defaultdict, 2, name='DefaultDict')
2053OrderedDict = _alias(collections.OrderedDict, 2)
2054Counter = _alias(collections.Counter, 1)
2055ChainMap = _alias(collections.ChainMap, 2)
2056Generator = _alias(collections.abc.Generator, 3)
2057AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2)
2058Type = _alias(type, 1, inst=False, name='Type')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002059Type.__doc__ = \
2060 """A special construct usable to annotate class objects.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07002061
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002062 For example, suppose we have the following classes::
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07002063
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002064 class User: ... # Abstract base for User classes
2065 class BasicUser(User): ...
2066 class ProUser(User): ...
2067 class TeamUser(User): ...
Guido van Rossumf17c2002015-12-03 17:31:24 -08002068
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002069 And a function that takes a class argument that's a subclass of
2070 User and returns an instance of the corresponding class::
Guido van Rossumf17c2002015-12-03 17:31:24 -08002071
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002072 U = TypeVar('U', bound=User)
2073 def new_user(user_class: Type[U]) -> U:
2074 user = user_class()
2075 # (Here we could write the user object to a database)
2076 return user
Guido van Rossumf17c2002015-12-03 17:31:24 -08002077
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002078 joe = new_user(BasicUser)
Guido van Rossumf17c2002015-12-03 17:31:24 -08002079
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002080 At this point the type checker knows that joe has type BasicUser.
2081 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002082
2083
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002084@runtime_checkable
2085class SupportsInt(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002086 """An ABC with one abstract method __int__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002087 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002088
2089 @abstractmethod
2090 def __int__(self) -> int:
2091 pass
2092
2093
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002094@runtime_checkable
2095class SupportsFloat(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002096 """An ABC with one abstract method __float__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002097 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002098
2099 @abstractmethod
2100 def __float__(self) -> float:
2101 pass
2102
2103
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002104@runtime_checkable
2105class SupportsComplex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002106 """An ABC with one abstract method __complex__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002107 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002108
2109 @abstractmethod
2110 def __complex__(self) -> complex:
2111 pass
2112
2113
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002114@runtime_checkable
2115class SupportsBytes(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002116 """An ABC with one abstract method __bytes__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002117 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002118
2119 @abstractmethod
2120 def __bytes__(self) -> bytes:
2121 pass
2122
2123
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002124@runtime_checkable
2125class SupportsIndex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002126 """An ABC with one abstract method __index__."""
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -07002127 __slots__ = ()
2128
2129 @abstractmethod
2130 def __index__(self) -> int:
2131 pass
2132
2133
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002134@runtime_checkable
2135class SupportsAbs(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002136 """An ABC with one abstract method __abs__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002137 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002138
2139 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02002140 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002141 pass
2142
2143
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002144@runtime_checkable
2145class SupportsRound(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002146 """An ABC with one abstract method __round__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002147 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002148
2149 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02002150 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002151 pass
2152
2153
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002154def _make_nmtuple(name, types, module, defaults = ()):
2155 fields = [n for n, t in types]
2156 types = {n: _type_check(t, f"field {n} annotation must be a type")
2157 for n, t in types}
2158 nm_tpl = collections.namedtuple(name, fields,
2159 defaults=defaults, module=module)
2160 nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002161 return nm_tpl
2162
2163
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002164# attributes prohibited to set in NamedTuple class syntax
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002165_prohibited = frozenset({'__new__', '__init__', '__slots__', '__getnewargs__',
2166 '_fields', '_field_defaults',
2167 '_make', '_replace', '_asdict', '_source'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002168
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002169_special = frozenset({'__module__', '__name__', '__annotations__'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002170
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002171
Guido van Rossum2f841442016-11-15 09:48:06 -08002172class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002173
Guido van Rossum2f841442016-11-15 09:48:06 -08002174 def __new__(cls, typename, bases, ns):
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002175 assert bases[0] is _NamedTuple
Guido van Rossum2f841442016-11-15 09:48:06 -08002176 types = ns.get('__annotations__', {})
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002177 default_names = []
Guido van Rossum3c268be2017-01-18 08:03:50 -08002178 for field_name in types:
2179 if field_name in ns:
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002180 default_names.append(field_name)
2181 elif default_names:
2182 raise TypeError(f"Non-default namedtuple field {field_name} "
2183 f"cannot follow default field"
2184 f"{'s' if len(default_names) > 1 else ''} "
2185 f"{', '.join(default_names)}")
2186 nm_tpl = _make_nmtuple(typename, types.items(),
2187 defaults=[ns[n] for n in default_names],
2188 module=ns['__module__'])
Guido van Rossum95919c02017-01-22 17:47:20 -08002189 # update from user namespace without overriding special namedtuple attributes
2190 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002191 if key in _prohibited:
2192 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
2193 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08002194 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08002195 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002196
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002197
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002198def NamedTuple(typename, fields=None, /, **kwargs):
Guido van Rossum2f841442016-11-15 09:48:06 -08002199 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002200
Guido van Rossum2f841442016-11-15 09:48:06 -08002201 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002202
Guido van Rossum2f841442016-11-15 09:48:06 -08002203 class Employee(NamedTuple):
2204 name: str
2205 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002206
Guido van Rossum2f841442016-11-15 09:48:06 -08002207 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002208
Guido van Rossum2f841442016-11-15 09:48:06 -08002209 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002210
Raymond Hettingerf7b57df2019-03-18 09:53:56 -07002211 The resulting class has an extra __annotations__ attribute, giving a
2212 dict that maps field names to types. (The field names are also in
2213 the _fields attribute, which is part of the namedtuple API.)
2214 Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002215
Guido van Rossum2f841442016-11-15 09:48:06 -08002216 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002217
Guido van Rossum2f841442016-11-15 09:48:06 -08002218 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002219
Guido van Rossum2f841442016-11-15 09:48:06 -08002220 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
2221 """
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002222 if fields is None:
2223 fields = kwargs.items()
2224 elif kwargs:
2225 raise TypeError("Either list of fields or keywords"
2226 " can be provided to NamedTuple, not both")
2227 try:
2228 module = sys._getframe(1).f_globals.get('__name__', '__main__')
2229 except (AttributeError, ValueError):
2230 module = None
2231 return _make_nmtuple(typename, fields, module=module)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002232
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002233_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})
2234
2235def _namedtuple_mro_entries(bases):
2236 if len(bases) > 1:
2237 raise TypeError("Multiple inheritance with NamedTuple is not supported")
2238 assert bases[0] is NamedTuple
2239 return (_NamedTuple,)
2240
2241NamedTuple.__mro_entries__ = _namedtuple_mro_entries
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002242
2243
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002244class _TypedDictMeta(type):
2245 def __new__(cls, name, bases, ns, total=True):
2246 """Create new typed dict class object.
2247
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002248 This method is called when TypedDict is subclassed,
2249 or when TypedDict is instantiated. This way
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002250 TypedDict supports all three syntax forms described in its docstring.
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002251 Subclasses and instances of TypedDict return actual dictionaries.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002252 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002253 for base in bases:
2254 if type(base) is not _TypedDictMeta:
2255 raise TypeError('cannot inherit from both a TypedDict type '
2256 'and a non-TypedDict base class')
2257 tp_dict = type.__new__(_TypedDictMeta, name, (dict,), ns)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002258
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002259 annotations = {}
2260 own_annotations = ns.get('__annotations__', {})
2261 own_annotation_keys = set(own_annotations.keys())
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002262 msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002263 own_annotations = {
Miss Islington (bot)480f29f2021-07-17 01:48:17 -07002264 n: _type_check(tp, msg, module=tp_dict.__module__)
2265 for n, tp in own_annotations.items()
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002266 }
2267 required_keys = set()
2268 optional_keys = set()
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002269
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002270 for base in bases:
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002271 annotations.update(base.__dict__.get('__annotations__', {}))
2272 required_keys.update(base.__dict__.get('__required_keys__', ()))
2273 optional_keys.update(base.__dict__.get('__optional_keys__', ()))
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002274
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002275 annotations.update(own_annotations)
2276 if total:
2277 required_keys.update(own_annotation_keys)
2278 else:
2279 optional_keys.update(own_annotation_keys)
2280
2281 tp_dict.__annotations__ = annotations
2282 tp_dict.__required_keys__ = frozenset(required_keys)
2283 tp_dict.__optional_keys__ = frozenset(optional_keys)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002284 if not hasattr(tp_dict, '__total__'):
2285 tp_dict.__total__ = total
2286 return tp_dict
2287
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002288 __call__ = dict # static method
2289
2290 def __subclasscheck__(cls, other):
2291 # Typed dicts are only for static structural subtyping.
2292 raise TypeError('TypedDict does not support instance and class checks')
2293
2294 __instancecheck__ = __subclasscheck__
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002295
2296
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002297def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002298 """A simple typed namespace. At runtime it is equivalent to a plain dict.
2299
2300 TypedDict creates a dictionary type that expects all of its
2301 instances to have a certain set of keys, where each key is
2302 associated with a value of a consistent type. This expectation
2303 is not checked at runtime but is only enforced by type checkers.
2304 Usage::
2305
2306 class Point2D(TypedDict):
2307 x: int
2308 y: int
2309 label: str
2310
2311 a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
2312 b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
2313
2314 assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
2315
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002316 The type info can be accessed via the Point2D.__annotations__ dict, and
2317 the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
2318 TypedDict supports two additional equivalent forms::
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002319
2320 Point2D = TypedDict('Point2D', x=int, y=int, label=str)
2321 Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
2322
ananthan-123ab6423f2020-02-19 10:03:05 +05302323 By default, all keys must be present in a TypedDict. It is possible
2324 to override this by specifying totality.
2325 Usage::
2326
2327 class point2D(TypedDict, total=False):
2328 x: int
2329 y: int
2330
2331 This means that a point2D TypedDict can have any of the keys omitted.A type
2332 checker is only expected to support a literal False or True as the value of
2333 the total argument. True is the default, and makes all items defined in the
2334 class body be required.
2335
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002336 The class syntax is only supported in Python 3.6+, while two other
2337 syntax forms work for Python 2.7 and 3.2+
2338 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002339 if fields is None:
2340 fields = kwargs
2341 elif kwargs:
2342 raise TypeError("TypedDict takes either a dict or keyword arguments,"
2343 " but not both")
2344
Alex Grönholm67b769f2020-12-10 23:49:05 +02002345 ns = {'__annotations__': dict(fields)}
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002346 try:
2347 # Setting correct module is necessary to make typed dict classes pickleable.
2348 ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
2349 except (AttributeError, ValueError):
2350 pass
2351
Alex Grönholm67b769f2020-12-10 23:49:05 +02002352 return _TypedDictMeta(typename, (), ns, total=total)
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002353
2354_TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {})
2355TypedDict.__mro_entries__ = lambda bases: (_TypedDict,)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002356
2357
Miss Islington (bot)c2f33df2021-07-20 08:24:57 -07002358class NewType:
Guido van Rossum91185fe2016-06-08 11:19:11 -07002359 """NewType creates simple unique types with almost zero
2360 runtime overhead. NewType(name, tp) is considered a subtype of tp
2361 by static type checkers. At runtime, NewType(name, tp) returns
2362 a dummy function that simply returns its argument. Usage::
2363
2364 UserId = NewType('UserId', int)
2365
2366 def name_by_id(user_id: UserId) -> str:
2367 ...
2368
2369 UserId('user') # Fails type check
2370
2371 name_by_id(42) # Fails type check
2372 name_by_id(UserId(42)) # OK
2373
2374 num = UserId(5) + 1 # type: int
2375 """
2376
Miss Islington (bot)c2f33df2021-07-20 08:24:57 -07002377 def __init__(self, name, tp):
2378 self.__name__ = name
2379 self.__qualname__ = name
2380 self.__module__ = _callee(default='typing')
2381 self.__supertype__ = tp
2382
2383 def __repr__(self):
2384 return f'{self.__module__}.{self.__qualname__}'
2385
2386 def __call__(self, x):
Guido van Rossum91185fe2016-06-08 11:19:11 -07002387 return x
2388
Miss Islington (bot)c2f33df2021-07-20 08:24:57 -07002389 def __or__(self, other):
2390 return Union[self, other]
2391
2392 def __ror__(self, other):
2393 return Union[other, self]
Guido van Rossum91185fe2016-06-08 11:19:11 -07002394
2395
Guido van Rossum0e0563c2016-04-05 14:54:25 -07002396# Python-version-specific alias (Python 2: unicode; Python 3: str)
2397Text = str
2398
2399
Guido van Rossum91185fe2016-06-08 11:19:11 -07002400# Constant that's True when type checking, but False here.
2401TYPE_CHECKING = False
2402
2403
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002404class IO(Generic[AnyStr]):
2405 """Generic base class for TextIO and BinaryIO.
2406
2407 This is an abstract, generic version of the return of open().
2408
2409 NOTE: This does not distinguish between the different possible
2410 classes (text vs. binary, read vs. write vs. read/write,
2411 append-only, unbuffered). The TextIO and BinaryIO subclasses
2412 below capture the distinctions between text vs. binary, which is
2413 pervasive in the interface; however we currently do not offer a
2414 way to track the other distinctions in the type system.
2415 """
2416
Guido van Rossumd70fe632015-08-05 12:11:06 +02002417 __slots__ = ()
2418
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002419 @property
2420 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002421 def mode(self) -> str:
2422 pass
2423
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002424 @property
2425 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002426 def name(self) -> str:
2427 pass
2428
2429 @abstractmethod
2430 def close(self) -> None:
2431 pass
2432
Shantanu2e6569b2020-01-29 18:52:36 -08002433 @property
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002434 @abstractmethod
2435 def closed(self) -> bool:
2436 pass
2437
2438 @abstractmethod
2439 def fileno(self) -> int:
2440 pass
2441
2442 @abstractmethod
2443 def flush(self) -> None:
2444 pass
2445
2446 @abstractmethod
2447 def isatty(self) -> bool:
2448 pass
2449
2450 @abstractmethod
2451 def read(self, n: int = -1) -> AnyStr:
2452 pass
2453
2454 @abstractmethod
2455 def readable(self) -> bool:
2456 pass
2457
2458 @abstractmethod
2459 def readline(self, limit: int = -1) -> AnyStr:
2460 pass
2461
2462 @abstractmethod
2463 def readlines(self, hint: int = -1) -> List[AnyStr]:
2464 pass
2465
2466 @abstractmethod
2467 def seek(self, offset: int, whence: int = 0) -> int:
2468 pass
2469
2470 @abstractmethod
2471 def seekable(self) -> bool:
2472 pass
2473
2474 @abstractmethod
2475 def tell(self) -> int:
2476 pass
2477
2478 @abstractmethod
2479 def truncate(self, size: int = None) -> int:
2480 pass
2481
2482 @abstractmethod
2483 def writable(self) -> bool:
2484 pass
2485
2486 @abstractmethod
2487 def write(self, s: AnyStr) -> int:
2488 pass
2489
2490 @abstractmethod
2491 def writelines(self, lines: List[AnyStr]) -> None:
2492 pass
2493
2494 @abstractmethod
2495 def __enter__(self) -> 'IO[AnyStr]':
2496 pass
2497
2498 @abstractmethod
2499 def __exit__(self, type, value, traceback) -> None:
2500 pass
2501
2502
2503class BinaryIO(IO[bytes]):
2504 """Typed version of the return of open() in binary mode."""
2505
Guido van Rossumd70fe632015-08-05 12:11:06 +02002506 __slots__ = ()
2507
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002508 @abstractmethod
2509 def write(self, s: Union[bytes, bytearray]) -> int:
2510 pass
2511
2512 @abstractmethod
2513 def __enter__(self) -> 'BinaryIO':
2514 pass
2515
2516
2517class TextIO(IO[str]):
2518 """Typed version of the return of open() in text mode."""
2519
Guido van Rossumd70fe632015-08-05 12:11:06 +02002520 __slots__ = ()
2521
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002522 @property
2523 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002524 def buffer(self) -> BinaryIO:
2525 pass
2526
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002527 @property
2528 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002529 def encoding(self) -> str:
2530 pass
2531
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002532 @property
2533 @abstractmethod
Guido van Rossum991d14f2016-11-09 13:12:51 -08002534 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002535 pass
2536
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002537 @property
2538 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002539 def line_buffering(self) -> bool:
2540 pass
2541
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002542 @property
2543 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002544 def newlines(self) -> Any:
2545 pass
2546
2547 @abstractmethod
2548 def __enter__(self) -> 'TextIO':
2549 pass
2550
2551
2552class io:
2553 """Wrapper namespace for IO generic classes."""
2554
2555 __all__ = ['IO', 'TextIO', 'BinaryIO']
2556 IO = IO
2557 TextIO = TextIO
2558 BinaryIO = BinaryIO
2559
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002560
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002561io.__name__ = __name__ + '.io'
2562sys.modules[io.__name__] = io
2563
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002564Pattern = _alias(stdlib_re.Pattern, 1)
2565Match = _alias(stdlib_re.Match, 1)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002566
2567class re:
2568 """Wrapper namespace for re type aliases."""
2569
2570 __all__ = ['Pattern', 'Match']
2571 Pattern = Pattern
2572 Match = Match
2573
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002574
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002575re.__name__ = __name__ + '.re'
2576sys.modules[re.__name__] = re