blob: 6f884e1fe9860243c8b8293fba5fd2f028147d1e [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")
Miss Islington (bot)8a37e8c2021-07-26 12:02:58 -0700170 if isinstance(arg, (type, TypeVar, ForwardRef, types.UnionType, ParamSpec)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000171 return arg
172 if not callable(arg):
173 raise TypeError(f"{msg} Got {arg!r:.100}.")
174 return arg
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700175
176
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000177def _type_repr(obj):
178 """Return the repr() of an object, special-casing types (internal helper).
179
180 If obj is a type, we return a shorter version than the default
181 type.__repr__, based on the module and qualified name, which is
182 typically enough to uniquely identify a type. For everything
183 else, we fall back on repr(obj).
184 """
kj1f7dfb22020-11-02 02:13:38 +0800185 if isinstance(obj, types.GenericAlias):
186 return repr(obj)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000187 if isinstance(obj, type):
188 if obj.__module__ == 'builtins':
189 return obj.__qualname__
190 return f'{obj.__module__}.{obj.__qualname__}'
191 if obj is ...:
192 return('...')
193 if isinstance(obj, types.FunctionType):
194 return obj.__name__
195 return repr(obj)
196
197
Ken Jina2721642021-07-19 22:22:59 +0800198def _collect_type_vars(types_, typevar_types=None):
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700199 """Collect all type variable contained
kj73607be2020-12-24 12:33:48 +0800200 in types in order of first appearance (lexicographic order). For example::
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000201
202 _collect_type_vars((T, List[S, T])) == (T, S)
203 """
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700204 if typevar_types is None:
205 typevar_types = TypeVar
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000206 tvars = []
Ken Jina2721642021-07-19 22:22:59 +0800207 for t in types_:
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700208 if isinstance(t, typevar_types) and t not in tvars:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000209 tvars.append(t)
Miss Islington (bot)8a37e8c2021-07-26 12:02:58 -0700210 if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):
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:
Miss Islington (bot)c8db2922021-08-02 00:08:24 -0700223 raise TypeError(f"Too {'many' if alen > elen else 'few'} arguments for {cls};"
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000224 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:
Miss Islington (bot)c8db2922021-08-02 00:08:24 -0700234 _check_generic(cls, params, len(cls.__parameters__))
kj73607be2020-12-24 12:33:48 +0800235 _params = []
236 # Convert lists to tuples to help other libraries cache the results.
237 for p, tvar in zip(params, cls.__parameters__):
238 if isinstance(tvar, ParamSpec) and isinstance(p, list):
239 p = tuple(p)
240 _params.append(p)
241 return tuple(_params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000242
Yurii Karabasf03d3182020-11-17 04:23:19 +0200243def _deduplicate(params):
244 # Weed out strict duplicates, preserving the first of each occurrence.
245 all_params = set(params)
246 if len(all_params) < len(params):
247 new_params = []
248 for t in params:
249 if t in all_params:
250 new_params.append(t)
251 all_params.remove(t)
252 params = new_params
253 assert not all_params, all_params
254 return params
255
256
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000257def _remove_dups_flatten(parameters):
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700258 """An internal helper for Union creation and substitution: flatten Unions
259 among parameters, then remove duplicates.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000260 """
261 # Flatten out Union[Union[...], ...].
262 params = []
263 for p in parameters:
Miss Islington (bot)8a37e8c2021-07-26 12:02:58 -0700264 if isinstance(p, (_UnionGenericAlias, types.UnionType)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000265 params.extend(p.__args__)
266 elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
267 params.extend(p[1:])
268 else:
269 params.append(p)
Yurii Karabasf03d3182020-11-17 04:23:19 +0200270
271 return tuple(_deduplicate(params))
272
273
274def _flatten_literal_params(parameters):
275 """An internal helper for Literal creation: flatten Literals among parameters"""
276 params = []
277 for p in parameters:
278 if isinstance(p, _LiteralGenericAlias):
279 params.extend(p.__args__)
280 else:
281 params.append(p)
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700282 return tuple(params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000283
284
285_cleanups = []
286
287
Yurii Karabasf03d3182020-11-17 04:23:19 +0200288def _tp_cache(func=None, /, *, typed=False):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000289 """Internal wrapper caching __getitem__ of generic types with a fallback to
290 original function for non-hashable arguments.
291 """
Yurii Karabasf03d3182020-11-17 04:23:19 +0200292 def decorator(func):
293 cached = functools.lru_cache(typed=typed)(func)
294 _cleanups.append(cached.cache_clear)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000295
Yurii Karabasf03d3182020-11-17 04:23:19 +0200296 @functools.wraps(func)
297 def inner(*args, **kwds):
298 try:
299 return cached(*args, **kwds)
300 except TypeError:
301 pass # All real errors (not unhashable args) are raised below.
302 return func(*args, **kwds)
303 return inner
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000304
Yurii Karabasf03d3182020-11-17 04:23:19 +0200305 if func is not None:
306 return decorator(func)
307
308 return decorator
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000309
wyfo653f4202020-07-22 21:47:28 +0200310def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
Graham Bleaney84ef33c2020-09-08 18:41:10 -0400311 """Evaluate all forward references in the given type t.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000312 For use of globalns and localns see the docstring for get_type_hints().
wyfo653f4202020-07-22 21:47:28 +0200313 recursive_guard is used to prevent prevent infinite recursion
314 with recursive ForwardRef.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000315 """
316 if isinstance(t, ForwardRef):
wyfo653f4202020-07-22 21:47:28 +0200317 return t._evaluate(globalns, localns, recursive_guard)
Miss Islington (bot)8a37e8c2021-07-26 12:02:58 -0700318 if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):
wyfo653f4202020-07-22 21:47:28 +0200319 ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000320 if ev_args == t.__args__:
321 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300322 if isinstance(t, GenericAlias):
323 return GenericAlias(t.__origin__, ev_args)
Miss Islington (bot)8a37e8c2021-07-26 12:02:58 -0700324 if isinstance(t, types.UnionType):
Miss Islington (bot)0aea99e2021-07-24 12:35:33 -0700325 return functools.reduce(operator.or_, ev_args)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300326 else:
327 return t.copy_with(ev_args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000328 return t
329
330
331class _Final:
332 """Mixin to prohibit subclassing"""
Guido van Rossum4cefe742016-09-27 15:20:12 -0700333
Guido van Rossum83ec3022017-01-17 20:43:28 -0800334 __slots__ = ('__weakref__',)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700335
Serhiy Storchaka2085bd02019-06-01 11:00:15 +0300336 def __init_subclass__(self, /, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000337 if '_root' not in kwds:
338 raise TypeError("Cannot subclass special typing classes")
339
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100340class _Immutable:
341 """Mixin to indicate that object should not be copied."""
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300342 __slots__ = ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000343
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100344 def __copy__(self):
345 return self
346
347 def __deepcopy__(self, memo):
348 return self
349
350
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300351# Internal indicator of special typing constructs.
352# See __doc__ instance attribute for specific docs.
353class _SpecialForm(_Final, _root=True):
354 __slots__ = ('_name', '__doc__', '_getitem')
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000355
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300356 def __init__(self, getitem):
357 self._getitem = getitem
358 self._name = getitem.__name__
359 self.__doc__ = getitem.__doc__
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000360
Miss Islington (bot)c895f2b2021-07-19 10:57:27 -0700361 def __getattr__(self, item):
362 if item in {'__name__', '__qualname__'}:
363 return self._name
364
365 raise AttributeError(item)
366
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300367 def __mro_entries__(self, bases):
368 raise TypeError(f"Cannot subclass {self!r}")
Guido van Rossum4cefe742016-09-27 15:20:12 -0700369
370 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000371 return 'typing.' + self._name
372
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100373 def __reduce__(self):
374 return self._name
Guido van Rossum4cefe742016-09-27 15:20:12 -0700375
376 def __call__(self, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000377 raise TypeError(f"Cannot instantiate {self!r}")
378
Ken Jinca5a4cf2021-07-24 22:49:25 +0800379 def __or__(self, other):
380 return Union[self, other]
381
382 def __ror__(self, other):
383 return Union[other, self]
384
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000385 def __instancecheck__(self, obj):
386 raise TypeError(f"{self} cannot be used with isinstance()")
387
388 def __subclasscheck__(self, cls):
389 raise TypeError(f"{self} cannot be used with issubclass()")
390
391 @_tp_cache
392 def __getitem__(self, parameters):
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300393 return self._getitem(self, parameters)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700394
Yurii Karabasf03d3182020-11-17 04:23:19 +0200395
396class _LiteralSpecialForm(_SpecialForm, _root=True):
397 @_tp_cache(typed=True)
398 def __getitem__(self, parameters):
399 return self._getitem(self, parameters)
400
401
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300402@_SpecialForm
403def Any(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000404 """Special type indicating an unconstrained type.
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700405
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000406 - Any is compatible with every type.
407 - Any assumed to have all methods.
408 - All values assumed to be instances of Any.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700409
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000410 Note that all the above statements are true from the point of view of
411 static type checkers. At runtime, Any should not be used with instance
412 or class checks.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300413 """
414 raise TypeError(f"{self} is not subscriptable")
Guido van Rossumd70fe632015-08-05 12:11:06 +0200415
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300416@_SpecialForm
417def NoReturn(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000418 """Special type indicating functions that never return.
419 Example::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700420
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000421 from typing import NoReturn
422
423 def stop() -> NoReturn:
424 raise Exception('no way')
425
426 This type is invalid in other positions, e.g., ``List[NoReturn]``
427 will fail in static type checkers.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300428 """
429 raise TypeError(f"{self} is not subscriptable")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000430
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300431@_SpecialForm
432def ClassVar(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000433 """Special type construct to mark class variables.
434
435 An annotation wrapped in ClassVar indicates that a given
436 attribute is intended to be used as a class variable and
437 should not be set on instances of that class. Usage::
438
439 class Starship:
440 stats: ClassVar[Dict[str, int]] = {} # class variable
441 damage: int = 10 # instance variable
442
443 ClassVar accepts only types and cannot be further subscribed.
444
445 Note that ClassVar is not a class itself, and should not
446 be used with isinstance() or issubclass().
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300447 """
448 item = _type_check(parameters, f'{self} accepts only single type.')
449 return _GenericAlias(self, (item,))
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000450
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300451@_SpecialForm
452def Final(self, parameters):
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100453 """Special typing construct to indicate final names to type checkers.
454
455 A final name cannot be re-assigned or overridden in a subclass.
456 For example:
457
458 MAX_SIZE: Final = 9000
459 MAX_SIZE += 1 # Error reported by type checker
460
461 class Connection:
462 TIMEOUT: Final[int] = 10
463
464 class FastConnector(Connection):
465 TIMEOUT = 1 # Error reported by type checker
466
467 There is no runtime checking of these properties.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300468 """
469 item = _type_check(parameters, f'{self} accepts only single type.')
470 return _GenericAlias(self, (item,))
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100471
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300472@_SpecialForm
473def Union(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000474 """Union type; Union[X, Y] means either X or Y.
475
476 To define a union, use e.g. Union[int, str]. Details:
477 - The arguments must be types and there must be at least one.
478 - None as an argument is a special case and is replaced by
479 type(None).
480 - Unions of unions are flattened, e.g.::
481
482 Union[Union[int, str], float] == Union[int, str, float]
483
484 - Unions of a single argument vanish, e.g.::
485
486 Union[int] == int # The constructor actually returns int
487
488 - Redundant arguments are skipped, e.g.::
489
490 Union[int, str, int] == Union[int, str]
491
492 - When comparing unions, the argument order is ignored, e.g.::
493
494 Union[int, str] == Union[str, int]
495
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000496 - You cannot subclass or instantiate a union.
497 - You can use Optional[X] as a shorthand for Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300498 """
499 if parameters == ():
500 raise TypeError("Cannot take a Union of no types.")
501 if not isinstance(parameters, tuple):
502 parameters = (parameters,)
503 msg = "Union[arg, ...]: each arg must be a type."
504 parameters = tuple(_type_check(p, msg) for p in parameters)
505 parameters = _remove_dups_flatten(parameters)
506 if len(parameters) == 1:
507 return parameters[0]
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300508 return _UnionGenericAlias(self, parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000509
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300510@_SpecialForm
511def Optional(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000512 """Optional type.
513
514 Optional[X] is equivalent to Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300515 """
516 arg = _type_check(parameters, f"{self} requires a single type.")
517 return Union[arg, type(None)]
Guido van Rossumb7dedc82016-10-29 12:44:29 -0700518
Yurii Karabasf03d3182020-11-17 04:23:19 +0200519@_LiteralSpecialForm
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300520def Literal(self, parameters):
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100521 """Special typing form to define literal types (a.k.a. value types).
522
523 This form can be used to indicate to type checkers that the corresponding
524 variable or function parameter has a value equivalent to the provided
525 literal (or one of several literals):
526
527 def validate_simple(data: Any) -> Literal[True]: # always returns True
528 ...
529
530 MODE = Literal['r', 'rb', 'w', 'wb']
531 def open_helper(file: str, mode: MODE) -> str:
532 ...
533
534 open_helper('/some/path', 'r') # Passes type check
535 open_helper('/other/path', 'typo') # Error in type checker
536
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300537 Literal[...] cannot be subclassed. At runtime, an arbitrary value
538 is allowed as type argument to Literal[...], but type checkers may
539 impose restrictions.
540 """
541 # There is no '_type_check' call because arguments to Literal[...] are
542 # values, not types.
Yurii Karabasf03d3182020-11-17 04:23:19 +0200543 if not isinstance(parameters, tuple):
544 parameters = (parameters,)
545
546 parameters = _flatten_literal_params(parameters)
547
548 try:
549 parameters = tuple(p for p, _ in _deduplicate(list(_value_and_type_iter(parameters))))
550 except TypeError: # unhashable parameters
551 pass
552
553 return _LiteralGenericAlias(self, parameters)
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100554
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700555
Mikhail Golubev4f3c2502020-10-08 00:44:31 +0300556@_SpecialForm
557def TypeAlias(self, parameters):
558 """Special marker indicating that an assignment should
559 be recognized as a proper type alias definition by type
560 checkers.
561
562 For example::
563
564 Predicate: TypeAlias = Callable[..., bool]
565
566 It's invalid when used anywhere except as in the example above.
567 """
568 raise TypeError(f"{self} is not subscriptable")
569
570
kj73607be2020-12-24 12:33:48 +0800571@_SpecialForm
572def Concatenate(self, parameters):
Ken Jin11276cd2021-01-02 08:45:50 +0800573 """Used in conjunction with ``ParamSpec`` and ``Callable`` to represent a
574 higher order function which adds, removes or transforms parameters of a
575 callable.
kj73607be2020-12-24 12:33:48 +0800576
577 For example::
578
579 Callable[Concatenate[int, P], int]
580
581 See PEP 612 for detailed information.
582 """
583 if parameters == ():
584 raise TypeError("Cannot take a Concatenate of no types.")
585 if not isinstance(parameters, tuple):
586 parameters = (parameters,)
587 if not isinstance(parameters[-1], ParamSpec):
588 raise TypeError("The last parameter to Concatenate should be a "
589 "ParamSpec variable.")
590 msg = "Concatenate[arg, ...]: each arg must be a type."
591 parameters = tuple(_type_check(p, msg) for p in parameters)
592 return _ConcatenateGenericAlias(self, parameters)
593
594
Ken Jin05ab4b62021-04-27 22:31:04 +0800595@_SpecialForm
596def TypeGuard(self, parameters):
597 """Special typing form used to annotate the return type of a user-defined
598 type guard function. ``TypeGuard`` only accepts a single type argument.
599 At runtime, functions marked this way should return a boolean.
600
601 ``TypeGuard`` aims to benefit *type narrowing* -- a technique used by static
602 type checkers to determine a more precise type of an expression within a
603 program's code flow. Usually type narrowing is done by analyzing
604 conditional code flow and applying the narrowing to a block of code. The
605 conditional expression here is sometimes referred to as a "type guard".
606
607 Sometimes it would be convenient to use a user-defined boolean function
608 as a type guard. Such a function should use ``TypeGuard[...]`` as its
609 return type to alert static type checkers to this intention.
610
611 Using ``-> TypeGuard`` tells the static type checker that for a given
612 function:
613
614 1. The return value is a boolean.
615 2. If the return value is ``True``, the type of its argument
616 is the type inside ``TypeGuard``.
617
618 For example::
619
620 def is_str(val: Union[str, float]):
621 # "isinstance" type guard
622 if isinstance(val, str):
623 # Type of ``val`` is narrowed to ``str``
624 ...
625 else:
626 # Else, type of ``val`` is narrowed to ``float``.
627 ...
628
629 Strict type narrowing is not enforced -- ``TypeB`` need not be a narrower
630 form of ``TypeA`` (it can even be a wider form) and this may lead to
631 type-unsafe results. The main reason is to allow for things like
632 narrowing ``List[object]`` to ``List[str]`` even though the latter is not
633 a subtype of the former, since ``List`` is invariant. The responsibility of
634 writing type-safe type guards is left to the user.
635
636 ``TypeGuard`` also works with type variables. For more information, see
637 PEP 647 (User-Defined Type Guards).
638 """
639 item = _type_check(parameters, f'{self} accepts only single type.')
640 return _GenericAlias(self, (item,))
641
642
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000643class ForwardRef(_Final, _root=True):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800644 """Internal wrapper to hold a forward reference."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700645
Guido van Rossum4cefe742016-09-27 15:20:12 -0700646 __slots__ = ('__forward_arg__', '__forward_code__',
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400647 '__forward_evaluated__', '__forward_value__',
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700648 '__forward_is_argument__', '__forward_module__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700649
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700650 def __init__(self, arg, is_argument=True, module=None):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700651 if not isinstance(arg, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000652 raise TypeError(f"Forward reference must be a string -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700653 try:
654 code = compile(arg, '<string>', 'eval')
655 except SyntaxError:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000656 raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700657 self.__forward_arg__ = arg
658 self.__forward_code__ = code
659 self.__forward_evaluated__ = False
660 self.__forward_value__ = None
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400661 self.__forward_is_argument__ = is_argument
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700662 self.__forward_module__ = module
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700663
wyfo653f4202020-07-22 21:47:28 +0200664 def _evaluate(self, globalns, localns, recursive_guard):
665 if self.__forward_arg__ in recursive_guard:
666 return self
Guido van Rossumdad17902016-11-10 08:29:18 -0800667 if not self.__forward_evaluated__ or localns is not globalns:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700668 if globalns is None and localns is None:
669 globalns = localns = {}
670 elif globalns is None:
671 globalns = localns
672 elif localns is None:
673 localns = globalns
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700674 if self.__forward_module__ is not None:
675 globalns = getattr(
676 sys.modules.get(self.__forward_module__, None), '__dict__', globalns
677 )
wyfo653f4202020-07-22 21:47:28 +0200678 type_ =_type_check(
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700679 eval(self.__forward_code__, globalns, localns),
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400680 "Forward references must evaluate to types.",
wyfo653f4202020-07-22 21:47:28 +0200681 is_argument=self.__forward_is_argument__,
682 )
683 self.__forward_value__ = _eval_type(
684 type_, globalns, localns, recursive_guard | {self.__forward_arg__}
685 )
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700686 self.__forward_evaluated__ = True
687 return self.__forward_value__
688
Guido van Rossum4cefe742016-09-27 15:20:12 -0700689 def __eq__(self, other):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000690 if not isinstance(other, ForwardRef):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700691 return NotImplemented
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100692 if self.__forward_evaluated__ and other.__forward_evaluated__:
693 return (self.__forward_arg__ == other.__forward_arg__ and
694 self.__forward_value__ == other.__forward_value__)
695 return self.__forward_arg__ == other.__forward_arg__
Guido van Rossum4cefe742016-09-27 15:20:12 -0700696
697 def __hash__(self):
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100698 return hash(self.__forward_arg__)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700699
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700700 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000701 return f'ForwardRef({self.__forward_arg__!r})'
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700702
kj73607be2020-12-24 12:33:48 +0800703class _TypeVarLike:
704 """Mixin for TypeVar-like types (TypeVar and ParamSpec)."""
705 def __init__(self, bound, covariant, contravariant):
706 """Used to setup TypeVars and ParamSpec's bound, covariant and
707 contravariant attributes.
708 """
709 if covariant and contravariant:
710 raise ValueError("Bivariant types are not supported.")
711 self.__covariant__ = bool(covariant)
712 self.__contravariant__ = bool(contravariant)
713 if bound:
714 self.__bound__ = _type_check(bound, "Bound must be a type.")
715 else:
716 self.__bound__ = None
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700717
kj73607be2020-12-24 12:33:48 +0800718 def __or__(self, right):
719 return Union[self, right]
720
Jelle Zijlstra90459192021-04-10 20:00:05 -0700721 def __ror__(self, left):
722 return Union[left, self]
kj73607be2020-12-24 12:33:48 +0800723
724 def __repr__(self):
725 if self.__covariant__:
726 prefix = '+'
727 elif self.__contravariant__:
728 prefix = '-'
729 else:
730 prefix = '~'
731 return prefix + self.__name__
732
733 def __reduce__(self):
734 return self.__name__
735
736
737class TypeVar( _Final, _Immutable, _TypeVarLike, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700738 """Type variable.
739
740 Usage::
741
742 T = TypeVar('T') # Can be anything
743 A = TypeVar('A', str, bytes) # Must be str or bytes
744
745 Type variables exist primarily for the benefit of static type
746 checkers. They serve as the parameters for generic types as well
747 as for generic function definitions. See class Generic for more
748 information on generic types. Generic functions work as follows:
749
Guido van Rossumb24569a2016-11-20 18:01:29 -0800750 def repeat(x: T, n: int) -> List[T]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700751 '''Return a list containing n references to x.'''
752 return [x]*n
753
754 def longest(x: A, y: A) -> A:
755 '''Return the longest of two strings.'''
756 return x if len(x) >= len(y) else y
757
758 The latter example's signature is essentially the overloading
759 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
760 that if the arguments are instances of some subclass of str,
761 the return type is still plain str.
762
Guido van Rossumb24569a2016-11-20 18:01:29 -0800763 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700764
Guido van Rossumefa798d2016-08-23 11:01:50 -0700765 Type variables defined with covariant=True or contravariant=True
João D. Ferreira86bfed32018-07-07 16:41:20 +0100766 can be used to declare covariant or contravariant generic types.
Guido van Rossumefa798d2016-08-23 11:01:50 -0700767 See PEP 484 for more details. By default generic types are invariant
768 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700769
770 Type variables can be introspected. e.g.:
771
772 T.__name__ == 'T'
773 T.__constraints__ == ()
774 T.__covariant__ == False
775 T.__contravariant__ = False
776 A.__constraints__ == (str, bytes)
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100777
778 Note that only type variables defined in global scope can be pickled.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700779 """
780
Guido van Rossum4cefe742016-09-27 15:20:12 -0700781 __slots__ = ('__name__', '__bound__', '__constraints__',
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300782 '__covariant__', '__contravariant__', '__dict__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700783
784 def __init__(self, name, *constraints, bound=None,
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800785 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700786 self.__name__ = name
kj73607be2020-12-24 12:33:48 +0800787 super().__init__(bound, covariant, contravariant)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700788 if constraints and bound is not None:
789 raise TypeError("Constraints cannot be combined with bound=...")
790 if constraints and len(constraints) == 1:
791 raise TypeError("A single constraint is not allowed")
792 msg = "TypeVar(name, constraint, ...): constraints must be types."
793 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
HongWeipenga25a04f2020-04-21 04:01:53 +0800794 try:
795 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling
796 except (AttributeError, ValueError):
797 def_mod = None
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300798 if def_mod != 'typing':
799 self.__module__ = def_mod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700800
Maggie Moss1b4552c2020-09-09 13:23:24 -0700801
Jelle Zijlstra52243362021-04-10 19:57:05 -0700802class ParamSpecArgs(_Final, _Immutable, _root=True):
803 """The args for a ParamSpec object.
804
805 Given a ParamSpec object P, P.args is an instance of ParamSpecArgs.
806
807 ParamSpecArgs objects have a reference back to their ParamSpec:
808
809 P.args.__origin__ is P
810
811 This type is meant for runtime introspection and has no special meaning to
812 static type checkers.
813 """
814 def __init__(self, origin):
815 self.__origin__ = origin
816
817 def __repr__(self):
818 return f"{self.__origin__.__name__}.args"
819
820
821class ParamSpecKwargs(_Final, _Immutable, _root=True):
822 """The kwargs for a ParamSpec object.
823
824 Given a ParamSpec object P, P.kwargs is an instance of ParamSpecKwargs.
825
826 ParamSpecKwargs objects have a reference back to their ParamSpec:
827
828 P.kwargs.__origin__ is P
829
830 This type is meant for runtime introspection and has no special meaning to
831 static type checkers.
832 """
833 def __init__(self, origin):
834 self.__origin__ = origin
835
836 def __repr__(self):
837 return f"{self.__origin__.__name__}.kwargs"
838
839
kj73607be2020-12-24 12:33:48 +0800840class ParamSpec(_Final, _Immutable, _TypeVarLike, _root=True):
841 """Parameter specification variable.
Maggie Moss1b4552c2020-09-09 13:23:24 -0700842
kj73607be2020-12-24 12:33:48 +0800843 Usage::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700844
kj73607be2020-12-24 12:33:48 +0800845 P = ParamSpec('P')
846
847 Parameter specification variables exist primarily for the benefit of static
848 type checkers. They are used to forward the parameter types of one
Ken Jin11276cd2021-01-02 08:45:50 +0800849 callable to another callable, a pattern commonly found in higher order
850 functions and decorators. They are only valid when used in ``Concatenate``,
851 or s the first argument to ``Callable``, or as parameters for user-defined
852 Generics. See class Generic for more information on generic types. An
853 example for annotating a decorator::
kj73607be2020-12-24 12:33:48 +0800854
855 T = TypeVar('T')
856 P = ParamSpec('P')
857
858 def add_logging(f: Callable[P, T]) -> Callable[P, T]:
859 '''A type-safe decorator to add logging to a function.'''
860 def inner(*args: P.args, **kwargs: P.kwargs) -> T:
861 logging.info(f'{f.__name__} was called')
862 return f(*args, **kwargs)
863 return inner
864
865 @add_logging
866 def add_two(x: float, y: float) -> float:
867 '''Add two numbers together.'''
868 return x + y
869
870 Parameter specification variables defined with covariant=True or
871 contravariant=True can be used to declare covariant or contravariant
872 generic types. These keyword arguments are valid, but their actual semantics
873 are yet to be decided. See PEP 612 for details.
874
875 Parameter specification variables can be introspected. e.g.:
876
877 P.__name__ == 'T'
878 P.__bound__ == None
879 P.__covariant__ == False
880 P.__contravariant__ == False
881
882 Note that only parameter specification variables defined in global scope can
883 be pickled.
884 """
885
886 __slots__ = ('__name__', '__bound__', '__covariant__', '__contravariant__',
887 '__dict__')
888
Jelle Zijlstra52243362021-04-10 19:57:05 -0700889 @property
890 def args(self):
891 return ParamSpecArgs(self)
892
893 @property
894 def kwargs(self):
895 return ParamSpecKwargs(self)
kj73607be2020-12-24 12:33:48 +0800896
Ken Jinace008c2021-01-11 08:11:41 +0800897 def __init__(self, name, *, bound=None, covariant=False, contravariant=False):
kj73607be2020-12-24 12:33:48 +0800898 self.__name__ = name
899 super().__init__(bound, covariant, contravariant)
900 try:
901 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__')
902 except (AttributeError, ValueError):
903 def_mod = None
904 if def_mod != 'typing':
905 self.__module__ = def_mod
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100906
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700907
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000908def _is_dunder(attr):
909 return attr.startswith('__') and attr.endswith('__')
Guido van Rossum83ec3022017-01-17 20:43:28 -0800910
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300911class _BaseGenericAlias(_Final, _root=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000912 """The central part of internal API.
913
914 This represents a generic version of type 'origin' with type arguments 'params'.
915 There are two kind of these aliases: user defined and special. The special ones
916 are wrappers around builtin collections and ABCs in collections.abc. These must
917 have 'name' always set. If 'inst' is False, then the alias can't be instantiated,
918 this is used by e.g. typing.List and typing.Dict.
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700919 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300920 def __init__(self, origin, *, inst=True, name=None):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000921 self._inst = inst
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000922 self._name = name
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700923 self.__origin__ = origin
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000924 self.__slots__ = None # This is not documented.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300925
926 def __call__(self, *args, **kwargs):
927 if not self._inst:
928 raise TypeError(f"Type {self._name} cannot be instantiated; "
929 f"use {self.__origin__.__name__}() instead")
930 result = self.__origin__(*args, **kwargs)
931 try:
932 result.__orig_class__ = self
933 except AttributeError:
934 pass
935 return result
936
937 def __mro_entries__(self, bases):
938 res = []
939 if self.__origin__ not in bases:
940 res.append(self.__origin__)
941 i = bases.index(self)
942 for b in bases[i+1:]:
943 if isinstance(b, _BaseGenericAlias) or issubclass(b, Generic):
944 break
945 else:
946 res.append(Generic)
947 return tuple(res)
948
949 def __getattr__(self, attr):
Miss Islington (bot)c895f2b2021-07-19 10:57:27 -0700950 if attr in {'__name__', '__qualname__'}:
951 return self._name
952
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300953 # We are careful for copy and pickle.
954 # Also for simplicity we just don't relay all dunder names
955 if '__origin__' in self.__dict__ and not _is_dunder(attr):
956 return getattr(self.__origin__, attr)
957 raise AttributeError(attr)
958
959 def __setattr__(self, attr, val):
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700960 if _is_dunder(attr) or attr in {'_name', '_inst', '_nparams',
961 '_typevar_types', '_paramspec_tvars'}:
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300962 super().__setattr__(attr, val)
963 else:
964 setattr(self.__origin__, attr, val)
965
966 def __instancecheck__(self, obj):
967 return self.__subclasscheck__(type(obj))
968
969 def __subclasscheck__(self, cls):
970 raise TypeError("Subscripted generics cannot be used with"
971 " class and instance checks")
972
973
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300974# Special typing constructs Union, Optional, Generic, Callable and Tuple
975# use three special attributes for internal bookkeeping of generic types:
976# * __parameters__ is a tuple of unique free type parameters of a generic
977# type, for example, Dict[T, T].__parameters__ == (T,);
978# * __origin__ keeps a reference to a type that was subscripted,
979# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
980# the type.
981# * __args__ is a tuple of all arguments used in subscripting,
982# e.g., Dict[T, int].__args__ == (T, int).
983
984
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300985class _GenericAlias(_BaseGenericAlias, _root=True):
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700986 def __init__(self, origin, params, *, inst=True, name=None,
987 _typevar_types=TypeVar,
988 _paramspec_tvars=False):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300989 super().__init__(origin, inst=inst, name=name)
990 if not isinstance(params, tuple):
991 params = (params,)
992 self.__args__ = tuple(... if a is _TypingEllipsis else
993 () if a is _TypingEmpty else
994 a for a in params)
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700995 self.__parameters__ = _collect_type_vars(params, typevar_types=_typevar_types)
996 self._typevar_types = _typevar_types
997 self._paramspec_tvars = _paramspec_tvars
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300998 if not name:
999 self.__module__ = origin.__module__
1000
1001 def __eq__(self, other):
1002 if not isinstance(other, _GenericAlias):
1003 return NotImplemented
1004 return (self.__origin__ == other.__origin__
1005 and self.__args__ == other.__args__)
1006
1007 def __hash__(self):
1008 return hash((self.__origin__, self.__args__))
1009
Maggie Moss1b4552c2020-09-09 13:23:24 -07001010 def __or__(self, right):
1011 return Union[self, right]
1012
Serhiy Storchaka80844d12021-07-16 16:42:04 +03001013 def __ror__(self, left):
1014 return Union[left, self]
Maggie Moss1b4552c2020-09-09 13:23:24 -07001015
Guido van Rossum4cefe742016-09-27 15:20:12 -07001016 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001017 def __getitem__(self, params):
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001018 if self.__origin__ in (Generic, Protocol):
1019 # Can't subscript Generic[...] or Protocol[...].
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001020 raise TypeError(f"Cannot subscript already-subscripted {self}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001021 if not isinstance(params, tuple):
1022 params = (params,)
kj73607be2020-12-24 12:33:48 +08001023 params = tuple(_type_convert(p) for p in params)
Miss Islington (bot)c8db2922021-08-02 00:08:24 -07001024 if (self._paramspec_tvars
1025 and any(isinstance(t, ParamSpec) for t in self.__parameters__)):
1026 params = _prepare_paramspec_params(self, params)
1027 else:
1028 _check_generic(self, params, len(self.__parameters__))
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001029
1030 subst = dict(zip(self.__parameters__, params))
1031 new_args = []
1032 for arg in self.__args__:
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001033 if isinstance(arg, self._typevar_types):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001034 arg = subst[arg]
Miss Islington (bot)8a37e8c2021-07-26 12:02:58 -07001035 elif isinstance(arg, (_GenericAlias, GenericAlias, types.UnionType)):
Serhiy Storchaka0122d482020-05-10 13:39:40 +03001036 subparams = arg.__parameters__
1037 if subparams:
1038 subargs = tuple(subst[x] for x in subparams)
1039 arg = arg[subargs]
kj73607be2020-12-24 12:33:48 +08001040 # Required to flatten out the args for CallableGenericAlias
1041 if self.__origin__ == collections.abc.Callable and isinstance(arg, tuple):
1042 new_args.extend(arg)
1043 else:
1044 new_args.append(arg)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001045 return self.copy_with(tuple(new_args))
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001046
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001047 def copy_with(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001048 return self.__class__(self.__origin__, params, name=self._name, inst=self._inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001049
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001050 def __repr__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001051 if self._name:
1052 name = 'typing.' + self._name
1053 else:
1054 name = _type_repr(self.__origin__)
1055 args = ", ".join([_type_repr(a) for a in self.__args__])
1056 return f'{name}[{args}]'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001057
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001058 def __reduce__(self):
1059 if self._name:
1060 origin = globals()[self._name]
1061 else:
1062 origin = self.__origin__
1063 args = tuple(self.__args__)
1064 if len(args) == 1 and not isinstance(args[0], tuple):
1065 args, = args
1066 return operator.getitem, (origin, args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001067
1068 def __mro_entries__(self, bases):
1069 if self._name: # generic version of an ABC or built-in class
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001070 return super().__mro_entries__(bases)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001071 if self.__origin__ is Generic:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001072 if Protocol in bases:
1073 return ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001074 i = bases.index(self)
1075 for b in bases[i+1:]:
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001076 if isinstance(b, _BaseGenericAlias) and b is not self:
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001077 return ()
1078 return (self.__origin__,)
1079
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001080
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001081# _nparams is the number of accepted parameters, e.g. 0 for Hashable,
1082# 1 for List and 2 for Dict. It may be -1 if variable number of
1083# parameters are accepted (needs custom __getitem__).
1084
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001085class _SpecialGenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001086 def __init__(self, origin, nparams, *, inst=True, name=None):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001087 if name is None:
1088 name = origin.__name__
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001089 super().__init__(origin, inst=inst, name=name)
1090 self._nparams = nparams
Serhiy Storchaka2fbc57a2020-05-10 15:14:27 +03001091 if origin.__module__ == 'builtins':
1092 self.__doc__ = f'A generic version of {origin.__qualname__}.'
1093 else:
1094 self.__doc__ = f'A generic version of {origin.__module__}.{origin.__qualname__}.'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001095
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001096 @_tp_cache
1097 def __getitem__(self, params):
1098 if not isinstance(params, tuple):
1099 params = (params,)
1100 msg = "Parameters to generic types must be types."
1101 params = tuple(_type_check(p, msg) for p in params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001102 _check_generic(self, params, self._nparams)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001103 return self.copy_with(params)
1104
1105 def copy_with(self, params):
1106 return _GenericAlias(self.__origin__, params,
1107 name=self._name, inst=self._inst)
1108
1109 def __repr__(self):
1110 return 'typing.' + self._name
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001111
1112 def __subclasscheck__(self, cls):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001113 if isinstance(cls, _SpecialGenericAlias):
1114 return issubclass(cls.__origin__, self.__origin__)
1115 if not isinstance(cls, _GenericAlias):
1116 return issubclass(cls, self.__origin__)
1117 return super().__subclasscheck__(cls)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001118
Ivan Levkivskyi83494032018-03-26 23:01:12 +01001119 def __reduce__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001120 return self._name
Ivan Levkivskyi83494032018-03-26 23:01:12 +01001121
Maggie Moss1b4552c2020-09-09 13:23:24 -07001122 def __or__(self, right):
1123 return Union[self, right]
1124
Serhiy Storchaka80844d12021-07-16 16:42:04 +03001125 def __ror__(self, left):
1126 return Union[left, self]
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001127
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001128class _CallableGenericAlias(_GenericAlias, _root=True):
1129 def __repr__(self):
1130 assert self._name == 'Callable'
kj73607be2020-12-24 12:33:48 +08001131 args = self.__args__
1132 if len(args) == 2 and (args[0] is Ellipsis
1133 or isinstance(args[0], (ParamSpec, _ConcatenateGenericAlias))):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001134 return super().__repr__()
1135 return (f'typing.Callable'
kj73607be2020-12-24 12:33:48 +08001136 f'[[{", ".join([_type_repr(a) for a in args[:-1]])}], '
1137 f'{_type_repr(args[-1])}]')
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001138
1139 def __reduce__(self):
1140 args = self.__args__
kj73607be2020-12-24 12:33:48 +08001141 if not (len(args) == 2 and (args[0] is Ellipsis
1142 or isinstance(args[0], (ParamSpec, _ConcatenateGenericAlias)))):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001143 args = list(args[:-1]), args[-1]
1144 return operator.getitem, (Callable, args)
1145
1146
1147class _CallableType(_SpecialGenericAlias, _root=True):
1148 def copy_with(self, params):
1149 return _CallableGenericAlias(self.__origin__, params,
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001150 name=self._name, inst=self._inst,
1151 _typevar_types=(TypeVar, ParamSpec),
1152 _paramspec_tvars=True)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001153
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001154 def __getitem__(self, params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001155 if not isinstance(params, tuple) or len(params) != 2:
1156 raise TypeError("Callable must be used as "
1157 "Callable[[arg, ...], result].")
1158 args, result = params
kj463c7d32020-12-14 02:38:24 +08001159 # This relaxes what args can be on purpose to allow things like
1160 # PEP 612 ParamSpec. Responsibility for whether a user is using
1161 # Callable[...] properly is deferred to static type checkers.
1162 if isinstance(args, list):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001163 params = (tuple(args), result)
kj463c7d32020-12-14 02:38:24 +08001164 else:
1165 params = (args, result)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001166 return self.__getitem_inner__(params)
1167
1168 @_tp_cache
1169 def __getitem_inner__(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001170 args, result = params
1171 msg = "Callable[args, result]: result must be a type."
1172 result = _type_check(result, msg)
1173 if args is Ellipsis:
1174 return self.copy_with((_TypingEllipsis, result))
kj463c7d32020-12-14 02:38:24 +08001175 if not isinstance(args, tuple):
1176 args = (args,)
1177 args = tuple(_type_convert(arg) for arg in args)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001178 params = args + (result,)
1179 return self.copy_with(params)
1180
1181
1182class _TupleType(_SpecialGenericAlias, _root=True):
1183 @_tp_cache
1184 def __getitem__(self, params):
1185 if params == ():
1186 return self.copy_with((_TypingEmpty,))
1187 if not isinstance(params, tuple):
1188 params = (params,)
1189 if len(params) == 2 and params[1] is ...:
1190 msg = "Tuple[t, ...]: t must be a type."
1191 p = _type_check(params[0], msg)
1192 return self.copy_with((p, _TypingEllipsis))
1193 msg = "Tuple[t0, t1, ...]: each t must be a type."
1194 params = tuple(_type_check(p, msg) for p in params)
1195 return self.copy_with(params)
1196
1197
1198class _UnionGenericAlias(_GenericAlias, _root=True):
1199 def copy_with(self, params):
1200 return Union[params]
1201
1202 def __eq__(self, other):
Miss Islington (bot)8a37e8c2021-07-26 12:02:58 -07001203 if not isinstance(other, (_UnionGenericAlias, types.UnionType)):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001204 return NotImplemented
1205 return set(self.__args__) == set(other.__args__)
1206
1207 def __hash__(self):
1208 return hash(frozenset(self.__args__))
1209
1210 def __repr__(self):
1211 args = self.__args__
1212 if len(args) == 2:
1213 if args[0] is type(None):
1214 return f'typing.Optional[{_type_repr(args[1])}]'
1215 elif args[1] is type(None):
1216 return f'typing.Optional[{_type_repr(args[0])}]'
1217 return super().__repr__()
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001218
Maggie Moss1b4552c2020-09-09 13:23:24 -07001219 def __instancecheck__(self, obj):
1220 return self.__subclasscheck__(type(obj))
1221
1222 def __subclasscheck__(self, cls):
1223 for arg in self.__args__:
1224 if issubclass(cls, arg):
1225 return True
1226
1227
Yurii Karabasf03d3182020-11-17 04:23:19 +02001228def _value_and_type_iter(parameters):
1229 return ((p, type(p)) for p in parameters)
1230
1231
1232class _LiteralGenericAlias(_GenericAlias, _root=True):
1233
1234 def __eq__(self, other):
1235 if not isinstance(other, _LiteralGenericAlias):
1236 return NotImplemented
1237
1238 return set(_value_and_type_iter(self.__args__)) == set(_value_and_type_iter(other.__args__))
1239
1240 def __hash__(self):
Yurii Karabas1b540772020-11-19 18:17:38 +02001241 return hash(frozenset(_value_and_type_iter(self.__args__)))
Yurii Karabasf03d3182020-11-17 04:23:19 +02001242
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001243
kj73607be2020-12-24 12:33:48 +08001244class _ConcatenateGenericAlias(_GenericAlias, _root=True):
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001245 def __init__(self, *args, **kwargs):
1246 super().__init__(*args, **kwargs,
1247 _typevar_types=(TypeVar, ParamSpec),
1248 _paramspec_tvars=True)
kj73607be2020-12-24 12:33:48 +08001249
1250
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001251class Generic:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001252 """Abstract base class for generic types.
1253
Guido van Rossumb24569a2016-11-20 18:01:29 -08001254 A generic type is typically declared by inheriting from
1255 this class parameterized with one or more type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001256 For example, a generic mapping type might be defined as::
1257
1258 class Mapping(Generic[KT, VT]):
1259 def __getitem__(self, key: KT) -> VT:
1260 ...
1261 # Etc.
1262
1263 This class can then be used as follows::
1264
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001265 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001266 try:
1267 return mapping[key]
1268 except KeyError:
1269 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001270 """
Guido van Rossumd70fe632015-08-05 12:11:06 +02001271 __slots__ = ()
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001272 _is_protocol = False
Guido van Rossumd70fe632015-08-05 12:11:06 +02001273
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001274 @_tp_cache
1275 def __class_getitem__(cls, params):
1276 if not isinstance(params, tuple):
1277 params = (params,)
1278 if not params and cls is not Tuple:
1279 raise TypeError(
1280 f"Parameter list to {cls.__qualname__}[...] cannot be empty")
kj73607be2020-12-24 12:33:48 +08001281 params = tuple(_type_convert(p) for p in params)
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001282 if cls in (Generic, Protocol):
1283 # Generic and Protocol can only be subscripted with unique type variables.
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001284 if not all(isinstance(p, (TypeVar, ParamSpec)) for p in params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001285 raise TypeError(
kj73607be2020-12-24 12:33:48 +08001286 f"Parameters to {cls.__name__}[...] must all be type variables "
1287 f"or parameter specification variables.")
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001288 if len(set(params)) != len(params):
1289 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001290 f"Parameters to {cls.__name__}[...] must all be unique")
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001291 else:
1292 # Subscripting a regular Generic subclass.
kj73607be2020-12-24 12:33:48 +08001293 if any(isinstance(t, ParamSpec) for t in cls.__parameters__):
1294 params = _prepare_paramspec_params(cls, params)
Miss Islington (bot)c8db2922021-08-02 00:08:24 -07001295 else:
1296 _check_generic(cls, params, len(cls.__parameters__))
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001297 return _GenericAlias(cls, params,
1298 _typevar_types=(TypeVar, ParamSpec),
1299 _paramspec_tvars=True)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001300
1301 def __init_subclass__(cls, *args, **kwargs):
Ivan Levkivskyiee566fe2018-04-04 17:00:15 +01001302 super().__init_subclass__(*args, **kwargs)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001303 tvars = []
1304 if '__orig_bases__' in cls.__dict__:
1305 error = Generic in cls.__orig_bases__
1306 else:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001307 error = Generic in cls.__bases__ and cls.__name__ != 'Protocol'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001308 if error:
1309 raise TypeError("Cannot inherit from plain Generic")
1310 if '__orig_bases__' in cls.__dict__:
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001311 tvars = _collect_type_vars(cls.__orig_bases__, (TypeVar, ParamSpec))
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001312 # Look for Generic[T1, ..., Tn].
1313 # If found, tvars must be a subset of it.
1314 # If not found, tvars is it.
1315 # Also check for and reject plain Generic,
1316 # and reject multiple Generic[...].
1317 gvars = None
1318 for base in cls.__orig_bases__:
1319 if (isinstance(base, _GenericAlias) and
1320 base.__origin__ is Generic):
1321 if gvars is not None:
1322 raise TypeError(
1323 "Cannot inherit from Generic[...] multiple types.")
1324 gvars = base.__parameters__
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001325 if gvars is not None:
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001326 tvarset = set(tvars)
1327 gvarset = set(gvars)
1328 if not tvarset <= gvarset:
1329 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
1330 s_args = ', '.join(str(g) for g in gvars)
1331 raise TypeError(f"Some type variables ({s_vars}) are"
1332 f" not listed in Generic[{s_args}]")
1333 tvars = gvars
1334 cls.__parameters__ = tuple(tvars)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001335
1336
1337class _TypingEmpty:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001338 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
1339 to allow empty list/tuple in specific places, without allowing them
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001340 to sneak in where prohibited.
1341 """
1342
1343
1344class _TypingEllipsis:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001345 """Internal placeholder for ... (ellipsis)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001346
1347
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001348_TYPING_INTERNALS = ['__parameters__', '__orig_bases__', '__orig_class__',
1349 '_is_protocol', '_is_runtime_protocol']
1350
1351_SPECIAL_NAMES = ['__abstractmethods__', '__annotations__', '__dict__', '__doc__',
1352 '__init__', '__module__', '__new__', '__slots__',
Guido van Rossum48b069a2020-04-07 09:50:06 -07001353 '__subclasshook__', '__weakref__', '__class_getitem__']
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001354
1355# These special attributes will be not collected as protocol members.
1356EXCLUDED_ATTRIBUTES = _TYPING_INTERNALS + _SPECIAL_NAMES + ['_MutableMapping__marker']
1357
1358
1359def _get_protocol_attrs(cls):
1360 """Collect protocol members from a protocol class objects.
1361
1362 This includes names actually defined in the class dictionary, as well
1363 as names that appear in annotations. Special names (above) are skipped.
1364 """
1365 attrs = set()
1366 for base in cls.__mro__[:-1]: # without object
1367 if base.__name__ in ('Protocol', 'Generic'):
1368 continue
1369 annotations = getattr(base, '__annotations__', {})
1370 for attr in list(base.__dict__.keys()) + list(annotations.keys()):
1371 if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
1372 attrs.add(attr)
1373 return attrs
1374
1375
1376def _is_callable_members_only(cls):
1377 # PEP 544 prohibits using issubclass() with protocols that have non-method members.
1378 return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls))
1379
1380
1381def _no_init(self, *args, **kwargs):
Miss Islington (bot)2cc19a52021-08-02 10:08:59 -07001382 raise TypeError('Protocols cannot be instantiated')
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001383
Miss Islington (bot)56122b02021-07-30 06:48:01 -07001384def _caller(depth=1, default='__main__'):
Miss Islington (bot)c2f33df2021-07-20 08:24:57 -07001385 try:
Miss Islington (bot)56122b02021-07-30 06:48:01 -07001386 return sys._getframe(depth + 1).f_globals.get('__name__', default)
Miss Islington (bot)c2f33df2021-07-20 08:24:57 -07001387 except (AttributeError, ValueError): # For platforms without _getframe()
Miss Islington (bot)56122b02021-07-30 06:48:01 -07001388 return None
Miss Islington (bot)c2f33df2021-07-20 08:24:57 -07001389
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001390
Miss Islington (bot)a2d94a02021-05-12 09:09:04 -07001391def _allow_reckless_class_checks(depth=3):
Nickolena Fishercfaf4c02020-04-26 12:49:11 -05001392 """Allow instance and class checks for special stdlib modules.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001393
1394 The abc and functools modules indiscriminately call isinstance() and
1395 issubclass() on the whole MRO of a user class, which may contain protocols.
1396 """
1397 try:
Miss Islington (bot)a2d94a02021-05-12 09:09:04 -07001398 return sys._getframe(depth).f_globals['__name__'] in ['abc', 'functools']
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001399 except (AttributeError, ValueError): # For platforms without _getframe().
1400 return True
1401
1402
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001403_PROTO_ALLOWLIST = {
Divij Rajkumar692a0dc2019-09-12 11:13:51 +01001404 'collections.abc': [
1405 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
1406 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
1407 ],
1408 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
1409}
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001410
1411
1412class _ProtocolMeta(ABCMeta):
1413 # This metaclass is really unfortunate and exists only because of
1414 # the lack of __instancehook__.
1415 def __instancecheck__(cls, instance):
1416 # We need this method for situations where attributes are
1417 # assigned in __init__.
Miss Islington (bot)a2d94a02021-05-12 09:09:04 -07001418 if (
1419 getattr(cls, '_is_protocol', False) and
1420 not getattr(cls, '_is_runtime_protocol', False) and
1421 not _allow_reckless_class_checks(depth=2)
1422 ):
1423 raise TypeError("Instance and class checks can only be used with"
1424 " @runtime_checkable protocols")
1425
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001426 if ((not getattr(cls, '_is_protocol', False) or
1427 _is_callable_members_only(cls)) and
1428 issubclass(instance.__class__, cls)):
1429 return True
1430 if cls._is_protocol:
1431 if all(hasattr(instance, attr) and
1432 # All *methods* can be blocked by setting them to None.
1433 (not callable(getattr(cls, attr, None)) or
1434 getattr(instance, attr) is not None)
1435 for attr in _get_protocol_attrs(cls)):
1436 return True
1437 return super().__instancecheck__(instance)
1438
1439
1440class Protocol(Generic, metaclass=_ProtocolMeta):
1441 """Base class for protocol classes.
1442
1443 Protocol classes are defined as::
1444
1445 class Proto(Protocol):
1446 def meth(self) -> int:
1447 ...
1448
1449 Such classes are primarily used with static type checkers that recognize
1450 structural subtyping (static duck-typing), for example::
1451
1452 class C:
1453 def meth(self) -> int:
1454 return 0
1455
1456 def func(x: Proto) -> int:
1457 return x.meth()
1458
1459 func(C()) # Passes static type check
1460
1461 See PEP 544 for details. Protocol classes decorated with
1462 @typing.runtime_checkable act as simple-minded runtime protocols that check
1463 only the presence of given attributes, ignoring their type signatures.
1464 Protocol classes can be generic, they are defined as::
1465
1466 class GenProto(Protocol[T]):
1467 def meth(self) -> T:
1468 ...
1469 """
1470 __slots__ = ()
1471 _is_protocol = True
1472 _is_runtime_protocol = False
1473
1474 def __init_subclass__(cls, *args, **kwargs):
1475 super().__init_subclass__(*args, **kwargs)
1476
1477 # Determine if this is a protocol or a concrete subclass.
1478 if not cls.__dict__.get('_is_protocol', False):
1479 cls._is_protocol = any(b is Protocol for b in cls.__bases__)
1480
1481 # Set (or override) the protocol subclass hook.
1482 def _proto_hook(other):
1483 if not cls.__dict__.get('_is_protocol', False):
1484 return NotImplemented
1485
1486 # First, perform various sanity checks.
1487 if not getattr(cls, '_is_runtime_protocol', False):
Rossc1af1282020-12-29 11:55:28 +00001488 if _allow_reckless_class_checks():
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001489 return NotImplemented
1490 raise TypeError("Instance and class checks can only be used with"
1491 " @runtime_checkable protocols")
1492 if not _is_callable_members_only(cls):
Rossc1af1282020-12-29 11:55:28 +00001493 if _allow_reckless_class_checks():
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001494 return NotImplemented
1495 raise TypeError("Protocols with non-method members"
1496 " don't support issubclass()")
1497 if not isinstance(other, type):
1498 # Same error message as for issubclass(1, int).
1499 raise TypeError('issubclass() arg 1 must be a class')
1500
1501 # Second, perform the actual structural compatibility check.
1502 for attr in _get_protocol_attrs(cls):
1503 for base in other.__mro__:
1504 # Check if the members appears in the class dictionary...
1505 if attr in base.__dict__:
1506 if base.__dict__[attr] is None:
1507 return NotImplemented
1508 break
1509
1510 # ...or in annotations, if it is a sub-protocol.
1511 annotations = getattr(base, '__annotations__', {})
1512 if (isinstance(annotations, collections.abc.Mapping) and
1513 attr in annotations and
1514 issubclass(other, Generic) and other._is_protocol):
1515 break
1516 else:
1517 return NotImplemented
1518 return True
1519
1520 if '__subclasshook__' not in cls.__dict__:
1521 cls.__subclasshook__ = _proto_hook
1522
1523 # We have nothing more to do for non-protocols...
1524 if not cls._is_protocol:
Miss Islington (bot)2cc19a52021-08-02 10:08:59 -07001525 if cls.__init__ == _no_init:
1526 for base in cls.__mro__:
1527 init = base.__dict__.get('__init__', _no_init)
1528 if init != _no_init:
1529 cls.__init__ = init
1530 break
1531 else:
1532 # should not happen
1533 cls.__init__ = object.__init__
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001534 return
1535
1536 # ... otherwise check consistency of bases, and prohibit instantiation.
1537 for base in cls.__bases__:
1538 if not (base in (object, Generic) or
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001539 base.__module__ in _PROTO_ALLOWLIST and
1540 base.__name__ in _PROTO_ALLOWLIST[base.__module__] or
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001541 issubclass(base, Generic) and base._is_protocol):
1542 raise TypeError('Protocols can only inherit from other'
1543 ' protocols, got %r' % base)
1544 cls.__init__ = _no_init
1545
1546
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001547class _AnnotatedAlias(_GenericAlias, _root=True):
1548 """Runtime representation of an annotated type.
1549
1550 At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't'
1551 with extra annotations. The alias behaves like a normal typing alias,
1552 instantiating is the same as instantiating the underlying type, binding
1553 it to types is also the same.
1554 """
1555 def __init__(self, origin, metadata):
1556 if isinstance(origin, _AnnotatedAlias):
1557 metadata = origin.__metadata__ + metadata
1558 origin = origin.__origin__
1559 super().__init__(origin, origin)
1560 self.__metadata__ = metadata
1561
1562 def copy_with(self, params):
1563 assert len(params) == 1
1564 new_type = params[0]
1565 return _AnnotatedAlias(new_type, self.__metadata__)
1566
1567 def __repr__(self):
1568 return "typing.Annotated[{}, {}]".format(
1569 _type_repr(self.__origin__),
1570 ", ".join(repr(a) for a in self.__metadata__)
1571 )
1572
1573 def __reduce__(self):
1574 return operator.getitem, (
1575 Annotated, (self.__origin__,) + self.__metadata__
1576 )
1577
1578 def __eq__(self, other):
1579 if not isinstance(other, _AnnotatedAlias):
1580 return NotImplemented
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001581 return (self.__origin__ == other.__origin__
1582 and self.__metadata__ == other.__metadata__)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001583
1584 def __hash__(self):
1585 return hash((self.__origin__, self.__metadata__))
1586
1587
1588class Annotated:
1589 """Add context specific metadata to a type.
1590
1591 Example: Annotated[int, runtime_check.Unsigned] indicates to the
1592 hypothetical runtime_check module that this type is an unsigned int.
1593 Every other consumer of this type can ignore this metadata and treat
1594 this type as int.
1595
1596 The first argument to Annotated must be a valid type.
1597
1598 Details:
1599
1600 - It's an error to call `Annotated` with less than two arguments.
1601 - Nested Annotated are flattened::
1602
1603 Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
1604
1605 - Instantiating an annotated type is equivalent to instantiating the
1606 underlying type::
1607
1608 Annotated[C, Ann1](5) == C(5)
1609
1610 - Annotated can be used as a generic type alias::
1611
1612 Optimized = Annotated[T, runtime.Optimize()]
1613 Optimized[int] == Annotated[int, runtime.Optimize()]
1614
1615 OptimizedList = Annotated[List[T], runtime.Optimize()]
1616 OptimizedList[int] == Annotated[List[int], runtime.Optimize()]
1617 """
1618
1619 __slots__ = ()
1620
1621 def __new__(cls, *args, **kwargs):
1622 raise TypeError("Type Annotated cannot be instantiated.")
1623
1624 @_tp_cache
1625 def __class_getitem__(cls, params):
1626 if not isinstance(params, tuple) or len(params) < 2:
1627 raise TypeError("Annotated[...] should be used "
1628 "with at least two arguments (a type and an "
1629 "annotation).")
1630 msg = "Annotated[t, ...]: t must be a type."
1631 origin = _type_check(params[0], msg)
1632 metadata = tuple(params[1:])
1633 return _AnnotatedAlias(origin, metadata)
1634
1635 def __init_subclass__(cls, *args, **kwargs):
1636 raise TypeError(
1637 "Cannot subclass {}.Annotated".format(cls.__module__)
1638 )
1639
1640
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001641def runtime_checkable(cls):
1642 """Mark a protocol class as a runtime protocol.
1643
1644 Such protocol can be used with isinstance() and issubclass().
1645 Raise TypeError if applied to a non-protocol class.
1646 This allows a simple-minded structural check very similar to
1647 one trick ponies in collections.abc such as Iterable.
1648 For example::
1649
1650 @runtime_checkable
1651 class Closable(Protocol):
1652 def close(self): ...
1653
1654 assert isinstance(open('/some/file'), Closable)
1655
1656 Warning: this will check only the presence of the required methods,
1657 not their type signatures!
1658 """
1659 if not issubclass(cls, Generic) or not cls._is_protocol:
1660 raise TypeError('@runtime_checkable can be only applied to protocol classes,'
1661 ' got %r' % cls)
1662 cls._is_runtime_protocol = True
1663 return cls
1664
1665
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001666def cast(typ, val):
1667 """Cast a value to a type.
1668
1669 This returns the value unchanged. To the type checker this
1670 signals that the return value has the designated type, but at
1671 runtime we intentionally don't check anything (we want this
1672 to be as fast as possible).
1673 """
1674 return val
1675
1676
1677def _get_defaults(func):
1678 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -08001679 try:
1680 code = func.__code__
1681 except AttributeError:
1682 # Some built-in functions don't have __code__, __defaults__, etc.
1683 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001684 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001685 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001686 arg_names = arg_names[:pos_count]
1687 defaults = func.__defaults__ or ()
1688 kwdefaults = func.__kwdefaults__
1689 res = dict(kwdefaults) if kwdefaults else {}
1690 pos_offset = pos_count - len(defaults)
1691 for name, value in zip(arg_names[pos_offset:], defaults):
1692 assert name not in res
1693 res[name] = value
1694 return res
1695
1696
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001697_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
1698 types.MethodType, types.ModuleType,
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02001699 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001700
1701
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001702def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
Guido van Rossum991d14f2016-11-09 13:12:51 -08001703 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001704
Guido van Rossum991d14f2016-11-09 13:12:51 -08001705 This is often the same as obj.__annotations__, but it handles
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001706 forward references encoded as string literals, adds Optional[t] if a
1707 default value equal to None is set and recursively replaces all
1708 'Annotated[T, ...]' with 'T' (unless 'include_extras=True').
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001709
Guido van Rossum991d14f2016-11-09 13:12:51 -08001710 The argument may be a module, class, method, or function. The annotations
1711 are returned as a dictionary. For classes, annotations include also
1712 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001713
Guido van Rossum991d14f2016-11-09 13:12:51 -08001714 TypeError is raised if the argument is not of a type that can contain
1715 annotations, and an empty dictionary is returned if no annotations are
1716 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001717
Guido van Rossum991d14f2016-11-09 13:12:51 -08001718 BEWARE -- the behavior of globalns and localns is counterintuitive
1719 (unless you are familiar with how eval() and exec() work). The
1720 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001721
Guido van Rossum991d14f2016-11-09 13:12:51 -08001722 - If no dict arguments are passed, an attempt is made to use the
Łukasz Langaf350a262017-09-14 14:33:00 -04001723 globals from obj (or the respective module's globals for classes),
1724 and these are also used as the locals. If the object does not appear
Ken Jin1b1f9852021-04-27 01:31:21 +08001725 to have globals, an empty dictionary is used. For classes, the search
1726 order is globals first then locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001727
Guido van Rossum991d14f2016-11-09 13:12:51 -08001728 - If one dict argument is passed, it is used for both globals and
1729 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001730
Guido van Rossum991d14f2016-11-09 13:12:51 -08001731 - If two dict arguments are passed, they specify globals and
1732 locals, respectively.
1733 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001734
Guido van Rossum991d14f2016-11-09 13:12:51 -08001735 if getattr(obj, '__no_type_check__', None):
1736 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001737 # Classes require a special treatment.
1738 if isinstance(obj, type):
1739 hints = {}
1740 for base in reversed(obj.__mro__):
Łukasz Langaf350a262017-09-14 14:33:00 -04001741 if globalns is None:
Miss Islington (bot)3df23b52021-06-26 16:52:28 -07001742 base_globals = getattr(sys.modules.get(base.__module__, None), '__dict__', {})
Łukasz Langaf350a262017-09-14 14:33:00 -04001743 else:
1744 base_globals = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001745 ann = base.__dict__.get('__annotations__', {})
larryhastings2f2b6982021-04-29 20:09:08 -07001746 if isinstance(ann, types.GetSetDescriptorType):
1747 ann = {}
Ken Jin852150d2021-04-13 01:23:12 +08001748 base_locals = dict(vars(base)) if localns is None else localns
Ken Jin1b1f9852021-04-27 01:31:21 +08001749 if localns is None and globalns is None:
1750 # This is surprising, but required. Before Python 3.10,
1751 # get_type_hints only evaluated the globalns of
1752 # a class. To maintain backwards compatibility, we reverse
1753 # the globalns and localns order so that eval() looks into
1754 # *base_globals* first rather than *base_locals*.
1755 # This only affects ForwardRefs.
1756 base_globals, base_locals = base_locals, base_globals
Guido van Rossum991d14f2016-11-09 13:12:51 -08001757 for name, value in ann.items():
1758 if value is None:
1759 value = type(None)
1760 if isinstance(value, str):
Nina Zakharenko0e61dff2018-05-22 20:32:10 -07001761 value = ForwardRef(value, is_argument=False)
Ken Jin852150d2021-04-13 01:23:12 +08001762 value = _eval_type(value, base_globals, base_locals)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001763 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()}
Łukasz Langaf350a262017-09-14 14:33:00 -04001765
1766 if globalns is None:
1767 if isinstance(obj, types.ModuleType):
1768 globalns = obj.__dict__
1769 else:
benedwards140aca3a32019-11-21 17:24:58 +00001770 nsobj = obj
1771 # Find globalns for the unwrapped object.
1772 while hasattr(nsobj, '__wrapped__'):
1773 nsobj = nsobj.__wrapped__
1774 globalns = getattr(nsobj, '__globals__', {})
Łukasz Langaf350a262017-09-14 14:33:00 -04001775 if localns is None:
1776 localns = globalns
1777 elif localns is None:
1778 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001779 hints = getattr(obj, '__annotations__', None)
1780 if hints is None:
1781 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001782 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001783 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001784 else:
1785 raise TypeError('{!r} is not a module, class, method, '
1786 'or function.'.format(obj))
1787 defaults = _get_defaults(obj)
1788 hints = dict(hints)
1789 for name, value in hints.items():
1790 if value is None:
1791 value = type(None)
1792 if isinstance(value, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001793 value = ForwardRef(value)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001794 value = _eval_type(value, globalns, localns)
1795 if name in defaults and defaults[name] is None:
1796 value = Optional[value]
1797 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001798 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
1799
1800
1801def _strip_annotations(t):
1802 """Strips the annotations from a given type.
1803 """
1804 if isinstance(t, _AnnotatedAlias):
1805 return _strip_annotations(t.__origin__)
1806 if isinstance(t, _GenericAlias):
1807 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1808 if stripped_args == t.__args__:
1809 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001810 return t.copy_with(stripped_args)
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001811 if isinstance(t, GenericAlias):
1812 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1813 if stripped_args == t.__args__:
1814 return t
1815 return GenericAlias(t.__origin__, stripped_args)
Miss Islington (bot)8a37e8c2021-07-26 12:02:58 -07001816 if isinstance(t, types.UnionType):
Ken Jina2721642021-07-19 22:22:59 +08001817 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1818 if stripped_args == t.__args__:
1819 return t
Miss Islington (bot)0aea99e2021-07-24 12:35:33 -07001820 return functools.reduce(operator.or_, stripped_args)
Ken Jina2721642021-07-19 22:22:59 +08001821
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001822 return t
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001823
1824
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001825def get_origin(tp):
1826 """Get the unsubscripted version of a type.
1827
Jakub Stasiak38aaaaa2020-02-07 02:15:12 +01001828 This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
1829 and Annotated. Return None for unsupported types. Examples::
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001830
1831 get_origin(Literal[42]) is Literal
1832 get_origin(int) is None
1833 get_origin(ClassVar[int]) is ClassVar
1834 get_origin(Generic) is Generic
1835 get_origin(Generic[T]) is Generic
1836 get_origin(Union[T, int]) is Union
1837 get_origin(List[Tuple[T, T]][int]) == list
Jelle Zijlstra52243362021-04-10 19:57:05 -07001838 get_origin(P.args) is P
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001839 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001840 if isinstance(tp, _AnnotatedAlias):
1841 return Annotated
Jelle Zijlstra52243362021-04-10 19:57:05 -07001842 if isinstance(tp, (_BaseGenericAlias, GenericAlias,
1843 ParamSpecArgs, ParamSpecKwargs)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001844 return tp.__origin__
1845 if tp is Generic:
1846 return Generic
Miss Islington (bot)8a37e8c2021-07-26 12:02:58 -07001847 if isinstance(tp, types.UnionType):
1848 return types.UnionType
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001849 return None
1850
1851
1852def get_args(tp):
1853 """Get type arguments with all substitutions performed.
1854
1855 For unions, basic simplifications used by Union constructor are performed.
1856 Examples::
1857 get_args(Dict[str, int]) == (str, int)
1858 get_args(int) == ()
1859 get_args(Union[int, Union[T, int], str][int]) == (int, str)
1860 get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
1861 get_args(Callable[[], T][int]) == ([], int)
1862 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001863 if isinstance(tp, _AnnotatedAlias):
1864 return (tp.__origin__,) + tp.__metadata__
Ken Jin4140f102020-12-29 04:06:19 +08001865 if isinstance(tp, (_GenericAlias, GenericAlias)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001866 res = tp.__args__
Ken Jinefb1f092020-12-29 10:26:19 +08001867 if (tp.__origin__ is collections.abc.Callable
1868 and not (res[0] is Ellipsis
1869 or isinstance(res[0], (ParamSpec, _ConcatenateGenericAlias)))):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001870 res = (list(res[:-1]), res[-1])
1871 return res
Miss Islington (bot)8a37e8c2021-07-26 12:02:58 -07001872 if isinstance(tp, types.UnionType):
Ken Jinefb1f092020-12-29 10:26:19 +08001873 return tp.__args__
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001874 return ()
1875
1876
Patrick Reader0705ec82020-09-16 05:58:32 +01001877def is_typeddict(tp):
1878 """Check if an annotation is a TypedDict class
1879
1880 For example::
1881 class Film(TypedDict):
1882 title: str
1883 year: int
1884
1885 is_typeddict(Film) # => True
1886 is_typeddict(Union[list, str]) # => False
1887 """
1888 return isinstance(tp, _TypedDictMeta)
1889
1890
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001891def no_type_check(arg):
1892 """Decorator to indicate that annotations are not type hints.
1893
1894 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001895 applies recursively to all methods and classes defined in that class
1896 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001897
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001898 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001899 """
1900 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001901 arg_attrs = arg.__dict__.copy()
1902 for attr, val in arg.__dict__.items():
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001903 if val in arg.__bases__ + (arg,):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001904 arg_attrs.pop(attr)
1905 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001906 if isinstance(obj, types.FunctionType):
1907 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001908 if isinstance(obj, type):
1909 no_type_check(obj)
1910 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001911 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001912 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001913 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001914 return arg
1915
1916
1917def no_type_check_decorator(decorator):
1918 """Decorator to give another decorator the @no_type_check effect.
1919
1920 This wraps the decorator with something that wraps the decorated
1921 function in @no_type_check.
1922 """
1923
1924 @functools.wraps(decorator)
1925 def wrapped_decorator(*args, **kwds):
1926 func = decorator(*args, **kwds)
1927 func = no_type_check(func)
1928 return func
1929
1930 return wrapped_decorator
1931
1932
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001933def _overload_dummy(*args, **kwds):
1934 """Helper for @overload to raise when called."""
1935 raise NotImplementedError(
1936 "You should not call an overloaded function. "
1937 "A series of @overload-decorated functions "
1938 "outside a stub module should always be followed "
1939 "by an implementation that is not @overload-ed.")
1940
1941
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001942def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001943 """Decorator for overloaded functions/methods.
1944
1945 In a stub file, place two or more stub definitions for the same
1946 function in a row, each decorated with @overload. For example:
1947
1948 @overload
1949 def utf8(value: None) -> None: ...
1950 @overload
1951 def utf8(value: bytes) -> bytes: ...
1952 @overload
1953 def utf8(value: str) -> bytes: ...
1954
1955 In a non-stub file (i.e. a regular .py file), do the same but
1956 follow it with an implementation. The implementation should *not*
1957 be decorated with @overload. For example:
1958
1959 @overload
1960 def utf8(value: None) -> None: ...
1961 @overload
1962 def utf8(value: bytes) -> bytes: ...
1963 @overload
1964 def utf8(value: str) -> bytes: ...
1965 def utf8(value):
1966 # implementation goes here
1967 """
1968 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001969
1970
Ivan Levkivskyif3672422019-05-26 09:37:07 +01001971def final(f):
1972 """A decorator to indicate final methods and final classes.
1973
1974 Use this decorator to indicate to type checkers that the decorated
1975 method cannot be overridden, and decorated class cannot be subclassed.
1976 For example:
1977
1978 class Base:
1979 @final
1980 def done(self) -> None:
1981 ...
1982 class Sub(Base):
1983 def done(self) -> None: # Error reported by type checker
1984 ...
1985
1986 @final
1987 class Leaf:
1988 ...
1989 class Other(Leaf): # Error reported by type checker
1990 ...
1991
1992 There is no runtime checking of these properties.
1993 """
1994 return f
1995
1996
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001997# Some unconstrained type variables. These are used by the container types.
1998# (These are not for export.)
1999T = TypeVar('T') # Any type.
2000KT = TypeVar('KT') # Key type.
2001VT = TypeVar('VT') # Value type.
2002T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
2003V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
2004VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
2005T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
2006# Internal type variable used for Type[].
2007CT_co = TypeVar('CT_co', covariant=True, bound=type)
2008
2009# A useful type variable with constraints. This represents string types.
2010# (This one *is* for export!)
2011AnyStr = TypeVar('AnyStr', bytes, str)
2012
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002013
2014# Various ABCs mimicking those in collections.abc.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03002015_alias = _SpecialGenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002016
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002017Hashable = _alias(collections.abc.Hashable, 0) # Not generic.
2018Awaitable = _alias(collections.abc.Awaitable, 1)
2019Coroutine = _alias(collections.abc.Coroutine, 3)
2020AsyncIterable = _alias(collections.abc.AsyncIterable, 1)
2021AsyncIterator = _alias(collections.abc.AsyncIterator, 1)
2022Iterable = _alias(collections.abc.Iterable, 1)
2023Iterator = _alias(collections.abc.Iterator, 1)
2024Reversible = _alias(collections.abc.Reversible, 1)
2025Sized = _alias(collections.abc.Sized, 0) # Not generic.
2026Container = _alias(collections.abc.Container, 1)
2027Collection = _alias(collections.abc.Collection, 1)
2028Callable = _CallableType(collections.abc.Callable, 2)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002029Callable.__doc__ = \
2030 """Callable type; Callable[[int], str] is a function of (int) -> str.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002031
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002032 The subscription syntax must always be used with exactly two
2033 values: the argument list and the return type. The argument list
2034 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002035
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002036 There is no syntax to indicate optional or keyword arguments,
2037 such function types are rarely used as callback types.
2038 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002039AbstractSet = _alias(collections.abc.Set, 1, name='AbstractSet')
2040MutableSet = _alias(collections.abc.MutableSet, 1)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002041# NOTE: Mapping is only covariant in the value type.
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002042Mapping = _alias(collections.abc.Mapping, 2)
2043MutableMapping = _alias(collections.abc.MutableMapping, 2)
2044Sequence = _alias(collections.abc.Sequence, 1)
2045MutableSequence = _alias(collections.abc.MutableSequence, 1)
2046ByteString = _alias(collections.abc.ByteString, 0) # Not generic
2047# Tuple accepts variable number of parameters.
2048Tuple = _TupleType(tuple, -1, inst=False, name='Tuple')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002049Tuple.__doc__ = \
2050 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07002051
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002052 Example: Tuple[T1, T2] is a tuple of two elements corresponding
2053 to type variables T1 and T2. Tuple[int, float, str] is a tuple
2054 of an int, a float and a string.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07002055
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002056 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
2057 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002058List = _alias(list, 1, inst=False, name='List')
2059Deque = _alias(collections.deque, 1, name='Deque')
2060Set = _alias(set, 1, inst=False, name='Set')
2061FrozenSet = _alias(frozenset, 1, inst=False, name='FrozenSet')
2062MappingView = _alias(collections.abc.MappingView, 1)
2063KeysView = _alias(collections.abc.KeysView, 1)
2064ItemsView = _alias(collections.abc.ItemsView, 2)
2065ValuesView = _alias(collections.abc.ValuesView, 1)
2066ContextManager = _alias(contextlib.AbstractContextManager, 1, name='ContextManager')
2067AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, 1, name='AsyncContextManager')
2068Dict = _alias(dict, 2, inst=False, name='Dict')
2069DefaultDict = _alias(collections.defaultdict, 2, name='DefaultDict')
2070OrderedDict = _alias(collections.OrderedDict, 2)
2071Counter = _alias(collections.Counter, 1)
2072ChainMap = _alias(collections.ChainMap, 2)
2073Generator = _alias(collections.abc.Generator, 3)
2074AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2)
2075Type = _alias(type, 1, inst=False, name='Type')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002076Type.__doc__ = \
2077 """A special construct usable to annotate class objects.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07002078
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002079 For example, suppose we have the following classes::
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07002080
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002081 class User: ... # Abstract base for User classes
2082 class BasicUser(User): ...
2083 class ProUser(User): ...
2084 class TeamUser(User): ...
Guido van Rossumf17c2002015-12-03 17:31:24 -08002085
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002086 And a function that takes a class argument that's a subclass of
2087 User and returns an instance of the corresponding class::
Guido van Rossumf17c2002015-12-03 17:31:24 -08002088
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002089 U = TypeVar('U', bound=User)
2090 def new_user(user_class: Type[U]) -> U:
2091 user = user_class()
2092 # (Here we could write the user object to a database)
2093 return user
Guido van Rossumf17c2002015-12-03 17:31:24 -08002094
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002095 joe = new_user(BasicUser)
Guido van Rossumf17c2002015-12-03 17:31:24 -08002096
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002097 At this point the type checker knows that joe has type BasicUser.
2098 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002099
2100
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002101@runtime_checkable
2102class SupportsInt(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002103 """An ABC with one abstract method __int__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002104 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002105
2106 @abstractmethod
2107 def __int__(self) -> int:
2108 pass
2109
2110
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002111@runtime_checkable
2112class SupportsFloat(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002113 """An ABC with one abstract method __float__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002114 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002115
2116 @abstractmethod
2117 def __float__(self) -> float:
2118 pass
2119
2120
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002121@runtime_checkable
2122class SupportsComplex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002123 """An ABC with one abstract method __complex__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002124 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002125
2126 @abstractmethod
2127 def __complex__(self) -> complex:
2128 pass
2129
2130
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002131@runtime_checkable
2132class SupportsBytes(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002133 """An ABC with one abstract method __bytes__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002134 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002135
2136 @abstractmethod
2137 def __bytes__(self) -> bytes:
2138 pass
2139
2140
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002141@runtime_checkable
2142class SupportsIndex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002143 """An ABC with one abstract method __index__."""
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -07002144 __slots__ = ()
2145
2146 @abstractmethod
2147 def __index__(self) -> int:
2148 pass
2149
2150
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002151@runtime_checkable
2152class SupportsAbs(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002153 """An ABC with one abstract method __abs__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002154 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002155
2156 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02002157 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002158 pass
2159
2160
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002161@runtime_checkable
2162class SupportsRound(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002163 """An ABC with one abstract method __round__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002164 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002165
2166 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02002167 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002168 pass
2169
2170
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002171def _make_nmtuple(name, types, module, defaults = ()):
2172 fields = [n for n, t in types]
2173 types = {n: _type_check(t, f"field {n} annotation must be a type")
2174 for n, t in types}
2175 nm_tpl = collections.namedtuple(name, fields,
2176 defaults=defaults, module=module)
2177 nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002178 return nm_tpl
2179
2180
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002181# attributes prohibited to set in NamedTuple class syntax
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002182_prohibited = frozenset({'__new__', '__init__', '__slots__', '__getnewargs__',
2183 '_fields', '_field_defaults',
2184 '_make', '_replace', '_asdict', '_source'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002185
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002186_special = frozenset({'__module__', '__name__', '__annotations__'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002187
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002188
Guido van Rossum2f841442016-11-15 09:48:06 -08002189class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002190
Guido van Rossum2f841442016-11-15 09:48:06 -08002191 def __new__(cls, typename, bases, ns):
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002192 assert bases[0] is _NamedTuple
Guido van Rossum2f841442016-11-15 09:48:06 -08002193 types = ns.get('__annotations__', {})
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002194 default_names = []
Guido van Rossum3c268be2017-01-18 08:03:50 -08002195 for field_name in types:
2196 if field_name in ns:
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002197 default_names.append(field_name)
2198 elif default_names:
2199 raise TypeError(f"Non-default namedtuple field {field_name} "
2200 f"cannot follow default field"
2201 f"{'s' if len(default_names) > 1 else ''} "
2202 f"{', '.join(default_names)}")
2203 nm_tpl = _make_nmtuple(typename, types.items(),
2204 defaults=[ns[n] for n in default_names],
2205 module=ns['__module__'])
Guido van Rossum95919c02017-01-22 17:47:20 -08002206 # update from user namespace without overriding special namedtuple attributes
2207 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002208 if key in _prohibited:
2209 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
2210 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08002211 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08002212 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002213
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002214
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002215def NamedTuple(typename, fields=None, /, **kwargs):
Guido van Rossum2f841442016-11-15 09:48:06 -08002216 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002217
Guido van Rossum2f841442016-11-15 09:48:06 -08002218 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002219
Guido van Rossum2f841442016-11-15 09:48:06 -08002220 class Employee(NamedTuple):
2221 name: str
2222 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002223
Guido van Rossum2f841442016-11-15 09:48:06 -08002224 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002225
Guido van Rossum2f841442016-11-15 09:48:06 -08002226 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002227
Raymond Hettingerf7b57df2019-03-18 09:53:56 -07002228 The resulting class has an extra __annotations__ attribute, giving a
2229 dict that maps field names to types. (The field names are also in
2230 the _fields attribute, which is part of the namedtuple API.)
2231 Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002232
Guido van Rossum2f841442016-11-15 09:48:06 -08002233 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002234
Guido van Rossum2f841442016-11-15 09:48:06 -08002235 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002236
Guido van Rossum2f841442016-11-15 09:48:06 -08002237 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
2238 """
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002239 if fields is None:
2240 fields = kwargs.items()
2241 elif kwargs:
2242 raise TypeError("Either list of fields or keywords"
2243 " can be provided to NamedTuple, not both")
2244 try:
2245 module = sys._getframe(1).f_globals.get('__name__', '__main__')
2246 except (AttributeError, ValueError):
2247 module = None
2248 return _make_nmtuple(typename, fields, module=module)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002249
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002250_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})
2251
2252def _namedtuple_mro_entries(bases):
2253 if len(bases) > 1:
2254 raise TypeError("Multiple inheritance with NamedTuple is not supported")
2255 assert bases[0] is NamedTuple
2256 return (_NamedTuple,)
2257
2258NamedTuple.__mro_entries__ = _namedtuple_mro_entries
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002259
2260
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002261class _TypedDictMeta(type):
2262 def __new__(cls, name, bases, ns, total=True):
2263 """Create new typed dict class object.
2264
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002265 This method is called when TypedDict is subclassed,
2266 or when TypedDict is instantiated. This way
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002267 TypedDict supports all three syntax forms described in its docstring.
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002268 Subclasses and instances of TypedDict return actual dictionaries.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002269 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002270 for base in bases:
2271 if type(base) is not _TypedDictMeta:
2272 raise TypeError('cannot inherit from both a TypedDict type '
2273 'and a non-TypedDict base class')
2274 tp_dict = type.__new__(_TypedDictMeta, name, (dict,), ns)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002275
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002276 annotations = {}
2277 own_annotations = ns.get('__annotations__', {})
2278 own_annotation_keys = set(own_annotations.keys())
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002279 msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002280 own_annotations = {
Miss Islington (bot)480f29f2021-07-17 01:48:17 -07002281 n: _type_check(tp, msg, module=tp_dict.__module__)
2282 for n, tp in own_annotations.items()
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002283 }
2284 required_keys = set()
2285 optional_keys = set()
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002286
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002287 for base in bases:
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002288 annotations.update(base.__dict__.get('__annotations__', {}))
2289 required_keys.update(base.__dict__.get('__required_keys__', ()))
2290 optional_keys.update(base.__dict__.get('__optional_keys__', ()))
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002291
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002292 annotations.update(own_annotations)
2293 if total:
2294 required_keys.update(own_annotation_keys)
2295 else:
2296 optional_keys.update(own_annotation_keys)
2297
2298 tp_dict.__annotations__ = annotations
2299 tp_dict.__required_keys__ = frozenset(required_keys)
2300 tp_dict.__optional_keys__ = frozenset(optional_keys)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002301 if not hasattr(tp_dict, '__total__'):
2302 tp_dict.__total__ = total
2303 return tp_dict
2304
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002305 __call__ = dict # static method
2306
2307 def __subclasscheck__(cls, other):
2308 # Typed dicts are only for static structural subtyping.
2309 raise TypeError('TypedDict does not support instance and class checks')
2310
2311 __instancecheck__ = __subclasscheck__
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002312
2313
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002314def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002315 """A simple typed namespace. At runtime it is equivalent to a plain dict.
2316
2317 TypedDict creates a dictionary type that expects all of its
2318 instances to have a certain set of keys, where each key is
2319 associated with a value of a consistent type. This expectation
2320 is not checked at runtime but is only enforced by type checkers.
2321 Usage::
2322
2323 class Point2D(TypedDict):
2324 x: int
2325 y: int
2326 label: str
2327
2328 a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
2329 b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
2330
2331 assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
2332
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002333 The type info can be accessed via the Point2D.__annotations__ dict, and
2334 the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
2335 TypedDict supports two additional equivalent forms::
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002336
2337 Point2D = TypedDict('Point2D', x=int, y=int, label=str)
2338 Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
2339
ananthan-123ab6423f2020-02-19 10:03:05 +05302340 By default, all keys must be present in a TypedDict. It is possible
2341 to override this by specifying totality.
2342 Usage::
2343
2344 class point2D(TypedDict, total=False):
2345 x: int
2346 y: int
2347
2348 This means that a point2D TypedDict can have any of the keys omitted.A type
2349 checker is only expected to support a literal False or True as the value of
2350 the total argument. True is the default, and makes all items defined in the
2351 class body be required.
2352
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002353 The class syntax is only supported in Python 3.6+, while two other
2354 syntax forms work for Python 2.7 and 3.2+
2355 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002356 if fields is None:
2357 fields = kwargs
2358 elif kwargs:
2359 raise TypeError("TypedDict takes either a dict or keyword arguments,"
2360 " but not both")
2361
Alex Grönholm67b769f2020-12-10 23:49:05 +02002362 ns = {'__annotations__': dict(fields)}
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002363 try:
2364 # Setting correct module is necessary to make typed dict classes pickleable.
2365 ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
2366 except (AttributeError, ValueError):
2367 pass
2368
Alex Grönholm67b769f2020-12-10 23:49:05 +02002369 return _TypedDictMeta(typename, (), ns, total=total)
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002370
2371_TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {})
2372TypedDict.__mro_entries__ = lambda bases: (_TypedDict,)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002373
2374
Miss Islington (bot)c2f33df2021-07-20 08:24:57 -07002375class NewType:
Guido van Rossum91185fe2016-06-08 11:19:11 -07002376 """NewType creates simple unique types with almost zero
2377 runtime overhead. NewType(name, tp) is considered a subtype of tp
2378 by static type checkers. At runtime, NewType(name, tp) returns
2379 a dummy function that simply returns its argument. Usage::
2380
2381 UserId = NewType('UserId', int)
2382
2383 def name_by_id(user_id: UserId) -> str:
2384 ...
2385
2386 UserId('user') # Fails type check
2387
2388 name_by_id(42) # Fails type check
2389 name_by_id(UserId(42)) # OK
2390
2391 num = UserId(5) + 1 # type: int
2392 """
2393
Miss Islington (bot)c2f33df2021-07-20 08:24:57 -07002394 def __init__(self, name, tp):
Miss Islington (bot)c2f33df2021-07-20 08:24:57 -07002395 self.__qualname__ = name
Łukasz Langa05f5d8e2021-07-24 12:07:56 +02002396 if '.' in name:
2397 name = name.rpartition('.')[-1]
2398 self.__name__ = name
Miss Islington (bot)c2f33df2021-07-20 08:24:57 -07002399 self.__supertype__ = tp
Miss Islington (bot)56122b02021-07-30 06:48:01 -07002400 def_mod = _caller()
2401 if def_mod != 'typing':
2402 self.__module__ = def_mod
Miss Islington (bot)c2f33df2021-07-20 08:24:57 -07002403
2404 def __repr__(self):
2405 return f'{self.__module__}.{self.__qualname__}'
2406
2407 def __call__(self, x):
Guido van Rossum91185fe2016-06-08 11:19:11 -07002408 return x
2409
Łukasz Langa05f5d8e2021-07-24 12:07:56 +02002410 def __reduce__(self):
2411 return self.__qualname__
2412
Miss Islington (bot)c2f33df2021-07-20 08:24:57 -07002413 def __or__(self, other):
2414 return Union[self, other]
2415
2416 def __ror__(self, other):
2417 return Union[other, self]
Guido van Rossum91185fe2016-06-08 11:19:11 -07002418
2419
Guido van Rossum0e0563c2016-04-05 14:54:25 -07002420# Python-version-specific alias (Python 2: unicode; Python 3: str)
2421Text = str
2422
2423
Guido van Rossum91185fe2016-06-08 11:19:11 -07002424# Constant that's True when type checking, but False here.
2425TYPE_CHECKING = False
2426
2427
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002428class IO(Generic[AnyStr]):
2429 """Generic base class for TextIO and BinaryIO.
2430
2431 This is an abstract, generic version of the return of open().
2432
2433 NOTE: This does not distinguish between the different possible
2434 classes (text vs. binary, read vs. write vs. read/write,
2435 append-only, unbuffered). The TextIO and BinaryIO subclasses
2436 below capture the distinctions between text vs. binary, which is
2437 pervasive in the interface; however we currently do not offer a
2438 way to track the other distinctions in the type system.
2439 """
2440
Guido van Rossumd70fe632015-08-05 12:11:06 +02002441 __slots__ = ()
2442
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002443 @property
2444 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002445 def mode(self) -> str:
2446 pass
2447
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002448 @property
2449 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002450 def name(self) -> str:
2451 pass
2452
2453 @abstractmethod
2454 def close(self) -> None:
2455 pass
2456
Shantanu2e6569b2020-01-29 18:52:36 -08002457 @property
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002458 @abstractmethod
2459 def closed(self) -> bool:
2460 pass
2461
2462 @abstractmethod
2463 def fileno(self) -> int:
2464 pass
2465
2466 @abstractmethod
2467 def flush(self) -> None:
2468 pass
2469
2470 @abstractmethod
2471 def isatty(self) -> bool:
2472 pass
2473
2474 @abstractmethod
2475 def read(self, n: int = -1) -> AnyStr:
2476 pass
2477
2478 @abstractmethod
2479 def readable(self) -> bool:
2480 pass
2481
2482 @abstractmethod
2483 def readline(self, limit: int = -1) -> AnyStr:
2484 pass
2485
2486 @abstractmethod
2487 def readlines(self, hint: int = -1) -> List[AnyStr]:
2488 pass
2489
2490 @abstractmethod
2491 def seek(self, offset: int, whence: int = 0) -> int:
2492 pass
2493
2494 @abstractmethod
2495 def seekable(self) -> bool:
2496 pass
2497
2498 @abstractmethod
2499 def tell(self) -> int:
2500 pass
2501
2502 @abstractmethod
2503 def truncate(self, size: int = None) -> int:
2504 pass
2505
2506 @abstractmethod
2507 def writable(self) -> bool:
2508 pass
2509
2510 @abstractmethod
2511 def write(self, s: AnyStr) -> int:
2512 pass
2513
2514 @abstractmethod
2515 def writelines(self, lines: List[AnyStr]) -> None:
2516 pass
2517
2518 @abstractmethod
2519 def __enter__(self) -> 'IO[AnyStr]':
2520 pass
2521
2522 @abstractmethod
2523 def __exit__(self, type, value, traceback) -> None:
2524 pass
2525
2526
2527class BinaryIO(IO[bytes]):
2528 """Typed version of the return of open() in binary mode."""
2529
Guido van Rossumd70fe632015-08-05 12:11:06 +02002530 __slots__ = ()
2531
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002532 @abstractmethod
2533 def write(self, s: Union[bytes, bytearray]) -> int:
2534 pass
2535
2536 @abstractmethod
2537 def __enter__(self) -> 'BinaryIO':
2538 pass
2539
2540
2541class TextIO(IO[str]):
2542 """Typed version of the return of open() in text mode."""
2543
Guido van Rossumd70fe632015-08-05 12:11:06 +02002544 __slots__ = ()
2545
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002546 @property
2547 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002548 def buffer(self) -> BinaryIO:
2549 pass
2550
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002551 @property
2552 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002553 def encoding(self) -> str:
2554 pass
2555
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002556 @property
2557 @abstractmethod
Guido van Rossum991d14f2016-11-09 13:12:51 -08002558 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002559 pass
2560
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002561 @property
2562 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002563 def line_buffering(self) -> bool:
2564 pass
2565
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002566 @property
2567 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002568 def newlines(self) -> Any:
2569 pass
2570
2571 @abstractmethod
2572 def __enter__(self) -> 'TextIO':
2573 pass
2574
2575
2576class io:
2577 """Wrapper namespace for IO generic classes."""
2578
2579 __all__ = ['IO', 'TextIO', 'BinaryIO']
2580 IO = IO
2581 TextIO = TextIO
2582 BinaryIO = BinaryIO
2583
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002584
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002585io.__name__ = __name__ + '.io'
2586sys.modules[io.__name__] = io
2587
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002588Pattern = _alias(stdlib_re.Pattern, 1)
2589Match = _alias(stdlib_re.Match, 1)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002590
2591class re:
2592 """Wrapper namespace for re type aliases."""
2593
2594 __all__ = ['Pattern', 'Match']
2595 Pattern = Pattern
2596 Match = Match
2597
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002598
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002599re.__name__ = __name__ + '.re'
2600sys.modules[re.__name__] = re