blob: 1823cb83ee915f7297838456fcd847eb4a9f6d96 [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
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700198def _collect_type_vars(types, typevar_types=None):
199 """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 = []
207 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)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300210 if isinstance(t, (_GenericAlias, GenericAlias)):
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)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300317 if isinstance(t, (_GenericAlias, GenericAlias)):
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)
323 else:
324 return t.copy_with(ev_args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000325 return t
326
327
328class _Final:
329 """Mixin to prohibit subclassing"""
Guido van Rossum4cefe742016-09-27 15:20:12 -0700330
Guido van Rossum83ec3022017-01-17 20:43:28 -0800331 __slots__ = ('__weakref__',)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700332
Serhiy Storchaka2085bd02019-06-01 11:00:15 +0300333 def __init_subclass__(self, /, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000334 if '_root' not in kwds:
335 raise TypeError("Cannot subclass special typing classes")
336
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100337class _Immutable:
338 """Mixin to indicate that object should not be copied."""
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300339 __slots__ = ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000340
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100341 def __copy__(self):
342 return self
343
344 def __deepcopy__(self, memo):
345 return self
346
347
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300348# Internal indicator of special typing constructs.
349# See __doc__ instance attribute for specific docs.
350class _SpecialForm(_Final, _root=True):
351 __slots__ = ('_name', '__doc__', '_getitem')
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000352
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300353 def __init__(self, getitem):
354 self._getitem = getitem
355 self._name = getitem.__name__
356 self.__doc__ = getitem.__doc__
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000357
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300358 def __mro_entries__(self, bases):
359 raise TypeError(f"Cannot subclass {self!r}")
Guido van Rossum4cefe742016-09-27 15:20:12 -0700360
361 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000362 return 'typing.' + self._name
363
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100364 def __reduce__(self):
365 return self._name
Guido van Rossum4cefe742016-09-27 15:20:12 -0700366
367 def __call__(self, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000368 raise TypeError(f"Cannot instantiate {self!r}")
369
370 def __instancecheck__(self, obj):
371 raise TypeError(f"{self} cannot be used with isinstance()")
372
373 def __subclasscheck__(self, cls):
374 raise TypeError(f"{self} cannot be used with issubclass()")
375
376 @_tp_cache
377 def __getitem__(self, parameters):
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300378 return self._getitem(self, parameters)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700379
Yurii Karabasf03d3182020-11-17 04:23:19 +0200380
381class _LiteralSpecialForm(_SpecialForm, _root=True):
382 @_tp_cache(typed=True)
383 def __getitem__(self, parameters):
384 return self._getitem(self, parameters)
385
386
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300387@_SpecialForm
388def Any(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000389 """Special type indicating an unconstrained type.
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700390
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000391 - Any is compatible with every type.
392 - Any assumed to have all methods.
393 - All values assumed to be instances of Any.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700394
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000395 Note that all the above statements are true from the point of view of
396 static type checkers. At runtime, Any should not be used with instance
397 or class checks.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300398 """
399 raise TypeError(f"{self} is not subscriptable")
Guido van Rossumd70fe632015-08-05 12:11:06 +0200400
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300401@_SpecialForm
402def NoReturn(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000403 """Special type indicating functions that never return.
404 Example::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700405
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000406 from typing import NoReturn
407
408 def stop() -> NoReturn:
409 raise Exception('no way')
410
411 This type is invalid in other positions, e.g., ``List[NoReturn]``
412 will fail in static type checkers.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300413 """
414 raise TypeError(f"{self} is not subscriptable")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000415
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300416@_SpecialForm
417def ClassVar(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000418 """Special type construct to mark class variables.
419
420 An annotation wrapped in ClassVar indicates that a given
421 attribute is intended to be used as a class variable and
422 should not be set on instances of that class. Usage::
423
424 class Starship:
425 stats: ClassVar[Dict[str, int]] = {} # class variable
426 damage: int = 10 # instance variable
427
428 ClassVar accepts only types and cannot be further subscribed.
429
430 Note that ClassVar is not a class itself, and should not
431 be used with isinstance() or issubclass().
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300432 """
433 item = _type_check(parameters, f'{self} accepts only single type.')
434 return _GenericAlias(self, (item,))
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000435
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300436@_SpecialForm
437def Final(self, parameters):
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100438 """Special typing construct to indicate final names to type checkers.
439
440 A final name cannot be re-assigned or overridden in a subclass.
441 For example:
442
443 MAX_SIZE: Final = 9000
444 MAX_SIZE += 1 # Error reported by type checker
445
446 class Connection:
447 TIMEOUT: Final[int] = 10
448
449 class FastConnector(Connection):
450 TIMEOUT = 1 # Error reported by type checker
451
452 There is no runtime checking of these properties.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300453 """
454 item = _type_check(parameters, f'{self} accepts only single type.')
455 return _GenericAlias(self, (item,))
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100456
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300457@_SpecialForm
458def Union(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000459 """Union type; Union[X, Y] means either X or Y.
460
461 To define a union, use e.g. Union[int, str]. Details:
462 - The arguments must be types and there must be at least one.
463 - None as an argument is a special case and is replaced by
464 type(None).
465 - Unions of unions are flattened, e.g.::
466
467 Union[Union[int, str], float] == Union[int, str, float]
468
469 - Unions of a single argument vanish, e.g.::
470
471 Union[int] == int # The constructor actually returns int
472
473 - Redundant arguments are skipped, e.g.::
474
475 Union[int, str, int] == Union[int, str]
476
477 - When comparing unions, the argument order is ignored, e.g.::
478
479 Union[int, str] == Union[str, int]
480
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000481 - You cannot subclass or instantiate a union.
482 - You can use Optional[X] as a shorthand for Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300483 """
484 if parameters == ():
485 raise TypeError("Cannot take a Union of no types.")
486 if not isinstance(parameters, tuple):
487 parameters = (parameters,)
488 msg = "Union[arg, ...]: each arg must be a type."
489 parameters = tuple(_type_check(p, msg) for p in parameters)
490 parameters = _remove_dups_flatten(parameters)
491 if len(parameters) == 1:
492 return parameters[0]
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300493 return _UnionGenericAlias(self, parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000494
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300495@_SpecialForm
496def Optional(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000497 """Optional type.
498
499 Optional[X] is equivalent to Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300500 """
501 arg = _type_check(parameters, f"{self} requires a single type.")
502 return Union[arg, type(None)]
Guido van Rossumb7dedc82016-10-29 12:44:29 -0700503
Yurii Karabasf03d3182020-11-17 04:23:19 +0200504@_LiteralSpecialForm
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300505def Literal(self, parameters):
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100506 """Special typing form to define literal types (a.k.a. value types).
507
508 This form can be used to indicate to type checkers that the corresponding
509 variable or function parameter has a value equivalent to the provided
510 literal (or one of several literals):
511
512 def validate_simple(data: Any) -> Literal[True]: # always returns True
513 ...
514
515 MODE = Literal['r', 'rb', 'w', 'wb']
516 def open_helper(file: str, mode: MODE) -> str:
517 ...
518
519 open_helper('/some/path', 'r') # Passes type check
520 open_helper('/other/path', 'typo') # Error in type checker
521
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300522 Literal[...] cannot be subclassed. At runtime, an arbitrary value
523 is allowed as type argument to Literal[...], but type checkers may
524 impose restrictions.
525 """
526 # There is no '_type_check' call because arguments to Literal[...] are
527 # values, not types.
Yurii Karabasf03d3182020-11-17 04:23:19 +0200528 if not isinstance(parameters, tuple):
529 parameters = (parameters,)
530
531 parameters = _flatten_literal_params(parameters)
532
533 try:
534 parameters = tuple(p for p, _ in _deduplicate(list(_value_and_type_iter(parameters))))
535 except TypeError: # unhashable parameters
536 pass
537
538 return _LiteralGenericAlias(self, parameters)
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100539
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700540
Mikhail Golubev4f3c2502020-10-08 00:44:31 +0300541@_SpecialForm
542def TypeAlias(self, parameters):
543 """Special marker indicating that an assignment should
544 be recognized as a proper type alias definition by type
545 checkers.
546
547 For example::
548
549 Predicate: TypeAlias = Callable[..., bool]
550
551 It's invalid when used anywhere except as in the example above.
552 """
553 raise TypeError(f"{self} is not subscriptable")
554
555
kj73607be2020-12-24 12:33:48 +0800556@_SpecialForm
557def Concatenate(self, parameters):
Ken Jin11276cd2021-01-02 08:45:50 +0800558 """Used in conjunction with ``ParamSpec`` and ``Callable`` to represent a
559 higher order function which adds, removes or transforms parameters of a
560 callable.
kj73607be2020-12-24 12:33:48 +0800561
562 For example::
563
564 Callable[Concatenate[int, P], int]
565
566 See PEP 612 for detailed information.
567 """
568 if parameters == ():
569 raise TypeError("Cannot take a Concatenate of no types.")
570 if not isinstance(parameters, tuple):
571 parameters = (parameters,)
572 if not isinstance(parameters[-1], ParamSpec):
573 raise TypeError("The last parameter to Concatenate should be a "
574 "ParamSpec variable.")
575 msg = "Concatenate[arg, ...]: each arg must be a type."
576 parameters = tuple(_type_check(p, msg) for p in parameters)
577 return _ConcatenateGenericAlias(self, parameters)
578
579
Ken Jin05ab4b62021-04-27 22:31:04 +0800580@_SpecialForm
581def TypeGuard(self, parameters):
582 """Special typing form used to annotate the return type of a user-defined
583 type guard function. ``TypeGuard`` only accepts a single type argument.
584 At runtime, functions marked this way should return a boolean.
585
586 ``TypeGuard`` aims to benefit *type narrowing* -- a technique used by static
587 type checkers to determine a more precise type of an expression within a
588 program's code flow. Usually type narrowing is done by analyzing
589 conditional code flow and applying the narrowing to a block of code. The
590 conditional expression here is sometimes referred to as a "type guard".
591
592 Sometimes it would be convenient to use a user-defined boolean function
593 as a type guard. Such a function should use ``TypeGuard[...]`` as its
594 return type to alert static type checkers to this intention.
595
596 Using ``-> TypeGuard`` tells the static type checker that for a given
597 function:
598
599 1. The return value is a boolean.
600 2. If the return value is ``True``, the type of its argument
601 is the type inside ``TypeGuard``.
602
603 For example::
604
605 def is_str(val: Union[str, float]):
606 # "isinstance" type guard
607 if isinstance(val, str):
608 # Type of ``val`` is narrowed to ``str``
609 ...
610 else:
611 # Else, type of ``val`` is narrowed to ``float``.
612 ...
613
614 Strict type narrowing is not enforced -- ``TypeB`` need not be a narrower
615 form of ``TypeA`` (it can even be a wider form) and this may lead to
616 type-unsafe results. The main reason is to allow for things like
617 narrowing ``List[object]`` to ``List[str]`` even though the latter is not
618 a subtype of the former, since ``List`` is invariant. The responsibility of
619 writing type-safe type guards is left to the user.
620
621 ``TypeGuard`` also works with type variables. For more information, see
622 PEP 647 (User-Defined Type Guards).
623 """
624 item = _type_check(parameters, f'{self} accepts only single type.')
625 return _GenericAlias(self, (item,))
626
627
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000628class ForwardRef(_Final, _root=True):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800629 """Internal wrapper to hold a forward reference."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700630
Guido van Rossum4cefe742016-09-27 15:20:12 -0700631 __slots__ = ('__forward_arg__', '__forward_code__',
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400632 '__forward_evaluated__', '__forward_value__',
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700633 '__forward_is_argument__', '__forward_module__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700634
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700635 def __init__(self, arg, is_argument=True, module=None):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700636 if not isinstance(arg, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000637 raise TypeError(f"Forward reference must be a string -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700638 try:
639 code = compile(arg, '<string>', 'eval')
640 except SyntaxError:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000641 raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700642 self.__forward_arg__ = arg
643 self.__forward_code__ = code
644 self.__forward_evaluated__ = False
645 self.__forward_value__ = None
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400646 self.__forward_is_argument__ = is_argument
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700647 self.__forward_module__ = module
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700648
wyfo653f4202020-07-22 21:47:28 +0200649 def _evaluate(self, globalns, localns, recursive_guard):
650 if self.__forward_arg__ in recursive_guard:
651 return self
Guido van Rossumdad17902016-11-10 08:29:18 -0800652 if not self.__forward_evaluated__ or localns is not globalns:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700653 if globalns is None and localns is None:
654 globalns = localns = {}
655 elif globalns is None:
656 globalns = localns
657 elif localns is None:
658 localns = globalns
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700659 if self.__forward_module__ is not None:
660 globalns = getattr(
661 sys.modules.get(self.__forward_module__, None), '__dict__', globalns
662 )
wyfo653f4202020-07-22 21:47:28 +0200663 type_ =_type_check(
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700664 eval(self.__forward_code__, globalns, localns),
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400665 "Forward references must evaluate to types.",
wyfo653f4202020-07-22 21:47:28 +0200666 is_argument=self.__forward_is_argument__,
667 )
668 self.__forward_value__ = _eval_type(
669 type_, globalns, localns, recursive_guard | {self.__forward_arg__}
670 )
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700671 self.__forward_evaluated__ = True
672 return self.__forward_value__
673
Guido van Rossum4cefe742016-09-27 15:20:12 -0700674 def __eq__(self, other):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000675 if not isinstance(other, ForwardRef):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700676 return NotImplemented
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100677 if self.__forward_evaluated__ and other.__forward_evaluated__:
678 return (self.__forward_arg__ == other.__forward_arg__ and
679 self.__forward_value__ == other.__forward_value__)
680 return self.__forward_arg__ == other.__forward_arg__
Guido van Rossum4cefe742016-09-27 15:20:12 -0700681
682 def __hash__(self):
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100683 return hash(self.__forward_arg__)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700684
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700685 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000686 return f'ForwardRef({self.__forward_arg__!r})'
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700687
kj73607be2020-12-24 12:33:48 +0800688class _TypeVarLike:
689 """Mixin for TypeVar-like types (TypeVar and ParamSpec)."""
690 def __init__(self, bound, covariant, contravariant):
691 """Used to setup TypeVars and ParamSpec's bound, covariant and
692 contravariant attributes.
693 """
694 if covariant and contravariant:
695 raise ValueError("Bivariant types are not supported.")
696 self.__covariant__ = bool(covariant)
697 self.__contravariant__ = bool(contravariant)
698 if bound:
699 self.__bound__ = _type_check(bound, "Bound must be a type.")
700 else:
701 self.__bound__ = None
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700702
kj73607be2020-12-24 12:33:48 +0800703 def __or__(self, right):
704 return Union[self, right]
705
Jelle Zijlstra90459192021-04-10 20:00:05 -0700706 def __ror__(self, left):
707 return Union[left, self]
kj73607be2020-12-24 12:33:48 +0800708
709 def __repr__(self):
710 if self.__covariant__:
711 prefix = '+'
712 elif self.__contravariant__:
713 prefix = '-'
714 else:
715 prefix = '~'
716 return prefix + self.__name__
717
718 def __reduce__(self):
719 return self.__name__
720
721
722class TypeVar( _Final, _Immutable, _TypeVarLike, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700723 """Type variable.
724
725 Usage::
726
727 T = TypeVar('T') # Can be anything
728 A = TypeVar('A', str, bytes) # Must be str or bytes
729
730 Type variables exist primarily for the benefit of static type
731 checkers. They serve as the parameters for generic types as well
732 as for generic function definitions. See class Generic for more
733 information on generic types. Generic functions work as follows:
734
Guido van Rossumb24569a2016-11-20 18:01:29 -0800735 def repeat(x: T, n: int) -> List[T]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700736 '''Return a list containing n references to x.'''
737 return [x]*n
738
739 def longest(x: A, y: A) -> A:
740 '''Return the longest of two strings.'''
741 return x if len(x) >= len(y) else y
742
743 The latter example's signature is essentially the overloading
744 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
745 that if the arguments are instances of some subclass of str,
746 the return type is still plain str.
747
Guido van Rossumb24569a2016-11-20 18:01:29 -0800748 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700749
Guido van Rossumefa798d2016-08-23 11:01:50 -0700750 Type variables defined with covariant=True or contravariant=True
João D. Ferreira86bfed32018-07-07 16:41:20 +0100751 can be used to declare covariant or contravariant generic types.
Guido van Rossumefa798d2016-08-23 11:01:50 -0700752 See PEP 484 for more details. By default generic types are invariant
753 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700754
755 Type variables can be introspected. e.g.:
756
757 T.__name__ == 'T'
758 T.__constraints__ == ()
759 T.__covariant__ == False
760 T.__contravariant__ = False
761 A.__constraints__ == (str, bytes)
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100762
763 Note that only type variables defined in global scope can be pickled.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700764 """
765
Guido van Rossum4cefe742016-09-27 15:20:12 -0700766 __slots__ = ('__name__', '__bound__', '__constraints__',
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300767 '__covariant__', '__contravariant__', '__dict__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700768
769 def __init__(self, name, *constraints, bound=None,
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800770 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700771 self.__name__ = name
kj73607be2020-12-24 12:33:48 +0800772 super().__init__(bound, covariant, contravariant)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700773 if constraints and bound is not None:
774 raise TypeError("Constraints cannot be combined with bound=...")
775 if constraints and len(constraints) == 1:
776 raise TypeError("A single constraint is not allowed")
777 msg = "TypeVar(name, constraint, ...): constraints must be types."
778 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
HongWeipenga25a04f2020-04-21 04:01:53 +0800779 try:
780 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling
781 except (AttributeError, ValueError):
782 def_mod = None
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300783 if def_mod != 'typing':
784 self.__module__ = def_mod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700785
Maggie Moss1b4552c2020-09-09 13:23:24 -0700786
Jelle Zijlstra52243362021-04-10 19:57:05 -0700787class ParamSpecArgs(_Final, _Immutable, _root=True):
788 """The args for a ParamSpec object.
789
790 Given a ParamSpec object P, P.args is an instance of ParamSpecArgs.
791
792 ParamSpecArgs objects have a reference back to their ParamSpec:
793
794 P.args.__origin__ is P
795
796 This type is meant for runtime introspection and has no special meaning to
797 static type checkers.
798 """
799 def __init__(self, origin):
800 self.__origin__ = origin
801
802 def __repr__(self):
803 return f"{self.__origin__.__name__}.args"
804
805
806class ParamSpecKwargs(_Final, _Immutable, _root=True):
807 """The kwargs for a ParamSpec object.
808
809 Given a ParamSpec object P, P.kwargs is an instance of ParamSpecKwargs.
810
811 ParamSpecKwargs objects have a reference back to their ParamSpec:
812
813 P.kwargs.__origin__ is P
814
815 This type is meant for runtime introspection and has no special meaning to
816 static type checkers.
817 """
818 def __init__(self, origin):
819 self.__origin__ = origin
820
821 def __repr__(self):
822 return f"{self.__origin__.__name__}.kwargs"
823
824
kj73607be2020-12-24 12:33:48 +0800825class ParamSpec(_Final, _Immutable, _TypeVarLike, _root=True):
826 """Parameter specification variable.
Maggie Moss1b4552c2020-09-09 13:23:24 -0700827
kj73607be2020-12-24 12:33:48 +0800828 Usage::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700829
kj73607be2020-12-24 12:33:48 +0800830 P = ParamSpec('P')
831
832 Parameter specification variables exist primarily for the benefit of static
833 type checkers. They are used to forward the parameter types of one
Ken Jin11276cd2021-01-02 08:45:50 +0800834 callable to another callable, a pattern commonly found in higher order
835 functions and decorators. They are only valid when used in ``Concatenate``,
836 or s the first argument to ``Callable``, or as parameters for user-defined
837 Generics. See class Generic for more information on generic types. An
838 example for annotating a decorator::
kj73607be2020-12-24 12:33:48 +0800839
840 T = TypeVar('T')
841 P = ParamSpec('P')
842
843 def add_logging(f: Callable[P, T]) -> Callable[P, T]:
844 '''A type-safe decorator to add logging to a function.'''
845 def inner(*args: P.args, **kwargs: P.kwargs) -> T:
846 logging.info(f'{f.__name__} was called')
847 return f(*args, **kwargs)
848 return inner
849
850 @add_logging
851 def add_two(x: float, y: float) -> float:
852 '''Add two numbers together.'''
853 return x + y
854
855 Parameter specification variables defined with covariant=True or
856 contravariant=True can be used to declare covariant or contravariant
857 generic types. These keyword arguments are valid, but their actual semantics
858 are yet to be decided. See PEP 612 for details.
859
860 Parameter specification variables can be introspected. e.g.:
861
862 P.__name__ == 'T'
863 P.__bound__ == None
864 P.__covariant__ == False
865 P.__contravariant__ == False
866
867 Note that only parameter specification variables defined in global scope can
868 be pickled.
869 """
870
871 __slots__ = ('__name__', '__bound__', '__covariant__', '__contravariant__',
872 '__dict__')
873
Jelle Zijlstra52243362021-04-10 19:57:05 -0700874 @property
875 def args(self):
876 return ParamSpecArgs(self)
877
878 @property
879 def kwargs(self):
880 return ParamSpecKwargs(self)
kj73607be2020-12-24 12:33:48 +0800881
Ken Jinace008c2021-01-11 08:11:41 +0800882 def __init__(self, name, *, bound=None, covariant=False, contravariant=False):
kj73607be2020-12-24 12:33:48 +0800883 self.__name__ = name
884 super().__init__(bound, covariant, contravariant)
885 try:
886 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__')
887 except (AttributeError, ValueError):
888 def_mod = None
889 if def_mod != 'typing':
890 self.__module__ = def_mod
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100891
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700892
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000893def _is_dunder(attr):
894 return attr.startswith('__') and attr.endswith('__')
Guido van Rossum83ec3022017-01-17 20:43:28 -0800895
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300896class _BaseGenericAlias(_Final, _root=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000897 """The central part of internal API.
898
899 This represents a generic version of type 'origin' with type arguments 'params'.
900 There are two kind of these aliases: user defined and special. The special ones
901 are wrappers around builtin collections and ABCs in collections.abc. These must
902 have 'name' always set. If 'inst' is False, then the alias can't be instantiated,
903 this is used by e.g. typing.List and typing.Dict.
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700904 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300905 def __init__(self, origin, *, inst=True, name=None):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000906 self._inst = inst
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000907 self._name = name
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700908 self.__origin__ = origin
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000909 self.__slots__ = None # This is not documented.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300910
911 def __call__(self, *args, **kwargs):
912 if not self._inst:
913 raise TypeError(f"Type {self._name} cannot be instantiated; "
914 f"use {self.__origin__.__name__}() instead")
915 result = self.__origin__(*args, **kwargs)
916 try:
917 result.__orig_class__ = self
918 except AttributeError:
919 pass
920 return result
921
922 def __mro_entries__(self, bases):
923 res = []
924 if self.__origin__ not in bases:
925 res.append(self.__origin__)
926 i = bases.index(self)
927 for b in bases[i+1:]:
928 if isinstance(b, _BaseGenericAlias) or issubclass(b, Generic):
929 break
930 else:
931 res.append(Generic)
932 return tuple(res)
933
934 def __getattr__(self, attr):
935 # We are careful for copy and pickle.
936 # Also for simplicity we just don't relay all dunder names
937 if '__origin__' in self.__dict__ and not _is_dunder(attr):
938 return getattr(self.__origin__, attr)
939 raise AttributeError(attr)
940
941 def __setattr__(self, attr, val):
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700942 if _is_dunder(attr) or attr in {'_name', '_inst', '_nparams',
943 '_typevar_types', '_paramspec_tvars'}:
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300944 super().__setattr__(attr, val)
945 else:
946 setattr(self.__origin__, attr, val)
947
948 def __instancecheck__(self, obj):
949 return self.__subclasscheck__(type(obj))
950
951 def __subclasscheck__(self, cls):
952 raise TypeError("Subscripted generics cannot be used with"
953 " class and instance checks")
954
955
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300956# Special typing constructs Union, Optional, Generic, Callable and Tuple
957# use three special attributes for internal bookkeeping of generic types:
958# * __parameters__ is a tuple of unique free type parameters of a generic
959# type, for example, Dict[T, T].__parameters__ == (T,);
960# * __origin__ keeps a reference to a type that was subscripted,
961# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
962# the type.
963# * __args__ is a tuple of all arguments used in subscripting,
964# e.g., Dict[T, int].__args__ == (T, int).
965
966
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300967class _GenericAlias(_BaseGenericAlias, _root=True):
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700968 def __init__(self, origin, params, *, inst=True, name=None,
969 _typevar_types=TypeVar,
970 _paramspec_tvars=False):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300971 super().__init__(origin, inst=inst, name=name)
972 if not isinstance(params, tuple):
973 params = (params,)
974 self.__args__ = tuple(... if a is _TypingEllipsis else
975 () if a is _TypingEmpty else
976 a for a in params)
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700977 self.__parameters__ = _collect_type_vars(params, typevar_types=_typevar_types)
978 self._typevar_types = _typevar_types
979 self._paramspec_tvars = _paramspec_tvars
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300980 if not name:
981 self.__module__ = origin.__module__
982
983 def __eq__(self, other):
984 if not isinstance(other, _GenericAlias):
985 return NotImplemented
986 return (self.__origin__ == other.__origin__
987 and self.__args__ == other.__args__)
988
989 def __hash__(self):
990 return hash((self.__origin__, self.__args__))
991
Maggie Moss1b4552c2020-09-09 13:23:24 -0700992 def __or__(self, right):
993 return Union[self, right]
994
Serhiy Storchaka80844d12021-07-16 16:42:04 +0300995 def __ror__(self, left):
996 return Union[left, self]
Maggie Moss1b4552c2020-09-09 13:23:24 -0700997
Guido van Rossum4cefe742016-09-27 15:20:12 -0700998 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700999 def __getitem__(self, params):
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001000 if self.__origin__ in (Generic, Protocol):
1001 # Can't subscript Generic[...] or Protocol[...].
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001002 raise TypeError(f"Cannot subscript already-subscripted {self}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001003 if not isinstance(params, tuple):
1004 params = (params,)
kj73607be2020-12-24 12:33:48 +08001005 params = tuple(_type_convert(p) for p in params)
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001006 if self._paramspec_tvars:
1007 if any(isinstance(t, ParamSpec) for t in self.__parameters__):
1008 params = _prepare_paramspec_params(self, params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001009 _check_generic(self, params, len(self.__parameters__))
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001010
1011 subst = dict(zip(self.__parameters__, params))
1012 new_args = []
1013 for arg in self.__args__:
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001014 if isinstance(arg, self._typevar_types):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001015 arg = subst[arg]
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001016 elif isinstance(arg, (_GenericAlias, GenericAlias)):
Serhiy Storchaka0122d482020-05-10 13:39:40 +03001017 subparams = arg.__parameters__
1018 if subparams:
1019 subargs = tuple(subst[x] for x in subparams)
1020 arg = arg[subargs]
kj73607be2020-12-24 12:33:48 +08001021 # Required to flatten out the args for CallableGenericAlias
1022 if self.__origin__ == collections.abc.Callable and isinstance(arg, tuple):
1023 new_args.extend(arg)
1024 else:
1025 new_args.append(arg)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001026 return self.copy_with(tuple(new_args))
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001027
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001028 def copy_with(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001029 return self.__class__(self.__origin__, params, name=self._name, inst=self._inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001030
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001031 def __repr__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001032 if self._name:
1033 name = 'typing.' + self._name
1034 else:
1035 name = _type_repr(self.__origin__)
1036 args = ", ".join([_type_repr(a) for a in self.__args__])
1037 return f'{name}[{args}]'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001038
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001039 def __reduce__(self):
1040 if self._name:
1041 origin = globals()[self._name]
1042 else:
1043 origin = self.__origin__
1044 args = tuple(self.__args__)
1045 if len(args) == 1 and not isinstance(args[0], tuple):
1046 args, = args
1047 return operator.getitem, (origin, args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001048
1049 def __mro_entries__(self, bases):
1050 if self._name: # generic version of an ABC or built-in class
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001051 return super().__mro_entries__(bases)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001052 if self.__origin__ is Generic:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001053 if Protocol in bases:
1054 return ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001055 i = bases.index(self)
1056 for b in bases[i+1:]:
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001057 if isinstance(b, _BaseGenericAlias) and b is not self:
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001058 return ()
1059 return (self.__origin__,)
1060
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001061
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001062# _nparams is the number of accepted parameters, e.g. 0 for Hashable,
1063# 1 for List and 2 for Dict. It may be -1 if variable number of
1064# parameters are accepted (needs custom __getitem__).
1065
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001066class _SpecialGenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001067 def __init__(self, origin, nparams, *, inst=True, name=None):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001068 if name is None:
1069 name = origin.__name__
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001070 super().__init__(origin, inst=inst, name=name)
1071 self._nparams = nparams
Serhiy Storchaka2fbc57a2020-05-10 15:14:27 +03001072 if origin.__module__ == 'builtins':
1073 self.__doc__ = f'A generic version of {origin.__qualname__}.'
1074 else:
1075 self.__doc__ = f'A generic version of {origin.__module__}.{origin.__qualname__}.'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001076
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001077 @_tp_cache
1078 def __getitem__(self, params):
1079 if not isinstance(params, tuple):
1080 params = (params,)
1081 msg = "Parameters to generic types must be types."
1082 params = tuple(_type_check(p, msg) for p in params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001083 _check_generic(self, params, self._nparams)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001084 return self.copy_with(params)
1085
1086 def copy_with(self, params):
1087 return _GenericAlias(self.__origin__, params,
1088 name=self._name, inst=self._inst)
1089
1090 def __repr__(self):
1091 return 'typing.' + self._name
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001092
1093 def __subclasscheck__(self, cls):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001094 if isinstance(cls, _SpecialGenericAlias):
1095 return issubclass(cls.__origin__, self.__origin__)
1096 if not isinstance(cls, _GenericAlias):
1097 return issubclass(cls, self.__origin__)
1098 return super().__subclasscheck__(cls)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001099
Ivan Levkivskyi83494032018-03-26 23:01:12 +01001100 def __reduce__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001101 return self._name
Ivan Levkivskyi83494032018-03-26 23:01:12 +01001102
Maggie Moss1b4552c2020-09-09 13:23:24 -07001103 def __or__(self, right):
1104 return Union[self, right]
1105
Serhiy Storchaka80844d12021-07-16 16:42:04 +03001106 def __ror__(self, left):
1107 return Union[left, self]
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001108
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001109class _CallableGenericAlias(_GenericAlias, _root=True):
1110 def __repr__(self):
1111 assert self._name == 'Callable'
kj73607be2020-12-24 12:33:48 +08001112 args = self.__args__
1113 if len(args) == 2 and (args[0] is Ellipsis
1114 or isinstance(args[0], (ParamSpec, _ConcatenateGenericAlias))):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001115 return super().__repr__()
1116 return (f'typing.Callable'
kj73607be2020-12-24 12:33:48 +08001117 f'[[{", ".join([_type_repr(a) for a in args[:-1]])}], '
1118 f'{_type_repr(args[-1])}]')
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001119
1120 def __reduce__(self):
1121 args = self.__args__
kj73607be2020-12-24 12:33:48 +08001122 if not (len(args) == 2 and (args[0] is Ellipsis
1123 or isinstance(args[0], (ParamSpec, _ConcatenateGenericAlias)))):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001124 args = list(args[:-1]), args[-1]
1125 return operator.getitem, (Callable, args)
1126
1127
1128class _CallableType(_SpecialGenericAlias, _root=True):
1129 def copy_with(self, params):
1130 return _CallableGenericAlias(self.__origin__, params,
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001131 name=self._name, inst=self._inst,
1132 _typevar_types=(TypeVar, ParamSpec),
1133 _paramspec_tvars=True)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001134
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001135 def __getitem__(self, params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001136 if not isinstance(params, tuple) or len(params) != 2:
1137 raise TypeError("Callable must be used as "
1138 "Callable[[arg, ...], result].")
1139 args, result = params
kj463c7d32020-12-14 02:38:24 +08001140 # This relaxes what args can be on purpose to allow things like
1141 # PEP 612 ParamSpec. Responsibility for whether a user is using
1142 # Callable[...] properly is deferred to static type checkers.
1143 if isinstance(args, list):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001144 params = (tuple(args), result)
kj463c7d32020-12-14 02:38:24 +08001145 else:
1146 params = (args, result)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001147 return self.__getitem_inner__(params)
1148
1149 @_tp_cache
1150 def __getitem_inner__(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001151 args, result = params
1152 msg = "Callable[args, result]: result must be a type."
1153 result = _type_check(result, msg)
1154 if args is Ellipsis:
1155 return self.copy_with((_TypingEllipsis, result))
kj463c7d32020-12-14 02:38:24 +08001156 if not isinstance(args, tuple):
1157 args = (args,)
1158 args = tuple(_type_convert(arg) for arg in args)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001159 params = args + (result,)
1160 return self.copy_with(params)
1161
1162
1163class _TupleType(_SpecialGenericAlias, _root=True):
1164 @_tp_cache
1165 def __getitem__(self, params):
1166 if params == ():
1167 return self.copy_with((_TypingEmpty,))
1168 if not isinstance(params, tuple):
1169 params = (params,)
1170 if len(params) == 2 and params[1] is ...:
1171 msg = "Tuple[t, ...]: t must be a type."
1172 p = _type_check(params[0], msg)
1173 return self.copy_with((p, _TypingEllipsis))
1174 msg = "Tuple[t0, t1, ...]: each t must be a type."
1175 params = tuple(_type_check(p, msg) for p in params)
1176 return self.copy_with(params)
1177
1178
1179class _UnionGenericAlias(_GenericAlias, _root=True):
1180 def copy_with(self, params):
1181 return Union[params]
1182
1183 def __eq__(self, other):
1184 if not isinstance(other, _UnionGenericAlias):
1185 return NotImplemented
1186 return set(self.__args__) == set(other.__args__)
1187
1188 def __hash__(self):
1189 return hash(frozenset(self.__args__))
1190
1191 def __repr__(self):
1192 args = self.__args__
1193 if len(args) == 2:
1194 if args[0] is type(None):
1195 return f'typing.Optional[{_type_repr(args[1])}]'
1196 elif args[1] is type(None):
1197 return f'typing.Optional[{_type_repr(args[0])}]'
1198 return super().__repr__()
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001199
Maggie Moss1b4552c2020-09-09 13:23:24 -07001200 def __instancecheck__(self, obj):
1201 return self.__subclasscheck__(type(obj))
1202
1203 def __subclasscheck__(self, cls):
1204 for arg in self.__args__:
1205 if issubclass(cls, arg):
1206 return True
1207
1208
Yurii Karabasf03d3182020-11-17 04:23:19 +02001209def _value_and_type_iter(parameters):
1210 return ((p, type(p)) for p in parameters)
1211
1212
1213class _LiteralGenericAlias(_GenericAlias, _root=True):
1214
1215 def __eq__(self, other):
1216 if not isinstance(other, _LiteralGenericAlias):
1217 return NotImplemented
1218
1219 return set(_value_and_type_iter(self.__args__)) == set(_value_and_type_iter(other.__args__))
1220
1221 def __hash__(self):
Yurii Karabas1b540772020-11-19 18:17:38 +02001222 return hash(frozenset(_value_and_type_iter(self.__args__)))
Yurii Karabasf03d3182020-11-17 04:23:19 +02001223
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001224
kj73607be2020-12-24 12:33:48 +08001225class _ConcatenateGenericAlias(_GenericAlias, _root=True):
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001226 def __init__(self, *args, **kwargs):
1227 super().__init__(*args, **kwargs,
1228 _typevar_types=(TypeVar, ParamSpec),
1229 _paramspec_tvars=True)
kj73607be2020-12-24 12:33:48 +08001230
1231
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001232class Generic:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001233 """Abstract base class for generic types.
1234
Guido van Rossumb24569a2016-11-20 18:01:29 -08001235 A generic type is typically declared by inheriting from
1236 this class parameterized with one or more type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001237 For example, a generic mapping type might be defined as::
1238
1239 class Mapping(Generic[KT, VT]):
1240 def __getitem__(self, key: KT) -> VT:
1241 ...
1242 # Etc.
1243
1244 This class can then be used as follows::
1245
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001246 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001247 try:
1248 return mapping[key]
1249 except KeyError:
1250 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001251 """
Guido van Rossumd70fe632015-08-05 12:11:06 +02001252 __slots__ = ()
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001253 _is_protocol = False
Guido van Rossumd70fe632015-08-05 12:11:06 +02001254
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001255 @_tp_cache
1256 def __class_getitem__(cls, params):
1257 if not isinstance(params, tuple):
1258 params = (params,)
1259 if not params and cls is not Tuple:
1260 raise TypeError(
1261 f"Parameter list to {cls.__qualname__}[...] cannot be empty")
kj73607be2020-12-24 12:33:48 +08001262 params = tuple(_type_convert(p) for p in params)
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001263 if cls in (Generic, Protocol):
1264 # Generic and Protocol can only be subscripted with unique type variables.
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001265 if not all(isinstance(p, (TypeVar, ParamSpec)) for p in params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001266 raise TypeError(
kj73607be2020-12-24 12:33:48 +08001267 f"Parameters to {cls.__name__}[...] must all be type variables "
1268 f"or parameter specification variables.")
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001269 if len(set(params)) != len(params):
1270 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001271 f"Parameters to {cls.__name__}[...] must all be unique")
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001272 else:
1273 # Subscripting a regular Generic subclass.
kj73607be2020-12-24 12:33:48 +08001274 if any(isinstance(t, ParamSpec) for t in cls.__parameters__):
1275 params = _prepare_paramspec_params(cls, params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001276 _check_generic(cls, params, len(cls.__parameters__))
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001277 return _GenericAlias(cls, params,
1278 _typevar_types=(TypeVar, ParamSpec),
1279 _paramspec_tvars=True)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001280
1281 def __init_subclass__(cls, *args, **kwargs):
Ivan Levkivskyiee566fe2018-04-04 17:00:15 +01001282 super().__init_subclass__(*args, **kwargs)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001283 tvars = []
1284 if '__orig_bases__' in cls.__dict__:
1285 error = Generic in cls.__orig_bases__
1286 else:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001287 error = Generic in cls.__bases__ and cls.__name__ != 'Protocol'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001288 if error:
1289 raise TypeError("Cannot inherit from plain Generic")
1290 if '__orig_bases__' in cls.__dict__:
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001291 tvars = _collect_type_vars(cls.__orig_bases__, (TypeVar, ParamSpec))
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001292 # Look for Generic[T1, ..., Tn].
1293 # If found, tvars must be a subset of it.
1294 # If not found, tvars is it.
1295 # Also check for and reject plain Generic,
1296 # and reject multiple Generic[...].
1297 gvars = None
1298 for base in cls.__orig_bases__:
1299 if (isinstance(base, _GenericAlias) and
1300 base.__origin__ is Generic):
1301 if gvars is not None:
1302 raise TypeError(
1303 "Cannot inherit from Generic[...] multiple types.")
1304 gvars = base.__parameters__
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001305 if gvars is not None:
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001306 tvarset = set(tvars)
1307 gvarset = set(gvars)
1308 if not tvarset <= gvarset:
1309 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
1310 s_args = ', '.join(str(g) for g in gvars)
1311 raise TypeError(f"Some type variables ({s_vars}) are"
1312 f" not listed in Generic[{s_args}]")
1313 tvars = gvars
1314 cls.__parameters__ = tuple(tvars)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001315
1316
1317class _TypingEmpty:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001318 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
1319 to allow empty list/tuple in specific places, without allowing them
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001320 to sneak in where prohibited.
1321 """
1322
1323
1324class _TypingEllipsis:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001325 """Internal placeholder for ... (ellipsis)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001326
1327
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001328_TYPING_INTERNALS = ['__parameters__', '__orig_bases__', '__orig_class__',
1329 '_is_protocol', '_is_runtime_protocol']
1330
1331_SPECIAL_NAMES = ['__abstractmethods__', '__annotations__', '__dict__', '__doc__',
1332 '__init__', '__module__', '__new__', '__slots__',
Guido van Rossum48b069a2020-04-07 09:50:06 -07001333 '__subclasshook__', '__weakref__', '__class_getitem__']
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001334
1335# These special attributes will be not collected as protocol members.
1336EXCLUDED_ATTRIBUTES = _TYPING_INTERNALS + _SPECIAL_NAMES + ['_MutableMapping__marker']
1337
1338
1339def _get_protocol_attrs(cls):
1340 """Collect protocol members from a protocol class objects.
1341
1342 This includes names actually defined in the class dictionary, as well
1343 as names that appear in annotations. Special names (above) are skipped.
1344 """
1345 attrs = set()
1346 for base in cls.__mro__[:-1]: # without object
1347 if base.__name__ in ('Protocol', 'Generic'):
1348 continue
1349 annotations = getattr(base, '__annotations__', {})
1350 for attr in list(base.__dict__.keys()) + list(annotations.keys()):
1351 if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
1352 attrs.add(attr)
1353 return attrs
1354
1355
1356def _is_callable_members_only(cls):
1357 # PEP 544 prohibits using issubclass() with protocols that have non-method members.
1358 return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls))
1359
1360
1361def _no_init(self, *args, **kwargs):
1362 if type(self)._is_protocol:
1363 raise TypeError('Protocols cannot be instantiated')
1364
1365
Miss Islington (bot)a2d94a02021-05-12 09:09:04 -07001366def _allow_reckless_class_checks(depth=3):
Nickolena Fishercfaf4c02020-04-26 12:49:11 -05001367 """Allow instance and class checks for special stdlib modules.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001368
1369 The abc and functools modules indiscriminately call isinstance() and
1370 issubclass() on the whole MRO of a user class, which may contain protocols.
1371 """
1372 try:
Miss Islington (bot)a2d94a02021-05-12 09:09:04 -07001373 return sys._getframe(depth).f_globals['__name__'] in ['abc', 'functools']
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001374 except (AttributeError, ValueError): # For platforms without _getframe().
1375 return True
1376
1377
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001378_PROTO_ALLOWLIST = {
Divij Rajkumar692a0dc2019-09-12 11:13:51 +01001379 'collections.abc': [
1380 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
1381 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
1382 ],
1383 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
1384}
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001385
1386
1387class _ProtocolMeta(ABCMeta):
1388 # This metaclass is really unfortunate and exists only because of
1389 # the lack of __instancehook__.
1390 def __instancecheck__(cls, instance):
1391 # We need this method for situations where attributes are
1392 # assigned in __init__.
Miss Islington (bot)a2d94a02021-05-12 09:09:04 -07001393 if (
1394 getattr(cls, '_is_protocol', False) and
1395 not getattr(cls, '_is_runtime_protocol', False) and
1396 not _allow_reckless_class_checks(depth=2)
1397 ):
1398 raise TypeError("Instance and class checks can only be used with"
1399 " @runtime_checkable protocols")
1400
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001401 if ((not getattr(cls, '_is_protocol', False) or
1402 _is_callable_members_only(cls)) and
1403 issubclass(instance.__class__, cls)):
1404 return True
1405 if cls._is_protocol:
1406 if all(hasattr(instance, attr) and
1407 # All *methods* can be blocked by setting them to None.
1408 (not callable(getattr(cls, attr, None)) or
1409 getattr(instance, attr) is not None)
1410 for attr in _get_protocol_attrs(cls)):
1411 return True
1412 return super().__instancecheck__(instance)
1413
1414
1415class Protocol(Generic, metaclass=_ProtocolMeta):
1416 """Base class for protocol classes.
1417
1418 Protocol classes are defined as::
1419
1420 class Proto(Protocol):
1421 def meth(self) -> int:
1422 ...
1423
1424 Such classes are primarily used with static type checkers that recognize
1425 structural subtyping (static duck-typing), for example::
1426
1427 class C:
1428 def meth(self) -> int:
1429 return 0
1430
1431 def func(x: Proto) -> int:
1432 return x.meth()
1433
1434 func(C()) # Passes static type check
1435
1436 See PEP 544 for details. Protocol classes decorated with
1437 @typing.runtime_checkable act as simple-minded runtime protocols that check
1438 only the presence of given attributes, ignoring their type signatures.
1439 Protocol classes can be generic, they are defined as::
1440
1441 class GenProto(Protocol[T]):
1442 def meth(self) -> T:
1443 ...
1444 """
1445 __slots__ = ()
1446 _is_protocol = True
1447 _is_runtime_protocol = False
1448
1449 def __init_subclass__(cls, *args, **kwargs):
1450 super().__init_subclass__(*args, **kwargs)
1451
1452 # Determine if this is a protocol or a concrete subclass.
1453 if not cls.__dict__.get('_is_protocol', False):
1454 cls._is_protocol = any(b is Protocol for b in cls.__bases__)
1455
1456 # Set (or override) the protocol subclass hook.
1457 def _proto_hook(other):
1458 if not cls.__dict__.get('_is_protocol', False):
1459 return NotImplemented
1460
1461 # First, perform various sanity checks.
1462 if not getattr(cls, '_is_runtime_protocol', False):
Rossc1af1282020-12-29 11:55:28 +00001463 if _allow_reckless_class_checks():
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001464 return NotImplemented
1465 raise TypeError("Instance and class checks can only be used with"
1466 " @runtime_checkable protocols")
1467 if not _is_callable_members_only(cls):
Rossc1af1282020-12-29 11:55:28 +00001468 if _allow_reckless_class_checks():
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001469 return NotImplemented
1470 raise TypeError("Protocols with non-method members"
1471 " don't support issubclass()")
1472 if not isinstance(other, type):
1473 # Same error message as for issubclass(1, int).
1474 raise TypeError('issubclass() arg 1 must be a class')
1475
1476 # Second, perform the actual structural compatibility check.
1477 for attr in _get_protocol_attrs(cls):
1478 for base in other.__mro__:
1479 # Check if the members appears in the class dictionary...
1480 if attr in base.__dict__:
1481 if base.__dict__[attr] is None:
1482 return NotImplemented
1483 break
1484
1485 # ...or in annotations, if it is a sub-protocol.
1486 annotations = getattr(base, '__annotations__', {})
1487 if (isinstance(annotations, collections.abc.Mapping) and
1488 attr in annotations and
1489 issubclass(other, Generic) and other._is_protocol):
1490 break
1491 else:
1492 return NotImplemented
1493 return True
1494
1495 if '__subclasshook__' not in cls.__dict__:
1496 cls.__subclasshook__ = _proto_hook
1497
1498 # We have nothing more to do for non-protocols...
1499 if not cls._is_protocol:
1500 return
1501
1502 # ... otherwise check consistency of bases, and prohibit instantiation.
1503 for base in cls.__bases__:
1504 if not (base in (object, Generic) or
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001505 base.__module__ in _PROTO_ALLOWLIST and
1506 base.__name__ in _PROTO_ALLOWLIST[base.__module__] or
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001507 issubclass(base, Generic) and base._is_protocol):
1508 raise TypeError('Protocols can only inherit from other'
1509 ' protocols, got %r' % base)
1510 cls.__init__ = _no_init
1511
1512
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001513class _AnnotatedAlias(_GenericAlias, _root=True):
1514 """Runtime representation of an annotated type.
1515
1516 At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't'
1517 with extra annotations. The alias behaves like a normal typing alias,
1518 instantiating is the same as instantiating the underlying type, binding
1519 it to types is also the same.
1520 """
1521 def __init__(self, origin, metadata):
1522 if isinstance(origin, _AnnotatedAlias):
1523 metadata = origin.__metadata__ + metadata
1524 origin = origin.__origin__
1525 super().__init__(origin, origin)
1526 self.__metadata__ = metadata
1527
1528 def copy_with(self, params):
1529 assert len(params) == 1
1530 new_type = params[0]
1531 return _AnnotatedAlias(new_type, self.__metadata__)
1532
1533 def __repr__(self):
1534 return "typing.Annotated[{}, {}]".format(
1535 _type_repr(self.__origin__),
1536 ", ".join(repr(a) for a in self.__metadata__)
1537 )
1538
1539 def __reduce__(self):
1540 return operator.getitem, (
1541 Annotated, (self.__origin__,) + self.__metadata__
1542 )
1543
1544 def __eq__(self, other):
1545 if not isinstance(other, _AnnotatedAlias):
1546 return NotImplemented
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001547 return (self.__origin__ == other.__origin__
1548 and self.__metadata__ == other.__metadata__)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001549
1550 def __hash__(self):
1551 return hash((self.__origin__, self.__metadata__))
1552
1553
1554class Annotated:
1555 """Add context specific metadata to a type.
1556
1557 Example: Annotated[int, runtime_check.Unsigned] indicates to the
1558 hypothetical runtime_check module that this type is an unsigned int.
1559 Every other consumer of this type can ignore this metadata and treat
1560 this type as int.
1561
1562 The first argument to Annotated must be a valid type.
1563
1564 Details:
1565
1566 - It's an error to call `Annotated` with less than two arguments.
1567 - Nested Annotated are flattened::
1568
1569 Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
1570
1571 - Instantiating an annotated type is equivalent to instantiating the
1572 underlying type::
1573
1574 Annotated[C, Ann1](5) == C(5)
1575
1576 - Annotated can be used as a generic type alias::
1577
1578 Optimized = Annotated[T, runtime.Optimize()]
1579 Optimized[int] == Annotated[int, runtime.Optimize()]
1580
1581 OptimizedList = Annotated[List[T], runtime.Optimize()]
1582 OptimizedList[int] == Annotated[List[int], runtime.Optimize()]
1583 """
1584
1585 __slots__ = ()
1586
1587 def __new__(cls, *args, **kwargs):
1588 raise TypeError("Type Annotated cannot be instantiated.")
1589
1590 @_tp_cache
1591 def __class_getitem__(cls, params):
1592 if not isinstance(params, tuple) or len(params) < 2:
1593 raise TypeError("Annotated[...] should be used "
1594 "with at least two arguments (a type and an "
1595 "annotation).")
1596 msg = "Annotated[t, ...]: t must be a type."
1597 origin = _type_check(params[0], msg)
1598 metadata = tuple(params[1:])
1599 return _AnnotatedAlias(origin, metadata)
1600
1601 def __init_subclass__(cls, *args, **kwargs):
1602 raise TypeError(
1603 "Cannot subclass {}.Annotated".format(cls.__module__)
1604 )
1605
1606
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001607def runtime_checkable(cls):
1608 """Mark a protocol class as a runtime protocol.
1609
1610 Such protocol can be used with isinstance() and issubclass().
1611 Raise TypeError if applied to a non-protocol class.
1612 This allows a simple-minded structural check very similar to
1613 one trick ponies in collections.abc such as Iterable.
1614 For example::
1615
1616 @runtime_checkable
1617 class Closable(Protocol):
1618 def close(self): ...
1619
1620 assert isinstance(open('/some/file'), Closable)
1621
1622 Warning: this will check only the presence of the required methods,
1623 not their type signatures!
1624 """
1625 if not issubclass(cls, Generic) or not cls._is_protocol:
1626 raise TypeError('@runtime_checkable can be only applied to protocol classes,'
1627 ' got %r' % cls)
1628 cls._is_runtime_protocol = True
1629 return cls
1630
1631
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001632def cast(typ, val):
1633 """Cast a value to a type.
1634
1635 This returns the value unchanged. To the type checker this
1636 signals that the return value has the designated type, but at
1637 runtime we intentionally don't check anything (we want this
1638 to be as fast as possible).
1639 """
1640 return val
1641
1642
1643def _get_defaults(func):
1644 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -08001645 try:
1646 code = func.__code__
1647 except AttributeError:
1648 # Some built-in functions don't have __code__, __defaults__, etc.
1649 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001650 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001651 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001652 arg_names = arg_names[:pos_count]
1653 defaults = func.__defaults__ or ()
1654 kwdefaults = func.__kwdefaults__
1655 res = dict(kwdefaults) if kwdefaults else {}
1656 pos_offset = pos_count - len(defaults)
1657 for name, value in zip(arg_names[pos_offset:], defaults):
1658 assert name not in res
1659 res[name] = value
1660 return res
1661
1662
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001663_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
1664 types.MethodType, types.ModuleType,
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02001665 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001666
1667
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001668def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
Guido van Rossum991d14f2016-11-09 13:12:51 -08001669 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001670
Guido van Rossum991d14f2016-11-09 13:12:51 -08001671 This is often the same as obj.__annotations__, but it handles
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001672 forward references encoded as string literals, adds Optional[t] if a
1673 default value equal to None is set and recursively replaces all
1674 'Annotated[T, ...]' with 'T' (unless 'include_extras=True').
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001675
Guido van Rossum991d14f2016-11-09 13:12:51 -08001676 The argument may be a module, class, method, or function. The annotations
1677 are returned as a dictionary. For classes, annotations include also
1678 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001679
Guido van Rossum991d14f2016-11-09 13:12:51 -08001680 TypeError is raised if the argument is not of a type that can contain
1681 annotations, and an empty dictionary is returned if no annotations are
1682 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001683
Guido van Rossum991d14f2016-11-09 13:12:51 -08001684 BEWARE -- the behavior of globalns and localns is counterintuitive
1685 (unless you are familiar with how eval() and exec() work). The
1686 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001687
Guido van Rossum991d14f2016-11-09 13:12:51 -08001688 - If no dict arguments are passed, an attempt is made to use the
Łukasz Langaf350a262017-09-14 14:33:00 -04001689 globals from obj (or the respective module's globals for classes),
1690 and these are also used as the locals. If the object does not appear
Ken Jin1b1f9852021-04-27 01:31:21 +08001691 to have globals, an empty dictionary is used. For classes, the search
1692 order is globals first then locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001693
Guido van Rossum991d14f2016-11-09 13:12:51 -08001694 - If one dict argument is passed, it is used for both globals and
1695 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001696
Guido van Rossum991d14f2016-11-09 13:12:51 -08001697 - If two dict arguments are passed, they specify globals and
1698 locals, respectively.
1699 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001700
Guido van Rossum991d14f2016-11-09 13:12:51 -08001701 if getattr(obj, '__no_type_check__', None):
1702 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001703 # Classes require a special treatment.
1704 if isinstance(obj, type):
1705 hints = {}
1706 for base in reversed(obj.__mro__):
Łukasz Langaf350a262017-09-14 14:33:00 -04001707 if globalns is None:
Miss Islington (bot)3df23b52021-06-26 16:52:28 -07001708 base_globals = getattr(sys.modules.get(base.__module__, None), '__dict__', {})
Łukasz Langaf350a262017-09-14 14:33:00 -04001709 else:
1710 base_globals = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001711 ann = base.__dict__.get('__annotations__', {})
larryhastings2f2b6982021-04-29 20:09:08 -07001712 if isinstance(ann, types.GetSetDescriptorType):
1713 ann = {}
Ken Jin852150d2021-04-13 01:23:12 +08001714 base_locals = dict(vars(base)) if localns is None else localns
Ken Jin1b1f9852021-04-27 01:31:21 +08001715 if localns is None and globalns is None:
1716 # This is surprising, but required. Before Python 3.10,
1717 # get_type_hints only evaluated the globalns of
1718 # a class. To maintain backwards compatibility, we reverse
1719 # the globalns and localns order so that eval() looks into
1720 # *base_globals* first rather than *base_locals*.
1721 # This only affects ForwardRefs.
1722 base_globals, base_locals = base_locals, base_globals
Guido van Rossum991d14f2016-11-09 13:12:51 -08001723 for name, value in ann.items():
1724 if value is None:
1725 value = type(None)
1726 if isinstance(value, str):
Nina Zakharenko0e61dff2018-05-22 20:32:10 -07001727 value = ForwardRef(value, is_argument=False)
Ken Jin852150d2021-04-13 01:23:12 +08001728 value = _eval_type(value, base_globals, base_locals)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001729 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001730 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
Łukasz Langaf350a262017-09-14 14:33:00 -04001731
1732 if globalns is None:
1733 if isinstance(obj, types.ModuleType):
1734 globalns = obj.__dict__
1735 else:
benedwards140aca3a32019-11-21 17:24:58 +00001736 nsobj = obj
1737 # Find globalns for the unwrapped object.
1738 while hasattr(nsobj, '__wrapped__'):
1739 nsobj = nsobj.__wrapped__
1740 globalns = getattr(nsobj, '__globals__', {})
Łukasz Langaf350a262017-09-14 14:33:00 -04001741 if localns is None:
1742 localns = globalns
1743 elif localns is None:
1744 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001745 hints = getattr(obj, '__annotations__', None)
1746 if hints is None:
1747 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001748 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001749 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001750 else:
1751 raise TypeError('{!r} is not a module, class, method, '
1752 'or function.'.format(obj))
1753 defaults = _get_defaults(obj)
1754 hints = dict(hints)
1755 for name, value in hints.items():
1756 if value is None:
1757 value = type(None)
1758 if isinstance(value, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001759 value = ForwardRef(value)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001760 value = _eval_type(value, globalns, localns)
1761 if name in defaults and defaults[name] is None:
1762 value = Optional[value]
1763 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001764 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
1765
1766
1767def _strip_annotations(t):
1768 """Strips the annotations from a given type.
1769 """
1770 if isinstance(t, _AnnotatedAlias):
1771 return _strip_annotations(t.__origin__)
1772 if isinstance(t, _GenericAlias):
1773 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1774 if stripped_args == t.__args__:
1775 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001776 return t.copy_with(stripped_args)
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001777 if isinstance(t, GenericAlias):
1778 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1779 if stripped_args == t.__args__:
1780 return t
1781 return GenericAlias(t.__origin__, stripped_args)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001782 return t
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001783
1784
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001785def get_origin(tp):
1786 """Get the unsubscripted version of a type.
1787
Jakub Stasiak38aaaaa2020-02-07 02:15:12 +01001788 This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
1789 and Annotated. Return None for unsupported types. Examples::
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001790
1791 get_origin(Literal[42]) is Literal
1792 get_origin(int) is None
1793 get_origin(ClassVar[int]) is ClassVar
1794 get_origin(Generic) is Generic
1795 get_origin(Generic[T]) is Generic
1796 get_origin(Union[T, int]) is Union
1797 get_origin(List[Tuple[T, T]][int]) == list
Jelle Zijlstra52243362021-04-10 19:57:05 -07001798 get_origin(P.args) is P
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001799 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001800 if isinstance(tp, _AnnotatedAlias):
1801 return Annotated
Jelle Zijlstra52243362021-04-10 19:57:05 -07001802 if isinstance(tp, (_BaseGenericAlias, GenericAlias,
1803 ParamSpecArgs, ParamSpecKwargs)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001804 return tp.__origin__
1805 if tp is Generic:
1806 return Generic
Ken Jinefb1f092020-12-29 10:26:19 +08001807 if isinstance(tp, types.Union):
1808 return types.Union
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001809 return None
1810
1811
1812def get_args(tp):
1813 """Get type arguments with all substitutions performed.
1814
1815 For unions, basic simplifications used by Union constructor are performed.
1816 Examples::
1817 get_args(Dict[str, int]) == (str, int)
1818 get_args(int) == ()
1819 get_args(Union[int, Union[T, int], str][int]) == (int, str)
1820 get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
1821 get_args(Callable[[], T][int]) == ([], int)
1822 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001823 if isinstance(tp, _AnnotatedAlias):
1824 return (tp.__origin__,) + tp.__metadata__
Ken Jin4140f102020-12-29 04:06:19 +08001825 if isinstance(tp, (_GenericAlias, GenericAlias)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001826 res = tp.__args__
Ken Jinefb1f092020-12-29 10:26:19 +08001827 if (tp.__origin__ is collections.abc.Callable
1828 and not (res[0] is Ellipsis
1829 or isinstance(res[0], (ParamSpec, _ConcatenateGenericAlias)))):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001830 res = (list(res[:-1]), res[-1])
1831 return res
Ken Jinefb1f092020-12-29 10:26:19 +08001832 if isinstance(tp, types.Union):
1833 return tp.__args__
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001834 return ()
1835
1836
Patrick Reader0705ec82020-09-16 05:58:32 +01001837def is_typeddict(tp):
1838 """Check if an annotation is a TypedDict class
1839
1840 For example::
1841 class Film(TypedDict):
1842 title: str
1843 year: int
1844
1845 is_typeddict(Film) # => True
1846 is_typeddict(Union[list, str]) # => False
1847 """
1848 return isinstance(tp, _TypedDictMeta)
1849
1850
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001851def no_type_check(arg):
1852 """Decorator to indicate that annotations are not type hints.
1853
1854 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001855 applies recursively to all methods and classes defined in that class
1856 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001857
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001858 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001859 """
1860 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001861 arg_attrs = arg.__dict__.copy()
1862 for attr, val in arg.__dict__.items():
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001863 if val in arg.__bases__ + (arg,):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001864 arg_attrs.pop(attr)
1865 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001866 if isinstance(obj, types.FunctionType):
1867 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001868 if isinstance(obj, type):
1869 no_type_check(obj)
1870 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001871 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001872 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001873 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001874 return arg
1875
1876
1877def no_type_check_decorator(decorator):
1878 """Decorator to give another decorator the @no_type_check effect.
1879
1880 This wraps the decorator with something that wraps the decorated
1881 function in @no_type_check.
1882 """
1883
1884 @functools.wraps(decorator)
1885 def wrapped_decorator(*args, **kwds):
1886 func = decorator(*args, **kwds)
1887 func = no_type_check(func)
1888 return func
1889
1890 return wrapped_decorator
1891
1892
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001893def _overload_dummy(*args, **kwds):
1894 """Helper for @overload to raise when called."""
1895 raise NotImplementedError(
1896 "You should not call an overloaded function. "
1897 "A series of @overload-decorated functions "
1898 "outside a stub module should always be followed "
1899 "by an implementation that is not @overload-ed.")
1900
1901
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001902def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001903 """Decorator for overloaded functions/methods.
1904
1905 In a stub file, place two or more stub definitions for the same
1906 function in a row, each decorated with @overload. For example:
1907
1908 @overload
1909 def utf8(value: None) -> None: ...
1910 @overload
1911 def utf8(value: bytes) -> bytes: ...
1912 @overload
1913 def utf8(value: str) -> bytes: ...
1914
1915 In a non-stub file (i.e. a regular .py file), do the same but
1916 follow it with an implementation. The implementation should *not*
1917 be decorated with @overload. For example:
1918
1919 @overload
1920 def utf8(value: None) -> None: ...
1921 @overload
1922 def utf8(value: bytes) -> bytes: ...
1923 @overload
1924 def utf8(value: str) -> bytes: ...
1925 def utf8(value):
1926 # implementation goes here
1927 """
1928 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001929
1930
Ivan Levkivskyif3672422019-05-26 09:37:07 +01001931def final(f):
1932 """A decorator to indicate final methods and final classes.
1933
1934 Use this decorator to indicate to type checkers that the decorated
1935 method cannot be overridden, and decorated class cannot be subclassed.
1936 For example:
1937
1938 class Base:
1939 @final
1940 def done(self) -> None:
1941 ...
1942 class Sub(Base):
1943 def done(self) -> None: # Error reported by type checker
1944 ...
1945
1946 @final
1947 class Leaf:
1948 ...
1949 class Other(Leaf): # Error reported by type checker
1950 ...
1951
1952 There is no runtime checking of these properties.
1953 """
1954 return f
1955
1956
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001957# Some unconstrained type variables. These are used by the container types.
1958# (These are not for export.)
1959T = TypeVar('T') # Any type.
1960KT = TypeVar('KT') # Key type.
1961VT = TypeVar('VT') # Value type.
1962T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
1963V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
1964VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
1965T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
1966# Internal type variable used for Type[].
1967CT_co = TypeVar('CT_co', covariant=True, bound=type)
1968
1969# A useful type variable with constraints. This represents string types.
1970# (This one *is* for export!)
1971AnyStr = TypeVar('AnyStr', bytes, str)
1972
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001973
1974# Various ABCs mimicking those in collections.abc.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001975_alias = _SpecialGenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001976
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001977Hashable = _alias(collections.abc.Hashable, 0) # Not generic.
1978Awaitable = _alias(collections.abc.Awaitable, 1)
1979Coroutine = _alias(collections.abc.Coroutine, 3)
1980AsyncIterable = _alias(collections.abc.AsyncIterable, 1)
1981AsyncIterator = _alias(collections.abc.AsyncIterator, 1)
1982Iterable = _alias(collections.abc.Iterable, 1)
1983Iterator = _alias(collections.abc.Iterator, 1)
1984Reversible = _alias(collections.abc.Reversible, 1)
1985Sized = _alias(collections.abc.Sized, 0) # Not generic.
1986Container = _alias(collections.abc.Container, 1)
1987Collection = _alias(collections.abc.Collection, 1)
1988Callable = _CallableType(collections.abc.Callable, 2)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001989Callable.__doc__ = \
1990 """Callable type; Callable[[int], str] is a function of (int) -> str.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001991
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001992 The subscription syntax must always be used with exactly two
1993 values: the argument list and the return type. The argument list
1994 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001995
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001996 There is no syntax to indicate optional or keyword arguments,
1997 such function types are rarely used as callback types.
1998 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001999AbstractSet = _alias(collections.abc.Set, 1, name='AbstractSet')
2000MutableSet = _alias(collections.abc.MutableSet, 1)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002001# NOTE: Mapping is only covariant in the value type.
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002002Mapping = _alias(collections.abc.Mapping, 2)
2003MutableMapping = _alias(collections.abc.MutableMapping, 2)
2004Sequence = _alias(collections.abc.Sequence, 1)
2005MutableSequence = _alias(collections.abc.MutableSequence, 1)
2006ByteString = _alias(collections.abc.ByteString, 0) # Not generic
2007# Tuple accepts variable number of parameters.
2008Tuple = _TupleType(tuple, -1, inst=False, name='Tuple')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002009Tuple.__doc__ = \
2010 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07002011
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002012 Example: Tuple[T1, T2] is a tuple of two elements corresponding
2013 to type variables T1 and T2. Tuple[int, float, str] is a tuple
2014 of an int, a float and a string.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07002015
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002016 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
2017 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002018List = _alias(list, 1, inst=False, name='List')
2019Deque = _alias(collections.deque, 1, name='Deque')
2020Set = _alias(set, 1, inst=False, name='Set')
2021FrozenSet = _alias(frozenset, 1, inst=False, name='FrozenSet')
2022MappingView = _alias(collections.abc.MappingView, 1)
2023KeysView = _alias(collections.abc.KeysView, 1)
2024ItemsView = _alias(collections.abc.ItemsView, 2)
2025ValuesView = _alias(collections.abc.ValuesView, 1)
2026ContextManager = _alias(contextlib.AbstractContextManager, 1, name='ContextManager')
2027AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, 1, name='AsyncContextManager')
2028Dict = _alias(dict, 2, inst=False, name='Dict')
2029DefaultDict = _alias(collections.defaultdict, 2, name='DefaultDict')
2030OrderedDict = _alias(collections.OrderedDict, 2)
2031Counter = _alias(collections.Counter, 1)
2032ChainMap = _alias(collections.ChainMap, 2)
2033Generator = _alias(collections.abc.Generator, 3)
2034AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2)
2035Type = _alias(type, 1, inst=False, name='Type')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002036Type.__doc__ = \
2037 """A special construct usable to annotate class objects.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07002038
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002039 For example, suppose we have the following classes::
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07002040
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002041 class User: ... # Abstract base for User classes
2042 class BasicUser(User): ...
2043 class ProUser(User): ...
2044 class TeamUser(User): ...
Guido van Rossumf17c2002015-12-03 17:31:24 -08002045
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002046 And a function that takes a class argument that's a subclass of
2047 User and returns an instance of the corresponding class::
Guido van Rossumf17c2002015-12-03 17:31:24 -08002048
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002049 U = TypeVar('U', bound=User)
2050 def new_user(user_class: Type[U]) -> U:
2051 user = user_class()
2052 # (Here we could write the user object to a database)
2053 return user
Guido van Rossumf17c2002015-12-03 17:31:24 -08002054
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002055 joe = new_user(BasicUser)
Guido van Rossumf17c2002015-12-03 17:31:24 -08002056
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002057 At this point the type checker knows that joe has type BasicUser.
2058 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002059
2060
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002061@runtime_checkable
2062class SupportsInt(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002063 """An ABC with one abstract method __int__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002064 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002065
2066 @abstractmethod
2067 def __int__(self) -> int:
2068 pass
2069
2070
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002071@runtime_checkable
2072class SupportsFloat(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002073 """An ABC with one abstract method __float__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002074 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002075
2076 @abstractmethod
2077 def __float__(self) -> float:
2078 pass
2079
2080
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002081@runtime_checkable
2082class SupportsComplex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002083 """An ABC with one abstract method __complex__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002084 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002085
2086 @abstractmethod
2087 def __complex__(self) -> complex:
2088 pass
2089
2090
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002091@runtime_checkable
2092class SupportsBytes(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002093 """An ABC with one abstract method __bytes__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002094 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002095
2096 @abstractmethod
2097 def __bytes__(self) -> bytes:
2098 pass
2099
2100
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002101@runtime_checkable
2102class SupportsIndex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002103 """An ABC with one abstract method __index__."""
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -07002104 __slots__ = ()
2105
2106 @abstractmethod
2107 def __index__(self) -> int:
2108 pass
2109
2110
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002111@runtime_checkable
2112class SupportsAbs(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002113 """An ABC with one abstract method __abs__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002114 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002115
2116 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02002117 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002118 pass
2119
2120
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002121@runtime_checkable
2122class SupportsRound(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002123 """An ABC with one abstract method __round__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002124 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002125
2126 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02002127 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002128 pass
2129
2130
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002131def _make_nmtuple(name, types, module, defaults = ()):
2132 fields = [n for n, t in types]
2133 types = {n: _type_check(t, f"field {n} annotation must be a type")
2134 for n, t in types}
2135 nm_tpl = collections.namedtuple(name, fields,
2136 defaults=defaults, module=module)
2137 nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002138 return nm_tpl
2139
2140
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002141# attributes prohibited to set in NamedTuple class syntax
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002142_prohibited = frozenset({'__new__', '__init__', '__slots__', '__getnewargs__',
2143 '_fields', '_field_defaults',
2144 '_make', '_replace', '_asdict', '_source'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002145
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002146_special = frozenset({'__module__', '__name__', '__annotations__'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002147
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002148
Guido van Rossum2f841442016-11-15 09:48:06 -08002149class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002150
Guido van Rossum2f841442016-11-15 09:48:06 -08002151 def __new__(cls, typename, bases, ns):
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002152 assert bases[0] is _NamedTuple
Guido van Rossum2f841442016-11-15 09:48:06 -08002153 types = ns.get('__annotations__', {})
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002154 default_names = []
Guido van Rossum3c268be2017-01-18 08:03:50 -08002155 for field_name in types:
2156 if field_name in ns:
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002157 default_names.append(field_name)
2158 elif default_names:
2159 raise TypeError(f"Non-default namedtuple field {field_name} "
2160 f"cannot follow default field"
2161 f"{'s' if len(default_names) > 1 else ''} "
2162 f"{', '.join(default_names)}")
2163 nm_tpl = _make_nmtuple(typename, types.items(),
2164 defaults=[ns[n] for n in default_names],
2165 module=ns['__module__'])
Guido van Rossum95919c02017-01-22 17:47:20 -08002166 # update from user namespace without overriding special namedtuple attributes
2167 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002168 if key in _prohibited:
2169 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
2170 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08002171 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08002172 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002173
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002174
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002175def NamedTuple(typename, fields=None, /, **kwargs):
Guido van Rossum2f841442016-11-15 09:48:06 -08002176 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002177
Guido van Rossum2f841442016-11-15 09:48:06 -08002178 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002179
Guido van Rossum2f841442016-11-15 09:48:06 -08002180 class Employee(NamedTuple):
2181 name: str
2182 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002183
Guido van Rossum2f841442016-11-15 09:48:06 -08002184 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002185
Guido van Rossum2f841442016-11-15 09:48:06 -08002186 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002187
Raymond Hettingerf7b57df2019-03-18 09:53:56 -07002188 The resulting class has an extra __annotations__ attribute, giving a
2189 dict that maps field names to types. (The field names are also in
2190 the _fields attribute, which is part of the namedtuple API.)
2191 Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002192
Guido van Rossum2f841442016-11-15 09:48:06 -08002193 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002194
Guido van Rossum2f841442016-11-15 09:48:06 -08002195 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002196
Guido van Rossum2f841442016-11-15 09:48:06 -08002197 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
2198 """
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002199 if fields is None:
2200 fields = kwargs.items()
2201 elif kwargs:
2202 raise TypeError("Either list of fields or keywords"
2203 " can be provided to NamedTuple, not both")
2204 try:
2205 module = sys._getframe(1).f_globals.get('__name__', '__main__')
2206 except (AttributeError, ValueError):
2207 module = None
2208 return _make_nmtuple(typename, fields, module=module)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002209
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002210_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})
2211
2212def _namedtuple_mro_entries(bases):
2213 if len(bases) > 1:
2214 raise TypeError("Multiple inheritance with NamedTuple is not supported")
2215 assert bases[0] is NamedTuple
2216 return (_NamedTuple,)
2217
2218NamedTuple.__mro_entries__ = _namedtuple_mro_entries
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002219
2220
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002221class _TypedDictMeta(type):
2222 def __new__(cls, name, bases, ns, total=True):
2223 """Create new typed dict class object.
2224
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002225 This method is called when TypedDict is subclassed,
2226 or when TypedDict is instantiated. This way
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002227 TypedDict supports all three syntax forms described in its docstring.
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002228 Subclasses and instances of TypedDict return actual dictionaries.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002229 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002230 for base in bases:
2231 if type(base) is not _TypedDictMeta:
2232 raise TypeError('cannot inherit from both a TypedDict type '
2233 'and a non-TypedDict base class')
2234 tp_dict = type.__new__(_TypedDictMeta, name, (dict,), ns)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002235
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002236 annotations = {}
2237 own_annotations = ns.get('__annotations__', {})
2238 own_annotation_keys = set(own_annotations.keys())
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002239 msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002240 own_annotations = {
Miss Islington (bot)480f29f2021-07-17 01:48:17 -07002241 n: _type_check(tp, msg, module=tp_dict.__module__)
2242 for n, tp in own_annotations.items()
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002243 }
2244 required_keys = set()
2245 optional_keys = set()
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002246
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002247 for base in bases:
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002248 annotations.update(base.__dict__.get('__annotations__', {}))
2249 required_keys.update(base.__dict__.get('__required_keys__', ()))
2250 optional_keys.update(base.__dict__.get('__optional_keys__', ()))
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002251
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002252 annotations.update(own_annotations)
2253 if total:
2254 required_keys.update(own_annotation_keys)
2255 else:
2256 optional_keys.update(own_annotation_keys)
2257
2258 tp_dict.__annotations__ = annotations
2259 tp_dict.__required_keys__ = frozenset(required_keys)
2260 tp_dict.__optional_keys__ = frozenset(optional_keys)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002261 if not hasattr(tp_dict, '__total__'):
2262 tp_dict.__total__ = total
2263 return tp_dict
2264
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002265 __call__ = dict # static method
2266
2267 def __subclasscheck__(cls, other):
2268 # Typed dicts are only for static structural subtyping.
2269 raise TypeError('TypedDict does not support instance and class checks')
2270
2271 __instancecheck__ = __subclasscheck__
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002272
2273
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002274def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002275 """A simple typed namespace. At runtime it is equivalent to a plain dict.
2276
2277 TypedDict creates a dictionary type that expects all of its
2278 instances to have a certain set of keys, where each key is
2279 associated with a value of a consistent type. This expectation
2280 is not checked at runtime but is only enforced by type checkers.
2281 Usage::
2282
2283 class Point2D(TypedDict):
2284 x: int
2285 y: int
2286 label: str
2287
2288 a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
2289 b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
2290
2291 assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
2292
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002293 The type info can be accessed via the Point2D.__annotations__ dict, and
2294 the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
2295 TypedDict supports two additional equivalent forms::
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002296
2297 Point2D = TypedDict('Point2D', x=int, y=int, label=str)
2298 Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
2299
ananthan-123ab6423f2020-02-19 10:03:05 +05302300 By default, all keys must be present in a TypedDict. It is possible
2301 to override this by specifying totality.
2302 Usage::
2303
2304 class point2D(TypedDict, total=False):
2305 x: int
2306 y: int
2307
2308 This means that a point2D TypedDict can have any of the keys omitted.A type
2309 checker is only expected to support a literal False or True as the value of
2310 the total argument. True is the default, and makes all items defined in the
2311 class body be required.
2312
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002313 The class syntax is only supported in Python 3.6+, while two other
2314 syntax forms work for Python 2.7 and 3.2+
2315 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002316 if fields is None:
2317 fields = kwargs
2318 elif kwargs:
2319 raise TypeError("TypedDict takes either a dict or keyword arguments,"
2320 " but not both")
2321
Alex Grönholm67b769f2020-12-10 23:49:05 +02002322 ns = {'__annotations__': dict(fields)}
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002323 try:
2324 # Setting correct module is necessary to make typed dict classes pickleable.
2325 ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
2326 except (AttributeError, ValueError):
2327 pass
2328
Alex Grönholm67b769f2020-12-10 23:49:05 +02002329 return _TypedDictMeta(typename, (), ns, total=total)
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002330
2331_TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {})
2332TypedDict.__mro_entries__ = lambda bases: (_TypedDict,)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002333
2334
Guido van Rossum91185fe2016-06-08 11:19:11 -07002335def NewType(name, tp):
2336 """NewType creates simple unique types with almost zero
2337 runtime overhead. NewType(name, tp) is considered a subtype of tp
2338 by static type checkers. At runtime, NewType(name, tp) returns
2339 a dummy function that simply returns its argument. Usage::
2340
2341 UserId = NewType('UserId', int)
2342
2343 def name_by_id(user_id: UserId) -> str:
2344 ...
2345
2346 UserId('user') # Fails type check
2347
2348 name_by_id(42) # Fails type check
2349 name_by_id(UserId(42)) # OK
2350
2351 num = UserId(5) + 1 # type: int
2352 """
2353
2354 def new_type(x):
2355 return x
2356
2357 new_type.__name__ = name
2358 new_type.__supertype__ = tp
2359 return new_type
2360
2361
Guido van Rossum0e0563c2016-04-05 14:54:25 -07002362# Python-version-specific alias (Python 2: unicode; Python 3: str)
2363Text = str
2364
2365
Guido van Rossum91185fe2016-06-08 11:19:11 -07002366# Constant that's True when type checking, but False here.
2367TYPE_CHECKING = False
2368
2369
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002370class IO(Generic[AnyStr]):
2371 """Generic base class for TextIO and BinaryIO.
2372
2373 This is an abstract, generic version of the return of open().
2374
2375 NOTE: This does not distinguish between the different possible
2376 classes (text vs. binary, read vs. write vs. read/write,
2377 append-only, unbuffered). The TextIO and BinaryIO subclasses
2378 below capture the distinctions between text vs. binary, which is
2379 pervasive in the interface; however we currently do not offer a
2380 way to track the other distinctions in the type system.
2381 """
2382
Guido van Rossumd70fe632015-08-05 12:11:06 +02002383 __slots__ = ()
2384
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002385 @property
2386 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002387 def mode(self) -> str:
2388 pass
2389
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002390 @property
2391 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002392 def name(self) -> str:
2393 pass
2394
2395 @abstractmethod
2396 def close(self) -> None:
2397 pass
2398
Shantanu2e6569b2020-01-29 18:52:36 -08002399 @property
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002400 @abstractmethod
2401 def closed(self) -> bool:
2402 pass
2403
2404 @abstractmethod
2405 def fileno(self) -> int:
2406 pass
2407
2408 @abstractmethod
2409 def flush(self) -> None:
2410 pass
2411
2412 @abstractmethod
2413 def isatty(self) -> bool:
2414 pass
2415
2416 @abstractmethod
2417 def read(self, n: int = -1) -> AnyStr:
2418 pass
2419
2420 @abstractmethod
2421 def readable(self) -> bool:
2422 pass
2423
2424 @abstractmethod
2425 def readline(self, limit: int = -1) -> AnyStr:
2426 pass
2427
2428 @abstractmethod
2429 def readlines(self, hint: int = -1) -> List[AnyStr]:
2430 pass
2431
2432 @abstractmethod
2433 def seek(self, offset: int, whence: int = 0) -> int:
2434 pass
2435
2436 @abstractmethod
2437 def seekable(self) -> bool:
2438 pass
2439
2440 @abstractmethod
2441 def tell(self) -> int:
2442 pass
2443
2444 @abstractmethod
2445 def truncate(self, size: int = None) -> int:
2446 pass
2447
2448 @abstractmethod
2449 def writable(self) -> bool:
2450 pass
2451
2452 @abstractmethod
2453 def write(self, s: AnyStr) -> int:
2454 pass
2455
2456 @abstractmethod
2457 def writelines(self, lines: List[AnyStr]) -> None:
2458 pass
2459
2460 @abstractmethod
2461 def __enter__(self) -> 'IO[AnyStr]':
2462 pass
2463
2464 @abstractmethod
2465 def __exit__(self, type, value, traceback) -> None:
2466 pass
2467
2468
2469class BinaryIO(IO[bytes]):
2470 """Typed version of the return of open() in binary mode."""
2471
Guido van Rossumd70fe632015-08-05 12:11:06 +02002472 __slots__ = ()
2473
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002474 @abstractmethod
2475 def write(self, s: Union[bytes, bytearray]) -> int:
2476 pass
2477
2478 @abstractmethod
2479 def __enter__(self) -> 'BinaryIO':
2480 pass
2481
2482
2483class TextIO(IO[str]):
2484 """Typed version of the return of open() in text mode."""
2485
Guido van Rossumd70fe632015-08-05 12:11:06 +02002486 __slots__ = ()
2487
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002488 @property
2489 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002490 def buffer(self) -> BinaryIO:
2491 pass
2492
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002493 @property
2494 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002495 def encoding(self) -> str:
2496 pass
2497
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002498 @property
2499 @abstractmethod
Guido van Rossum991d14f2016-11-09 13:12:51 -08002500 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002501 pass
2502
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002503 @property
2504 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002505 def line_buffering(self) -> bool:
2506 pass
2507
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002508 @property
2509 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002510 def newlines(self) -> Any:
2511 pass
2512
2513 @abstractmethod
2514 def __enter__(self) -> 'TextIO':
2515 pass
2516
2517
2518class io:
2519 """Wrapper namespace for IO generic classes."""
2520
2521 __all__ = ['IO', 'TextIO', 'BinaryIO']
2522 IO = IO
2523 TextIO = TextIO
2524 BinaryIO = BinaryIO
2525
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002526
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002527io.__name__ = __name__ + '.io'
2528sys.modules[io.__name__] = io
2529
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002530Pattern = _alias(stdlib_re.Pattern, 1)
2531Match = _alias(stdlib_re.Match, 1)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002532
2533class re:
2534 """Wrapper namespace for re type aliases."""
2535
2536 __all__ = ['Pattern', 'Match']
2537 Pattern = Pattern
2538 Match = Match
2539
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002540
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002541re.__name__ = __name__ + '.re'
2542sys.modules[re.__name__] = re