blob: 5873d536a9ec443ea022c20d2ec98da32df7bf26 [file] [log] [blame]
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001"""
2The typing module: Support for gradual typing as defined by PEP 484.
3
4At large scale, the structure of the module is following:
Tim McNamara5265b3a2018-09-01 20:56:58 +12005* Imports and exports, all public names should be explicitly added to __all__.
Ivan Levkivskyid911e402018-01-20 11:23:59 +00006* Internal helper functions: these should never be used in code outside this module.
kj73607be2020-12-24 12:33:48 +08007* _SpecialForm and its instances (special forms):
8 Any, NoReturn, ClassVar, Union, Optional, Concatenate
9* Classes whose instances can be type arguments in addition to types:
10 ForwardRef, TypeVar and ParamSpec
Ivan Levkivskyid911e402018-01-20 11:23:59 +000011* The core of internal generics API: _GenericAlias and _VariadicGenericAlias, the latter is
12 currently only used by Tuple and Callable. All subscripted types like X[int], Union[int, str],
13 etc., are instances of either of these classes.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +010014* The public counterpart of the generics API consists of two classes: Generic and Protocol.
Ivan Levkivskyid911e402018-01-20 11:23:59 +000015* Public helper functions: get_type_hints, overload, cast, no_type_check,
16 no_type_check_decorator.
17* Generic aliases for collections.abc ABCs and few additional protocols.
ananthan-123ab6423f2020-02-19 10:03:05 +053018* Special types: NewType, NamedTuple, TypedDict.
Ivan Levkivskyid911e402018-01-20 11:23:59 +000019* Wrapper submodules for re and io related types.
20"""
21
HongWeipeng6ce03ec2019-09-27 15:54:26 +080022from abc import abstractmethod, ABCMeta
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070023import collections
Ivan Levkivskyid911e402018-01-20 11:23:59 +000024import collections.abc
Brett Cannonf3ad0422016-04-15 10:51:30 -070025import contextlib
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070026import functools
Serhiy Storchaka09f32212018-05-26 21:19:26 +030027import operator
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070028import re as stdlib_re # Avoid confusion with the re we export.
29import sys
30import types
Guido van Rossum48b069a2020-04-07 09:50:06 -070031from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType, GenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070032
33# Please keep __all__ alphabetized within each category.
34__all__ = [
35 # Super-special typing primitives.
Jakub Stasiakcf5b1092020-02-05 02:10:19 +010036 'Annotated',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070037 'Any',
38 'Callable',
Guido van Rossum0a6976d2016-09-11 15:34:56 -070039 'ClassVar',
kj73607be2020-12-24 12:33:48 +080040 'Concatenate',
Ivan Levkivskyif3672422019-05-26 09:37:07 +010041 'Final',
Anthony Sottiled30da5d2019-05-29 11:19:38 -070042 'ForwardRef',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070043 'Generic',
Ivan Levkivskyib891c462019-05-26 09:37:48 +010044 'Literal',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070045 'Optional',
kj73607be2020-12-24 12:33:48 +080046 'ParamSpec',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +010047 'Protocol',
Guido van Rossumeb9aca32016-05-24 16:38:22 -070048 'Tuple',
49 'Type',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070050 'TypeVar',
51 'Union',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070052
53 # ABCs (from collections.abc).
54 'AbstractSet', # collections.abc.Set.
55 'ByteString',
56 'Container',
Ivan Levkivskyi29fda8d2017-06-10 21:57:56 +020057 'ContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070058 'Hashable',
59 'ItemsView',
60 'Iterable',
61 'Iterator',
62 'KeysView',
63 'Mapping',
64 'MappingView',
65 'MutableMapping',
66 'MutableSequence',
67 'MutableSet',
68 'Sequence',
69 'Sized',
70 'ValuesView',
Ivan Levkivskyid911e402018-01-20 11:23:59 +000071 'Awaitable',
72 'AsyncIterator',
73 'AsyncIterable',
74 'Coroutine',
75 'Collection',
76 'AsyncGenerator',
77 'AsyncContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070078
79 # Structural checks, a.k.a. protocols.
80 'Reversible',
81 'SupportsAbs',
Ivan Levkivskyif06e0212017-05-02 19:14:07 +020082 'SupportsBytes',
83 'SupportsComplex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070084 'SupportsFloat',
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -070085 'SupportsIndex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070086 'SupportsInt',
87 'SupportsRound',
88
89 # Concrete collection types.
Anthony Sottiled30da5d2019-05-29 11:19:38 -070090 'ChainMap',
Ivan Levkivskyib692dc82017-02-13 22:50:14 +010091 'Counter',
Raymond Hettinger80490522017-01-16 22:42:37 -080092 'Deque',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070093 'Dict',
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070094 'DefaultDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070095 'List',
Anthony Sottiled30da5d2019-05-29 11:19:38 -070096 'OrderedDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070097 'Set',
Guido van Rossumefa798d2016-08-23 11:01:50 -070098 'FrozenSet',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070099 'NamedTuple', # Not really a type.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +0100100 'TypedDict', # Not really a type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700101 'Generator',
Miss Islington (bot)e1bcc882021-05-04 02:51:33 -0700102
103 # Other concrete types.
104 'BinaryIO',
105 'IO',
106 'Match',
107 'Pattern',
108 'TextIO',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700109
110 # One-off things.
111 'AnyStr',
112 'cast',
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100113 'final',
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +0100114 'get_args',
115 'get_origin',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700116 'get_type_hints',
Patrick Reader0705ec82020-09-16 05:58:32 +0100117 'is_typeddict',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700118 'NewType',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700119 'no_type_check',
120 'no_type_check_decorator',
aetracht45738202018-03-19 14:41:32 -0400121 'NoReturn',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700122 'overload',
Jelle Zijlstra52243362021-04-10 19:57:05 -0700123 'ParamSpecArgs',
124 'ParamSpecKwargs',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100125 'runtime_checkable',
Guido van Rossum0e0563c2016-04-05 14:54:25 -0700126 'Text',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700127 'TYPE_CHECKING',
Mikhail Golubev4f3c2502020-10-08 00:44:31 +0300128 'TypeAlias',
Ken Jin05ab4b62021-04-27 22:31:04 +0800129 'TypeGuard',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700130]
131
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700132# The pseudo-submodules 're' and 'io' are part of the public
133# namespace, but excluded from __all__ because they might stomp on
134# legitimate imports of those modules.
135
kj463c7d32020-12-14 02:38:24 +0800136
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700137def _type_convert(arg, module=None):
kj463c7d32020-12-14 02:38:24 +0800138 """For converting None to type(None), and strings to ForwardRef."""
139 if arg is None:
140 return type(None)
141 if isinstance(arg, str):
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700142 return ForwardRef(arg, module=module)
kj463c7d32020-12-14 02:38:24 +0800143 return arg
144
145
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700146def _type_check(arg, msg, is_argument=True, module=None):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000147 """Check that the argument is a type, and return it (internal helper).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700148
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000149 As a special case, accept None and return type(None) instead. Also wrap strings
150 into ForwardRef instances. Consider several corner cases, for example plain
151 special forms like Union are not valid, while Union[int, str] is OK, etc.
152 The msg argument is a human-readable error message, e.g::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700153
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000154 "Union[arg, ...]: arg should be a type."
Guido van Rossum4cefe742016-09-27 15:20:12 -0700155
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000156 We append the repr() of the actual value (truncated to 100 chars).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700157 """
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100158 invalid_generic_forms = (Generic, Protocol)
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700159 if is_argument:
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100160 invalid_generic_forms = invalid_generic_forms + (ClassVar, Final)
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400161
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700162 arg = _type_convert(arg, module=module)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000163 if (isinstance(arg, _GenericAlias) and
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400164 arg.__origin__ in invalid_generic_forms):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000165 raise TypeError(f"{arg} is not valid as type argument")
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300166 if arg in (Any, NoReturn):
167 return arg
168 if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000169 raise TypeError(f"Plain {arg} is not valid as type argument")
Miss Islington (bot)8a37e8c2021-07-26 12:02:58 -0700170 if isinstance(arg, (type, TypeVar, ForwardRef, types.UnionType, ParamSpec)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000171 return arg
172 if not callable(arg):
173 raise TypeError(f"{msg} Got {arg!r:.100}.")
174 return arg
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700175
176
Miss Islington (bot)536e35a2021-08-04 13:36:01 -0700177def _is_param_expr(arg):
178 return arg is ... or isinstance(arg,
179 (tuple, list, ParamSpec, _ConcatenateGenericAlias))
180
181
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000182def _type_repr(obj):
183 """Return the repr() of an object, special-casing types (internal helper).
184
185 If obj is a type, we return a shorter version than the default
186 type.__repr__, based on the module and qualified name, which is
187 typically enough to uniquely identify a type. For everything
188 else, we fall back on repr(obj).
189 """
kj1f7dfb22020-11-02 02:13:38 +0800190 if isinstance(obj, types.GenericAlias):
191 return repr(obj)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000192 if isinstance(obj, type):
193 if obj.__module__ == 'builtins':
194 return obj.__qualname__
195 return f'{obj.__module__}.{obj.__qualname__}'
196 if obj is ...:
197 return('...')
198 if isinstance(obj, types.FunctionType):
199 return obj.__name__
200 return repr(obj)
201
202
Ken Jina2721642021-07-19 22:22:59 +0800203def _collect_type_vars(types_, typevar_types=None):
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700204 """Collect all type variable contained
kj73607be2020-12-24 12:33:48 +0800205 in types in order of first appearance (lexicographic order). For example::
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000206
207 _collect_type_vars((T, List[S, T])) == (T, S)
208 """
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700209 if typevar_types is None:
210 typevar_types = TypeVar
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000211 tvars = []
Ken Jina2721642021-07-19 22:22:59 +0800212 for t in types_:
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700213 if isinstance(t, typevar_types) and t not in tvars:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000214 tvars.append(t)
Miss Islington (bot)8a37e8c2021-07-26 12:02:58 -0700215 if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000216 tvars.extend([t for t in t.__parameters__ if t not in tvars])
217 return tuple(tvars)
218
219
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300220def _check_generic(cls, parameters, elen):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000221 """Check correct count for parameters of a generic cls (internal helper).
222 This gives a nice error message in case of count mismatch.
223 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300224 if not elen:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000225 raise TypeError(f"{cls} is not a generic class")
226 alen = len(parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000227 if alen != elen:
Miss Islington (bot)c8db2922021-08-02 00:08:24 -0700228 raise TypeError(f"Too {'many' if alen > elen else 'few'} arguments for {cls};"
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000229 f" actual {alen}, expected {elen}")
230
kj73607be2020-12-24 12:33:48 +0800231def _prepare_paramspec_params(cls, params):
232 """Prepares the parameters for a Generic containing ParamSpec
233 variables (internal helper).
234 """
235 # Special case where Z[[int, str, bool]] == Z[int, str, bool] in PEP 612.
Miss Islington (bot)536e35a2021-08-04 13:36:01 -0700236 if (len(cls.__parameters__) == 1
237 and params and not _is_param_expr(params[0])):
238 assert isinstance(cls.__parameters__[0], ParamSpec)
kj73607be2020-12-24 12:33:48 +0800239 return (params,)
240 else:
Miss Islington (bot)c8db2922021-08-02 00:08:24 -0700241 _check_generic(cls, params, len(cls.__parameters__))
kj73607be2020-12-24 12:33:48 +0800242 _params = []
243 # Convert lists to tuples to help other libraries cache the results.
244 for p, tvar in zip(params, cls.__parameters__):
245 if isinstance(tvar, ParamSpec) and isinstance(p, list):
246 p = tuple(p)
247 _params.append(p)
248 return tuple(_params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000249
Yurii Karabasf03d3182020-11-17 04:23:19 +0200250def _deduplicate(params):
251 # Weed out strict duplicates, preserving the first of each occurrence.
252 all_params = set(params)
253 if len(all_params) < len(params):
254 new_params = []
255 for t in params:
256 if t in all_params:
257 new_params.append(t)
258 all_params.remove(t)
259 params = new_params
260 assert not all_params, all_params
261 return params
262
263
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000264def _remove_dups_flatten(parameters):
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700265 """An internal helper for Union creation and substitution: flatten Unions
266 among parameters, then remove duplicates.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000267 """
268 # Flatten out Union[Union[...], ...].
269 params = []
270 for p in parameters:
Miss Islington (bot)8a37e8c2021-07-26 12:02:58 -0700271 if isinstance(p, (_UnionGenericAlias, types.UnionType)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000272 params.extend(p.__args__)
273 elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
274 params.extend(p[1:])
275 else:
276 params.append(p)
Yurii Karabasf03d3182020-11-17 04:23:19 +0200277
278 return tuple(_deduplicate(params))
279
280
281def _flatten_literal_params(parameters):
282 """An internal helper for Literal creation: flatten Literals among parameters"""
283 params = []
284 for p in parameters:
285 if isinstance(p, _LiteralGenericAlias):
286 params.extend(p.__args__)
287 else:
288 params.append(p)
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700289 return tuple(params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000290
291
292_cleanups = []
293
294
Yurii Karabasf03d3182020-11-17 04:23:19 +0200295def _tp_cache(func=None, /, *, typed=False):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000296 """Internal wrapper caching __getitem__ of generic types with a fallback to
297 original function for non-hashable arguments.
298 """
Yurii Karabasf03d3182020-11-17 04:23:19 +0200299 def decorator(func):
300 cached = functools.lru_cache(typed=typed)(func)
301 _cleanups.append(cached.cache_clear)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000302
Yurii Karabasf03d3182020-11-17 04:23:19 +0200303 @functools.wraps(func)
304 def inner(*args, **kwds):
305 try:
306 return cached(*args, **kwds)
307 except TypeError:
308 pass # All real errors (not unhashable args) are raised below.
309 return func(*args, **kwds)
310 return inner
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000311
Yurii Karabasf03d3182020-11-17 04:23:19 +0200312 if func is not None:
313 return decorator(func)
314
315 return decorator
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000316
wyfo653f4202020-07-22 21:47:28 +0200317def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
Graham Bleaney84ef33c2020-09-08 18:41:10 -0400318 """Evaluate all forward references in the given type t.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000319 For use of globalns and localns see the docstring for get_type_hints().
wyfo653f4202020-07-22 21:47:28 +0200320 recursive_guard is used to prevent prevent infinite recursion
321 with recursive ForwardRef.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000322 """
323 if isinstance(t, ForwardRef):
wyfo653f4202020-07-22 21:47:28 +0200324 return t._evaluate(globalns, localns, recursive_guard)
Miss Islington (bot)8a37e8c2021-07-26 12:02:58 -0700325 if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):
wyfo653f4202020-07-22 21:47:28 +0200326 ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000327 if ev_args == t.__args__:
328 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300329 if isinstance(t, GenericAlias):
330 return GenericAlias(t.__origin__, ev_args)
Miss Islington (bot)8a37e8c2021-07-26 12:02:58 -0700331 if isinstance(t, types.UnionType):
Miss Islington (bot)0aea99e2021-07-24 12:35:33 -0700332 return functools.reduce(operator.or_, ev_args)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300333 else:
334 return t.copy_with(ev_args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000335 return t
336
337
338class _Final:
339 """Mixin to prohibit subclassing"""
Guido van Rossum4cefe742016-09-27 15:20:12 -0700340
Guido van Rossum83ec3022017-01-17 20:43:28 -0800341 __slots__ = ('__weakref__',)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700342
Serhiy Storchaka2085bd02019-06-01 11:00:15 +0300343 def __init_subclass__(self, /, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000344 if '_root' not in kwds:
345 raise TypeError("Cannot subclass special typing classes")
346
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100347class _Immutable:
348 """Mixin to indicate that object should not be copied."""
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300349 __slots__ = ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000350
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100351 def __copy__(self):
352 return self
353
354 def __deepcopy__(self, memo):
355 return self
356
357
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300358# Internal indicator of special typing constructs.
359# See __doc__ instance attribute for specific docs.
360class _SpecialForm(_Final, _root=True):
361 __slots__ = ('_name', '__doc__', '_getitem')
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000362
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300363 def __init__(self, getitem):
364 self._getitem = getitem
365 self._name = getitem.__name__
366 self.__doc__ = getitem.__doc__
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000367
Miss Islington (bot)c895f2b2021-07-19 10:57:27 -0700368 def __getattr__(self, item):
369 if item in {'__name__', '__qualname__'}:
370 return self._name
371
372 raise AttributeError(item)
373
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300374 def __mro_entries__(self, bases):
375 raise TypeError(f"Cannot subclass {self!r}")
Guido van Rossum4cefe742016-09-27 15:20:12 -0700376
377 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000378 return 'typing.' + self._name
379
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100380 def __reduce__(self):
381 return self._name
Guido van Rossum4cefe742016-09-27 15:20:12 -0700382
383 def __call__(self, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000384 raise TypeError(f"Cannot instantiate {self!r}")
385
Ken Jinca5a4cf2021-07-24 22:49:25 +0800386 def __or__(self, other):
387 return Union[self, other]
388
389 def __ror__(self, other):
390 return Union[other, self]
391
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000392 def __instancecheck__(self, obj):
393 raise TypeError(f"{self} cannot be used with isinstance()")
394
395 def __subclasscheck__(self, cls):
396 raise TypeError(f"{self} cannot be used with issubclass()")
397
398 @_tp_cache
399 def __getitem__(self, parameters):
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300400 return self._getitem(self, parameters)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700401
Yurii Karabasf03d3182020-11-17 04:23:19 +0200402
403class _LiteralSpecialForm(_SpecialForm, _root=True):
404 @_tp_cache(typed=True)
405 def __getitem__(self, parameters):
406 return self._getitem(self, parameters)
407
408
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300409@_SpecialForm
410def Any(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000411 """Special type indicating an unconstrained type.
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700412
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000413 - Any is compatible with every type.
414 - Any assumed to have all methods.
415 - All values assumed to be instances of Any.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700416
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000417 Note that all the above statements are true from the point of view of
418 static type checkers. At runtime, Any should not be used with instance
419 or class checks.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300420 """
421 raise TypeError(f"{self} is not subscriptable")
Guido van Rossumd70fe632015-08-05 12:11:06 +0200422
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300423@_SpecialForm
424def NoReturn(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000425 """Special type indicating functions that never return.
426 Example::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700427
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000428 from typing import NoReturn
429
430 def stop() -> NoReturn:
431 raise Exception('no way')
432
433 This type is invalid in other positions, e.g., ``List[NoReturn]``
434 will fail in static type checkers.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300435 """
436 raise TypeError(f"{self} is not subscriptable")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000437
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300438@_SpecialForm
439def ClassVar(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000440 """Special type construct to mark class variables.
441
442 An annotation wrapped in ClassVar indicates that a given
443 attribute is intended to be used as a class variable and
444 should not be set on instances of that class. Usage::
445
446 class Starship:
447 stats: ClassVar[Dict[str, int]] = {} # class variable
448 damage: int = 10 # instance variable
449
450 ClassVar accepts only types and cannot be further subscribed.
451
452 Note that ClassVar is not a class itself, and should not
453 be used with isinstance() or issubclass().
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300454 """
455 item = _type_check(parameters, f'{self} accepts only single type.')
Miss Islington (bot)5bd27c32021-08-21 02:33:14 -0700456 return _GenericAlias(self, (item,))
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000457
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300458@_SpecialForm
459def Final(self, parameters):
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100460 """Special typing construct to indicate final names to type checkers.
461
462 A final name cannot be re-assigned or overridden in a subclass.
463 For example:
464
465 MAX_SIZE: Final = 9000
466 MAX_SIZE += 1 # Error reported by type checker
467
468 class Connection:
469 TIMEOUT: Final[int] = 10
470
471 class FastConnector(Connection):
472 TIMEOUT = 1 # Error reported by type checker
473
474 There is no runtime checking of these properties.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300475 """
476 item = _type_check(parameters, f'{self} accepts only single type.')
Miss Islington (bot)5bd27c32021-08-21 02:33:14 -0700477 return _GenericAlias(self, (item,))
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100478
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300479@_SpecialForm
480def Union(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000481 """Union type; Union[X, Y] means either X or Y.
482
483 To define a union, use e.g. Union[int, str]. Details:
484 - The arguments must be types and there must be at least one.
485 - None as an argument is a special case and is replaced by
486 type(None).
487 - Unions of unions are flattened, e.g.::
488
489 Union[Union[int, str], float] == Union[int, str, float]
490
491 - Unions of a single argument vanish, e.g.::
492
493 Union[int] == int # The constructor actually returns int
494
495 - Redundant arguments are skipped, e.g.::
496
497 Union[int, str, int] == Union[int, str]
498
499 - When comparing unions, the argument order is ignored, e.g.::
500
501 Union[int, str] == Union[str, int]
502
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000503 - You cannot subclass or instantiate a union.
504 - You can use Optional[X] as a shorthand for Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300505 """
506 if parameters == ():
507 raise TypeError("Cannot take a Union of no types.")
508 if not isinstance(parameters, tuple):
509 parameters = (parameters,)
510 msg = "Union[arg, ...]: each arg must be a type."
511 parameters = tuple(_type_check(p, msg) for p in parameters)
512 parameters = _remove_dups_flatten(parameters)
513 if len(parameters) == 1:
514 return parameters[0]
Miss Islington (bot)36a24972021-08-06 10:08:27 -0700515 if len(parameters) == 2 and type(None) in parameters:
Miss Islington (bot)5bd27c32021-08-21 02:33:14 -0700516 return _UnionGenericAlias(self, parameters, name="Optional")
517 return _UnionGenericAlias(self, parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000518
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300519@_SpecialForm
520def Optional(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000521 """Optional type.
522
523 Optional[X] is equivalent to Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300524 """
525 arg = _type_check(parameters, f"{self} requires a single type.")
526 return Union[arg, type(None)]
Guido van Rossumb7dedc82016-10-29 12:44:29 -0700527
Yurii Karabasf03d3182020-11-17 04:23:19 +0200528@_LiteralSpecialForm
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300529def Literal(self, parameters):
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100530 """Special typing form to define literal types (a.k.a. value types).
531
532 This form can be used to indicate to type checkers that the corresponding
533 variable or function parameter has a value equivalent to the provided
534 literal (or one of several literals):
535
536 def validate_simple(data: Any) -> Literal[True]: # always returns True
537 ...
538
539 MODE = Literal['r', 'rb', 'w', 'wb']
540 def open_helper(file: str, mode: MODE) -> str:
541 ...
542
543 open_helper('/some/path', 'r') # Passes type check
544 open_helper('/other/path', 'typo') # Error in type checker
545
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300546 Literal[...] cannot be subclassed. At runtime, an arbitrary value
547 is allowed as type argument to Literal[...], but type checkers may
548 impose restrictions.
549 """
550 # There is no '_type_check' call because arguments to Literal[...] are
551 # values, not types.
Yurii Karabasf03d3182020-11-17 04:23:19 +0200552 if not isinstance(parameters, tuple):
553 parameters = (parameters,)
554
555 parameters = _flatten_literal_params(parameters)
556
557 try:
558 parameters = tuple(p for p, _ in _deduplicate(list(_value_and_type_iter(parameters))))
559 except TypeError: # unhashable parameters
560 pass
561
Miss Islington (bot)5bd27c32021-08-21 02:33:14 -0700562 return _LiteralGenericAlias(self, parameters)
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100563
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700564
Mikhail Golubev4f3c2502020-10-08 00:44:31 +0300565@_SpecialForm
566def TypeAlias(self, parameters):
567 """Special marker indicating that an assignment should
568 be recognized as a proper type alias definition by type
569 checkers.
570
571 For example::
572
573 Predicate: TypeAlias = Callable[..., bool]
574
575 It's invalid when used anywhere except as in the example above.
576 """
577 raise TypeError(f"{self} is not subscriptable")
578
579
kj73607be2020-12-24 12:33:48 +0800580@_SpecialForm
581def Concatenate(self, parameters):
Ken Jin11276cd2021-01-02 08:45:50 +0800582 """Used in conjunction with ``ParamSpec`` and ``Callable`` to represent a
583 higher order function which adds, removes or transforms parameters of a
584 callable.
kj73607be2020-12-24 12:33:48 +0800585
586 For example::
587
588 Callable[Concatenate[int, P], int]
589
590 See PEP 612 for detailed information.
591 """
592 if parameters == ():
593 raise TypeError("Cannot take a Concatenate of no types.")
594 if not isinstance(parameters, tuple):
595 parameters = (parameters,)
596 if not isinstance(parameters[-1], ParamSpec):
597 raise TypeError("The last parameter to Concatenate should be a "
598 "ParamSpec variable.")
599 msg = "Concatenate[arg, ...]: each arg must be a type."
600 parameters = tuple(_type_check(p, msg) for p in parameters)
Miss Islington (bot)5bd27c32021-08-21 02:33:14 -0700601 return _ConcatenateGenericAlias(self, parameters)
kj73607be2020-12-24 12:33:48 +0800602
603
Ken Jin05ab4b62021-04-27 22:31:04 +0800604@_SpecialForm
605def TypeGuard(self, parameters):
606 """Special typing form used to annotate the return type of a user-defined
607 type guard function. ``TypeGuard`` only accepts a single type argument.
608 At runtime, functions marked this way should return a boolean.
609
610 ``TypeGuard`` aims to benefit *type narrowing* -- a technique used by static
611 type checkers to determine a more precise type of an expression within a
612 program's code flow. Usually type narrowing is done by analyzing
613 conditional code flow and applying the narrowing to a block of code. The
614 conditional expression here is sometimes referred to as a "type guard".
615
616 Sometimes it would be convenient to use a user-defined boolean function
617 as a type guard. Such a function should use ``TypeGuard[...]`` as its
618 return type to alert static type checkers to this intention.
619
620 Using ``-> TypeGuard`` tells the static type checker that for a given
621 function:
622
623 1. The return value is a boolean.
624 2. If the return value is ``True``, the type of its argument
625 is the type inside ``TypeGuard``.
626
627 For example::
628
629 def is_str(val: Union[str, float]):
630 # "isinstance" type guard
631 if isinstance(val, str):
632 # Type of ``val`` is narrowed to ``str``
633 ...
634 else:
635 # Else, type of ``val`` is narrowed to ``float``.
636 ...
637
638 Strict type narrowing is not enforced -- ``TypeB`` need not be a narrower
639 form of ``TypeA`` (it can even be a wider form) and this may lead to
640 type-unsafe results. The main reason is to allow for things like
641 narrowing ``List[object]`` to ``List[str]`` even though the latter is not
642 a subtype of the former, since ``List`` is invariant. The responsibility of
643 writing type-safe type guards is left to the user.
644
645 ``TypeGuard`` also works with type variables. For more information, see
646 PEP 647 (User-Defined Type Guards).
647 """
648 item = _type_check(parameters, f'{self} accepts only single type.')
Miss Islington (bot)5bd27c32021-08-21 02:33:14 -0700649 return _GenericAlias(self, (item,))
Ken Jin05ab4b62021-04-27 22:31:04 +0800650
651
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000652class ForwardRef(_Final, _root=True):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800653 """Internal wrapper to hold a forward reference."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700654
Guido van Rossum4cefe742016-09-27 15:20:12 -0700655 __slots__ = ('__forward_arg__', '__forward_code__',
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400656 '__forward_evaluated__', '__forward_value__',
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700657 '__forward_is_argument__', '__forward_module__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700658
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700659 def __init__(self, arg, is_argument=True, module=None):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700660 if not isinstance(arg, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000661 raise TypeError(f"Forward reference must be a string -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700662 try:
663 code = compile(arg, '<string>', 'eval')
664 except SyntaxError:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000665 raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700666 self.__forward_arg__ = arg
667 self.__forward_code__ = code
668 self.__forward_evaluated__ = False
669 self.__forward_value__ = None
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400670 self.__forward_is_argument__ = is_argument
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700671 self.__forward_module__ = module
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700672
wyfo653f4202020-07-22 21:47:28 +0200673 def _evaluate(self, globalns, localns, recursive_guard):
674 if self.__forward_arg__ in recursive_guard:
675 return self
Guido van Rossumdad17902016-11-10 08:29:18 -0800676 if not self.__forward_evaluated__ or localns is not globalns:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700677 if globalns is None and localns is None:
678 globalns = localns = {}
679 elif globalns is None:
680 globalns = localns
681 elif localns is None:
682 localns = globalns
Miss Islington (bot)480f29f2021-07-17 01:48:17 -0700683 if self.__forward_module__ is not None:
684 globalns = getattr(
685 sys.modules.get(self.__forward_module__, None), '__dict__', globalns
686 )
wyfo653f4202020-07-22 21:47:28 +0200687 type_ =_type_check(
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700688 eval(self.__forward_code__, globalns, localns),
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400689 "Forward references must evaluate to types.",
wyfo653f4202020-07-22 21:47:28 +0200690 is_argument=self.__forward_is_argument__,
691 )
692 self.__forward_value__ = _eval_type(
693 type_, globalns, localns, recursive_guard | {self.__forward_arg__}
694 )
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700695 self.__forward_evaluated__ = True
696 return self.__forward_value__
697
Guido van Rossum4cefe742016-09-27 15:20:12 -0700698 def __eq__(self, other):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000699 if not isinstance(other, ForwardRef):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700700 return NotImplemented
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100701 if self.__forward_evaluated__ and other.__forward_evaluated__:
702 return (self.__forward_arg__ == other.__forward_arg__ and
703 self.__forward_value__ == other.__forward_value__)
704 return self.__forward_arg__ == other.__forward_arg__
Guido van Rossum4cefe742016-09-27 15:20:12 -0700705
706 def __hash__(self):
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100707 return hash(self.__forward_arg__)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700708
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700709 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000710 return f'ForwardRef({self.__forward_arg__!r})'
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700711
kj73607be2020-12-24 12:33:48 +0800712class _TypeVarLike:
713 """Mixin for TypeVar-like types (TypeVar and ParamSpec)."""
714 def __init__(self, bound, covariant, contravariant):
715 """Used to setup TypeVars and ParamSpec's bound, covariant and
716 contravariant attributes.
717 """
718 if covariant and contravariant:
719 raise ValueError("Bivariant types are not supported.")
720 self.__covariant__ = bool(covariant)
721 self.__contravariant__ = bool(contravariant)
722 if bound:
723 self.__bound__ = _type_check(bound, "Bound must be a type.")
724 else:
725 self.__bound__ = None
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700726
kj73607be2020-12-24 12:33:48 +0800727 def __or__(self, right):
728 return Union[self, right]
729
Jelle Zijlstra90459192021-04-10 20:00:05 -0700730 def __ror__(self, left):
731 return Union[left, self]
kj73607be2020-12-24 12:33:48 +0800732
733 def __repr__(self):
734 if self.__covariant__:
735 prefix = '+'
736 elif self.__contravariant__:
737 prefix = '-'
738 else:
739 prefix = '~'
740 return prefix + self.__name__
741
742 def __reduce__(self):
743 return self.__name__
744
745
746class TypeVar( _Final, _Immutable, _TypeVarLike, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700747 """Type variable.
748
749 Usage::
750
751 T = TypeVar('T') # Can be anything
752 A = TypeVar('A', str, bytes) # Must be str or bytes
753
754 Type variables exist primarily for the benefit of static type
755 checkers. They serve as the parameters for generic types as well
756 as for generic function definitions. See class Generic for more
757 information on generic types. Generic functions work as follows:
758
Guido van Rossumb24569a2016-11-20 18:01:29 -0800759 def repeat(x: T, n: int) -> List[T]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700760 '''Return a list containing n references to x.'''
761 return [x]*n
762
763 def longest(x: A, y: A) -> A:
764 '''Return the longest of two strings.'''
765 return x if len(x) >= len(y) else y
766
767 The latter example's signature is essentially the overloading
768 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
769 that if the arguments are instances of some subclass of str,
770 the return type is still plain str.
771
Guido van Rossumb24569a2016-11-20 18:01:29 -0800772 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700773
Guido van Rossumefa798d2016-08-23 11:01:50 -0700774 Type variables defined with covariant=True or contravariant=True
João D. Ferreira86bfed32018-07-07 16:41:20 +0100775 can be used to declare covariant or contravariant generic types.
Guido van Rossumefa798d2016-08-23 11:01:50 -0700776 See PEP 484 for more details. By default generic types are invariant
777 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700778
779 Type variables can be introspected. e.g.:
780
781 T.__name__ == 'T'
782 T.__constraints__ == ()
783 T.__covariant__ == False
784 T.__contravariant__ = False
785 A.__constraints__ == (str, bytes)
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100786
787 Note that only type variables defined in global scope can be pickled.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700788 """
789
Guido van Rossum4cefe742016-09-27 15:20:12 -0700790 __slots__ = ('__name__', '__bound__', '__constraints__',
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300791 '__covariant__', '__contravariant__', '__dict__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700792
793 def __init__(self, name, *constraints, bound=None,
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800794 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700795 self.__name__ = name
kj73607be2020-12-24 12:33:48 +0800796 super().__init__(bound, covariant, contravariant)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700797 if constraints and bound is not None:
798 raise TypeError("Constraints cannot be combined with bound=...")
799 if constraints and len(constraints) == 1:
800 raise TypeError("A single constraint is not allowed")
801 msg = "TypeVar(name, constraint, ...): constraints must be types."
802 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
HongWeipenga25a04f2020-04-21 04:01:53 +0800803 try:
804 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling
805 except (AttributeError, ValueError):
806 def_mod = None
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300807 if def_mod != 'typing':
808 self.__module__ = def_mod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700809
Maggie Moss1b4552c2020-09-09 13:23:24 -0700810
Jelle Zijlstra52243362021-04-10 19:57:05 -0700811class ParamSpecArgs(_Final, _Immutable, _root=True):
812 """The args for a ParamSpec object.
813
814 Given a ParamSpec object P, P.args is an instance of ParamSpecArgs.
815
816 ParamSpecArgs objects have a reference back to their ParamSpec:
817
818 P.args.__origin__ is P
819
820 This type is meant for runtime introspection and has no special meaning to
821 static type checkers.
822 """
823 def __init__(self, origin):
824 self.__origin__ = origin
825
826 def __repr__(self):
827 return f"{self.__origin__.__name__}.args"
828
829
830class ParamSpecKwargs(_Final, _Immutable, _root=True):
831 """The kwargs for a ParamSpec object.
832
833 Given a ParamSpec object P, P.kwargs is an instance of ParamSpecKwargs.
834
835 ParamSpecKwargs objects have a reference back to their ParamSpec:
836
837 P.kwargs.__origin__ is P
838
839 This type is meant for runtime introspection and has no special meaning to
840 static type checkers.
841 """
842 def __init__(self, origin):
843 self.__origin__ = origin
844
845 def __repr__(self):
846 return f"{self.__origin__.__name__}.kwargs"
847
848
kj73607be2020-12-24 12:33:48 +0800849class ParamSpec(_Final, _Immutable, _TypeVarLike, _root=True):
850 """Parameter specification variable.
Maggie Moss1b4552c2020-09-09 13:23:24 -0700851
kj73607be2020-12-24 12:33:48 +0800852 Usage::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700853
kj73607be2020-12-24 12:33:48 +0800854 P = ParamSpec('P')
855
856 Parameter specification variables exist primarily for the benefit of static
857 type checkers. They are used to forward the parameter types of one
Ken Jin11276cd2021-01-02 08:45:50 +0800858 callable to another callable, a pattern commonly found in higher order
859 functions and decorators. They are only valid when used in ``Concatenate``,
860 or s the first argument to ``Callable``, or as parameters for user-defined
861 Generics. See class Generic for more information on generic types. An
862 example for annotating a decorator::
kj73607be2020-12-24 12:33:48 +0800863
864 T = TypeVar('T')
865 P = ParamSpec('P')
866
867 def add_logging(f: Callable[P, T]) -> Callable[P, T]:
868 '''A type-safe decorator to add logging to a function.'''
869 def inner(*args: P.args, **kwargs: P.kwargs) -> T:
870 logging.info(f'{f.__name__} was called')
871 return f(*args, **kwargs)
872 return inner
873
874 @add_logging
875 def add_two(x: float, y: float) -> float:
876 '''Add two numbers together.'''
877 return x + y
878
879 Parameter specification variables defined with covariant=True or
880 contravariant=True can be used to declare covariant or contravariant
881 generic types. These keyword arguments are valid, but their actual semantics
882 are yet to be decided. See PEP 612 for details.
883
884 Parameter specification variables can be introspected. e.g.:
885
886 P.__name__ == 'T'
887 P.__bound__ == None
888 P.__covariant__ == False
889 P.__contravariant__ == False
890
891 Note that only parameter specification variables defined in global scope can
892 be pickled.
893 """
894
895 __slots__ = ('__name__', '__bound__', '__covariant__', '__contravariant__',
896 '__dict__')
897
Jelle Zijlstra52243362021-04-10 19:57:05 -0700898 @property
899 def args(self):
900 return ParamSpecArgs(self)
901
902 @property
903 def kwargs(self):
904 return ParamSpecKwargs(self)
kj73607be2020-12-24 12:33:48 +0800905
Ken Jinace008c2021-01-11 08:11:41 +0800906 def __init__(self, name, *, bound=None, covariant=False, contravariant=False):
kj73607be2020-12-24 12:33:48 +0800907 self.__name__ = name
908 super().__init__(bound, covariant, contravariant)
909 try:
910 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__')
911 except (AttributeError, ValueError):
912 def_mod = None
913 if def_mod != 'typing':
914 self.__module__ = def_mod
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100915
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700916
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000917def _is_dunder(attr):
918 return attr.startswith('__') and attr.endswith('__')
Guido van Rossum83ec3022017-01-17 20:43:28 -0800919
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300920class _BaseGenericAlias(_Final, _root=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000921 """The central part of internal API.
922
923 This represents a generic version of type 'origin' with type arguments 'params'.
924 There are two kind of these aliases: user defined and special. The special ones
925 are wrappers around builtin collections and ABCs in collections.abc. These must
926 have 'name' always set. If 'inst' is False, then the alias can't be instantiated,
927 this is used by e.g. typing.List and typing.Dict.
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700928 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300929 def __init__(self, origin, *, inst=True, name=None):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000930 self._inst = inst
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000931 self._name = name
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700932 self.__origin__ = origin
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000933 self.__slots__ = None # This is not documented.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300934
935 def __call__(self, *args, **kwargs):
936 if not self._inst:
937 raise TypeError(f"Type {self._name} cannot be instantiated; "
938 f"use {self.__origin__.__name__}() instead")
939 result = self.__origin__(*args, **kwargs)
940 try:
941 result.__orig_class__ = self
942 except AttributeError:
943 pass
944 return result
945
946 def __mro_entries__(self, bases):
947 res = []
948 if self.__origin__ not in bases:
949 res.append(self.__origin__)
950 i = bases.index(self)
951 for b in bases[i+1:]:
952 if isinstance(b, _BaseGenericAlias) or issubclass(b, Generic):
953 break
954 else:
955 res.append(Generic)
956 return tuple(res)
957
958 def __getattr__(self, attr):
Miss Islington (bot)c895f2b2021-07-19 10:57:27 -0700959 if attr in {'__name__', '__qualname__'}:
Miss Islington (bot)5bd27c32021-08-21 02:33:14 -0700960 return self._name or self.__origin__.__name__
Miss Islington (bot)c895f2b2021-07-19 10:57:27 -0700961
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300962 # We are careful for copy and pickle.
963 # Also for simplicity we just don't relay all dunder names
964 if '__origin__' in self.__dict__ and not _is_dunder(attr):
965 return getattr(self.__origin__, attr)
966 raise AttributeError(attr)
967
968 def __setattr__(self, attr, val):
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700969 if _is_dunder(attr) or attr in {'_name', '_inst', '_nparams',
970 '_typevar_types', '_paramspec_tvars'}:
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300971 super().__setattr__(attr, val)
972 else:
973 setattr(self.__origin__, attr, val)
974
975 def __instancecheck__(self, obj):
976 return self.__subclasscheck__(type(obj))
977
978 def __subclasscheck__(self, cls):
979 raise TypeError("Subscripted generics cannot be used with"
980 " class and instance checks")
981
982
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300983# Special typing constructs Union, Optional, Generic, Callable and Tuple
984# use three special attributes for internal bookkeeping of generic types:
985# * __parameters__ is a tuple of unique free type parameters of a generic
986# type, for example, Dict[T, T].__parameters__ == (T,);
987# * __origin__ keeps a reference to a type that was subscripted,
988# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
989# the type.
990# * __args__ is a tuple of all arguments used in subscripting,
991# e.g., Dict[T, int].__args__ == (T, int).
992
993
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300994class _GenericAlias(_BaseGenericAlias, _root=True):
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -0700995 def __init__(self, origin, params, *, inst=True, name=None,
996 _typevar_types=TypeVar,
997 _paramspec_tvars=False):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300998 super().__init__(origin, inst=inst, name=name)
999 if not isinstance(params, tuple):
1000 params = (params,)
1001 self.__args__ = tuple(... if a is _TypingEllipsis else
1002 () if a is _TypingEmpty else
1003 a for a in params)
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001004 self.__parameters__ = _collect_type_vars(params, typevar_types=_typevar_types)
1005 self._typevar_types = _typevar_types
1006 self._paramspec_tvars = _paramspec_tvars
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001007 if not name:
1008 self.__module__ = origin.__module__
1009
1010 def __eq__(self, other):
1011 if not isinstance(other, _GenericAlias):
1012 return NotImplemented
1013 return (self.__origin__ == other.__origin__
1014 and self.__args__ == other.__args__)
1015
1016 def __hash__(self):
1017 return hash((self.__origin__, self.__args__))
1018
Maggie Moss1b4552c2020-09-09 13:23:24 -07001019 def __or__(self, right):
1020 return Union[self, right]
1021
Serhiy Storchaka80844d12021-07-16 16:42:04 +03001022 def __ror__(self, left):
1023 return Union[left, self]
Maggie Moss1b4552c2020-09-09 13:23:24 -07001024
Guido van Rossum4cefe742016-09-27 15:20:12 -07001025 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001026 def __getitem__(self, params):
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001027 if self.__origin__ in (Generic, Protocol):
1028 # Can't subscript Generic[...] or Protocol[...].
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001029 raise TypeError(f"Cannot subscript already-subscripted {self}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001030 if not isinstance(params, tuple):
1031 params = (params,)
kj73607be2020-12-24 12:33:48 +08001032 params = tuple(_type_convert(p) for p in params)
Miss Islington (bot)c8db2922021-08-02 00:08:24 -07001033 if (self._paramspec_tvars
1034 and any(isinstance(t, ParamSpec) for t in self.__parameters__)):
1035 params = _prepare_paramspec_params(self, params)
1036 else:
1037 _check_generic(self, params, len(self.__parameters__))
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001038
1039 subst = dict(zip(self.__parameters__, params))
1040 new_args = []
1041 for arg in self.__args__:
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001042 if isinstance(arg, self._typevar_types):
Miss Islington (bot)536e35a2021-08-04 13:36:01 -07001043 if isinstance(arg, ParamSpec):
1044 arg = subst[arg]
1045 if not _is_param_expr(arg):
1046 raise TypeError(f"Expected a list of types, an ellipsis, "
1047 f"ParamSpec, or Concatenate. Got {arg}")
1048 else:
1049 arg = subst[arg]
Miss Islington (bot)8a37e8c2021-07-26 12:02:58 -07001050 elif isinstance(arg, (_GenericAlias, GenericAlias, types.UnionType)):
Serhiy Storchaka0122d482020-05-10 13:39:40 +03001051 subparams = arg.__parameters__
1052 if subparams:
1053 subargs = tuple(subst[x] for x in subparams)
1054 arg = arg[subargs]
kj73607be2020-12-24 12:33:48 +08001055 # Required to flatten out the args for CallableGenericAlias
1056 if self.__origin__ == collections.abc.Callable and isinstance(arg, tuple):
1057 new_args.extend(arg)
1058 else:
1059 new_args.append(arg)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001060 return self.copy_with(tuple(new_args))
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001061
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001062 def copy_with(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001063 return self.__class__(self.__origin__, params, name=self._name, inst=self._inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001064
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001065 def __repr__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001066 if self._name:
1067 name = 'typing.' + self._name
1068 else:
1069 name = _type_repr(self.__origin__)
1070 args = ", ".join([_type_repr(a) for a in self.__args__])
1071 return f'{name}[{args}]'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001072
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001073 def __reduce__(self):
1074 if self._name:
1075 origin = globals()[self._name]
1076 else:
1077 origin = self.__origin__
1078 args = tuple(self.__args__)
1079 if len(args) == 1 and not isinstance(args[0], tuple):
1080 args, = args
1081 return operator.getitem, (origin, args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001082
1083 def __mro_entries__(self, bases):
Miss Islington (bot)81fa08c2021-08-28 11:09:45 -07001084 if isinstance(self.__origin__, _SpecialForm):
1085 raise TypeError(f"Cannot subclass {self!r}")
1086
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001087 if self._name: # generic version of an ABC or built-in class
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001088 return super().__mro_entries__(bases)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001089 if self.__origin__ is Generic:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001090 if Protocol in bases:
1091 return ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001092 i = bases.index(self)
1093 for b in bases[i+1:]:
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001094 if isinstance(b, _BaseGenericAlias) and b is not self:
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001095 return ()
1096 return (self.__origin__,)
1097
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001098
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001099# _nparams is the number of accepted parameters, e.g. 0 for Hashable,
1100# 1 for List and 2 for Dict. It may be -1 if variable number of
1101# parameters are accepted (needs custom __getitem__).
1102
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001103class _SpecialGenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001104 def __init__(self, origin, nparams, *, inst=True, name=None):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001105 if name is None:
1106 name = origin.__name__
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001107 super().__init__(origin, inst=inst, name=name)
1108 self._nparams = nparams
Serhiy Storchaka2fbc57a2020-05-10 15:14:27 +03001109 if origin.__module__ == 'builtins':
1110 self.__doc__ = f'A generic version of {origin.__qualname__}.'
1111 else:
1112 self.__doc__ = f'A generic version of {origin.__module__}.{origin.__qualname__}.'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001113
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001114 @_tp_cache
1115 def __getitem__(self, params):
1116 if not isinstance(params, tuple):
1117 params = (params,)
1118 msg = "Parameters to generic types must be types."
1119 params = tuple(_type_check(p, msg) for p in params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001120 _check_generic(self, params, self._nparams)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001121 return self.copy_with(params)
1122
1123 def copy_with(self, params):
1124 return _GenericAlias(self.__origin__, params,
1125 name=self._name, inst=self._inst)
1126
1127 def __repr__(self):
1128 return 'typing.' + self._name
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001129
1130 def __subclasscheck__(self, cls):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001131 if isinstance(cls, _SpecialGenericAlias):
1132 return issubclass(cls.__origin__, self.__origin__)
1133 if not isinstance(cls, _GenericAlias):
1134 return issubclass(cls, self.__origin__)
1135 return super().__subclasscheck__(cls)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001136
Ivan Levkivskyi83494032018-03-26 23:01:12 +01001137 def __reduce__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001138 return self._name
Ivan Levkivskyi83494032018-03-26 23:01:12 +01001139
Maggie Moss1b4552c2020-09-09 13:23:24 -07001140 def __or__(self, right):
1141 return Union[self, right]
1142
Serhiy Storchaka80844d12021-07-16 16:42:04 +03001143 def __ror__(self, left):
1144 return Union[left, self]
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001145
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001146class _CallableGenericAlias(_GenericAlias, _root=True):
1147 def __repr__(self):
1148 assert self._name == 'Callable'
kj73607be2020-12-24 12:33:48 +08001149 args = self.__args__
Miss Islington (bot)536e35a2021-08-04 13:36:01 -07001150 if len(args) == 2 and _is_param_expr(args[0]):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001151 return super().__repr__()
1152 return (f'typing.Callable'
kj73607be2020-12-24 12:33:48 +08001153 f'[[{", ".join([_type_repr(a) for a in args[:-1]])}], '
1154 f'{_type_repr(args[-1])}]')
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001155
1156 def __reduce__(self):
1157 args = self.__args__
Miss Islington (bot)536e35a2021-08-04 13:36:01 -07001158 if not (len(args) == 2 and _is_param_expr(args[0])):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001159 args = list(args[:-1]), args[-1]
1160 return operator.getitem, (Callable, args)
1161
1162
1163class _CallableType(_SpecialGenericAlias, _root=True):
1164 def copy_with(self, params):
1165 return _CallableGenericAlias(self.__origin__, params,
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001166 name=self._name, inst=self._inst,
1167 _typevar_types=(TypeVar, ParamSpec),
1168 _paramspec_tvars=True)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001169
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001170 def __getitem__(self, params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001171 if not isinstance(params, tuple) or len(params) != 2:
1172 raise TypeError("Callable must be used as "
1173 "Callable[[arg, ...], result].")
1174 args, result = params
kj463c7d32020-12-14 02:38:24 +08001175 # This relaxes what args can be on purpose to allow things like
1176 # PEP 612 ParamSpec. Responsibility for whether a user is using
1177 # Callable[...] properly is deferred to static type checkers.
1178 if isinstance(args, list):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001179 params = (tuple(args), result)
kj463c7d32020-12-14 02:38:24 +08001180 else:
1181 params = (args, result)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001182 return self.__getitem_inner__(params)
1183
1184 @_tp_cache
1185 def __getitem_inner__(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001186 args, result = params
1187 msg = "Callable[args, result]: result must be a type."
1188 result = _type_check(result, msg)
1189 if args is Ellipsis:
1190 return self.copy_with((_TypingEllipsis, result))
kj463c7d32020-12-14 02:38:24 +08001191 if not isinstance(args, tuple):
1192 args = (args,)
1193 args = tuple(_type_convert(arg) for arg in args)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001194 params = args + (result,)
1195 return self.copy_with(params)
1196
1197
1198class _TupleType(_SpecialGenericAlias, _root=True):
1199 @_tp_cache
1200 def __getitem__(self, params):
1201 if params == ():
1202 return self.copy_with((_TypingEmpty,))
1203 if not isinstance(params, tuple):
1204 params = (params,)
1205 if len(params) == 2 and params[1] is ...:
1206 msg = "Tuple[t, ...]: t must be a type."
1207 p = _type_check(params[0], msg)
1208 return self.copy_with((p, _TypingEllipsis))
1209 msg = "Tuple[t0, t1, ...]: each t must be a type."
1210 params = tuple(_type_check(p, msg) for p in params)
1211 return self.copy_with(params)
1212
1213
1214class _UnionGenericAlias(_GenericAlias, _root=True):
1215 def copy_with(self, params):
1216 return Union[params]
1217
1218 def __eq__(self, other):
Miss Islington (bot)8a37e8c2021-07-26 12:02:58 -07001219 if not isinstance(other, (_UnionGenericAlias, types.UnionType)):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001220 return NotImplemented
1221 return set(self.__args__) == set(other.__args__)
1222
1223 def __hash__(self):
1224 return hash(frozenset(self.__args__))
1225
1226 def __repr__(self):
1227 args = self.__args__
1228 if len(args) == 2:
1229 if args[0] is type(None):
1230 return f'typing.Optional[{_type_repr(args[1])}]'
1231 elif args[1] is type(None):
1232 return f'typing.Optional[{_type_repr(args[0])}]'
1233 return super().__repr__()
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001234
Maggie Moss1b4552c2020-09-09 13:23:24 -07001235 def __instancecheck__(self, obj):
1236 return self.__subclasscheck__(type(obj))
1237
1238 def __subclasscheck__(self, cls):
1239 for arg in self.__args__:
1240 if issubclass(cls, arg):
1241 return True
1242
Miss Islington (bot)36a24972021-08-06 10:08:27 -07001243 def __reduce__(self):
1244 func, (origin, args) = super().__reduce__()
1245 return func, (Union, args)
1246
Maggie Moss1b4552c2020-09-09 13:23:24 -07001247
Yurii Karabasf03d3182020-11-17 04:23:19 +02001248def _value_and_type_iter(parameters):
1249 return ((p, type(p)) for p in parameters)
1250
1251
1252class _LiteralGenericAlias(_GenericAlias, _root=True):
1253
1254 def __eq__(self, other):
1255 if not isinstance(other, _LiteralGenericAlias):
1256 return NotImplemented
1257
1258 return set(_value_and_type_iter(self.__args__)) == set(_value_and_type_iter(other.__args__))
1259
1260 def __hash__(self):
Yurii Karabas1b540772020-11-19 18:17:38 +02001261 return hash(frozenset(_value_and_type_iter(self.__args__)))
Yurii Karabasf03d3182020-11-17 04:23:19 +02001262
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001263
kj73607be2020-12-24 12:33:48 +08001264class _ConcatenateGenericAlias(_GenericAlias, _root=True):
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001265 def __init__(self, *args, **kwargs):
1266 super().__init__(*args, **kwargs,
1267 _typevar_types=(TypeVar, ParamSpec),
1268 _paramspec_tvars=True)
kj73607be2020-12-24 12:33:48 +08001269
1270
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001271class Generic:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001272 """Abstract base class for generic types.
1273
Guido van Rossumb24569a2016-11-20 18:01:29 -08001274 A generic type is typically declared by inheriting from
1275 this class parameterized with one or more type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001276 For example, a generic mapping type might be defined as::
1277
1278 class Mapping(Generic[KT, VT]):
1279 def __getitem__(self, key: KT) -> VT:
1280 ...
1281 # Etc.
1282
1283 This class can then be used as follows::
1284
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001285 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001286 try:
1287 return mapping[key]
1288 except KeyError:
1289 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001290 """
Guido van Rossumd70fe632015-08-05 12:11:06 +02001291 __slots__ = ()
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001292 _is_protocol = False
Guido van Rossumd70fe632015-08-05 12:11:06 +02001293
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001294 @_tp_cache
1295 def __class_getitem__(cls, params):
1296 if not isinstance(params, tuple):
1297 params = (params,)
1298 if not params and cls is not Tuple:
1299 raise TypeError(
1300 f"Parameter list to {cls.__qualname__}[...] cannot be empty")
kj73607be2020-12-24 12:33:48 +08001301 params = tuple(_type_convert(p) for p in params)
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001302 if cls in (Generic, Protocol):
1303 # Generic and Protocol can only be subscripted with unique type variables.
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001304 if not all(isinstance(p, (TypeVar, ParamSpec)) for p in params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001305 raise TypeError(
kj73607be2020-12-24 12:33:48 +08001306 f"Parameters to {cls.__name__}[...] must all be type variables "
1307 f"or parameter specification variables.")
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001308 if len(set(params)) != len(params):
1309 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001310 f"Parameters to {cls.__name__}[...] must all be unique")
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001311 else:
1312 # Subscripting a regular Generic subclass.
kj73607be2020-12-24 12:33:48 +08001313 if any(isinstance(t, ParamSpec) for t in cls.__parameters__):
1314 params = _prepare_paramspec_params(cls, params)
Miss Islington (bot)c8db2922021-08-02 00:08:24 -07001315 else:
1316 _check_generic(cls, params, len(cls.__parameters__))
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001317 return _GenericAlias(cls, params,
1318 _typevar_types=(TypeVar, ParamSpec),
1319 _paramspec_tvars=True)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001320
1321 def __init_subclass__(cls, *args, **kwargs):
Ivan Levkivskyiee566fe2018-04-04 17:00:15 +01001322 super().__init_subclass__(*args, **kwargs)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001323 tvars = []
1324 if '__orig_bases__' in cls.__dict__:
1325 error = Generic in cls.__orig_bases__
1326 else:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001327 error = Generic in cls.__bases__ and cls.__name__ != 'Protocol'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001328 if error:
1329 raise TypeError("Cannot inherit from plain Generic")
1330 if '__orig_bases__' in cls.__dict__:
Miss Islington (bot)c55ff1b2021-05-13 10:19:24 -07001331 tvars = _collect_type_vars(cls.__orig_bases__, (TypeVar, ParamSpec))
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001332 # Look for Generic[T1, ..., Tn].
1333 # If found, tvars must be a subset of it.
1334 # If not found, tvars is it.
1335 # Also check for and reject plain Generic,
1336 # and reject multiple Generic[...].
1337 gvars = None
1338 for base in cls.__orig_bases__:
1339 if (isinstance(base, _GenericAlias) and
1340 base.__origin__ is Generic):
1341 if gvars is not None:
1342 raise TypeError(
1343 "Cannot inherit from Generic[...] multiple types.")
1344 gvars = base.__parameters__
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001345 if gvars is not None:
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001346 tvarset = set(tvars)
1347 gvarset = set(gvars)
1348 if not tvarset <= gvarset:
1349 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
1350 s_args = ', '.join(str(g) for g in gvars)
1351 raise TypeError(f"Some type variables ({s_vars}) are"
1352 f" not listed in Generic[{s_args}]")
1353 tvars = gvars
1354 cls.__parameters__ = tuple(tvars)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001355
1356
1357class _TypingEmpty:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001358 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
1359 to allow empty list/tuple in specific places, without allowing them
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001360 to sneak in where prohibited.
1361 """
1362
1363
1364class _TypingEllipsis:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001365 """Internal placeholder for ... (ellipsis)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001366
1367
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001368_TYPING_INTERNALS = ['__parameters__', '__orig_bases__', '__orig_class__',
1369 '_is_protocol', '_is_runtime_protocol']
1370
1371_SPECIAL_NAMES = ['__abstractmethods__', '__annotations__', '__dict__', '__doc__',
1372 '__init__', '__module__', '__new__', '__slots__',
Guido van Rossum48b069a2020-04-07 09:50:06 -07001373 '__subclasshook__', '__weakref__', '__class_getitem__']
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001374
1375# These special attributes will be not collected as protocol members.
1376EXCLUDED_ATTRIBUTES = _TYPING_INTERNALS + _SPECIAL_NAMES + ['_MutableMapping__marker']
1377
1378
1379def _get_protocol_attrs(cls):
1380 """Collect protocol members from a protocol class objects.
1381
1382 This includes names actually defined in the class dictionary, as well
1383 as names that appear in annotations. Special names (above) are skipped.
1384 """
1385 attrs = set()
1386 for base in cls.__mro__[:-1]: # without object
1387 if base.__name__ in ('Protocol', 'Generic'):
1388 continue
1389 annotations = getattr(base, '__annotations__', {})
1390 for attr in list(base.__dict__.keys()) + list(annotations.keys()):
1391 if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
1392 attrs.add(attr)
1393 return attrs
1394
1395
1396def _is_callable_members_only(cls):
1397 # PEP 544 prohibits using issubclass() with protocols that have non-method members.
1398 return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls))
1399
1400
Miss Islington (bot)79e9f5a2021-09-02 23:26:53 -07001401def _no_init_or_replace_init(self, *args, **kwargs):
1402 cls = type(self)
1403
1404 if cls._is_protocol:
1405 raise TypeError('Protocols cannot be instantiated')
1406
Miss Islington (bot)c0816492021-09-08 08:05:23 -07001407 # Already using a custom `__init__`. No need to calculate correct
1408 # `__init__` to call. This can lead to RecursionError. See bpo-45121.
1409 if cls.__init__ is not _no_init_or_replace_init:
1410 return
1411
Miss Islington (bot)79e9f5a2021-09-02 23:26:53 -07001412 # Initially, `__init__` of a protocol subclass is set to `_no_init_or_replace_init`.
1413 # The first instantiation of the subclass will call `_no_init_or_replace_init` which
1414 # searches for a proper new `__init__` in the MRO. The new `__init__`
1415 # replaces the subclass' old `__init__` (ie `_no_init_or_replace_init`). Subsequent
1416 # instantiation of the protocol subclass will thus use the new
1417 # `__init__` and no longer call `_no_init_or_replace_init`.
1418 for base in cls.__mro__:
1419 init = base.__dict__.get('__init__', _no_init_or_replace_init)
1420 if init is not _no_init_or_replace_init:
1421 cls.__init__ = init
1422 break
1423 else:
1424 # should not happen
1425 cls.__init__ = object.__init__
1426
1427 cls.__init__(self, *args, **kwargs)
1428
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001429
Miss Islington (bot)56122b02021-07-30 06:48:01 -07001430def _caller(depth=1, default='__main__'):
Miss Islington (bot)c2f33df2021-07-20 08:24:57 -07001431 try:
Miss Islington (bot)56122b02021-07-30 06:48:01 -07001432 return sys._getframe(depth + 1).f_globals.get('__name__', default)
Miss Islington (bot)c2f33df2021-07-20 08:24:57 -07001433 except (AttributeError, ValueError): # For platforms without _getframe()
Miss Islington (bot)56122b02021-07-30 06:48:01 -07001434 return None
Miss Islington (bot)c2f33df2021-07-20 08:24:57 -07001435
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001436
Miss Islington (bot)a2d94a02021-05-12 09:09:04 -07001437def _allow_reckless_class_checks(depth=3):
Nickolena Fishercfaf4c02020-04-26 12:49:11 -05001438 """Allow instance and class checks for special stdlib modules.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001439
1440 The abc and functools modules indiscriminately call isinstance() and
1441 issubclass() on the whole MRO of a user class, which may contain protocols.
1442 """
1443 try:
Miss Islington (bot)a2d94a02021-05-12 09:09:04 -07001444 return sys._getframe(depth).f_globals['__name__'] in ['abc', 'functools']
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001445 except (AttributeError, ValueError): # For platforms without _getframe().
1446 return True
1447
1448
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001449_PROTO_ALLOWLIST = {
Divij Rajkumar692a0dc2019-09-12 11:13:51 +01001450 'collections.abc': [
1451 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
1452 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
1453 ],
1454 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
1455}
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001456
1457
1458class _ProtocolMeta(ABCMeta):
1459 # This metaclass is really unfortunate and exists only because of
1460 # the lack of __instancehook__.
1461 def __instancecheck__(cls, instance):
1462 # We need this method for situations where attributes are
1463 # assigned in __init__.
Miss Islington (bot)a2d94a02021-05-12 09:09:04 -07001464 if (
1465 getattr(cls, '_is_protocol', False) and
1466 not getattr(cls, '_is_runtime_protocol', False) and
1467 not _allow_reckless_class_checks(depth=2)
1468 ):
1469 raise TypeError("Instance and class checks can only be used with"
1470 " @runtime_checkable protocols")
1471
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001472 if ((not getattr(cls, '_is_protocol', False) or
1473 _is_callable_members_only(cls)) and
1474 issubclass(instance.__class__, cls)):
1475 return True
1476 if cls._is_protocol:
1477 if all(hasattr(instance, attr) and
1478 # All *methods* can be blocked by setting them to None.
1479 (not callable(getattr(cls, attr, None)) or
1480 getattr(instance, attr) is not None)
1481 for attr in _get_protocol_attrs(cls)):
1482 return True
1483 return super().__instancecheck__(instance)
1484
1485
1486class Protocol(Generic, metaclass=_ProtocolMeta):
1487 """Base class for protocol classes.
1488
1489 Protocol classes are defined as::
1490
1491 class Proto(Protocol):
1492 def meth(self) -> int:
1493 ...
1494
1495 Such classes are primarily used with static type checkers that recognize
1496 structural subtyping (static duck-typing), for example::
1497
1498 class C:
1499 def meth(self) -> int:
1500 return 0
1501
1502 def func(x: Proto) -> int:
1503 return x.meth()
1504
1505 func(C()) # Passes static type check
1506
1507 See PEP 544 for details. Protocol classes decorated with
1508 @typing.runtime_checkable act as simple-minded runtime protocols that check
1509 only the presence of given attributes, ignoring their type signatures.
1510 Protocol classes can be generic, they are defined as::
1511
1512 class GenProto(Protocol[T]):
1513 def meth(self) -> T:
1514 ...
1515 """
1516 __slots__ = ()
1517 _is_protocol = True
1518 _is_runtime_protocol = False
1519
1520 def __init_subclass__(cls, *args, **kwargs):
1521 super().__init_subclass__(*args, **kwargs)
1522
1523 # Determine if this is a protocol or a concrete subclass.
1524 if not cls.__dict__.get('_is_protocol', False):
1525 cls._is_protocol = any(b is Protocol for b in cls.__bases__)
1526
1527 # Set (or override) the protocol subclass hook.
1528 def _proto_hook(other):
1529 if not cls.__dict__.get('_is_protocol', False):
1530 return NotImplemented
1531
1532 # First, perform various sanity checks.
1533 if not getattr(cls, '_is_runtime_protocol', False):
Rossc1af1282020-12-29 11:55:28 +00001534 if _allow_reckless_class_checks():
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001535 return NotImplemented
1536 raise TypeError("Instance and class checks can only be used with"
1537 " @runtime_checkable protocols")
1538 if not _is_callable_members_only(cls):
Rossc1af1282020-12-29 11:55:28 +00001539 if _allow_reckless_class_checks():
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001540 return NotImplemented
1541 raise TypeError("Protocols with non-method members"
1542 " don't support issubclass()")
1543 if not isinstance(other, type):
1544 # Same error message as for issubclass(1, int).
1545 raise TypeError('issubclass() arg 1 must be a class')
1546
1547 # Second, perform the actual structural compatibility check.
1548 for attr in _get_protocol_attrs(cls):
1549 for base in other.__mro__:
1550 # Check if the members appears in the class dictionary...
1551 if attr in base.__dict__:
1552 if base.__dict__[attr] is None:
1553 return NotImplemented
1554 break
1555
1556 # ...or in annotations, if it is a sub-protocol.
1557 annotations = getattr(base, '__annotations__', {})
1558 if (isinstance(annotations, collections.abc.Mapping) and
1559 attr in annotations and
1560 issubclass(other, Generic) and other._is_protocol):
1561 break
1562 else:
1563 return NotImplemented
1564 return True
1565
1566 if '__subclasshook__' not in cls.__dict__:
1567 cls.__subclasshook__ = _proto_hook
1568
1569 # We have nothing more to do for non-protocols...
1570 if not cls._is_protocol:
1571 return
1572
1573 # ... otherwise check consistency of bases, and prohibit instantiation.
1574 for base in cls.__bases__:
1575 if not (base in (object, Generic) or
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001576 base.__module__ in _PROTO_ALLOWLIST and
1577 base.__name__ in _PROTO_ALLOWLIST[base.__module__] or
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001578 issubclass(base, Generic) and base._is_protocol):
1579 raise TypeError('Protocols can only inherit from other'
1580 ' protocols, got %r' % base)
Miss Islington (bot)79e9f5a2021-09-02 23:26:53 -07001581 cls.__init__ = _no_init_or_replace_init
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001582
1583
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001584class _AnnotatedAlias(_GenericAlias, _root=True):
1585 """Runtime representation of an annotated type.
1586
1587 At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't'
1588 with extra annotations. The alias behaves like a normal typing alias,
1589 instantiating is the same as instantiating the underlying type, binding
1590 it to types is also the same.
1591 """
1592 def __init__(self, origin, metadata):
1593 if isinstance(origin, _AnnotatedAlias):
1594 metadata = origin.__metadata__ + metadata
1595 origin = origin.__origin__
Miss Islington (bot)06e9a352021-08-25 11:36:47 -07001596 super().__init__(origin, origin)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001597 self.__metadata__ = metadata
1598
1599 def copy_with(self, params):
1600 assert len(params) == 1
1601 new_type = params[0]
1602 return _AnnotatedAlias(new_type, self.__metadata__)
1603
1604 def __repr__(self):
1605 return "typing.Annotated[{}, {}]".format(
1606 _type_repr(self.__origin__),
1607 ", ".join(repr(a) for a in self.__metadata__)
1608 )
1609
1610 def __reduce__(self):
1611 return operator.getitem, (
1612 Annotated, (self.__origin__,) + self.__metadata__
1613 )
1614
1615 def __eq__(self, other):
1616 if not isinstance(other, _AnnotatedAlias):
1617 return NotImplemented
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001618 return (self.__origin__ == other.__origin__
1619 and self.__metadata__ == other.__metadata__)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001620
1621 def __hash__(self):
1622 return hash((self.__origin__, self.__metadata__))
1623
Miss Islington (bot)06e9a352021-08-25 11:36:47 -07001624 def __getattr__(self, attr):
1625 if attr in {'__name__', '__qualname__'}:
1626 return 'Annotated'
1627 return super().__getattr__(attr)
1628
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001629
1630class Annotated:
1631 """Add context specific metadata to a type.
1632
1633 Example: Annotated[int, runtime_check.Unsigned] indicates to the
1634 hypothetical runtime_check module that this type is an unsigned int.
1635 Every other consumer of this type can ignore this metadata and treat
1636 this type as int.
1637
1638 The first argument to Annotated must be a valid type.
1639
1640 Details:
1641
1642 - It's an error to call `Annotated` with less than two arguments.
1643 - Nested Annotated are flattened::
1644
1645 Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
1646
1647 - Instantiating an annotated type is equivalent to instantiating the
1648 underlying type::
1649
1650 Annotated[C, Ann1](5) == C(5)
1651
1652 - Annotated can be used as a generic type alias::
1653
1654 Optimized = Annotated[T, runtime.Optimize()]
1655 Optimized[int] == Annotated[int, runtime.Optimize()]
1656
1657 OptimizedList = Annotated[List[T], runtime.Optimize()]
1658 OptimizedList[int] == Annotated[List[int], runtime.Optimize()]
1659 """
1660
1661 __slots__ = ()
1662
1663 def __new__(cls, *args, **kwargs):
1664 raise TypeError("Type Annotated cannot be instantiated.")
1665
1666 @_tp_cache
1667 def __class_getitem__(cls, params):
1668 if not isinstance(params, tuple) or len(params) < 2:
1669 raise TypeError("Annotated[...] should be used "
1670 "with at least two arguments (a type and an "
1671 "annotation).")
1672 msg = "Annotated[t, ...]: t must be a type."
1673 origin = _type_check(params[0], msg)
1674 metadata = tuple(params[1:])
1675 return _AnnotatedAlias(origin, metadata)
1676
1677 def __init_subclass__(cls, *args, **kwargs):
1678 raise TypeError(
1679 "Cannot subclass {}.Annotated".format(cls.__module__)
1680 )
1681
1682
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001683def runtime_checkable(cls):
1684 """Mark a protocol class as a runtime protocol.
1685
1686 Such protocol can be used with isinstance() and issubclass().
1687 Raise TypeError if applied to a non-protocol class.
1688 This allows a simple-minded structural check very similar to
1689 one trick ponies in collections.abc such as Iterable.
1690 For example::
1691
1692 @runtime_checkable
1693 class Closable(Protocol):
1694 def close(self): ...
1695
1696 assert isinstance(open('/some/file'), Closable)
1697
1698 Warning: this will check only the presence of the required methods,
1699 not their type signatures!
1700 """
1701 if not issubclass(cls, Generic) or not cls._is_protocol:
1702 raise TypeError('@runtime_checkable can be only applied to protocol classes,'
1703 ' got %r' % cls)
1704 cls._is_runtime_protocol = True
1705 return cls
1706
1707
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001708def cast(typ, val):
1709 """Cast a value to a type.
1710
1711 This returns the value unchanged. To the type checker this
1712 signals that the return value has the designated type, but at
1713 runtime we intentionally don't check anything (we want this
1714 to be as fast as possible).
1715 """
1716 return val
1717
1718
1719def _get_defaults(func):
1720 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -08001721 try:
1722 code = func.__code__
1723 except AttributeError:
1724 # Some built-in functions don't have __code__, __defaults__, etc.
1725 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001726 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001727 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001728 arg_names = arg_names[:pos_count]
1729 defaults = func.__defaults__ or ()
1730 kwdefaults = func.__kwdefaults__
1731 res = dict(kwdefaults) if kwdefaults else {}
1732 pos_offset = pos_count - len(defaults)
1733 for name, value in zip(arg_names[pos_offset:], defaults):
1734 assert name not in res
1735 res[name] = value
1736 return res
1737
1738
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001739_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
1740 types.MethodType, types.ModuleType,
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02001741 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001742
1743
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001744def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
Guido van Rossum991d14f2016-11-09 13:12:51 -08001745 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001746
Guido van Rossum991d14f2016-11-09 13:12:51 -08001747 This is often the same as obj.__annotations__, but it handles
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001748 forward references encoded as string literals, adds Optional[t] if a
1749 default value equal to None is set and recursively replaces all
1750 'Annotated[T, ...]' with 'T' (unless 'include_extras=True').
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001751
Guido van Rossum991d14f2016-11-09 13:12:51 -08001752 The argument may be a module, class, method, or function. The annotations
1753 are returned as a dictionary. For classes, annotations include also
1754 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001755
Guido van Rossum991d14f2016-11-09 13:12:51 -08001756 TypeError is raised if the argument is not of a type that can contain
1757 annotations, and an empty dictionary is returned if no annotations are
1758 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001759
Guido van Rossum991d14f2016-11-09 13:12:51 -08001760 BEWARE -- the behavior of globalns and localns is counterintuitive
1761 (unless you are familiar with how eval() and exec() work). The
1762 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001763
Guido van Rossum991d14f2016-11-09 13:12:51 -08001764 - If no dict arguments are passed, an attempt is made to use the
Łukasz Langaf350a262017-09-14 14:33:00 -04001765 globals from obj (or the respective module's globals for classes),
1766 and these are also used as the locals. If the object does not appear
Ken Jin1b1f9852021-04-27 01:31:21 +08001767 to have globals, an empty dictionary is used. For classes, the search
1768 order is globals first then locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001769
Guido van Rossum991d14f2016-11-09 13:12:51 -08001770 - If one dict argument is passed, it is used for both globals and
1771 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001772
Guido van Rossum991d14f2016-11-09 13:12:51 -08001773 - If two dict arguments are passed, they specify globals and
1774 locals, respectively.
1775 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001776
Guido van Rossum991d14f2016-11-09 13:12:51 -08001777 if getattr(obj, '__no_type_check__', None):
1778 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001779 # Classes require a special treatment.
1780 if isinstance(obj, type):
1781 hints = {}
1782 for base in reversed(obj.__mro__):
Łukasz Langaf350a262017-09-14 14:33:00 -04001783 if globalns is None:
Miss Islington (bot)3df23b52021-06-26 16:52:28 -07001784 base_globals = getattr(sys.modules.get(base.__module__, None), '__dict__', {})
Łukasz Langaf350a262017-09-14 14:33:00 -04001785 else:
1786 base_globals = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001787 ann = base.__dict__.get('__annotations__', {})
larryhastings2f2b6982021-04-29 20:09:08 -07001788 if isinstance(ann, types.GetSetDescriptorType):
1789 ann = {}
Ken Jin852150d2021-04-13 01:23:12 +08001790 base_locals = dict(vars(base)) if localns is None else localns
Ken Jin1b1f9852021-04-27 01:31:21 +08001791 if localns is None and globalns is None:
1792 # This is surprising, but required. Before Python 3.10,
1793 # get_type_hints only evaluated the globalns of
1794 # a class. To maintain backwards compatibility, we reverse
1795 # the globalns and localns order so that eval() looks into
1796 # *base_globals* first rather than *base_locals*.
1797 # This only affects ForwardRefs.
1798 base_globals, base_locals = base_locals, base_globals
Guido van Rossum991d14f2016-11-09 13:12:51 -08001799 for name, value in ann.items():
1800 if value is None:
1801 value = type(None)
1802 if isinstance(value, str):
Nina Zakharenko0e61dff2018-05-22 20:32:10 -07001803 value = ForwardRef(value, is_argument=False)
Ken Jin852150d2021-04-13 01:23:12 +08001804 value = _eval_type(value, base_globals, base_locals)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001805 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001806 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
Łukasz Langaf350a262017-09-14 14:33:00 -04001807
1808 if globalns is None:
1809 if isinstance(obj, types.ModuleType):
1810 globalns = obj.__dict__
1811 else:
benedwards140aca3a32019-11-21 17:24:58 +00001812 nsobj = obj
1813 # Find globalns for the unwrapped object.
1814 while hasattr(nsobj, '__wrapped__'):
1815 nsobj = nsobj.__wrapped__
1816 globalns = getattr(nsobj, '__globals__', {})
Łukasz Langaf350a262017-09-14 14:33:00 -04001817 if localns is None:
1818 localns = globalns
1819 elif localns is None:
1820 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001821 hints = getattr(obj, '__annotations__', None)
1822 if hints is None:
1823 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001824 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001825 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001826 else:
1827 raise TypeError('{!r} is not a module, class, method, '
1828 'or function.'.format(obj))
1829 defaults = _get_defaults(obj)
1830 hints = dict(hints)
1831 for name, value in hints.items():
1832 if value is None:
1833 value = type(None)
1834 if isinstance(value, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001835 value = ForwardRef(value)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001836 value = _eval_type(value, globalns, localns)
1837 if name in defaults and defaults[name] is None:
1838 value = Optional[value]
1839 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001840 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
1841
1842
1843def _strip_annotations(t):
1844 """Strips the annotations from a given type.
1845 """
1846 if isinstance(t, _AnnotatedAlias):
1847 return _strip_annotations(t.__origin__)
1848 if isinstance(t, _GenericAlias):
1849 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1850 if stripped_args == t.__args__:
1851 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001852 return t.copy_with(stripped_args)
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001853 if isinstance(t, GenericAlias):
1854 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1855 if stripped_args == t.__args__:
1856 return t
1857 return GenericAlias(t.__origin__, stripped_args)
Miss Islington (bot)8a37e8c2021-07-26 12:02:58 -07001858 if isinstance(t, types.UnionType):
Ken Jina2721642021-07-19 22:22:59 +08001859 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1860 if stripped_args == t.__args__:
1861 return t
Miss Islington (bot)0aea99e2021-07-24 12:35:33 -07001862 return functools.reduce(operator.or_, stripped_args)
Ken Jina2721642021-07-19 22:22:59 +08001863
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001864 return t
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001865
1866
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001867def get_origin(tp):
1868 """Get the unsubscripted version of a type.
1869
Jakub Stasiak38aaaaa2020-02-07 02:15:12 +01001870 This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
1871 and Annotated. Return None for unsupported types. Examples::
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001872
1873 get_origin(Literal[42]) is Literal
1874 get_origin(int) is None
1875 get_origin(ClassVar[int]) is ClassVar
1876 get_origin(Generic) is Generic
1877 get_origin(Generic[T]) is Generic
1878 get_origin(Union[T, int]) is Union
1879 get_origin(List[Tuple[T, T]][int]) == list
Jelle Zijlstra52243362021-04-10 19:57:05 -07001880 get_origin(P.args) is P
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001881 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001882 if isinstance(tp, _AnnotatedAlias):
1883 return Annotated
Jelle Zijlstra52243362021-04-10 19:57:05 -07001884 if isinstance(tp, (_BaseGenericAlias, GenericAlias,
1885 ParamSpecArgs, ParamSpecKwargs)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001886 return tp.__origin__
1887 if tp is Generic:
1888 return Generic
Miss Islington (bot)8a37e8c2021-07-26 12:02:58 -07001889 if isinstance(tp, types.UnionType):
1890 return types.UnionType
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001891 return None
1892
1893
1894def get_args(tp):
1895 """Get type arguments with all substitutions performed.
1896
1897 For unions, basic simplifications used by Union constructor are performed.
1898 Examples::
1899 get_args(Dict[str, int]) == (str, int)
1900 get_args(int) == ()
1901 get_args(Union[int, Union[T, int], str][int]) == (int, str)
1902 get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
1903 get_args(Callable[[], T][int]) == ([], int)
1904 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001905 if isinstance(tp, _AnnotatedAlias):
1906 return (tp.__origin__,) + tp.__metadata__
Ken Jin4140f102020-12-29 04:06:19 +08001907 if isinstance(tp, (_GenericAlias, GenericAlias)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001908 res = tp.__args__
Ken Jinefb1f092020-12-29 10:26:19 +08001909 if (tp.__origin__ is collections.abc.Callable
Miss Islington (bot)536e35a2021-08-04 13:36:01 -07001910 and not (len(res) == 2 and _is_param_expr(res[0]))):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001911 res = (list(res[:-1]), res[-1])
1912 return res
Miss Islington (bot)8a37e8c2021-07-26 12:02:58 -07001913 if isinstance(tp, types.UnionType):
Ken Jinefb1f092020-12-29 10:26:19 +08001914 return tp.__args__
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001915 return ()
1916
1917
Patrick Reader0705ec82020-09-16 05:58:32 +01001918def is_typeddict(tp):
1919 """Check if an annotation is a TypedDict class
1920
1921 For example::
1922 class Film(TypedDict):
1923 title: str
1924 year: int
1925
1926 is_typeddict(Film) # => True
1927 is_typeddict(Union[list, str]) # => False
1928 """
1929 return isinstance(tp, _TypedDictMeta)
1930
1931
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001932def no_type_check(arg):
1933 """Decorator to indicate that annotations are not type hints.
1934
1935 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001936 applies recursively to all methods and classes defined in that class
1937 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001938
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001939 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001940 """
1941 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001942 arg_attrs = arg.__dict__.copy()
1943 for attr, val in arg.__dict__.items():
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001944 if val in arg.__bases__ + (arg,):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001945 arg_attrs.pop(attr)
1946 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001947 if isinstance(obj, types.FunctionType):
1948 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001949 if isinstance(obj, type):
1950 no_type_check(obj)
1951 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001952 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001953 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001954 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001955 return arg
1956
1957
1958def no_type_check_decorator(decorator):
1959 """Decorator to give another decorator the @no_type_check effect.
1960
1961 This wraps the decorator with something that wraps the decorated
1962 function in @no_type_check.
1963 """
1964
1965 @functools.wraps(decorator)
1966 def wrapped_decorator(*args, **kwds):
1967 func = decorator(*args, **kwds)
1968 func = no_type_check(func)
1969 return func
1970
1971 return wrapped_decorator
1972
1973
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001974def _overload_dummy(*args, **kwds):
1975 """Helper for @overload to raise when called."""
1976 raise NotImplementedError(
1977 "You should not call an overloaded function. "
1978 "A series of @overload-decorated functions "
1979 "outside a stub module should always be followed "
1980 "by an implementation that is not @overload-ed.")
1981
1982
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001983def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001984 """Decorator for overloaded functions/methods.
1985
1986 In a stub file, place two or more stub definitions for the same
1987 function in a row, each decorated with @overload. For example:
1988
1989 @overload
1990 def utf8(value: None) -> None: ...
1991 @overload
1992 def utf8(value: bytes) -> bytes: ...
1993 @overload
1994 def utf8(value: str) -> bytes: ...
1995
1996 In a non-stub file (i.e. a regular .py file), do the same but
1997 follow it with an implementation. The implementation should *not*
1998 be decorated with @overload. For example:
1999
2000 @overload
2001 def utf8(value: None) -> None: ...
2002 @overload
2003 def utf8(value: bytes) -> bytes: ...
2004 @overload
2005 def utf8(value: str) -> bytes: ...
2006 def utf8(value):
2007 # implementation goes here
2008 """
2009 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002010
2011
Ivan Levkivskyif3672422019-05-26 09:37:07 +01002012def final(f):
2013 """A decorator to indicate final methods and final classes.
2014
2015 Use this decorator to indicate to type checkers that the decorated
2016 method cannot be overridden, and decorated class cannot be subclassed.
2017 For example:
2018
2019 class Base:
2020 @final
2021 def done(self) -> None:
2022 ...
2023 class Sub(Base):
2024 def done(self) -> None: # Error reported by type checker
2025 ...
2026
2027 @final
2028 class Leaf:
2029 ...
2030 class Other(Leaf): # Error reported by type checker
2031 ...
2032
2033 There is no runtime checking of these properties.
2034 """
2035 return f
2036
2037
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002038# Some unconstrained type variables. These are used by the container types.
2039# (These are not for export.)
2040T = TypeVar('T') # Any type.
2041KT = TypeVar('KT') # Key type.
2042VT = TypeVar('VT') # Value type.
2043T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
2044V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
2045VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
2046T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
2047# Internal type variable used for Type[].
2048CT_co = TypeVar('CT_co', covariant=True, bound=type)
2049
2050# A useful type variable with constraints. This represents string types.
2051# (This one *is* for export!)
2052AnyStr = TypeVar('AnyStr', bytes, str)
2053
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002054
2055# Various ABCs mimicking those in collections.abc.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03002056_alias = _SpecialGenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002057
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002058Hashable = _alias(collections.abc.Hashable, 0) # Not generic.
2059Awaitable = _alias(collections.abc.Awaitable, 1)
2060Coroutine = _alias(collections.abc.Coroutine, 3)
2061AsyncIterable = _alias(collections.abc.AsyncIterable, 1)
2062AsyncIterator = _alias(collections.abc.AsyncIterator, 1)
2063Iterable = _alias(collections.abc.Iterable, 1)
2064Iterator = _alias(collections.abc.Iterator, 1)
2065Reversible = _alias(collections.abc.Reversible, 1)
2066Sized = _alias(collections.abc.Sized, 0) # Not generic.
2067Container = _alias(collections.abc.Container, 1)
2068Collection = _alias(collections.abc.Collection, 1)
2069Callable = _CallableType(collections.abc.Callable, 2)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002070Callable.__doc__ = \
2071 """Callable type; Callable[[int], str] is a function of (int) -> str.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002072
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002073 The subscription syntax must always be used with exactly two
2074 values: the argument list and the return type. The argument list
2075 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002076
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002077 There is no syntax to indicate optional or keyword arguments,
2078 such function types are rarely used as callback types.
2079 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002080AbstractSet = _alias(collections.abc.Set, 1, name='AbstractSet')
2081MutableSet = _alias(collections.abc.MutableSet, 1)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002082# NOTE: Mapping is only covariant in the value type.
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002083Mapping = _alias(collections.abc.Mapping, 2)
2084MutableMapping = _alias(collections.abc.MutableMapping, 2)
2085Sequence = _alias(collections.abc.Sequence, 1)
2086MutableSequence = _alias(collections.abc.MutableSequence, 1)
2087ByteString = _alias(collections.abc.ByteString, 0) # Not generic
2088# Tuple accepts variable number of parameters.
2089Tuple = _TupleType(tuple, -1, inst=False, name='Tuple')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002090Tuple.__doc__ = \
2091 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07002092
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002093 Example: Tuple[T1, T2] is a tuple of two elements corresponding
2094 to type variables T1 and T2. Tuple[int, float, str] is a tuple
2095 of an int, a float and a string.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07002096
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002097 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
2098 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002099List = _alias(list, 1, inst=False, name='List')
2100Deque = _alias(collections.deque, 1, name='Deque')
2101Set = _alias(set, 1, inst=False, name='Set')
2102FrozenSet = _alias(frozenset, 1, inst=False, name='FrozenSet')
2103MappingView = _alias(collections.abc.MappingView, 1)
2104KeysView = _alias(collections.abc.KeysView, 1)
2105ItemsView = _alias(collections.abc.ItemsView, 2)
2106ValuesView = _alias(collections.abc.ValuesView, 1)
2107ContextManager = _alias(contextlib.AbstractContextManager, 1, name='ContextManager')
2108AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, 1, name='AsyncContextManager')
2109Dict = _alias(dict, 2, inst=False, name='Dict')
2110DefaultDict = _alias(collections.defaultdict, 2, name='DefaultDict')
2111OrderedDict = _alias(collections.OrderedDict, 2)
2112Counter = _alias(collections.Counter, 1)
2113ChainMap = _alias(collections.ChainMap, 2)
2114Generator = _alias(collections.abc.Generator, 3)
2115AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2)
2116Type = _alias(type, 1, inst=False, name='Type')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002117Type.__doc__ = \
2118 """A special construct usable to annotate class objects.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07002119
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002120 For example, suppose we have the following classes::
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07002121
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002122 class User: ... # Abstract base for User classes
2123 class BasicUser(User): ...
2124 class ProUser(User): ...
2125 class TeamUser(User): ...
Guido van Rossumf17c2002015-12-03 17:31:24 -08002126
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002127 And a function that takes a class argument that's a subclass of
2128 User and returns an instance of the corresponding class::
Guido van Rossumf17c2002015-12-03 17:31:24 -08002129
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002130 U = TypeVar('U', bound=User)
2131 def new_user(user_class: Type[U]) -> U:
2132 user = user_class()
2133 # (Here we could write the user object to a database)
2134 return user
Guido van Rossumf17c2002015-12-03 17:31:24 -08002135
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002136 joe = new_user(BasicUser)
Guido van Rossumf17c2002015-12-03 17:31:24 -08002137
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002138 At this point the type checker knows that joe has type BasicUser.
2139 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002140
2141
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002142@runtime_checkable
2143class SupportsInt(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002144 """An ABC with one abstract method __int__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002145 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002146
2147 @abstractmethod
2148 def __int__(self) -> int:
2149 pass
2150
2151
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002152@runtime_checkable
2153class SupportsFloat(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002154 """An ABC with one abstract method __float__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002155 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002156
2157 @abstractmethod
2158 def __float__(self) -> float:
2159 pass
2160
2161
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002162@runtime_checkable
2163class SupportsComplex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002164 """An ABC with one abstract method __complex__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002165 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002166
2167 @abstractmethod
2168 def __complex__(self) -> complex:
2169 pass
2170
2171
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002172@runtime_checkable
2173class SupportsBytes(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002174 """An ABC with one abstract method __bytes__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002175 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002176
2177 @abstractmethod
2178 def __bytes__(self) -> bytes:
2179 pass
2180
2181
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002182@runtime_checkable
2183class SupportsIndex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002184 """An ABC with one abstract method __index__."""
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -07002185 __slots__ = ()
2186
2187 @abstractmethod
2188 def __index__(self) -> int:
2189 pass
2190
2191
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002192@runtime_checkable
2193class SupportsAbs(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002194 """An ABC with one abstract method __abs__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002195 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002196
2197 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02002198 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002199 pass
2200
2201
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002202@runtime_checkable
2203class SupportsRound(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002204 """An ABC with one abstract method __round__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002205 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002206
2207 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02002208 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002209 pass
2210
2211
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002212def _make_nmtuple(name, types, module, defaults = ()):
2213 fields = [n for n, t in types]
2214 types = {n: _type_check(t, f"field {n} annotation must be a type")
2215 for n, t in types}
2216 nm_tpl = collections.namedtuple(name, fields,
2217 defaults=defaults, module=module)
2218 nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002219 return nm_tpl
2220
2221
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002222# attributes prohibited to set in NamedTuple class syntax
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002223_prohibited = frozenset({'__new__', '__init__', '__slots__', '__getnewargs__',
2224 '_fields', '_field_defaults',
2225 '_make', '_replace', '_asdict', '_source'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002226
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002227_special = frozenset({'__module__', '__name__', '__annotations__'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002228
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002229
Guido van Rossum2f841442016-11-15 09:48:06 -08002230class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002231
Guido van Rossum2f841442016-11-15 09:48:06 -08002232 def __new__(cls, typename, bases, ns):
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002233 assert bases[0] is _NamedTuple
Guido van Rossum2f841442016-11-15 09:48:06 -08002234 types = ns.get('__annotations__', {})
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002235 default_names = []
Guido van Rossum3c268be2017-01-18 08:03:50 -08002236 for field_name in types:
2237 if field_name in ns:
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002238 default_names.append(field_name)
2239 elif default_names:
2240 raise TypeError(f"Non-default namedtuple field {field_name} "
2241 f"cannot follow default field"
2242 f"{'s' if len(default_names) > 1 else ''} "
2243 f"{', '.join(default_names)}")
2244 nm_tpl = _make_nmtuple(typename, types.items(),
2245 defaults=[ns[n] for n in default_names],
2246 module=ns['__module__'])
Guido van Rossum95919c02017-01-22 17:47:20 -08002247 # update from user namespace without overriding special namedtuple attributes
2248 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002249 if key in _prohibited:
2250 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
2251 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08002252 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08002253 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002254
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002255
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002256def NamedTuple(typename, fields=None, /, **kwargs):
Guido van Rossum2f841442016-11-15 09:48:06 -08002257 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002258
Guido van Rossum2f841442016-11-15 09:48:06 -08002259 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002260
Guido van Rossum2f841442016-11-15 09:48:06 -08002261 class Employee(NamedTuple):
2262 name: str
2263 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002264
Guido van Rossum2f841442016-11-15 09:48:06 -08002265 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002266
Guido van Rossum2f841442016-11-15 09:48:06 -08002267 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002268
Raymond Hettingerf7b57df2019-03-18 09:53:56 -07002269 The resulting class has an extra __annotations__ attribute, giving a
2270 dict that maps field names to types. (The field names are also in
2271 the _fields attribute, which is part of the namedtuple API.)
2272 Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002273
Guido van Rossum2f841442016-11-15 09:48:06 -08002274 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002275
Guido van Rossum2f841442016-11-15 09:48:06 -08002276 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002277
Guido van Rossum2f841442016-11-15 09:48:06 -08002278 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
2279 """
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002280 if fields is None:
2281 fields = kwargs.items()
2282 elif kwargs:
2283 raise TypeError("Either list of fields or keywords"
2284 " can be provided to NamedTuple, not both")
2285 try:
2286 module = sys._getframe(1).f_globals.get('__name__', '__main__')
2287 except (AttributeError, ValueError):
2288 module = None
2289 return _make_nmtuple(typename, fields, module=module)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002290
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002291_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})
2292
2293def _namedtuple_mro_entries(bases):
2294 if len(bases) > 1:
2295 raise TypeError("Multiple inheritance with NamedTuple is not supported")
2296 assert bases[0] is NamedTuple
2297 return (_NamedTuple,)
2298
2299NamedTuple.__mro_entries__ = _namedtuple_mro_entries
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002300
2301
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002302class _TypedDictMeta(type):
2303 def __new__(cls, name, bases, ns, total=True):
2304 """Create new typed dict class object.
2305
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002306 This method is called when TypedDict is subclassed,
2307 or when TypedDict is instantiated. This way
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002308 TypedDict supports all three syntax forms described in its docstring.
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002309 Subclasses and instances of TypedDict return actual dictionaries.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002310 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002311 for base in bases:
2312 if type(base) is not _TypedDictMeta:
2313 raise TypeError('cannot inherit from both a TypedDict type '
2314 'and a non-TypedDict base class')
2315 tp_dict = type.__new__(_TypedDictMeta, name, (dict,), ns)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002316
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002317 annotations = {}
2318 own_annotations = ns.get('__annotations__', {})
2319 own_annotation_keys = set(own_annotations.keys())
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002320 msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002321 own_annotations = {
Miss Islington (bot)480f29f2021-07-17 01:48:17 -07002322 n: _type_check(tp, msg, module=tp_dict.__module__)
2323 for n, tp in own_annotations.items()
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002324 }
2325 required_keys = set()
2326 optional_keys = set()
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002327
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002328 for base in bases:
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002329 annotations.update(base.__dict__.get('__annotations__', {}))
2330 required_keys.update(base.__dict__.get('__required_keys__', ()))
2331 optional_keys.update(base.__dict__.get('__optional_keys__', ()))
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002332
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002333 annotations.update(own_annotations)
2334 if total:
2335 required_keys.update(own_annotation_keys)
2336 else:
2337 optional_keys.update(own_annotation_keys)
2338
2339 tp_dict.__annotations__ = annotations
2340 tp_dict.__required_keys__ = frozenset(required_keys)
2341 tp_dict.__optional_keys__ = frozenset(optional_keys)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002342 if not hasattr(tp_dict, '__total__'):
2343 tp_dict.__total__ = total
2344 return tp_dict
2345
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002346 __call__ = dict # static method
2347
2348 def __subclasscheck__(cls, other):
2349 # Typed dicts are only for static structural subtyping.
2350 raise TypeError('TypedDict does not support instance and class checks')
2351
2352 __instancecheck__ = __subclasscheck__
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002353
2354
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002355def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002356 """A simple typed namespace. At runtime it is equivalent to a plain dict.
2357
2358 TypedDict creates a dictionary type that expects all of its
2359 instances to have a certain set of keys, where each key is
2360 associated with a value of a consistent type. This expectation
2361 is not checked at runtime but is only enforced by type checkers.
2362 Usage::
2363
2364 class Point2D(TypedDict):
2365 x: int
2366 y: int
2367 label: str
2368
2369 a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
2370 b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
2371
2372 assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
2373
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002374 The type info can be accessed via the Point2D.__annotations__ dict, and
2375 the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
2376 TypedDict supports two additional equivalent forms::
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002377
2378 Point2D = TypedDict('Point2D', x=int, y=int, label=str)
2379 Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
2380
ananthan-123ab6423f2020-02-19 10:03:05 +05302381 By default, all keys must be present in a TypedDict. It is possible
2382 to override this by specifying totality.
2383 Usage::
2384
2385 class point2D(TypedDict, total=False):
2386 x: int
2387 y: int
2388
2389 This means that a point2D TypedDict can have any of the keys omitted.A type
2390 checker is only expected to support a literal False or True as the value of
2391 the total argument. True is the default, and makes all items defined in the
2392 class body be required.
2393
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002394 The class syntax is only supported in Python 3.6+, while two other
2395 syntax forms work for Python 2.7 and 3.2+
2396 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002397 if fields is None:
2398 fields = kwargs
2399 elif kwargs:
2400 raise TypeError("TypedDict takes either a dict or keyword arguments,"
2401 " but not both")
2402
Alex Grönholm67b769f2020-12-10 23:49:05 +02002403 ns = {'__annotations__': dict(fields)}
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002404 try:
2405 # Setting correct module is necessary to make typed dict classes pickleable.
2406 ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
2407 except (AttributeError, ValueError):
2408 pass
2409
Alex Grönholm67b769f2020-12-10 23:49:05 +02002410 return _TypedDictMeta(typename, (), ns, total=total)
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002411
2412_TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {})
2413TypedDict.__mro_entries__ = lambda bases: (_TypedDict,)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002414
2415
Miss Islington (bot)c2f33df2021-07-20 08:24:57 -07002416class NewType:
Guido van Rossum91185fe2016-06-08 11:19:11 -07002417 """NewType creates simple unique types with almost zero
2418 runtime overhead. NewType(name, tp) is considered a subtype of tp
2419 by static type checkers. At runtime, NewType(name, tp) returns
2420 a dummy function that simply returns its argument. Usage::
2421
2422 UserId = NewType('UserId', int)
2423
2424 def name_by_id(user_id: UserId) -> str:
2425 ...
2426
2427 UserId('user') # Fails type check
2428
2429 name_by_id(42) # Fails type check
2430 name_by_id(UserId(42)) # OK
2431
2432 num = UserId(5) + 1 # type: int
2433 """
2434
Miss Islington (bot)c2f33df2021-07-20 08:24:57 -07002435 def __init__(self, name, tp):
Miss Islington (bot)c2f33df2021-07-20 08:24:57 -07002436 self.__qualname__ = name
Łukasz Langa05f5d8e2021-07-24 12:07:56 +02002437 if '.' in name:
2438 name = name.rpartition('.')[-1]
2439 self.__name__ = name
Miss Islington (bot)c2f33df2021-07-20 08:24:57 -07002440 self.__supertype__ = tp
Miss Islington (bot)56122b02021-07-30 06:48:01 -07002441 def_mod = _caller()
2442 if def_mod != 'typing':
2443 self.__module__ = def_mod
Miss Islington (bot)c2f33df2021-07-20 08:24:57 -07002444
2445 def __repr__(self):
2446 return f'{self.__module__}.{self.__qualname__}'
2447
2448 def __call__(self, x):
Guido van Rossum91185fe2016-06-08 11:19:11 -07002449 return x
2450
Łukasz Langa05f5d8e2021-07-24 12:07:56 +02002451 def __reduce__(self):
2452 return self.__qualname__
2453
Miss Islington (bot)c2f33df2021-07-20 08:24:57 -07002454 def __or__(self, other):
2455 return Union[self, other]
2456
2457 def __ror__(self, other):
2458 return Union[other, self]
Guido van Rossum91185fe2016-06-08 11:19:11 -07002459
2460
Guido van Rossum0e0563c2016-04-05 14:54:25 -07002461# Python-version-specific alias (Python 2: unicode; Python 3: str)
2462Text = str
2463
2464
Guido van Rossum91185fe2016-06-08 11:19:11 -07002465# Constant that's True when type checking, but False here.
2466TYPE_CHECKING = False
2467
2468
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002469class IO(Generic[AnyStr]):
2470 """Generic base class for TextIO and BinaryIO.
2471
2472 This is an abstract, generic version of the return of open().
2473
2474 NOTE: This does not distinguish between the different possible
2475 classes (text vs. binary, read vs. write vs. read/write,
2476 append-only, unbuffered). The TextIO and BinaryIO subclasses
2477 below capture the distinctions between text vs. binary, which is
2478 pervasive in the interface; however we currently do not offer a
2479 way to track the other distinctions in the type system.
2480 """
2481
Guido van Rossumd70fe632015-08-05 12:11:06 +02002482 __slots__ = ()
2483
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002484 @property
2485 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002486 def mode(self) -> str:
2487 pass
2488
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002489 @property
2490 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002491 def name(self) -> str:
2492 pass
2493
2494 @abstractmethod
2495 def close(self) -> None:
2496 pass
2497
Shantanu2e6569b2020-01-29 18:52:36 -08002498 @property
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002499 @abstractmethod
2500 def closed(self) -> bool:
2501 pass
2502
2503 @abstractmethod
2504 def fileno(self) -> int:
2505 pass
2506
2507 @abstractmethod
2508 def flush(self) -> None:
2509 pass
2510
2511 @abstractmethod
2512 def isatty(self) -> bool:
2513 pass
2514
2515 @abstractmethod
2516 def read(self, n: int = -1) -> AnyStr:
2517 pass
2518
2519 @abstractmethod
2520 def readable(self) -> bool:
2521 pass
2522
2523 @abstractmethod
2524 def readline(self, limit: int = -1) -> AnyStr:
2525 pass
2526
2527 @abstractmethod
2528 def readlines(self, hint: int = -1) -> List[AnyStr]:
2529 pass
2530
2531 @abstractmethod
2532 def seek(self, offset: int, whence: int = 0) -> int:
2533 pass
2534
2535 @abstractmethod
2536 def seekable(self) -> bool:
2537 pass
2538
2539 @abstractmethod
2540 def tell(self) -> int:
2541 pass
2542
2543 @abstractmethod
2544 def truncate(self, size: int = None) -> int:
2545 pass
2546
2547 @abstractmethod
2548 def writable(self) -> bool:
2549 pass
2550
2551 @abstractmethod
2552 def write(self, s: AnyStr) -> int:
2553 pass
2554
2555 @abstractmethod
2556 def writelines(self, lines: List[AnyStr]) -> None:
2557 pass
2558
2559 @abstractmethod
2560 def __enter__(self) -> 'IO[AnyStr]':
2561 pass
2562
2563 @abstractmethod
2564 def __exit__(self, type, value, traceback) -> None:
2565 pass
2566
2567
2568class BinaryIO(IO[bytes]):
2569 """Typed version of the return of open() in binary mode."""
2570
Guido van Rossumd70fe632015-08-05 12:11:06 +02002571 __slots__ = ()
2572
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002573 @abstractmethod
2574 def write(self, s: Union[bytes, bytearray]) -> int:
2575 pass
2576
2577 @abstractmethod
2578 def __enter__(self) -> 'BinaryIO':
2579 pass
2580
2581
2582class TextIO(IO[str]):
2583 """Typed version of the return of open() in text mode."""
2584
Guido van Rossumd70fe632015-08-05 12:11:06 +02002585 __slots__ = ()
2586
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002587 @property
2588 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002589 def buffer(self) -> BinaryIO:
2590 pass
2591
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002592 @property
2593 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002594 def encoding(self) -> str:
2595 pass
2596
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002597 @property
2598 @abstractmethod
Guido van Rossum991d14f2016-11-09 13:12:51 -08002599 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002600 pass
2601
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002602 @property
2603 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002604 def line_buffering(self) -> bool:
2605 pass
2606
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002607 @property
2608 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002609 def newlines(self) -> Any:
2610 pass
2611
2612 @abstractmethod
2613 def __enter__(self) -> 'TextIO':
2614 pass
2615
2616
2617class io:
2618 """Wrapper namespace for IO generic classes."""
2619
2620 __all__ = ['IO', 'TextIO', 'BinaryIO']
2621 IO = IO
2622 TextIO = TextIO
2623 BinaryIO = BinaryIO
2624
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002625
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002626io.__name__ = __name__ + '.io'
2627sys.modules[io.__name__] = io
2628
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002629Pattern = _alias(stdlib_re.Pattern, 1)
2630Match = _alias(stdlib_re.Match, 1)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002631
2632class re:
2633 """Wrapper namespace for re type aliases."""
2634
2635 __all__ = ['Pattern', 'Match']
2636 Pattern = Pattern
2637 Match = Match
2638
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002639
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002640re.__name__ = __name__ + '.re'
2641sys.modules[re.__name__] = re