blob: 660ad3526f4bd4c652f88a5e8c9005ce21580ada [file] [log] [blame]
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001"""
2The typing module: Support for gradual typing as defined by PEP 484.
3
4At large scale, the structure of the module is following:
Tim McNamara5265b3a2018-09-01 20:56:58 +12005* Imports and exports, all public names should be explicitly added to __all__.
Ivan Levkivskyid911e402018-01-20 11:23:59 +00006* Internal helper functions: these should never be used in code outside this module.
kj73607be2020-12-24 12:33:48 +08007* _SpecialForm and its instances (special forms):
8 Any, NoReturn, ClassVar, Union, Optional, Concatenate
9* Classes whose instances can be type arguments in addition to types:
10 ForwardRef, TypeVar and ParamSpec
Ivan Levkivskyid911e402018-01-20 11:23:59 +000011* The core of internal generics API: _GenericAlias and _VariadicGenericAlias, the latter is
12 currently only used by Tuple and Callable. All subscripted types like X[int], Union[int, str],
13 etc., are instances of either of these classes.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +010014* The public counterpart of the generics API consists of two classes: Generic and Protocol.
Ivan Levkivskyid911e402018-01-20 11:23:59 +000015* Public helper functions: get_type_hints, overload, cast, no_type_check,
16 no_type_check_decorator.
17* Generic aliases for collections.abc ABCs and few additional protocols.
ananthan-123ab6423f2020-02-19 10:03:05 +053018* Special types: NewType, NamedTuple, TypedDict.
Ivan Levkivskyid911e402018-01-20 11:23:59 +000019* Wrapper submodules for re and io related types.
20"""
21
HongWeipeng6ce03ec2019-09-27 15:54:26 +080022from abc import abstractmethod, ABCMeta
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070023import collections
Ivan Levkivskyid911e402018-01-20 11:23:59 +000024import collections.abc
Brett Cannonf3ad0422016-04-15 10:51:30 -070025import contextlib
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070026import functools
Serhiy Storchaka09f32212018-05-26 21:19:26 +030027import operator
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070028import re as stdlib_re # Avoid confusion with the re we export.
29import sys
30import types
Guido van Rossum48b069a2020-04-07 09:50:06 -070031from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType, GenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070032
33# Please keep __all__ alphabetized within each category.
34__all__ = [
35 # Super-special typing primitives.
Jakub Stasiakcf5b1092020-02-05 02:10:19 +010036 'Annotated',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070037 'Any',
38 'Callable',
Guido van Rossum0a6976d2016-09-11 15:34:56 -070039 'ClassVar',
kj73607be2020-12-24 12:33:48 +080040 'Concatenate',
Ivan Levkivskyif3672422019-05-26 09:37:07 +010041 'Final',
Anthony Sottiled30da5d2019-05-29 11:19:38 -070042 'ForwardRef',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070043 'Generic',
Ivan Levkivskyib891c462019-05-26 09:37:48 +010044 'Literal',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070045 'Optional',
kj73607be2020-12-24 12:33:48 +080046 'ParamSpec',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +010047 'Protocol',
Guido van Rossumeb9aca32016-05-24 16:38:22 -070048 'Tuple',
49 'Type',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070050 'TypeVar',
51 'Union',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070052
53 # ABCs (from collections.abc).
54 'AbstractSet', # collections.abc.Set.
55 'ByteString',
56 'Container',
Ivan Levkivskyi29fda8d2017-06-10 21:57:56 +020057 'ContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070058 'Hashable',
59 'ItemsView',
60 'Iterable',
61 'Iterator',
62 'KeysView',
63 'Mapping',
64 'MappingView',
65 'MutableMapping',
66 'MutableSequence',
67 'MutableSet',
68 'Sequence',
69 'Sized',
70 'ValuesView',
Ivan Levkivskyid911e402018-01-20 11:23:59 +000071 'Awaitable',
72 'AsyncIterator',
73 'AsyncIterable',
74 'Coroutine',
75 'Collection',
76 'AsyncGenerator',
77 'AsyncContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070078
79 # Structural checks, a.k.a. protocols.
80 'Reversible',
81 'SupportsAbs',
Ivan Levkivskyif06e0212017-05-02 19:14:07 +020082 'SupportsBytes',
83 'SupportsComplex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070084 'SupportsFloat',
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -070085 'SupportsIndex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070086 'SupportsInt',
87 'SupportsRound',
88
89 # Concrete collection types.
Anthony Sottiled30da5d2019-05-29 11:19:38 -070090 'ChainMap',
Ivan Levkivskyib692dc82017-02-13 22:50:14 +010091 'Counter',
Raymond Hettinger80490522017-01-16 22:42:37 -080092 'Deque',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070093 'Dict',
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070094 'DefaultDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070095 'List',
Anthony Sottiled30da5d2019-05-29 11:19:38 -070096 'OrderedDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070097 'Set',
Guido van Rossumefa798d2016-08-23 11:01:50 -070098 'FrozenSet',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070099 'NamedTuple', # Not really a type.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +0100100 'TypedDict', # Not really a type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700101 'Generator',
Miss Islington (bot)e1bcc882021-05-04 02:51:33 -0700102
103 # Other concrete types.
104 'BinaryIO',
105 'IO',
106 'Match',
107 'Pattern',
108 'TextIO',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700109
110 # One-off things.
111 'AnyStr',
112 'cast',
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100113 'final',
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +0100114 'get_args',
115 'get_origin',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700116 'get_type_hints',
Patrick Reader0705ec82020-09-16 05:58:32 +0100117 'is_typeddict',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700118 'NewType',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700119 'no_type_check',
120 'no_type_check_decorator',
aetracht45738202018-03-19 14:41:32 -0400121 'NoReturn',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700122 'overload',
Jelle Zijlstra52243362021-04-10 19:57:05 -0700123 'ParamSpecArgs',
124 'ParamSpecKwargs',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100125 'runtime_checkable',
Guido van Rossum0e0563c2016-04-05 14:54:25 -0700126 'Text',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700127 'TYPE_CHECKING',
Mikhail Golubev4f3c2502020-10-08 00:44:31 +0300128 'TypeAlias',
Ken Jin05ab4b62021-04-27 22:31:04 +0800129 'TypeGuard',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700130]
131
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700132# The pseudo-submodules 're' and 'io' are part of the public
133# namespace, but excluded from __all__ because they might stomp on
134# legitimate imports of those modules.
135
kj463c7d32020-12-14 02:38:24 +0800136
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700137def _type_convert(arg, module=None):
kj463c7d32020-12-14 02:38:24 +0800138 """For converting None to type(None), and strings to ForwardRef."""
139 if arg is None:
140 return type(None)
141 if isinstance(arg, str):
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700142 return ForwardRef(arg, module=module)
kj463c7d32020-12-14 02:38:24 +0800143 return arg
144
145
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700146def _type_check(arg, msg, is_argument=True, module=None):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000147 """Check that the argument is a type, and return it (internal helper).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700148
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000149 As a special case, accept None and return type(None) instead. Also wrap strings
150 into ForwardRef instances. Consider several corner cases, for example plain
151 special forms like Union are not valid, while Union[int, str] is OK, etc.
152 The msg argument is a human-readable error message, e.g::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700153
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000154 "Union[arg, ...]: arg should be a type."
Guido van Rossum4cefe742016-09-27 15:20:12 -0700155
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000156 We append the repr() of the actual value (truncated to 100 chars).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700157 """
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100158 invalid_generic_forms = (Generic, Protocol)
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700159 if is_argument:
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100160 invalid_generic_forms = invalid_generic_forms + (ClassVar, Final)
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400161
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700162 arg = _type_convert(arg, module=module)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000163 if (isinstance(arg, _GenericAlias) and
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400164 arg.__origin__ in invalid_generic_forms):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000165 raise TypeError(f"{arg} is not valid as type argument")
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300166 if arg in (Any, NoReturn):
167 return arg
168 if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000169 raise TypeError(f"Plain {arg} is not valid as type argument")
kj73607be2020-12-24 12:33:48 +0800170 if isinstance(arg, (type, TypeVar, ForwardRef, types.Union, ParamSpec)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000171 return arg
172 if not callable(arg):
173 raise TypeError(f"{msg} Got {arg!r:.100}.")
174 return arg
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700175
176
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000177def _type_repr(obj):
178 """Return the repr() of an object, special-casing types (internal helper).
179
180 If obj is a type, we return a shorter version than the default
181 type.__repr__, based on the module and qualified name, which is
182 typically enough to uniquely identify a type. For everything
183 else, we fall back on repr(obj).
184 """
kj1f7dfb22020-11-02 02:13:38 +0800185 if isinstance(obj, types.GenericAlias):
186 return repr(obj)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000187 if isinstance(obj, type):
188 if obj.__module__ == 'builtins':
189 return obj.__qualname__
190 return f'{obj.__module__}.{obj.__qualname__}'
191 if obj is ...:
192 return('...')
193 if isinstance(obj, types.FunctionType):
194 return obj.__name__
195 return repr(obj)
196
197
Ken Jina2721642021-07-19 22:22:59 +0800198def _collect_type_vars(types_, typevar_types=None):
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700199 """Collect all type variable contained
kj73607be2020-12-24 12:33:48 +0800200 in types in order of first appearance (lexicographic order). For example::
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000201
202 _collect_type_vars((T, List[S, T])) == (T, S)
203 """
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700204 if typevar_types is None:
205 typevar_types = TypeVar
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000206 tvars = []
Ken Jina2721642021-07-19 22:22:59 +0800207 for t in types_:
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700208 if isinstance(t, typevar_types) and t not in tvars:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000209 tvars.append(t)
Ken Jina2721642021-07-19 22:22:59 +0800210 if isinstance(t, (_GenericAlias, GenericAlias, types.Union)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000211 tvars.extend([t for t in t.__parameters__ if t not in tvars])
212 return tuple(tvars)
213
214
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300215def _check_generic(cls, parameters, elen):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000216 """Check correct count for parameters of a generic cls (internal helper).
217 This gives a nice error message in case of count mismatch.
218 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300219 if not elen:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000220 raise TypeError(f"{cls} is not a generic class")
221 alen = len(parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000222 if alen != elen:
223 raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
224 f" actual {alen}, expected {elen}")
225
kj73607be2020-12-24 12:33:48 +0800226def _prepare_paramspec_params(cls, params):
227 """Prepares the parameters for a Generic containing ParamSpec
228 variables (internal helper).
229 """
230 # Special case where Z[[int, str, bool]] == Z[int, str, bool] in PEP 612.
231 if len(cls.__parameters__) == 1 and len(params) > 1:
232 return (params,)
233 else:
234 _params = []
235 # Convert lists to tuples to help other libraries cache the results.
236 for p, tvar in zip(params, cls.__parameters__):
237 if isinstance(tvar, ParamSpec) and isinstance(p, list):
238 p = tuple(p)
239 _params.append(p)
240 return tuple(_params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000241
Yurii Karabasf03d3182020-11-17 04:23:19 +0200242def _deduplicate(params):
243 # Weed out strict duplicates, preserving the first of each occurrence.
244 all_params = set(params)
245 if len(all_params) < len(params):
246 new_params = []
247 for t in params:
248 if t in all_params:
249 new_params.append(t)
250 all_params.remove(t)
251 params = new_params
252 assert not all_params, all_params
253 return params
254
255
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000256def _remove_dups_flatten(parameters):
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700257 """An internal helper for Union creation and substitution: flatten Unions
258 among parameters, then remove duplicates.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000259 """
260 # Flatten out Union[Union[...], ...].
261 params = []
262 for p in parameters:
Maggie Moss1b4552c2020-09-09 13:23:24 -0700263 if isinstance(p, (_UnionGenericAlias, types.Union)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000264 params.extend(p.__args__)
265 elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
266 params.extend(p[1:])
267 else:
268 params.append(p)
Yurii Karabasf03d3182020-11-17 04:23:19 +0200269
270 return tuple(_deduplicate(params))
271
272
273def _flatten_literal_params(parameters):
274 """An internal helper for Literal creation: flatten Literals among parameters"""
275 params = []
276 for p in parameters:
277 if isinstance(p, _LiteralGenericAlias):
278 params.extend(p.__args__)
279 else:
280 params.append(p)
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700281 return tuple(params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000282
283
284_cleanups = []
285
286
Yurii Karabasf03d3182020-11-17 04:23:19 +0200287def _tp_cache(func=None, /, *, typed=False):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000288 """Internal wrapper caching __getitem__ of generic types with a fallback to
289 original function for non-hashable arguments.
290 """
Yurii Karabasf03d3182020-11-17 04:23:19 +0200291 def decorator(func):
292 cached = functools.lru_cache(typed=typed)(func)
293 _cleanups.append(cached.cache_clear)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000294
Yurii Karabasf03d3182020-11-17 04:23:19 +0200295 @functools.wraps(func)
296 def inner(*args, **kwds):
297 try:
298 return cached(*args, **kwds)
299 except TypeError:
300 pass # All real errors (not unhashable args) are raised below.
301 return func(*args, **kwds)
302 return inner
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000303
Yurii Karabasf03d3182020-11-17 04:23:19 +0200304 if func is not None:
305 return decorator(func)
306
307 return decorator
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000308
wyfo653f4202020-07-22 21:47:28 +0200309def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
Graham Bleaney84ef33c2020-09-08 18:41:10 -0400310 """Evaluate all forward references in the given type t.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000311 For use of globalns and localns see the docstring for get_type_hints().
wyfo653f4202020-07-22 21:47:28 +0200312 recursive_guard is used to prevent prevent infinite recursion
313 with recursive ForwardRef.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000314 """
315 if isinstance(t, ForwardRef):
wyfo653f4202020-07-22 21:47:28 +0200316 return t._evaluate(globalns, localns, recursive_guard)
Ken Jina2721642021-07-19 22:22:59 +0800317 if isinstance(t, (_GenericAlias, GenericAlias, types.Union)):
wyfo653f4202020-07-22 21:47:28 +0200318 ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000319 if ev_args == t.__args__:
320 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300321 if isinstance(t, GenericAlias):
322 return GenericAlias(t.__origin__, ev_args)
Ken Jina2721642021-07-19 22:22:59 +0800323 if isinstance(t, types.Union):
324 return functools.reduce(operator.or_, ev_args)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300325 else:
326 return t.copy_with(ev_args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000327 return t
328
329
330class _Final:
331 """Mixin to prohibit subclassing"""
Guido van Rossum4cefe742016-09-27 15:20:12 -0700332
Guido van Rossum83ec3022017-01-17 20:43:28 -0800333 __slots__ = ('__weakref__',)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700334
Serhiy Storchaka2085bd02019-06-01 11:00:15 +0300335 def __init_subclass__(self, /, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000336 if '_root' not in kwds:
337 raise TypeError("Cannot subclass special typing classes")
338
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100339class _Immutable:
340 """Mixin to indicate that object should not be copied."""
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300341 __slots__ = ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000342
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100343 def __copy__(self):
344 return self
345
346 def __deepcopy__(self, memo):
347 return self
348
349
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300350# Internal indicator of special typing constructs.
351# See __doc__ instance attribute for specific docs.
352class _SpecialForm(_Final, _root=True):
353 __slots__ = ('_name', '__doc__', '_getitem')
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000354
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300355 def __init__(self, getitem):
356 self._getitem = getitem
357 self._name = getitem.__name__
358 self.__doc__ = getitem.__doc__
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000359
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300360 def __mro_entries__(self, bases):
361 raise TypeError(f"Cannot subclass {self!r}")
Guido van Rossum4cefe742016-09-27 15:20:12 -0700362
363 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000364 return 'typing.' + self._name
365
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100366 def __reduce__(self):
367 return self._name
Guido van Rossum4cefe742016-09-27 15:20:12 -0700368
369 def __call__(self, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000370 raise TypeError(f"Cannot instantiate {self!r}")
371
372 def __instancecheck__(self, obj):
373 raise TypeError(f"{self} cannot be used with isinstance()")
374
375 def __subclasscheck__(self, cls):
376 raise TypeError(f"{self} cannot be used with issubclass()")
377
378 @_tp_cache
379 def __getitem__(self, parameters):
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300380 return self._getitem(self, parameters)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700381
Yurii Karabasf03d3182020-11-17 04:23:19 +0200382
383class _LiteralSpecialForm(_SpecialForm, _root=True):
384 @_tp_cache(typed=True)
385 def __getitem__(self, parameters):
386 return self._getitem(self, parameters)
387
388
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300389@_SpecialForm
390def Any(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000391 """Special type indicating an unconstrained type.
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700392
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000393 - Any is compatible with every type.
394 - Any assumed to have all methods.
395 - All values assumed to be instances of Any.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700396
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000397 Note that all the above statements are true from the point of view of
398 static type checkers. At runtime, Any should not be used with instance
399 or class checks.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300400 """
401 raise TypeError(f"{self} is not subscriptable")
Guido van Rossumd70fe632015-08-05 12:11:06 +0200402
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300403@_SpecialForm
404def NoReturn(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000405 """Special type indicating functions that never return.
406 Example::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700407
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000408 from typing import NoReturn
409
410 def stop() -> NoReturn:
411 raise Exception('no way')
412
413 This type is invalid in other positions, e.g., ``List[NoReturn]``
414 will fail in static type checkers.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300415 """
416 raise TypeError(f"{self} is not subscriptable")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000417
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300418@_SpecialForm
419def ClassVar(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000420 """Special type construct to mark class variables.
421
422 An annotation wrapped in ClassVar indicates that a given
423 attribute is intended to be used as a class variable and
424 should not be set on instances of that class. Usage::
425
426 class Starship:
427 stats: ClassVar[Dict[str, int]] = {} # class variable
428 damage: int = 10 # instance variable
429
430 ClassVar accepts only types and cannot be further subscribed.
431
432 Note that ClassVar is not a class itself, and should not
433 be used with isinstance() or issubclass().
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300434 """
435 item = _type_check(parameters, f'{self} accepts only single type.')
436 return _GenericAlias(self, (item,))
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000437
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300438@_SpecialForm
439def Final(self, parameters):
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100440 """Special typing construct to indicate final names to type checkers.
441
442 A final name cannot be re-assigned or overridden in a subclass.
443 For example:
444
445 MAX_SIZE: Final = 9000
446 MAX_SIZE += 1 # Error reported by type checker
447
448 class Connection:
449 TIMEOUT: Final[int] = 10
450
451 class FastConnector(Connection):
452 TIMEOUT = 1 # Error reported by type checker
453
454 There is no runtime checking of these properties.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300455 """
456 item = _type_check(parameters, f'{self} accepts only single type.')
457 return _GenericAlias(self, (item,))
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100458
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300459@_SpecialForm
460def Union(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000461 """Union type; Union[X, Y] means either X or Y.
462
463 To define a union, use e.g. Union[int, str]. Details:
464 - The arguments must be types and there must be at least one.
465 - None as an argument is a special case and is replaced by
466 type(None).
467 - Unions of unions are flattened, e.g.::
468
469 Union[Union[int, str], float] == Union[int, str, float]
470
471 - Unions of a single argument vanish, e.g.::
472
473 Union[int] == int # The constructor actually returns int
474
475 - Redundant arguments are skipped, e.g.::
476
477 Union[int, str, int] == Union[int, str]
478
479 - When comparing unions, the argument order is ignored, e.g.::
480
481 Union[int, str] == Union[str, int]
482
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000483 - You cannot subclass or instantiate a union.
484 - You can use Optional[X] as a shorthand for Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300485 """
486 if parameters == ():
487 raise TypeError("Cannot take a Union of no types.")
488 if not isinstance(parameters, tuple):
489 parameters = (parameters,)
490 msg = "Union[arg, ...]: each arg must be a type."
491 parameters = tuple(_type_check(p, msg) for p in parameters)
492 parameters = _remove_dups_flatten(parameters)
493 if len(parameters) == 1:
494 return parameters[0]
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300495 return _UnionGenericAlias(self, parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000496
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300497@_SpecialForm
498def Optional(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000499 """Optional type.
500
501 Optional[X] is equivalent to Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300502 """
503 arg = _type_check(parameters, f"{self} requires a single type.")
504 return Union[arg, type(None)]
Guido van Rossumb7dedc82016-10-29 12:44:29 -0700505
Yurii Karabasf03d3182020-11-17 04:23:19 +0200506@_LiteralSpecialForm
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300507def Literal(self, parameters):
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100508 """Special typing form to define literal types (a.k.a. value types).
509
510 This form can be used to indicate to type checkers that the corresponding
511 variable or function parameter has a value equivalent to the provided
512 literal (or one of several literals):
513
514 def validate_simple(data: Any) -> Literal[True]: # always returns True
515 ...
516
517 MODE = Literal['r', 'rb', 'w', 'wb']
518 def open_helper(file: str, mode: MODE) -> str:
519 ...
520
521 open_helper('/some/path', 'r') # Passes type check
522 open_helper('/other/path', 'typo') # Error in type checker
523
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300524 Literal[...] cannot be subclassed. At runtime, an arbitrary value
525 is allowed as type argument to Literal[...], but type checkers may
526 impose restrictions.
527 """
528 # There is no '_type_check' call because arguments to Literal[...] are
529 # values, not types.
Yurii Karabasf03d3182020-11-17 04:23:19 +0200530 if not isinstance(parameters, tuple):
531 parameters = (parameters,)
532
533 parameters = _flatten_literal_params(parameters)
534
535 try:
536 parameters = tuple(p for p, _ in _deduplicate(list(_value_and_type_iter(parameters))))
537 except TypeError: # unhashable parameters
538 pass
539
540 return _LiteralGenericAlias(self, parameters)
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100541
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700542
Mikhail Golubev4f3c2502020-10-08 00:44:31 +0300543@_SpecialForm
544def TypeAlias(self, parameters):
545 """Special marker indicating that an assignment should
546 be recognized as a proper type alias definition by type
547 checkers.
548
549 For example::
550
551 Predicate: TypeAlias = Callable[..., bool]
552
553 It's invalid when used anywhere except as in the example above.
554 """
555 raise TypeError(f"{self} is not subscriptable")
556
557
kj73607be2020-12-24 12:33:48 +0800558@_SpecialForm
559def Concatenate(self, parameters):
Ken Jin11276cd2021-01-02 08:45:50 +0800560 """Used in conjunction with ``ParamSpec`` and ``Callable`` to represent a
561 higher order function which adds, removes or transforms parameters of a
562 callable.
kj73607be2020-12-24 12:33:48 +0800563
564 For example::
565
566 Callable[Concatenate[int, P], int]
567
568 See PEP 612 for detailed information.
569 """
570 if parameters == ():
571 raise TypeError("Cannot take a Concatenate of no types.")
572 if not isinstance(parameters, tuple):
573 parameters = (parameters,)
574 if not isinstance(parameters[-1], ParamSpec):
575 raise TypeError("The last parameter to Concatenate should be a "
576 "ParamSpec variable.")
577 msg = "Concatenate[arg, ...]: each arg must be a type."
578 parameters = tuple(_type_check(p, msg) for p in parameters)
579 return _ConcatenateGenericAlias(self, parameters)
580
581
Ken Jin05ab4b62021-04-27 22:31:04 +0800582@_SpecialForm
583def TypeGuard(self, parameters):
584 """Special typing form used to annotate the return type of a user-defined
585 type guard function. ``TypeGuard`` only accepts a single type argument.
586 At runtime, functions marked this way should return a boolean.
587
588 ``TypeGuard`` aims to benefit *type narrowing* -- a technique used by static
589 type checkers to determine a more precise type of an expression within a
590 program's code flow. Usually type narrowing is done by analyzing
591 conditional code flow and applying the narrowing to a block of code. The
592 conditional expression here is sometimes referred to as a "type guard".
593
594 Sometimes it would be convenient to use a user-defined boolean function
595 as a type guard. Such a function should use ``TypeGuard[...]`` as its
596 return type to alert static type checkers to this intention.
597
598 Using ``-> TypeGuard`` tells the static type checker that for a given
599 function:
600
601 1. The return value is a boolean.
602 2. If the return value is ``True``, the type of its argument
603 is the type inside ``TypeGuard``.
604
605 For example::
606
607 def is_str(val: Union[str, float]):
608 # "isinstance" type guard
609 if isinstance(val, str):
610 # Type of ``val`` is narrowed to ``str``
611 ...
612 else:
613 # Else, type of ``val`` is narrowed to ``float``.
614 ...
615
616 Strict type narrowing is not enforced -- ``TypeB`` need not be a narrower
617 form of ``TypeA`` (it can even be a wider form) and this may lead to
618 type-unsafe results. The main reason is to allow for things like
619 narrowing ``List[object]`` to ``List[str]`` even though the latter is not
620 a subtype of the former, since ``List`` is invariant. The responsibility of
621 writing type-safe type guards is left to the user.
622
623 ``TypeGuard`` also works with type variables. For more information, see
624 PEP 647 (User-Defined Type Guards).
625 """
626 item = _type_check(parameters, f'{self} accepts only single type.')
627 return _GenericAlias(self, (item,))
628
629
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000630class ForwardRef(_Final, _root=True):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800631 """Internal wrapper to hold a forward reference."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700632
Guido van Rossum4cefe742016-09-27 15:20:12 -0700633 __slots__ = ('__forward_arg__', '__forward_code__',
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400634 '__forward_evaluated__', '__forward_value__',
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700635 '__forward_is_argument__', '__forward_module__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700636
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700637 def __init__(self, arg, is_argument=True, module=None):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700638 if not isinstance(arg, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000639 raise TypeError(f"Forward reference must be a string -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700640 try:
641 code = compile(arg, '<string>', 'eval')
642 except SyntaxError:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000643 raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700644 self.__forward_arg__ = arg
645 self.__forward_code__ = code
646 self.__forward_evaluated__ = False
647 self.__forward_value__ = None
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400648 self.__forward_is_argument__ = is_argument
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700649 self.__forward_module__ = module
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700650
wyfo653f4202020-07-22 21:47:28 +0200651 def _evaluate(self, globalns, localns, recursive_guard):
652 if self.__forward_arg__ in recursive_guard:
653 return self
Guido van Rossumdad17902016-11-10 08:29:18 -0800654 if not self.__forward_evaluated__ or localns is not globalns:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700655 if globalns is None and localns is None:
656 globalns = localns = {}
657 elif globalns is None:
658 globalns = localns
659 elif localns is None:
660 localns = globalns
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700661 if self.__forward_module__ is not None:
662 globalns = getattr(
663 sys.modules.get(self.__forward_module__, None), '__dict__', globalns
664 )
wyfo653f4202020-07-22 21:47:28 +0200665 type_ =_type_check(
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700666 eval(self.__forward_code__, globalns, localns),
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400667 "Forward references must evaluate to types.",
wyfo653f4202020-07-22 21:47:28 +0200668 is_argument=self.__forward_is_argument__,
669 )
670 self.__forward_value__ = _eval_type(
671 type_, globalns, localns, recursive_guard | {self.__forward_arg__}
672 )
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700673 self.__forward_evaluated__ = True
674 return self.__forward_value__
675
Guido van Rossum4cefe742016-09-27 15:20:12 -0700676 def __eq__(self, other):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000677 if not isinstance(other, ForwardRef):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700678 return NotImplemented
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100679 if self.__forward_evaluated__ and other.__forward_evaluated__:
680 return (self.__forward_arg__ == other.__forward_arg__ and
681 self.__forward_value__ == other.__forward_value__)
682 return self.__forward_arg__ == other.__forward_arg__
Guido van Rossum4cefe742016-09-27 15:20:12 -0700683
684 def __hash__(self):
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100685 return hash(self.__forward_arg__)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700686
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700687 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000688 return f'ForwardRef({self.__forward_arg__!r})'
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700689
kj73607be2020-12-24 12:33:48 +0800690class _TypeVarLike:
691 """Mixin for TypeVar-like types (TypeVar and ParamSpec)."""
692 def __init__(self, bound, covariant, contravariant):
693 """Used to setup TypeVars and ParamSpec's bound, covariant and
694 contravariant attributes.
695 """
696 if covariant and contravariant:
697 raise ValueError("Bivariant types are not supported.")
698 self.__covariant__ = bool(covariant)
699 self.__contravariant__ = bool(contravariant)
700 if bound:
701 self.__bound__ = _type_check(bound, "Bound must be a type.")
702 else:
703 self.__bound__ = None
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700704
kj73607be2020-12-24 12:33:48 +0800705 def __or__(self, right):
706 return Union[self, right]
707
Jelle Zijlstra90459192021-04-10 20:00:05 -0700708 def __ror__(self, left):
709 return Union[left, self]
kj73607be2020-12-24 12:33:48 +0800710
711 def __repr__(self):
712 if self.__covariant__:
713 prefix = '+'
714 elif self.__contravariant__:
715 prefix = '-'
716 else:
717 prefix = '~'
718 return prefix + self.__name__
719
720 def __reduce__(self):
721 return self.__name__
722
723
724class TypeVar( _Final, _Immutable, _TypeVarLike, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700725 """Type variable.
726
727 Usage::
728
729 T = TypeVar('T') # Can be anything
730 A = TypeVar('A', str, bytes) # Must be str or bytes
731
732 Type variables exist primarily for the benefit of static type
733 checkers. They serve as the parameters for generic types as well
734 as for generic function definitions. See class Generic for more
735 information on generic types. Generic functions work as follows:
736
Guido van Rossumb24569a2016-11-20 18:01:29 -0800737 def repeat(x: T, n: int) -> List[T]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700738 '''Return a list containing n references to x.'''
739 return [x]*n
740
741 def longest(x: A, y: A) -> A:
742 '''Return the longest of two strings.'''
743 return x if len(x) >= len(y) else y
744
745 The latter example's signature is essentially the overloading
746 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
747 that if the arguments are instances of some subclass of str,
748 the return type is still plain str.
749
Guido van Rossumb24569a2016-11-20 18:01:29 -0800750 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700751
Guido van Rossumefa798d2016-08-23 11:01:50 -0700752 Type variables defined with covariant=True or contravariant=True
João D. Ferreira86bfed32018-07-07 16:41:20 +0100753 can be used to declare covariant or contravariant generic types.
Guido van Rossumefa798d2016-08-23 11:01:50 -0700754 See PEP 484 for more details. By default generic types are invariant
755 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700756
757 Type variables can be introspected. e.g.:
758
759 T.__name__ == 'T'
760 T.__constraints__ == ()
761 T.__covariant__ == False
762 T.__contravariant__ = False
763 A.__constraints__ == (str, bytes)
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100764
765 Note that only type variables defined in global scope can be pickled.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700766 """
767
Guido van Rossum4cefe742016-09-27 15:20:12 -0700768 __slots__ = ('__name__', '__bound__', '__constraints__',
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300769 '__covariant__', '__contravariant__', '__dict__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700770
771 def __init__(self, name, *constraints, bound=None,
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800772 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700773 self.__name__ = name
kj73607be2020-12-24 12:33:48 +0800774 super().__init__(bound, covariant, contravariant)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700775 if constraints and bound is not None:
776 raise TypeError("Constraints cannot be combined with bound=...")
777 if constraints and len(constraints) == 1:
778 raise TypeError("A single constraint is not allowed")
779 msg = "TypeVar(name, constraint, ...): constraints must be types."
780 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
HongWeipenga25a04f2020-04-21 04:01:53 +0800781 try:
782 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling
783 except (AttributeError, ValueError):
784 def_mod = None
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300785 if def_mod != 'typing':
786 self.__module__ = def_mod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700787
Maggie Moss1b4552c2020-09-09 13:23:24 -0700788
Jelle Zijlstra52243362021-04-10 19:57:05 -0700789class ParamSpecArgs(_Final, _Immutable, _root=True):
790 """The args for a ParamSpec object.
791
792 Given a ParamSpec object P, P.args is an instance of ParamSpecArgs.
793
794 ParamSpecArgs objects have a reference back to their ParamSpec:
795
796 P.args.__origin__ is P
797
798 This type is meant for runtime introspection and has no special meaning to
799 static type checkers.
800 """
801 def __init__(self, origin):
802 self.__origin__ = origin
803
804 def __repr__(self):
805 return f"{self.__origin__.__name__}.args"
806
807
808class ParamSpecKwargs(_Final, _Immutable, _root=True):
809 """The kwargs for a ParamSpec object.
810
811 Given a ParamSpec object P, P.kwargs is an instance of ParamSpecKwargs.
812
813 ParamSpecKwargs objects have a reference back to their ParamSpec:
814
815 P.kwargs.__origin__ is P
816
817 This type is meant for runtime introspection and has no special meaning to
818 static type checkers.
819 """
820 def __init__(self, origin):
821 self.__origin__ = origin
822
823 def __repr__(self):
824 return f"{self.__origin__.__name__}.kwargs"
825
826
kj73607be2020-12-24 12:33:48 +0800827class ParamSpec(_Final, _Immutable, _TypeVarLike, _root=True):
828 """Parameter specification variable.
Maggie Moss1b4552c2020-09-09 13:23:24 -0700829
kj73607be2020-12-24 12:33:48 +0800830 Usage::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700831
kj73607be2020-12-24 12:33:48 +0800832 P = ParamSpec('P')
833
834 Parameter specification variables exist primarily for the benefit of static
835 type checkers. They are used to forward the parameter types of one
Ken Jin11276cd2021-01-02 08:45:50 +0800836 callable to another callable, a pattern commonly found in higher order
837 functions and decorators. They are only valid when used in ``Concatenate``,
838 or s the first argument to ``Callable``, or as parameters for user-defined
839 Generics. See class Generic for more information on generic types. An
840 example for annotating a decorator::
kj73607be2020-12-24 12:33:48 +0800841
842 T = TypeVar('T')
843 P = ParamSpec('P')
844
845 def add_logging(f: Callable[P, T]) -> Callable[P, T]:
846 '''A type-safe decorator to add logging to a function.'''
847 def inner(*args: P.args, **kwargs: P.kwargs) -> T:
848 logging.info(f'{f.__name__} was called')
849 return f(*args, **kwargs)
850 return inner
851
852 @add_logging
853 def add_two(x: float, y: float) -> float:
854 '''Add two numbers together.'''
855 return x + y
856
857 Parameter specification variables defined with covariant=True or
858 contravariant=True can be used to declare covariant or contravariant
859 generic types. These keyword arguments are valid, but their actual semantics
860 are yet to be decided. See PEP 612 for details.
861
862 Parameter specification variables can be introspected. e.g.:
863
864 P.__name__ == 'T'
865 P.__bound__ == None
866 P.__covariant__ == False
867 P.__contravariant__ == False
868
869 Note that only parameter specification variables defined in global scope can
870 be pickled.
871 """
872
873 __slots__ = ('__name__', '__bound__', '__covariant__', '__contravariant__',
874 '__dict__')
875
Jelle Zijlstra52243362021-04-10 19:57:05 -0700876 @property
877 def args(self):
878 return ParamSpecArgs(self)
879
880 @property
881 def kwargs(self):
882 return ParamSpecKwargs(self)
kj73607be2020-12-24 12:33:48 +0800883
Ken Jinace008c2021-01-11 08:11:41 +0800884 def __init__(self, name, *, bound=None, covariant=False, contravariant=False):
kj73607be2020-12-24 12:33:48 +0800885 self.__name__ = name
886 super().__init__(bound, covariant, contravariant)
887 try:
888 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__')
889 except (AttributeError, ValueError):
890 def_mod = None
891 if def_mod != 'typing':
892 self.__module__ = def_mod
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100893
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700894
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000895def _is_dunder(attr):
896 return attr.startswith('__') and attr.endswith('__')
Guido van Rossum83ec3022017-01-17 20:43:28 -0800897
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300898class _BaseGenericAlias(_Final, _root=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000899 """The central part of internal API.
900
901 This represents a generic version of type 'origin' with type arguments 'params'.
902 There are two kind of these aliases: user defined and special. The special ones
903 are wrappers around builtin collections and ABCs in collections.abc. These must
904 have 'name' always set. If 'inst' is False, then the alias can't be instantiated,
905 this is used by e.g. typing.List and typing.Dict.
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700906 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300907 def __init__(self, origin, *, inst=True, name=None):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000908 self._inst = inst
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000909 self._name = name
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700910 self.__origin__ = origin
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000911 self.__slots__ = None # This is not documented.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300912
913 def __call__(self, *args, **kwargs):
914 if not self._inst:
915 raise TypeError(f"Type {self._name} cannot be instantiated; "
916 f"use {self.__origin__.__name__}() instead")
917 result = self.__origin__(*args, **kwargs)
918 try:
919 result.__orig_class__ = self
920 except AttributeError:
921 pass
922 return result
923
924 def __mro_entries__(self, bases):
925 res = []
926 if self.__origin__ not in bases:
927 res.append(self.__origin__)
928 i = bases.index(self)
929 for b in bases[i+1:]:
930 if isinstance(b, _BaseGenericAlias) or issubclass(b, Generic):
931 break
932 else:
933 res.append(Generic)
934 return tuple(res)
935
936 def __getattr__(self, attr):
937 # We are careful for copy and pickle.
938 # Also for simplicity we just don't relay all dunder names
939 if '__origin__' in self.__dict__ and not _is_dunder(attr):
940 return getattr(self.__origin__, attr)
941 raise AttributeError(attr)
942
943 def __setattr__(self, attr, val):
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700944 if _is_dunder(attr) or attr in {'_name', '_inst', '_nparams',
945 '_typevar_types', '_paramspec_tvars'}:
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300946 super().__setattr__(attr, val)
947 else:
948 setattr(self.__origin__, attr, val)
949
950 def __instancecheck__(self, obj):
951 return self.__subclasscheck__(type(obj))
952
953 def __subclasscheck__(self, cls):
954 raise TypeError("Subscripted generics cannot be used with"
955 " class and instance checks")
956
957
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300958# Special typing constructs Union, Optional, Generic, Callable and Tuple
959# use three special attributes for internal bookkeeping of generic types:
960# * __parameters__ is a tuple of unique free type parameters of a generic
961# type, for example, Dict[T, T].__parameters__ == (T,);
962# * __origin__ keeps a reference to a type that was subscripted,
963# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
964# the type.
965# * __args__ is a tuple of all arguments used in subscripting,
966# e.g., Dict[T, int].__args__ == (T, int).
967
968
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300969class _GenericAlias(_BaseGenericAlias, _root=True):
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700970 def __init__(self, origin, params, *, inst=True, name=None,
971 _typevar_types=TypeVar,
972 _paramspec_tvars=False):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300973 super().__init__(origin, inst=inst, name=name)
974 if not isinstance(params, tuple):
975 params = (params,)
976 self.__args__ = tuple(... if a is _TypingEllipsis else
977 () if a is _TypingEmpty else
978 a for a in params)
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700979 self.__parameters__ = _collect_type_vars(params, typevar_types=_typevar_types)
980 self._typevar_types = _typevar_types
981 self._paramspec_tvars = _paramspec_tvars
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300982 if not name:
983 self.__module__ = origin.__module__
984
985 def __eq__(self, other):
986 if not isinstance(other, _GenericAlias):
987 return NotImplemented
988 return (self.__origin__ == other.__origin__
989 and self.__args__ == other.__args__)
990
991 def __hash__(self):
992 return hash((self.__origin__, self.__args__))
993
Maggie Moss1b4552c2020-09-09 13:23:24 -0700994 def __or__(self, right):
995 return Union[self, right]
996
Serhiy Storchaka80844d12021-07-16 16:42:04 +0300997 def __ror__(self, left):
998 return Union[left, self]
Maggie Moss1b4552c2020-09-09 13:23:24 -0700999
Guido van Rossum4cefe742016-09-27 15:20:12 -07001000 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001001 def __getitem__(self, params):
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001002 if self.__origin__ in (Generic, Protocol):
1003 # Can't subscript Generic[...] or Protocol[...].
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001004 raise TypeError(f"Cannot subscript already-subscripted {self}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001005 if not isinstance(params, tuple):
1006 params = (params,)
kj73607be2020-12-24 12:33:48 +08001007 params = tuple(_type_convert(p) for p in params)
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001008 if self._paramspec_tvars:
1009 if any(isinstance(t, ParamSpec) for t in self.__parameters__):
1010 params = _prepare_paramspec_params(self, params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001011 _check_generic(self, params, len(self.__parameters__))
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001012
1013 subst = dict(zip(self.__parameters__, params))
1014 new_args = []
1015 for arg in self.__args__:
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001016 if isinstance(arg, self._typevar_types):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001017 arg = subst[arg]
Ken Jina2721642021-07-19 22:22:59 +08001018 elif isinstance(arg, (_GenericAlias, GenericAlias, types.Union)):
Serhiy Storchaka0122d482020-05-10 13:39:40 +03001019 subparams = arg.__parameters__
1020 if subparams:
1021 subargs = tuple(subst[x] for x in subparams)
1022 arg = arg[subargs]
kj73607be2020-12-24 12:33:48 +08001023 # Required to flatten out the args for CallableGenericAlias
1024 if self.__origin__ == collections.abc.Callable and isinstance(arg, tuple):
1025 new_args.extend(arg)
1026 else:
1027 new_args.append(arg)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001028 return self.copy_with(tuple(new_args))
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001029
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001030 def copy_with(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001031 return self.__class__(self.__origin__, params, name=self._name, inst=self._inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001032
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001033 def __repr__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001034 if self._name:
1035 name = 'typing.' + self._name
1036 else:
1037 name = _type_repr(self.__origin__)
1038 args = ", ".join([_type_repr(a) for a in self.__args__])
1039 return f'{name}[{args}]'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001040
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001041 def __reduce__(self):
1042 if self._name:
1043 origin = globals()[self._name]
1044 else:
1045 origin = self.__origin__
1046 args = tuple(self.__args__)
1047 if len(args) == 1 and not isinstance(args[0], tuple):
1048 args, = args
1049 return operator.getitem, (origin, args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001050
1051 def __mro_entries__(self, bases):
1052 if self._name: # generic version of an ABC or built-in class
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001053 return super().__mro_entries__(bases)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001054 if self.__origin__ is Generic:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001055 if Protocol in bases:
1056 return ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001057 i = bases.index(self)
1058 for b in bases[i+1:]:
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001059 if isinstance(b, _BaseGenericAlias) and b is not self:
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001060 return ()
1061 return (self.__origin__,)
1062
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001063
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001064# _nparams is the number of accepted parameters, e.g. 0 for Hashable,
1065# 1 for List and 2 for Dict. It may be -1 if variable number of
1066# parameters are accepted (needs custom __getitem__).
1067
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001068class _SpecialGenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001069 def __init__(self, origin, nparams, *, inst=True, name=None):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001070 if name is None:
1071 name = origin.__name__
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001072 super().__init__(origin, inst=inst, name=name)
1073 self._nparams = nparams
Serhiy Storchaka2fbc57a2020-05-10 15:14:27 +03001074 if origin.__module__ == 'builtins':
1075 self.__doc__ = f'A generic version of {origin.__qualname__}.'
1076 else:
1077 self.__doc__ = f'A generic version of {origin.__module__}.{origin.__qualname__}.'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001078
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001079 @_tp_cache
1080 def __getitem__(self, params):
1081 if not isinstance(params, tuple):
1082 params = (params,)
1083 msg = "Parameters to generic types must be types."
1084 params = tuple(_type_check(p, msg) for p in params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001085 _check_generic(self, params, self._nparams)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001086 return self.copy_with(params)
1087
1088 def copy_with(self, params):
1089 return _GenericAlias(self.__origin__, params,
1090 name=self._name, inst=self._inst)
1091
1092 def __repr__(self):
1093 return 'typing.' + self._name
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001094
1095 def __subclasscheck__(self, cls):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001096 if isinstance(cls, _SpecialGenericAlias):
1097 return issubclass(cls.__origin__, self.__origin__)
1098 if not isinstance(cls, _GenericAlias):
1099 return issubclass(cls, self.__origin__)
1100 return super().__subclasscheck__(cls)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001101
Ivan Levkivskyi83494032018-03-26 23:01:12 +01001102 def __reduce__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001103 return self._name
Ivan Levkivskyi83494032018-03-26 23:01:12 +01001104
Maggie Moss1b4552c2020-09-09 13:23:24 -07001105 def __or__(self, right):
1106 return Union[self, right]
1107
Serhiy Storchaka80844d12021-07-16 16:42:04 +03001108 def __ror__(self, left):
1109 return Union[left, self]
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001110
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001111class _CallableGenericAlias(_GenericAlias, _root=True):
1112 def __repr__(self):
1113 assert self._name == 'Callable'
kj73607be2020-12-24 12:33:48 +08001114 args = self.__args__
1115 if len(args) == 2 and (args[0] is Ellipsis
1116 or isinstance(args[0], (ParamSpec, _ConcatenateGenericAlias))):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001117 return super().__repr__()
1118 return (f'typing.Callable'
kj73607be2020-12-24 12:33:48 +08001119 f'[[{", ".join([_type_repr(a) for a in args[:-1]])}], '
1120 f'{_type_repr(args[-1])}]')
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001121
1122 def __reduce__(self):
1123 args = self.__args__
kj73607be2020-12-24 12:33:48 +08001124 if not (len(args) == 2 and (args[0] is Ellipsis
1125 or isinstance(args[0], (ParamSpec, _ConcatenateGenericAlias)))):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001126 args = list(args[:-1]), args[-1]
1127 return operator.getitem, (Callable, args)
1128
1129
1130class _CallableType(_SpecialGenericAlias, _root=True):
1131 def copy_with(self, params):
1132 return _CallableGenericAlias(self.__origin__, params,
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001133 name=self._name, inst=self._inst,
1134 _typevar_types=(TypeVar, ParamSpec),
1135 _paramspec_tvars=True)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001136
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001137 def __getitem__(self, params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001138 if not isinstance(params, tuple) or len(params) != 2:
1139 raise TypeError("Callable must be used as "
1140 "Callable[[arg, ...], result].")
1141 args, result = params
kj463c7d32020-12-14 02:38:24 +08001142 # This relaxes what args can be on purpose to allow things like
1143 # PEP 612 ParamSpec. Responsibility for whether a user is using
1144 # Callable[...] properly is deferred to static type checkers.
1145 if isinstance(args, list):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001146 params = (tuple(args), result)
kj463c7d32020-12-14 02:38:24 +08001147 else:
1148 params = (args, result)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001149 return self.__getitem_inner__(params)
1150
1151 @_tp_cache
1152 def __getitem_inner__(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001153 args, result = params
1154 msg = "Callable[args, result]: result must be a type."
1155 result = _type_check(result, msg)
1156 if args is Ellipsis:
1157 return self.copy_with((_TypingEllipsis, result))
kj463c7d32020-12-14 02:38:24 +08001158 if not isinstance(args, tuple):
1159 args = (args,)
1160 args = tuple(_type_convert(arg) for arg in args)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001161 params = args + (result,)
1162 return self.copy_with(params)
1163
1164
1165class _TupleType(_SpecialGenericAlias, _root=True):
1166 @_tp_cache
1167 def __getitem__(self, params):
1168 if params == ():
1169 return self.copy_with((_TypingEmpty,))
1170 if not isinstance(params, tuple):
1171 params = (params,)
1172 if len(params) == 2 and params[1] is ...:
1173 msg = "Tuple[t, ...]: t must be a type."
1174 p = _type_check(params[0], msg)
1175 return self.copy_with((p, _TypingEllipsis))
1176 msg = "Tuple[t0, t1, ...]: each t must be a type."
1177 params = tuple(_type_check(p, msg) for p in params)
1178 return self.copy_with(params)
1179
1180
1181class _UnionGenericAlias(_GenericAlias, _root=True):
1182 def copy_with(self, params):
1183 return Union[params]
1184
1185 def __eq__(self, other):
Miss Islington (bot)03aad302021-07-17 14:10:21 -07001186 if not isinstance(other, (_UnionGenericAlias, types.Union)):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001187 return NotImplemented
1188 return set(self.__args__) == set(other.__args__)
1189
1190 def __hash__(self):
1191 return hash(frozenset(self.__args__))
1192
1193 def __repr__(self):
1194 args = self.__args__
1195 if len(args) == 2:
1196 if args[0] is type(None):
1197 return f'typing.Optional[{_type_repr(args[1])}]'
1198 elif args[1] is type(None):
1199 return f'typing.Optional[{_type_repr(args[0])}]'
1200 return super().__repr__()
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001201
Maggie Moss1b4552c2020-09-09 13:23:24 -07001202 def __instancecheck__(self, obj):
1203 return self.__subclasscheck__(type(obj))
1204
1205 def __subclasscheck__(self, cls):
1206 for arg in self.__args__:
1207 if issubclass(cls, arg):
1208 return True
1209
1210
Yurii Karabasf03d3182020-11-17 04:23:19 +02001211def _value_and_type_iter(parameters):
1212 return ((p, type(p)) for p in parameters)
1213
1214
1215class _LiteralGenericAlias(_GenericAlias, _root=True):
1216
1217 def __eq__(self, other):
1218 if not isinstance(other, _LiteralGenericAlias):
1219 return NotImplemented
1220
1221 return set(_value_and_type_iter(self.__args__)) == set(_value_and_type_iter(other.__args__))
1222
1223 def __hash__(self):
Yurii Karabas1b540772020-11-19 18:17:38 +02001224 return hash(frozenset(_value_and_type_iter(self.__args__)))
Yurii Karabasf03d3182020-11-17 04:23:19 +02001225
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001226
kj73607be2020-12-24 12:33:48 +08001227class _ConcatenateGenericAlias(_GenericAlias, _root=True):
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001228 def __init__(self, *args, **kwargs):
1229 super().__init__(*args, **kwargs,
1230 _typevar_types=(TypeVar, ParamSpec),
1231 _paramspec_tvars=True)
kj73607be2020-12-24 12:33:48 +08001232
1233
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001234class Generic:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001235 """Abstract base class for generic types.
1236
Guido van Rossumb24569a2016-11-20 18:01:29 -08001237 A generic type is typically declared by inheriting from
1238 this class parameterized with one or more type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001239 For example, a generic mapping type might be defined as::
1240
1241 class Mapping(Generic[KT, VT]):
1242 def __getitem__(self, key: KT) -> VT:
1243 ...
1244 # Etc.
1245
1246 This class can then be used as follows::
1247
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001248 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001249 try:
1250 return mapping[key]
1251 except KeyError:
1252 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001253 """
Guido van Rossumd70fe632015-08-05 12:11:06 +02001254 __slots__ = ()
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001255 _is_protocol = False
Guido van Rossumd70fe632015-08-05 12:11:06 +02001256
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001257 @_tp_cache
1258 def __class_getitem__(cls, params):
1259 if not isinstance(params, tuple):
1260 params = (params,)
1261 if not params and cls is not Tuple:
1262 raise TypeError(
1263 f"Parameter list to {cls.__qualname__}[...] cannot be empty")
kj73607be2020-12-24 12:33:48 +08001264 params = tuple(_type_convert(p) for p in params)
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001265 if cls in (Generic, Protocol):
1266 # Generic and Protocol can only be subscripted with unique type variables.
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001267 if not all(isinstance(p, (TypeVar, ParamSpec)) for p in params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001268 raise TypeError(
kj73607be2020-12-24 12:33:48 +08001269 f"Parameters to {cls.__name__}[...] must all be type variables "
1270 f"or parameter specification variables.")
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001271 if len(set(params)) != len(params):
1272 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001273 f"Parameters to {cls.__name__}[...] must all be unique")
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001274 else:
1275 # Subscripting a regular Generic subclass.
kj73607be2020-12-24 12:33:48 +08001276 if any(isinstance(t, ParamSpec) for t in cls.__parameters__):
1277 params = _prepare_paramspec_params(cls, params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001278 _check_generic(cls, params, len(cls.__parameters__))
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001279 return _GenericAlias(cls, params,
1280 _typevar_types=(TypeVar, ParamSpec),
1281 _paramspec_tvars=True)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001282
1283 def __init_subclass__(cls, *args, **kwargs):
Ivan Levkivskyiee566fe2018-04-04 17:00:15 +01001284 super().__init_subclass__(*args, **kwargs)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001285 tvars = []
1286 if '__orig_bases__' in cls.__dict__:
1287 error = Generic in cls.__orig_bases__
1288 else:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001289 error = Generic in cls.__bases__ and cls.__name__ != 'Protocol'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001290 if error:
1291 raise TypeError("Cannot inherit from plain Generic")
1292 if '__orig_bases__' in cls.__dict__:
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001293 tvars = _collect_type_vars(cls.__orig_bases__, (TypeVar, ParamSpec))
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001294 # Look for Generic[T1, ..., Tn].
1295 # If found, tvars must be a subset of it.
1296 # If not found, tvars is it.
1297 # Also check for and reject plain Generic,
1298 # and reject multiple Generic[...].
1299 gvars = None
1300 for base in cls.__orig_bases__:
1301 if (isinstance(base, _GenericAlias) and
1302 base.__origin__ is Generic):
1303 if gvars is not None:
1304 raise TypeError(
1305 "Cannot inherit from Generic[...] multiple types.")
1306 gvars = base.__parameters__
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001307 if gvars is not None:
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001308 tvarset = set(tvars)
1309 gvarset = set(gvars)
1310 if not tvarset <= gvarset:
1311 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
1312 s_args = ', '.join(str(g) for g in gvars)
1313 raise TypeError(f"Some type variables ({s_vars}) are"
1314 f" not listed in Generic[{s_args}]")
1315 tvars = gvars
1316 cls.__parameters__ = tuple(tvars)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001317
1318
1319class _TypingEmpty:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001320 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
1321 to allow empty list/tuple in specific places, without allowing them
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001322 to sneak in where prohibited.
1323 """
1324
1325
1326class _TypingEllipsis:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001327 """Internal placeholder for ... (ellipsis)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001328
1329
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001330_TYPING_INTERNALS = ['__parameters__', '__orig_bases__', '__orig_class__',
1331 '_is_protocol', '_is_runtime_protocol']
1332
1333_SPECIAL_NAMES = ['__abstractmethods__', '__annotations__', '__dict__', '__doc__',
1334 '__init__', '__module__', '__new__', '__slots__',
Guido van Rossum48b069a2020-04-07 09:50:06 -07001335 '__subclasshook__', '__weakref__', '__class_getitem__']
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001336
1337# These special attributes will be not collected as protocol members.
1338EXCLUDED_ATTRIBUTES = _TYPING_INTERNALS + _SPECIAL_NAMES + ['_MutableMapping__marker']
1339
1340
1341def _get_protocol_attrs(cls):
1342 """Collect protocol members from a protocol class objects.
1343
1344 This includes names actually defined in the class dictionary, as well
1345 as names that appear in annotations. Special names (above) are skipped.
1346 """
1347 attrs = set()
1348 for base in cls.__mro__[:-1]: # without object
1349 if base.__name__ in ('Protocol', 'Generic'):
1350 continue
1351 annotations = getattr(base, '__annotations__', {})
1352 for attr in list(base.__dict__.keys()) + list(annotations.keys()):
1353 if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
1354 attrs.add(attr)
1355 return attrs
1356
1357
1358def _is_callable_members_only(cls):
1359 # PEP 544 prohibits using issubclass() with protocols that have non-method members.
1360 return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls))
1361
1362
1363def _no_init(self, *args, **kwargs):
1364 if type(self)._is_protocol:
1365 raise TypeError('Protocols cannot be instantiated')
1366
1367
Miss Islington (bot)a2d94a02021-05-12 09:09:04 -07001368def _allow_reckless_class_checks(depth=3):
Nickolena Fishercfaf4c02020-04-26 12:49:11 -05001369 """Allow instance and class checks for special stdlib modules.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001370
1371 The abc and functools modules indiscriminately call isinstance() and
1372 issubclass() on the whole MRO of a user class, which may contain protocols.
1373 """
1374 try:
Miss Islington (bot)a2d94a02021-05-12 09:09:04 -07001375 return sys._getframe(depth).f_globals['__name__'] in ['abc', 'functools']
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001376 except (AttributeError, ValueError): # For platforms without _getframe().
1377 return True
1378
1379
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001380_PROTO_ALLOWLIST = {
Divij Rajkumar692a0dc2019-09-12 11:13:51 +01001381 'collections.abc': [
1382 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
1383 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
1384 ],
1385 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
1386}
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001387
1388
1389class _ProtocolMeta(ABCMeta):
1390 # This metaclass is really unfortunate and exists only because of
1391 # the lack of __instancehook__.
1392 def __instancecheck__(cls, instance):
1393 # We need this method for situations where attributes are
1394 # assigned in __init__.
Miss Islington (bot)a2d94a02021-05-12 09:09:04 -07001395 if (
1396 getattr(cls, '_is_protocol', False) and
1397 not getattr(cls, '_is_runtime_protocol', False) and
1398 not _allow_reckless_class_checks(depth=2)
1399 ):
1400 raise TypeError("Instance and class checks can only be used with"
1401 " @runtime_checkable protocols")
1402
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001403 if ((not getattr(cls, '_is_protocol', False) or
1404 _is_callable_members_only(cls)) and
1405 issubclass(instance.__class__, cls)):
1406 return True
1407 if cls._is_protocol:
1408 if all(hasattr(instance, attr) and
1409 # All *methods* can be blocked by setting them to None.
1410 (not callable(getattr(cls, attr, None)) or
1411 getattr(instance, attr) is not None)
1412 for attr in _get_protocol_attrs(cls)):
1413 return True
1414 return super().__instancecheck__(instance)
1415
1416
1417class Protocol(Generic, metaclass=_ProtocolMeta):
1418 """Base class for protocol classes.
1419
1420 Protocol classes are defined as::
1421
1422 class Proto(Protocol):
1423 def meth(self) -> int:
1424 ...
1425
1426 Such classes are primarily used with static type checkers that recognize
1427 structural subtyping (static duck-typing), for example::
1428
1429 class C:
1430 def meth(self) -> int:
1431 return 0
1432
1433 def func(x: Proto) -> int:
1434 return x.meth()
1435
1436 func(C()) # Passes static type check
1437
1438 See PEP 544 for details. Protocol classes decorated with
1439 @typing.runtime_checkable act as simple-minded runtime protocols that check
1440 only the presence of given attributes, ignoring their type signatures.
1441 Protocol classes can be generic, they are defined as::
1442
1443 class GenProto(Protocol[T]):
1444 def meth(self) -> T:
1445 ...
1446 """
1447 __slots__ = ()
1448 _is_protocol = True
1449 _is_runtime_protocol = False
1450
1451 def __init_subclass__(cls, *args, **kwargs):
1452 super().__init_subclass__(*args, **kwargs)
1453
1454 # Determine if this is a protocol or a concrete subclass.
1455 if not cls.__dict__.get('_is_protocol', False):
1456 cls._is_protocol = any(b is Protocol for b in cls.__bases__)
1457
1458 # Set (or override) the protocol subclass hook.
1459 def _proto_hook(other):
1460 if not cls.__dict__.get('_is_protocol', False):
1461 return NotImplemented
1462
1463 # First, perform various sanity checks.
1464 if not getattr(cls, '_is_runtime_protocol', False):
Rossc1af1282020-12-29 11:55:28 +00001465 if _allow_reckless_class_checks():
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001466 return NotImplemented
1467 raise TypeError("Instance and class checks can only be used with"
1468 " @runtime_checkable protocols")
1469 if not _is_callable_members_only(cls):
Rossc1af1282020-12-29 11:55:28 +00001470 if _allow_reckless_class_checks():
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001471 return NotImplemented
1472 raise TypeError("Protocols with non-method members"
1473 " don't support issubclass()")
1474 if not isinstance(other, type):
1475 # Same error message as for issubclass(1, int).
1476 raise TypeError('issubclass() arg 1 must be a class')
1477
1478 # Second, perform the actual structural compatibility check.
1479 for attr in _get_protocol_attrs(cls):
1480 for base in other.__mro__:
1481 # Check if the members appears in the class dictionary...
1482 if attr in base.__dict__:
1483 if base.__dict__[attr] is None:
1484 return NotImplemented
1485 break
1486
1487 # ...or in annotations, if it is a sub-protocol.
1488 annotations = getattr(base, '__annotations__', {})
1489 if (isinstance(annotations, collections.abc.Mapping) and
1490 attr in annotations and
1491 issubclass(other, Generic) and other._is_protocol):
1492 break
1493 else:
1494 return NotImplemented
1495 return True
1496
1497 if '__subclasshook__' not in cls.__dict__:
1498 cls.__subclasshook__ = _proto_hook
1499
1500 # We have nothing more to do for non-protocols...
1501 if not cls._is_protocol:
1502 return
1503
1504 # ... otherwise check consistency of bases, and prohibit instantiation.
1505 for base in cls.__bases__:
1506 if not (base in (object, Generic) or
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001507 base.__module__ in _PROTO_ALLOWLIST and
1508 base.__name__ in _PROTO_ALLOWLIST[base.__module__] or
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001509 issubclass(base, Generic) and base._is_protocol):
1510 raise TypeError('Protocols can only inherit from other'
1511 ' protocols, got %r' % base)
1512 cls.__init__ = _no_init
1513
1514
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001515class _AnnotatedAlias(_GenericAlias, _root=True):
1516 """Runtime representation of an annotated type.
1517
1518 At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't'
1519 with extra annotations. The alias behaves like a normal typing alias,
1520 instantiating is the same as instantiating the underlying type, binding
1521 it to types is also the same.
1522 """
1523 def __init__(self, origin, metadata):
1524 if isinstance(origin, _AnnotatedAlias):
1525 metadata = origin.__metadata__ + metadata
1526 origin = origin.__origin__
1527 super().__init__(origin, origin)
1528 self.__metadata__ = metadata
1529
1530 def copy_with(self, params):
1531 assert len(params) == 1
1532 new_type = params[0]
1533 return _AnnotatedAlias(new_type, self.__metadata__)
1534
1535 def __repr__(self):
1536 return "typing.Annotated[{}, {}]".format(
1537 _type_repr(self.__origin__),
1538 ", ".join(repr(a) for a in self.__metadata__)
1539 )
1540
1541 def __reduce__(self):
1542 return operator.getitem, (
1543 Annotated, (self.__origin__,) + self.__metadata__
1544 )
1545
1546 def __eq__(self, other):
1547 if not isinstance(other, _AnnotatedAlias):
1548 return NotImplemented
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001549 return (self.__origin__ == other.__origin__
1550 and self.__metadata__ == other.__metadata__)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001551
1552 def __hash__(self):
1553 return hash((self.__origin__, self.__metadata__))
1554
1555
1556class Annotated:
1557 """Add context specific metadata to a type.
1558
1559 Example: Annotated[int, runtime_check.Unsigned] indicates to the
1560 hypothetical runtime_check module that this type is an unsigned int.
1561 Every other consumer of this type can ignore this metadata and treat
1562 this type as int.
1563
1564 The first argument to Annotated must be a valid type.
1565
1566 Details:
1567
1568 - It's an error to call `Annotated` with less than two arguments.
1569 - Nested Annotated are flattened::
1570
1571 Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
1572
1573 - Instantiating an annotated type is equivalent to instantiating the
1574 underlying type::
1575
1576 Annotated[C, Ann1](5) == C(5)
1577
1578 - Annotated can be used as a generic type alias::
1579
1580 Optimized = Annotated[T, runtime.Optimize()]
1581 Optimized[int] == Annotated[int, runtime.Optimize()]
1582
1583 OptimizedList = Annotated[List[T], runtime.Optimize()]
1584 OptimizedList[int] == Annotated[List[int], runtime.Optimize()]
1585 """
1586
1587 __slots__ = ()
1588
1589 def __new__(cls, *args, **kwargs):
1590 raise TypeError("Type Annotated cannot be instantiated.")
1591
1592 @_tp_cache
1593 def __class_getitem__(cls, params):
1594 if not isinstance(params, tuple) or len(params) < 2:
1595 raise TypeError("Annotated[...] should be used "
1596 "with at least two arguments (a type and an "
1597 "annotation).")
1598 msg = "Annotated[t, ...]: t must be a type."
1599 origin = _type_check(params[0], msg)
1600 metadata = tuple(params[1:])
1601 return _AnnotatedAlias(origin, metadata)
1602
1603 def __init_subclass__(cls, *args, **kwargs):
1604 raise TypeError(
1605 "Cannot subclass {}.Annotated".format(cls.__module__)
1606 )
1607
1608
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001609def runtime_checkable(cls):
1610 """Mark a protocol class as a runtime protocol.
1611
1612 Such protocol can be used with isinstance() and issubclass().
1613 Raise TypeError if applied to a non-protocol class.
1614 This allows a simple-minded structural check very similar to
1615 one trick ponies in collections.abc such as Iterable.
1616 For example::
1617
1618 @runtime_checkable
1619 class Closable(Protocol):
1620 def close(self): ...
1621
1622 assert isinstance(open('/some/file'), Closable)
1623
1624 Warning: this will check only the presence of the required methods,
1625 not their type signatures!
1626 """
1627 if not issubclass(cls, Generic) or not cls._is_protocol:
1628 raise TypeError('@runtime_checkable can be only applied to protocol classes,'
1629 ' got %r' % cls)
1630 cls._is_runtime_protocol = True
1631 return cls
1632
1633
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001634def cast(typ, val):
1635 """Cast a value to a type.
1636
1637 This returns the value unchanged. To the type checker this
1638 signals that the return value has the designated type, but at
1639 runtime we intentionally don't check anything (we want this
1640 to be as fast as possible).
1641 """
1642 return val
1643
1644
1645def _get_defaults(func):
1646 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -08001647 try:
1648 code = func.__code__
1649 except AttributeError:
1650 # Some built-in functions don't have __code__, __defaults__, etc.
1651 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001652 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001653 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001654 arg_names = arg_names[:pos_count]
1655 defaults = func.__defaults__ or ()
1656 kwdefaults = func.__kwdefaults__
1657 res = dict(kwdefaults) if kwdefaults else {}
1658 pos_offset = pos_count - len(defaults)
1659 for name, value in zip(arg_names[pos_offset:], defaults):
1660 assert name not in res
1661 res[name] = value
1662 return res
1663
1664
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001665_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
1666 types.MethodType, types.ModuleType,
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02001667 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001668
1669
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001670def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
Guido van Rossum991d14f2016-11-09 13:12:51 -08001671 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001672
Guido van Rossum991d14f2016-11-09 13:12:51 -08001673 This is often the same as obj.__annotations__, but it handles
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001674 forward references encoded as string literals, adds Optional[t] if a
1675 default value equal to None is set and recursively replaces all
1676 'Annotated[T, ...]' with 'T' (unless 'include_extras=True').
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001677
Guido van Rossum991d14f2016-11-09 13:12:51 -08001678 The argument may be a module, class, method, or function. The annotations
1679 are returned as a dictionary. For classes, annotations include also
1680 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001681
Guido van Rossum991d14f2016-11-09 13:12:51 -08001682 TypeError is raised if the argument is not of a type that can contain
1683 annotations, and an empty dictionary is returned if no annotations are
1684 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001685
Guido van Rossum991d14f2016-11-09 13:12:51 -08001686 BEWARE -- the behavior of globalns and localns is counterintuitive
1687 (unless you are familiar with how eval() and exec() work). The
1688 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001689
Guido van Rossum991d14f2016-11-09 13:12:51 -08001690 - If no dict arguments are passed, an attempt is made to use the
Łukasz Langaf350a262017-09-14 14:33:00 -04001691 globals from obj (or the respective module's globals for classes),
1692 and these are also used as the locals. If the object does not appear
Ken Jin1b1f9852021-04-27 01:31:21 +08001693 to have globals, an empty dictionary is used. For classes, the search
1694 order is globals first then locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001695
Guido van Rossum991d14f2016-11-09 13:12:51 -08001696 - If one dict argument is passed, it is used for both globals and
1697 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001698
Guido van Rossum991d14f2016-11-09 13:12:51 -08001699 - If two dict arguments are passed, they specify globals and
1700 locals, respectively.
1701 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001702
Guido van Rossum991d14f2016-11-09 13:12:51 -08001703 if getattr(obj, '__no_type_check__', None):
1704 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001705 # Classes require a special treatment.
1706 if isinstance(obj, type):
1707 hints = {}
1708 for base in reversed(obj.__mro__):
Łukasz Langaf350a262017-09-14 14:33:00 -04001709 if globalns is None:
Miss Islington (bot)3df23b52021-06-26 16:52:28 -07001710 base_globals = getattr(sys.modules.get(base.__module__, None), '__dict__', {})
Łukasz Langaf350a262017-09-14 14:33:00 -04001711 else:
1712 base_globals = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001713 ann = base.__dict__.get('__annotations__', {})
larryhastings2f2b6982021-04-29 20:09:08 -07001714 if isinstance(ann, types.GetSetDescriptorType):
1715 ann = {}
Ken Jin852150d2021-04-13 01:23:12 +08001716 base_locals = dict(vars(base)) if localns is None else localns
Ken Jin1b1f9852021-04-27 01:31:21 +08001717 if localns is None and globalns is None:
1718 # This is surprising, but required. Before Python 3.10,
1719 # get_type_hints only evaluated the globalns of
1720 # a class. To maintain backwards compatibility, we reverse
1721 # the globalns and localns order so that eval() looks into
1722 # *base_globals* first rather than *base_locals*.
1723 # This only affects ForwardRefs.
1724 base_globals, base_locals = base_locals, base_globals
Guido van Rossum991d14f2016-11-09 13:12:51 -08001725 for name, value in ann.items():
1726 if value is None:
1727 value = type(None)
1728 if isinstance(value, str):
Nina Zakharenko0e61dff2018-05-22 20:32:10 -07001729 value = ForwardRef(value, is_argument=False)
Ken Jin852150d2021-04-13 01:23:12 +08001730 value = _eval_type(value, base_globals, base_locals)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001731 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001732 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
Łukasz Langaf350a262017-09-14 14:33:00 -04001733
1734 if globalns is None:
1735 if isinstance(obj, types.ModuleType):
1736 globalns = obj.__dict__
1737 else:
benedwards140aca3a32019-11-21 17:24:58 +00001738 nsobj = obj
1739 # Find globalns for the unwrapped object.
1740 while hasattr(nsobj, '__wrapped__'):
1741 nsobj = nsobj.__wrapped__
1742 globalns = getattr(nsobj, '__globals__', {})
Łukasz Langaf350a262017-09-14 14:33:00 -04001743 if localns is None:
1744 localns = globalns
1745 elif localns is None:
1746 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001747 hints = getattr(obj, '__annotations__', None)
1748 if hints is None:
1749 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001750 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001751 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001752 else:
1753 raise TypeError('{!r} is not a module, class, method, '
1754 'or function.'.format(obj))
1755 defaults = _get_defaults(obj)
1756 hints = dict(hints)
1757 for name, value in hints.items():
1758 if value is None:
1759 value = type(None)
1760 if isinstance(value, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001761 value = ForwardRef(value)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001762 value = _eval_type(value, globalns, localns)
1763 if name in defaults and defaults[name] is None:
1764 value = Optional[value]
1765 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001766 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
1767
1768
1769def _strip_annotations(t):
1770 """Strips the annotations from a given type.
1771 """
1772 if isinstance(t, _AnnotatedAlias):
1773 return _strip_annotations(t.__origin__)
1774 if isinstance(t, _GenericAlias):
1775 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1776 if stripped_args == t.__args__:
1777 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001778 return t.copy_with(stripped_args)
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001779 if isinstance(t, GenericAlias):
1780 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1781 if stripped_args == t.__args__:
1782 return t
1783 return GenericAlias(t.__origin__, stripped_args)
Ken Jina2721642021-07-19 22:22:59 +08001784 if isinstance(t, types.Union):
1785 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1786 if stripped_args == t.__args__:
1787 return t
1788 return functools.reduce(operator.or_, stripped_args)
1789
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001790 return t
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001791
1792
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001793def get_origin(tp):
1794 """Get the unsubscripted version of a type.
1795
Jakub Stasiak38aaaaa2020-02-07 02:15:12 +01001796 This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
1797 and Annotated. Return None for unsupported types. Examples::
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001798
1799 get_origin(Literal[42]) is Literal
1800 get_origin(int) is None
1801 get_origin(ClassVar[int]) is ClassVar
1802 get_origin(Generic) is Generic
1803 get_origin(Generic[T]) is Generic
1804 get_origin(Union[T, int]) is Union
1805 get_origin(List[Tuple[T, T]][int]) == list
Jelle Zijlstra52243362021-04-10 19:57:05 -07001806 get_origin(P.args) is P
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001807 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001808 if isinstance(tp, _AnnotatedAlias):
1809 return Annotated
Jelle Zijlstra52243362021-04-10 19:57:05 -07001810 if isinstance(tp, (_BaseGenericAlias, GenericAlias,
1811 ParamSpecArgs, ParamSpecKwargs)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001812 return tp.__origin__
1813 if tp is Generic:
1814 return Generic
Ken Jinefb1f092020-12-29 10:26:19 +08001815 if isinstance(tp, types.Union):
1816 return types.Union
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001817 return None
1818
1819
1820def get_args(tp):
1821 """Get type arguments with all substitutions performed.
1822
1823 For unions, basic simplifications used by Union constructor are performed.
1824 Examples::
1825 get_args(Dict[str, int]) == (str, int)
1826 get_args(int) == ()
1827 get_args(Union[int, Union[T, int], str][int]) == (int, str)
1828 get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
1829 get_args(Callable[[], T][int]) == ([], int)
1830 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001831 if isinstance(tp, _AnnotatedAlias):
1832 return (tp.__origin__,) + tp.__metadata__
Ken Jin4140f102020-12-29 04:06:19 +08001833 if isinstance(tp, (_GenericAlias, GenericAlias)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001834 res = tp.__args__
Ken Jinefb1f092020-12-29 10:26:19 +08001835 if (tp.__origin__ is collections.abc.Callable
1836 and not (res[0] is Ellipsis
1837 or isinstance(res[0], (ParamSpec, _ConcatenateGenericAlias)))):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001838 res = (list(res[:-1]), res[-1])
1839 return res
Ken Jinefb1f092020-12-29 10:26:19 +08001840 if isinstance(tp, types.Union):
1841 return tp.__args__
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001842 return ()
1843
1844
Patrick Reader0705ec82020-09-16 05:58:32 +01001845def is_typeddict(tp):
1846 """Check if an annotation is a TypedDict class
1847
1848 For example::
1849 class Film(TypedDict):
1850 title: str
1851 year: int
1852
1853 is_typeddict(Film) # => True
1854 is_typeddict(Union[list, str]) # => False
1855 """
1856 return isinstance(tp, _TypedDictMeta)
1857
1858
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001859def no_type_check(arg):
1860 """Decorator to indicate that annotations are not type hints.
1861
1862 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001863 applies recursively to all methods and classes defined in that class
1864 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001865
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001866 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001867 """
1868 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001869 arg_attrs = arg.__dict__.copy()
1870 for attr, val in arg.__dict__.items():
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001871 if val in arg.__bases__ + (arg,):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001872 arg_attrs.pop(attr)
1873 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001874 if isinstance(obj, types.FunctionType):
1875 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001876 if isinstance(obj, type):
1877 no_type_check(obj)
1878 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001879 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001880 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001881 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001882 return arg
1883
1884
1885def no_type_check_decorator(decorator):
1886 """Decorator to give another decorator the @no_type_check effect.
1887
1888 This wraps the decorator with something that wraps the decorated
1889 function in @no_type_check.
1890 """
1891
1892 @functools.wraps(decorator)
1893 def wrapped_decorator(*args, **kwds):
1894 func = decorator(*args, **kwds)
1895 func = no_type_check(func)
1896 return func
1897
1898 return wrapped_decorator
1899
1900
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001901def _overload_dummy(*args, **kwds):
1902 """Helper for @overload to raise when called."""
1903 raise NotImplementedError(
1904 "You should not call an overloaded function. "
1905 "A series of @overload-decorated functions "
1906 "outside a stub module should always be followed "
1907 "by an implementation that is not @overload-ed.")
1908
1909
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001910def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001911 """Decorator for overloaded functions/methods.
1912
1913 In a stub file, place two or more stub definitions for the same
1914 function in a row, each decorated with @overload. For example:
1915
1916 @overload
1917 def utf8(value: None) -> None: ...
1918 @overload
1919 def utf8(value: bytes) -> bytes: ...
1920 @overload
1921 def utf8(value: str) -> bytes: ...
1922
1923 In a non-stub file (i.e. a regular .py file), do the same but
1924 follow it with an implementation. The implementation should *not*
1925 be decorated with @overload. For example:
1926
1927 @overload
1928 def utf8(value: None) -> None: ...
1929 @overload
1930 def utf8(value: bytes) -> bytes: ...
1931 @overload
1932 def utf8(value: str) -> bytes: ...
1933 def utf8(value):
1934 # implementation goes here
1935 """
1936 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001937
1938
Ivan Levkivskyif3672422019-05-26 09:37:07 +01001939def final(f):
1940 """A decorator to indicate final methods and final classes.
1941
1942 Use this decorator to indicate to type checkers that the decorated
1943 method cannot be overridden, and decorated class cannot be subclassed.
1944 For example:
1945
1946 class Base:
1947 @final
1948 def done(self) -> None:
1949 ...
1950 class Sub(Base):
1951 def done(self) -> None: # Error reported by type checker
1952 ...
1953
1954 @final
1955 class Leaf:
1956 ...
1957 class Other(Leaf): # Error reported by type checker
1958 ...
1959
1960 There is no runtime checking of these properties.
1961 """
1962 return f
1963
1964
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001965# Some unconstrained type variables. These are used by the container types.
1966# (These are not for export.)
1967T = TypeVar('T') # Any type.
1968KT = TypeVar('KT') # Key type.
1969VT = TypeVar('VT') # Value type.
1970T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
1971V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
1972VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
1973T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
1974# Internal type variable used for Type[].
1975CT_co = TypeVar('CT_co', covariant=True, bound=type)
1976
1977# A useful type variable with constraints. This represents string types.
1978# (This one *is* for export!)
1979AnyStr = TypeVar('AnyStr', bytes, str)
1980
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001981
1982# Various ABCs mimicking those in collections.abc.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001983_alias = _SpecialGenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001984
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001985Hashable = _alias(collections.abc.Hashable, 0) # Not generic.
1986Awaitable = _alias(collections.abc.Awaitable, 1)
1987Coroutine = _alias(collections.abc.Coroutine, 3)
1988AsyncIterable = _alias(collections.abc.AsyncIterable, 1)
1989AsyncIterator = _alias(collections.abc.AsyncIterator, 1)
1990Iterable = _alias(collections.abc.Iterable, 1)
1991Iterator = _alias(collections.abc.Iterator, 1)
1992Reversible = _alias(collections.abc.Reversible, 1)
1993Sized = _alias(collections.abc.Sized, 0) # Not generic.
1994Container = _alias(collections.abc.Container, 1)
1995Collection = _alias(collections.abc.Collection, 1)
1996Callable = _CallableType(collections.abc.Callable, 2)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001997Callable.__doc__ = \
1998 """Callable type; Callable[[int], str] is a function of (int) -> str.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001999
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002000 The subscription syntax must always be used with exactly two
2001 values: the argument list and the return type. The argument list
2002 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002003
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002004 There is no syntax to indicate optional or keyword arguments,
2005 such function types are rarely used as callback types.
2006 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002007AbstractSet = _alias(collections.abc.Set, 1, name='AbstractSet')
2008MutableSet = _alias(collections.abc.MutableSet, 1)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002009# NOTE: Mapping is only covariant in the value type.
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002010Mapping = _alias(collections.abc.Mapping, 2)
2011MutableMapping = _alias(collections.abc.MutableMapping, 2)
2012Sequence = _alias(collections.abc.Sequence, 1)
2013MutableSequence = _alias(collections.abc.MutableSequence, 1)
2014ByteString = _alias(collections.abc.ByteString, 0) # Not generic
2015# Tuple accepts variable number of parameters.
2016Tuple = _TupleType(tuple, -1, inst=False, name='Tuple')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002017Tuple.__doc__ = \
2018 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07002019
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002020 Example: Tuple[T1, T2] is a tuple of two elements corresponding
2021 to type variables T1 and T2. Tuple[int, float, str] is a tuple
2022 of an int, a float and a string.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07002023
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002024 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
2025 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002026List = _alias(list, 1, inst=False, name='List')
2027Deque = _alias(collections.deque, 1, name='Deque')
2028Set = _alias(set, 1, inst=False, name='Set')
2029FrozenSet = _alias(frozenset, 1, inst=False, name='FrozenSet')
2030MappingView = _alias(collections.abc.MappingView, 1)
2031KeysView = _alias(collections.abc.KeysView, 1)
2032ItemsView = _alias(collections.abc.ItemsView, 2)
2033ValuesView = _alias(collections.abc.ValuesView, 1)
2034ContextManager = _alias(contextlib.AbstractContextManager, 1, name='ContextManager')
2035AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, 1, name='AsyncContextManager')
2036Dict = _alias(dict, 2, inst=False, name='Dict')
2037DefaultDict = _alias(collections.defaultdict, 2, name='DefaultDict')
2038OrderedDict = _alias(collections.OrderedDict, 2)
2039Counter = _alias(collections.Counter, 1)
2040ChainMap = _alias(collections.ChainMap, 2)
2041Generator = _alias(collections.abc.Generator, 3)
2042AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2)
2043Type = _alias(type, 1, inst=False, name='Type')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002044Type.__doc__ = \
2045 """A special construct usable to annotate class objects.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07002046
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002047 For example, suppose we have the following classes::
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07002048
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002049 class User: ... # Abstract base for User classes
2050 class BasicUser(User): ...
2051 class ProUser(User): ...
2052 class TeamUser(User): ...
Guido van Rossumf17c2002015-12-03 17:31:24 -08002053
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002054 And a function that takes a class argument that's a subclass of
2055 User and returns an instance of the corresponding class::
Guido van Rossumf17c2002015-12-03 17:31:24 -08002056
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002057 U = TypeVar('U', bound=User)
2058 def new_user(user_class: Type[U]) -> U:
2059 user = user_class()
2060 # (Here we could write the user object to a database)
2061 return user
Guido van Rossumf17c2002015-12-03 17:31:24 -08002062
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002063 joe = new_user(BasicUser)
Guido van Rossumf17c2002015-12-03 17:31:24 -08002064
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002065 At this point the type checker knows that joe has type BasicUser.
2066 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002067
2068
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002069@runtime_checkable
2070class SupportsInt(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002071 """An ABC with one abstract method __int__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002072 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002073
2074 @abstractmethod
2075 def __int__(self) -> int:
2076 pass
2077
2078
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002079@runtime_checkable
2080class SupportsFloat(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002081 """An ABC with one abstract method __float__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002082 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002083
2084 @abstractmethod
2085 def __float__(self) -> float:
2086 pass
2087
2088
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002089@runtime_checkable
2090class SupportsComplex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002091 """An ABC with one abstract method __complex__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002092 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002093
2094 @abstractmethod
2095 def __complex__(self) -> complex:
2096 pass
2097
2098
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002099@runtime_checkable
2100class SupportsBytes(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002101 """An ABC with one abstract method __bytes__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002102 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002103
2104 @abstractmethod
2105 def __bytes__(self) -> bytes:
2106 pass
2107
2108
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002109@runtime_checkable
2110class SupportsIndex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002111 """An ABC with one abstract method __index__."""
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -07002112 __slots__ = ()
2113
2114 @abstractmethod
2115 def __index__(self) -> int:
2116 pass
2117
2118
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002119@runtime_checkable
2120class SupportsAbs(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002121 """An ABC with one abstract method __abs__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002122 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002123
2124 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02002125 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002126 pass
2127
2128
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002129@runtime_checkable
2130class SupportsRound(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002131 """An ABC with one abstract method __round__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002132 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002133
2134 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02002135 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002136 pass
2137
2138
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002139def _make_nmtuple(name, types, module, defaults = ()):
2140 fields = [n for n, t in types]
2141 types = {n: _type_check(t, f"field {n} annotation must be a type")
2142 for n, t in types}
2143 nm_tpl = collections.namedtuple(name, fields,
2144 defaults=defaults, module=module)
2145 nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002146 return nm_tpl
2147
2148
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002149# attributes prohibited to set in NamedTuple class syntax
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002150_prohibited = frozenset({'__new__', '__init__', '__slots__', '__getnewargs__',
2151 '_fields', '_field_defaults',
2152 '_make', '_replace', '_asdict', '_source'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002153
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002154_special = frozenset({'__module__', '__name__', '__annotations__'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002155
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002156
Guido van Rossum2f841442016-11-15 09:48:06 -08002157class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002158
Guido van Rossum2f841442016-11-15 09:48:06 -08002159 def __new__(cls, typename, bases, ns):
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002160 assert bases[0] is _NamedTuple
Guido van Rossum2f841442016-11-15 09:48:06 -08002161 types = ns.get('__annotations__', {})
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002162 default_names = []
Guido van Rossum3c268be2017-01-18 08:03:50 -08002163 for field_name in types:
2164 if field_name in ns:
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002165 default_names.append(field_name)
2166 elif default_names:
2167 raise TypeError(f"Non-default namedtuple field {field_name} "
2168 f"cannot follow default field"
2169 f"{'s' if len(default_names) > 1 else ''} "
2170 f"{', '.join(default_names)}")
2171 nm_tpl = _make_nmtuple(typename, types.items(),
2172 defaults=[ns[n] for n in default_names],
2173 module=ns['__module__'])
Guido van Rossum95919c02017-01-22 17:47:20 -08002174 # update from user namespace without overriding special namedtuple attributes
2175 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002176 if key in _prohibited:
2177 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
2178 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08002179 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08002180 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002181
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002182
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002183def NamedTuple(typename, fields=None, /, **kwargs):
Guido van Rossum2f841442016-11-15 09:48:06 -08002184 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002185
Guido van Rossum2f841442016-11-15 09:48:06 -08002186 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002187
Guido van Rossum2f841442016-11-15 09:48:06 -08002188 class Employee(NamedTuple):
2189 name: str
2190 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002191
Guido van Rossum2f841442016-11-15 09:48:06 -08002192 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002193
Guido van Rossum2f841442016-11-15 09:48:06 -08002194 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002195
Raymond Hettingerf7b57df2019-03-18 09:53:56 -07002196 The resulting class has an extra __annotations__ attribute, giving a
2197 dict that maps field names to types. (The field names are also in
2198 the _fields attribute, which is part of the namedtuple API.)
2199 Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002200
Guido van Rossum2f841442016-11-15 09:48:06 -08002201 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002202
Guido van Rossum2f841442016-11-15 09:48:06 -08002203 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002204
Guido van Rossum2f841442016-11-15 09:48:06 -08002205 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
2206 """
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002207 if fields is None:
2208 fields = kwargs.items()
2209 elif kwargs:
2210 raise TypeError("Either list of fields or keywords"
2211 " can be provided to NamedTuple, not both")
2212 try:
2213 module = sys._getframe(1).f_globals.get('__name__', '__main__')
2214 except (AttributeError, ValueError):
2215 module = None
2216 return _make_nmtuple(typename, fields, module=module)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002217
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002218_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})
2219
2220def _namedtuple_mro_entries(bases):
2221 if len(bases) > 1:
2222 raise TypeError("Multiple inheritance with NamedTuple is not supported")
2223 assert bases[0] is NamedTuple
2224 return (_NamedTuple,)
2225
2226NamedTuple.__mro_entries__ = _namedtuple_mro_entries
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002227
2228
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002229class _TypedDictMeta(type):
2230 def __new__(cls, name, bases, ns, total=True):
2231 """Create new typed dict class object.
2232
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002233 This method is called when TypedDict is subclassed,
2234 or when TypedDict is instantiated. This way
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002235 TypedDict supports all three syntax forms described in its docstring.
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002236 Subclasses and instances of TypedDict return actual dictionaries.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002237 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002238 for base in bases:
2239 if type(base) is not _TypedDictMeta:
2240 raise TypeError('cannot inherit from both a TypedDict type '
2241 'and a non-TypedDict base class')
2242 tp_dict = type.__new__(_TypedDictMeta, name, (dict,), ns)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002243
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002244 annotations = {}
2245 own_annotations = ns.get('__annotations__', {})
2246 own_annotation_keys = set(own_annotations.keys())
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002247 msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002248 own_annotations = {
Miss Islington (bot)480f29f2021-07-17 01:48:17 -07002249 n: _type_check(tp, msg, module=tp_dict.__module__)
2250 for n, tp in own_annotations.items()
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002251 }
2252 required_keys = set()
2253 optional_keys = set()
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002254
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002255 for base in bases:
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002256 annotations.update(base.__dict__.get('__annotations__', {}))
2257 required_keys.update(base.__dict__.get('__required_keys__', ()))
2258 optional_keys.update(base.__dict__.get('__optional_keys__', ()))
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002259
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002260 annotations.update(own_annotations)
2261 if total:
2262 required_keys.update(own_annotation_keys)
2263 else:
2264 optional_keys.update(own_annotation_keys)
2265
2266 tp_dict.__annotations__ = annotations
2267 tp_dict.__required_keys__ = frozenset(required_keys)
2268 tp_dict.__optional_keys__ = frozenset(optional_keys)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002269 if not hasattr(tp_dict, '__total__'):
2270 tp_dict.__total__ = total
2271 return tp_dict
2272
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002273 __call__ = dict # static method
2274
2275 def __subclasscheck__(cls, other):
2276 # Typed dicts are only for static structural subtyping.
2277 raise TypeError('TypedDict does not support instance and class checks')
2278
2279 __instancecheck__ = __subclasscheck__
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002280
2281
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002282def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002283 """A simple typed namespace. At runtime it is equivalent to a plain dict.
2284
2285 TypedDict creates a dictionary type that expects all of its
2286 instances to have a certain set of keys, where each key is
2287 associated with a value of a consistent type. This expectation
2288 is not checked at runtime but is only enforced by type checkers.
2289 Usage::
2290
2291 class Point2D(TypedDict):
2292 x: int
2293 y: int
2294 label: str
2295
2296 a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
2297 b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
2298
2299 assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
2300
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002301 The type info can be accessed via the Point2D.__annotations__ dict, and
2302 the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
2303 TypedDict supports two additional equivalent forms::
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002304
2305 Point2D = TypedDict('Point2D', x=int, y=int, label=str)
2306 Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
2307
ananthan-123ab6423f2020-02-19 10:03:05 +05302308 By default, all keys must be present in a TypedDict. It is possible
2309 to override this by specifying totality.
2310 Usage::
2311
2312 class point2D(TypedDict, total=False):
2313 x: int
2314 y: int
2315
2316 This means that a point2D TypedDict can have any of the keys omitted.A type
2317 checker is only expected to support a literal False or True as the value of
2318 the total argument. True is the default, and makes all items defined in the
2319 class body be required.
2320
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002321 The class syntax is only supported in Python 3.6+, while two other
2322 syntax forms work for Python 2.7 and 3.2+
2323 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002324 if fields is None:
2325 fields = kwargs
2326 elif kwargs:
2327 raise TypeError("TypedDict takes either a dict or keyword arguments,"
2328 " but not both")
2329
Alex Grönholm67b769f2020-12-10 23:49:05 +02002330 ns = {'__annotations__': dict(fields)}
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002331 try:
2332 # Setting correct module is necessary to make typed dict classes pickleable.
2333 ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
2334 except (AttributeError, ValueError):
2335 pass
2336
Alex Grönholm67b769f2020-12-10 23:49:05 +02002337 return _TypedDictMeta(typename, (), ns, total=total)
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002338
2339_TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {})
2340TypedDict.__mro_entries__ = lambda bases: (_TypedDict,)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002341
2342
Guido van Rossum91185fe2016-06-08 11:19:11 -07002343def NewType(name, tp):
2344 """NewType creates simple unique types with almost zero
2345 runtime overhead. NewType(name, tp) is considered a subtype of tp
2346 by static type checkers. At runtime, NewType(name, tp) returns
2347 a dummy function that simply returns its argument. Usage::
2348
2349 UserId = NewType('UserId', int)
2350
2351 def name_by_id(user_id: UserId) -> str:
2352 ...
2353
2354 UserId('user') # Fails type check
2355
2356 name_by_id(42) # Fails type check
2357 name_by_id(UserId(42)) # OK
2358
2359 num = UserId(5) + 1 # type: int
2360 """
2361
2362 def new_type(x):
2363 return x
2364
2365 new_type.__name__ = name
2366 new_type.__supertype__ = tp
2367 return new_type
2368
2369
Guido van Rossum0e0563c2016-04-05 14:54:25 -07002370# Python-version-specific alias (Python 2: unicode; Python 3: str)
2371Text = str
2372
2373
Guido van Rossum91185fe2016-06-08 11:19:11 -07002374# Constant that's True when type checking, but False here.
2375TYPE_CHECKING = False
2376
2377
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002378class IO(Generic[AnyStr]):
2379 """Generic base class for TextIO and BinaryIO.
2380
2381 This is an abstract, generic version of the return of open().
2382
2383 NOTE: This does not distinguish between the different possible
2384 classes (text vs. binary, read vs. write vs. read/write,
2385 append-only, unbuffered). The TextIO and BinaryIO subclasses
2386 below capture the distinctions between text vs. binary, which is
2387 pervasive in the interface; however we currently do not offer a
2388 way to track the other distinctions in the type system.
2389 """
2390
Guido van Rossumd70fe632015-08-05 12:11:06 +02002391 __slots__ = ()
2392
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002393 @property
2394 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002395 def mode(self) -> str:
2396 pass
2397
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002398 @property
2399 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002400 def name(self) -> str:
2401 pass
2402
2403 @abstractmethod
2404 def close(self) -> None:
2405 pass
2406
Shantanu2e6569b2020-01-29 18:52:36 -08002407 @property
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002408 @abstractmethod
2409 def closed(self) -> bool:
2410 pass
2411
2412 @abstractmethod
2413 def fileno(self) -> int:
2414 pass
2415
2416 @abstractmethod
2417 def flush(self) -> None:
2418 pass
2419
2420 @abstractmethod
2421 def isatty(self) -> bool:
2422 pass
2423
2424 @abstractmethod
2425 def read(self, n: int = -1) -> AnyStr:
2426 pass
2427
2428 @abstractmethod
2429 def readable(self) -> bool:
2430 pass
2431
2432 @abstractmethod
2433 def readline(self, limit: int = -1) -> AnyStr:
2434 pass
2435
2436 @abstractmethod
2437 def readlines(self, hint: int = -1) -> List[AnyStr]:
2438 pass
2439
2440 @abstractmethod
2441 def seek(self, offset: int, whence: int = 0) -> int:
2442 pass
2443
2444 @abstractmethod
2445 def seekable(self) -> bool:
2446 pass
2447
2448 @abstractmethod
2449 def tell(self) -> int:
2450 pass
2451
2452 @abstractmethod
2453 def truncate(self, size: int = None) -> int:
2454 pass
2455
2456 @abstractmethod
2457 def writable(self) -> bool:
2458 pass
2459
2460 @abstractmethod
2461 def write(self, s: AnyStr) -> int:
2462 pass
2463
2464 @abstractmethod
2465 def writelines(self, lines: List[AnyStr]) -> None:
2466 pass
2467
2468 @abstractmethod
2469 def __enter__(self) -> 'IO[AnyStr]':
2470 pass
2471
2472 @abstractmethod
2473 def __exit__(self, type, value, traceback) -> None:
2474 pass
2475
2476
2477class BinaryIO(IO[bytes]):
2478 """Typed version of the return of open() in binary mode."""
2479
Guido van Rossumd70fe632015-08-05 12:11:06 +02002480 __slots__ = ()
2481
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002482 @abstractmethod
2483 def write(self, s: Union[bytes, bytearray]) -> int:
2484 pass
2485
2486 @abstractmethod
2487 def __enter__(self) -> 'BinaryIO':
2488 pass
2489
2490
2491class TextIO(IO[str]):
2492 """Typed version of the return of open() in text mode."""
2493
Guido van Rossumd70fe632015-08-05 12:11:06 +02002494 __slots__ = ()
2495
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002496 @property
2497 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002498 def buffer(self) -> BinaryIO:
2499 pass
2500
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002501 @property
2502 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002503 def encoding(self) -> str:
2504 pass
2505
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002506 @property
2507 @abstractmethod
Guido van Rossum991d14f2016-11-09 13:12:51 -08002508 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002509 pass
2510
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002511 @property
2512 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002513 def line_buffering(self) -> bool:
2514 pass
2515
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002516 @property
2517 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002518 def newlines(self) -> Any:
2519 pass
2520
2521 @abstractmethod
2522 def __enter__(self) -> 'TextIO':
2523 pass
2524
2525
2526class io:
2527 """Wrapper namespace for IO generic classes."""
2528
2529 __all__ = ['IO', 'TextIO', 'BinaryIO']
2530 IO = IO
2531 TextIO = TextIO
2532 BinaryIO = BinaryIO
2533
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002534
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002535io.__name__ = __name__ + '.io'
2536sys.modules[io.__name__] = io
2537
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002538Pattern = _alias(stdlib_re.Pattern, 1)
2539Match = _alias(stdlib_re.Match, 1)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002540
2541class re:
2542 """Wrapper namespace for re type aliases."""
2543
2544 __all__ = ['Pattern', 'Match']
2545 Pattern = Pattern
2546 Match = Match
2547
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002548
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002549re.__name__ = __name__ + '.re'
2550sys.modules[re.__name__] = re