blob: ff964343c5336ac699b154c5ec790132f58eb392 [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',
102
103 # One-off things.
104 'AnyStr',
105 'cast',
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100106 'final',
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +0100107 'get_args',
108 'get_origin',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700109 'get_type_hints',
Patrick Reader0705ec82020-09-16 05:58:32 +0100110 'is_typeddict',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700111 'NewType',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700112 'no_type_check',
113 'no_type_check_decorator',
aetracht45738202018-03-19 14:41:32 -0400114 'NoReturn',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700115 'overload',
Jelle Zijlstra52243362021-04-10 19:57:05 -0700116 'ParamSpecArgs',
117 'ParamSpecKwargs',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100118 'runtime_checkable',
Guido van Rossum0e0563c2016-04-05 14:54:25 -0700119 'Text',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700120 'TYPE_CHECKING',
Mikhail Golubev4f3c2502020-10-08 00:44:31 +0300121 'TypeAlias',
Ken Jin05ab4b62021-04-27 22:31:04 +0800122 'TypeGuard',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700123]
124
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700125# The pseudo-submodules 're' and 'io' are part of the public
126# namespace, but excluded from __all__ because they might stomp on
127# legitimate imports of those modules.
128
kj463c7d32020-12-14 02:38:24 +0800129
130def _type_convert(arg):
131 """For converting None to type(None), and strings to ForwardRef."""
132 if arg is None:
133 return type(None)
134 if isinstance(arg, str):
135 return ForwardRef(arg)
136 return arg
137
138
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700139def _type_check(arg, msg, is_argument=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000140 """Check that the argument is a type, and return it (internal helper).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700141
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000142 As a special case, accept None and return type(None) instead. Also wrap strings
143 into ForwardRef instances. Consider several corner cases, for example plain
144 special forms like Union are not valid, while Union[int, str] is OK, etc.
145 The msg argument is a human-readable error message, e.g::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700146
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000147 "Union[arg, ...]: arg should be a type."
Guido van Rossum4cefe742016-09-27 15:20:12 -0700148
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000149 We append the repr() of the actual value (truncated to 100 chars).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700150 """
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100151 invalid_generic_forms = (Generic, Protocol)
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700152 if is_argument:
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100153 invalid_generic_forms = invalid_generic_forms + (ClassVar, Final)
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400154
kj463c7d32020-12-14 02:38:24 +0800155 arg = _type_convert(arg)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000156 if (isinstance(arg, _GenericAlias) and
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400157 arg.__origin__ in invalid_generic_forms):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000158 raise TypeError(f"{arg} is not valid as type argument")
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300159 if arg in (Any, NoReturn):
160 return arg
161 if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000162 raise TypeError(f"Plain {arg} is not valid as type argument")
kj73607be2020-12-24 12:33:48 +0800163 if isinstance(arg, (type, TypeVar, ForwardRef, types.Union, ParamSpec)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000164 return arg
165 if not callable(arg):
166 raise TypeError(f"{msg} Got {arg!r:.100}.")
167 return arg
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700168
169
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000170def _type_repr(obj):
171 """Return the repr() of an object, special-casing types (internal helper).
172
173 If obj is a type, we return a shorter version than the default
174 type.__repr__, based on the module and qualified name, which is
175 typically enough to uniquely identify a type. For everything
176 else, we fall back on repr(obj).
177 """
kj1f7dfb22020-11-02 02:13:38 +0800178 if isinstance(obj, types.GenericAlias):
179 return repr(obj)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000180 if isinstance(obj, type):
181 if obj.__module__ == 'builtins':
182 return obj.__qualname__
183 return f'{obj.__module__}.{obj.__qualname__}'
184 if obj is ...:
185 return('...')
186 if isinstance(obj, types.FunctionType):
187 return obj.__name__
188 return repr(obj)
189
190
191def _collect_type_vars(types):
kj73607be2020-12-24 12:33:48 +0800192 """Collect all type variable-like variables contained
193 in types in order of first appearance (lexicographic order). For example::
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000194
195 _collect_type_vars((T, List[S, T])) == (T, S)
196 """
197 tvars = []
198 for t in types:
kj73607be2020-12-24 12:33:48 +0800199 if isinstance(t, _TypeVarLike) and t not in tvars:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000200 tvars.append(t)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300201 if isinstance(t, (_GenericAlias, GenericAlias)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000202 tvars.extend([t for t in t.__parameters__ if t not in tvars])
203 return tuple(tvars)
204
205
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300206def _check_generic(cls, parameters, elen):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000207 """Check correct count for parameters of a generic cls (internal helper).
208 This gives a nice error message in case of count mismatch.
209 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300210 if not elen:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000211 raise TypeError(f"{cls} is not a generic class")
212 alen = len(parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000213 if alen != elen:
214 raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
215 f" actual {alen}, expected {elen}")
216
kj73607be2020-12-24 12:33:48 +0800217def _prepare_paramspec_params(cls, params):
218 """Prepares the parameters for a Generic containing ParamSpec
219 variables (internal helper).
220 """
221 # Special case where Z[[int, str, bool]] == Z[int, str, bool] in PEP 612.
222 if len(cls.__parameters__) == 1 and len(params) > 1:
223 return (params,)
224 else:
225 _params = []
226 # Convert lists to tuples to help other libraries cache the results.
227 for p, tvar in zip(params, cls.__parameters__):
228 if isinstance(tvar, ParamSpec) and isinstance(p, list):
229 p = tuple(p)
230 _params.append(p)
231 return tuple(_params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000232
Yurii Karabasf03d3182020-11-17 04:23:19 +0200233def _deduplicate(params):
234 # Weed out strict duplicates, preserving the first of each occurrence.
235 all_params = set(params)
236 if len(all_params) < len(params):
237 new_params = []
238 for t in params:
239 if t in all_params:
240 new_params.append(t)
241 all_params.remove(t)
242 params = new_params
243 assert not all_params, all_params
244 return params
245
246
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000247def _remove_dups_flatten(parameters):
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700248 """An internal helper for Union creation and substitution: flatten Unions
249 among parameters, then remove duplicates.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000250 """
251 # Flatten out Union[Union[...], ...].
252 params = []
253 for p in parameters:
Maggie Moss1b4552c2020-09-09 13:23:24 -0700254 if isinstance(p, (_UnionGenericAlias, types.Union)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000255 params.extend(p.__args__)
256 elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
257 params.extend(p[1:])
258 else:
259 params.append(p)
Yurii Karabasf03d3182020-11-17 04:23:19 +0200260
261 return tuple(_deduplicate(params))
262
263
264def _flatten_literal_params(parameters):
265 """An internal helper for Literal creation: flatten Literals among parameters"""
266 params = []
267 for p in parameters:
268 if isinstance(p, _LiteralGenericAlias):
269 params.extend(p.__args__)
270 else:
271 params.append(p)
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700272 return tuple(params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000273
274
275_cleanups = []
276
277
Yurii Karabasf03d3182020-11-17 04:23:19 +0200278def _tp_cache(func=None, /, *, typed=False):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000279 """Internal wrapper caching __getitem__ of generic types with a fallback to
280 original function for non-hashable arguments.
281 """
Yurii Karabasf03d3182020-11-17 04:23:19 +0200282 def decorator(func):
283 cached = functools.lru_cache(typed=typed)(func)
284 _cleanups.append(cached.cache_clear)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000285
Yurii Karabasf03d3182020-11-17 04:23:19 +0200286 @functools.wraps(func)
287 def inner(*args, **kwds):
288 try:
289 return cached(*args, **kwds)
290 except TypeError:
291 pass # All real errors (not unhashable args) are raised below.
292 return func(*args, **kwds)
293 return inner
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000294
Yurii Karabasf03d3182020-11-17 04:23:19 +0200295 if func is not None:
296 return decorator(func)
297
298 return decorator
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000299
wyfo653f4202020-07-22 21:47:28 +0200300def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
Graham Bleaney84ef33c2020-09-08 18:41:10 -0400301 """Evaluate all forward references in the given type t.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000302 For use of globalns and localns see the docstring for get_type_hints().
wyfo653f4202020-07-22 21:47:28 +0200303 recursive_guard is used to prevent prevent infinite recursion
304 with recursive ForwardRef.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000305 """
306 if isinstance(t, ForwardRef):
wyfo653f4202020-07-22 21:47:28 +0200307 return t._evaluate(globalns, localns, recursive_guard)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300308 if isinstance(t, (_GenericAlias, GenericAlias)):
wyfo653f4202020-07-22 21:47:28 +0200309 ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000310 if ev_args == t.__args__:
311 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300312 if isinstance(t, GenericAlias):
313 return GenericAlias(t.__origin__, ev_args)
314 else:
315 return t.copy_with(ev_args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000316 return t
317
318
319class _Final:
320 """Mixin to prohibit subclassing"""
Guido van Rossum4cefe742016-09-27 15:20:12 -0700321
Guido van Rossum83ec3022017-01-17 20:43:28 -0800322 __slots__ = ('__weakref__',)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700323
Serhiy Storchaka2085bd02019-06-01 11:00:15 +0300324 def __init_subclass__(self, /, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000325 if '_root' not in kwds:
326 raise TypeError("Cannot subclass special typing classes")
327
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100328class _Immutable:
329 """Mixin to indicate that object should not be copied."""
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300330 __slots__ = ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000331
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100332 def __copy__(self):
333 return self
334
335 def __deepcopy__(self, memo):
336 return self
337
338
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300339# Internal indicator of special typing constructs.
340# See __doc__ instance attribute for specific docs.
341class _SpecialForm(_Final, _root=True):
342 __slots__ = ('_name', '__doc__', '_getitem')
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000343
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300344 def __init__(self, getitem):
345 self._getitem = getitem
346 self._name = getitem.__name__
347 self.__doc__ = getitem.__doc__
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000348
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300349 def __mro_entries__(self, bases):
350 raise TypeError(f"Cannot subclass {self!r}")
Guido van Rossum4cefe742016-09-27 15:20:12 -0700351
352 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000353 return 'typing.' + self._name
354
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100355 def __reduce__(self):
356 return self._name
Guido van Rossum4cefe742016-09-27 15:20:12 -0700357
358 def __call__(self, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000359 raise TypeError(f"Cannot instantiate {self!r}")
360
361 def __instancecheck__(self, obj):
362 raise TypeError(f"{self} cannot be used with isinstance()")
363
364 def __subclasscheck__(self, cls):
365 raise TypeError(f"{self} cannot be used with issubclass()")
366
367 @_tp_cache
368 def __getitem__(self, parameters):
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300369 return self._getitem(self, parameters)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700370
Yurii Karabasf03d3182020-11-17 04:23:19 +0200371
372class _LiteralSpecialForm(_SpecialForm, _root=True):
373 @_tp_cache(typed=True)
374 def __getitem__(self, parameters):
375 return self._getitem(self, parameters)
376
377
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300378@_SpecialForm
379def Any(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000380 """Special type indicating an unconstrained type.
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700381
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000382 - Any is compatible with every type.
383 - Any assumed to have all methods.
384 - All values assumed to be instances of Any.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700385
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000386 Note that all the above statements are true from the point of view of
387 static type checkers. At runtime, Any should not be used with instance
388 or class checks.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300389 """
390 raise TypeError(f"{self} is not subscriptable")
Guido van Rossumd70fe632015-08-05 12:11:06 +0200391
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300392@_SpecialForm
393def NoReturn(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000394 """Special type indicating functions that never return.
395 Example::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700396
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000397 from typing import NoReturn
398
399 def stop() -> NoReturn:
400 raise Exception('no way')
401
402 This type is invalid in other positions, e.g., ``List[NoReturn]``
403 will fail in static type checkers.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300404 """
405 raise TypeError(f"{self} is not subscriptable")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000406
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300407@_SpecialForm
408def ClassVar(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000409 """Special type construct to mark class variables.
410
411 An annotation wrapped in ClassVar indicates that a given
412 attribute is intended to be used as a class variable and
413 should not be set on instances of that class. Usage::
414
415 class Starship:
416 stats: ClassVar[Dict[str, int]] = {} # class variable
417 damage: int = 10 # instance variable
418
419 ClassVar accepts only types and cannot be further subscribed.
420
421 Note that ClassVar is not a class itself, and should not
422 be used with isinstance() or issubclass().
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300423 """
424 item = _type_check(parameters, f'{self} accepts only single type.')
425 return _GenericAlias(self, (item,))
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000426
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300427@_SpecialForm
428def Final(self, parameters):
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100429 """Special typing construct to indicate final names to type checkers.
430
431 A final name cannot be re-assigned or overridden in a subclass.
432 For example:
433
434 MAX_SIZE: Final = 9000
435 MAX_SIZE += 1 # Error reported by type checker
436
437 class Connection:
438 TIMEOUT: Final[int] = 10
439
440 class FastConnector(Connection):
441 TIMEOUT = 1 # Error reported by type checker
442
443 There is no runtime checking of these properties.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300444 """
445 item = _type_check(parameters, f'{self} accepts only single type.')
446 return _GenericAlias(self, (item,))
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100447
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300448@_SpecialForm
449def Union(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000450 """Union type; Union[X, Y] means either X or Y.
451
452 To define a union, use e.g. Union[int, str]. Details:
453 - The arguments must be types and there must be at least one.
454 - None as an argument is a special case and is replaced by
455 type(None).
456 - Unions of unions are flattened, e.g.::
457
458 Union[Union[int, str], float] == Union[int, str, float]
459
460 - Unions of a single argument vanish, e.g.::
461
462 Union[int] == int # The constructor actually returns int
463
464 - Redundant arguments are skipped, e.g.::
465
466 Union[int, str, int] == Union[int, str]
467
468 - When comparing unions, the argument order is ignored, e.g.::
469
470 Union[int, str] == Union[str, int]
471
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000472 - You cannot subclass or instantiate a union.
473 - You can use Optional[X] as a shorthand for Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300474 """
475 if parameters == ():
476 raise TypeError("Cannot take a Union of no types.")
477 if not isinstance(parameters, tuple):
478 parameters = (parameters,)
479 msg = "Union[arg, ...]: each arg must be a type."
480 parameters = tuple(_type_check(p, msg) for p in parameters)
481 parameters = _remove_dups_flatten(parameters)
482 if len(parameters) == 1:
483 return parameters[0]
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300484 return _UnionGenericAlias(self, parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000485
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300486@_SpecialForm
487def Optional(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000488 """Optional type.
489
490 Optional[X] is equivalent to Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300491 """
492 arg = _type_check(parameters, f"{self} requires a single type.")
493 return Union[arg, type(None)]
Guido van Rossumb7dedc82016-10-29 12:44:29 -0700494
Yurii Karabasf03d3182020-11-17 04:23:19 +0200495@_LiteralSpecialForm
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300496def Literal(self, parameters):
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100497 """Special typing form to define literal types (a.k.a. value types).
498
499 This form can be used to indicate to type checkers that the corresponding
500 variable or function parameter has a value equivalent to the provided
501 literal (or one of several literals):
502
503 def validate_simple(data: Any) -> Literal[True]: # always returns True
504 ...
505
506 MODE = Literal['r', 'rb', 'w', 'wb']
507 def open_helper(file: str, mode: MODE) -> str:
508 ...
509
510 open_helper('/some/path', 'r') # Passes type check
511 open_helper('/other/path', 'typo') # Error in type checker
512
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300513 Literal[...] cannot be subclassed. At runtime, an arbitrary value
514 is allowed as type argument to Literal[...], but type checkers may
515 impose restrictions.
516 """
517 # There is no '_type_check' call because arguments to Literal[...] are
518 # values, not types.
Yurii Karabasf03d3182020-11-17 04:23:19 +0200519 if not isinstance(parameters, tuple):
520 parameters = (parameters,)
521
522 parameters = _flatten_literal_params(parameters)
523
524 try:
525 parameters = tuple(p for p, _ in _deduplicate(list(_value_and_type_iter(parameters))))
526 except TypeError: # unhashable parameters
527 pass
528
529 return _LiteralGenericAlias(self, parameters)
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100530
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700531
Mikhail Golubev4f3c2502020-10-08 00:44:31 +0300532@_SpecialForm
533def TypeAlias(self, parameters):
534 """Special marker indicating that an assignment should
535 be recognized as a proper type alias definition by type
536 checkers.
537
538 For example::
539
540 Predicate: TypeAlias = Callable[..., bool]
541
542 It's invalid when used anywhere except as in the example above.
543 """
544 raise TypeError(f"{self} is not subscriptable")
545
546
kj73607be2020-12-24 12:33:48 +0800547@_SpecialForm
548def Concatenate(self, parameters):
Ken Jin11276cd2021-01-02 08:45:50 +0800549 """Used in conjunction with ``ParamSpec`` and ``Callable`` to represent a
550 higher order function which adds, removes or transforms parameters of a
551 callable.
kj73607be2020-12-24 12:33:48 +0800552
553 For example::
554
555 Callable[Concatenate[int, P], int]
556
557 See PEP 612 for detailed information.
558 """
559 if parameters == ():
560 raise TypeError("Cannot take a Concatenate of no types.")
561 if not isinstance(parameters, tuple):
562 parameters = (parameters,)
563 if not isinstance(parameters[-1], ParamSpec):
564 raise TypeError("The last parameter to Concatenate should be a "
565 "ParamSpec variable.")
566 msg = "Concatenate[arg, ...]: each arg must be a type."
567 parameters = tuple(_type_check(p, msg) for p in parameters)
568 return _ConcatenateGenericAlias(self, parameters)
569
570
Ken Jin05ab4b62021-04-27 22:31:04 +0800571@_SpecialForm
572def TypeGuard(self, parameters):
573 """Special typing form used to annotate the return type of a user-defined
574 type guard function. ``TypeGuard`` only accepts a single type argument.
575 At runtime, functions marked this way should return a boolean.
576
577 ``TypeGuard`` aims to benefit *type narrowing* -- a technique used by static
578 type checkers to determine a more precise type of an expression within a
579 program's code flow. Usually type narrowing is done by analyzing
580 conditional code flow and applying the narrowing to a block of code. The
581 conditional expression here is sometimes referred to as a "type guard".
582
583 Sometimes it would be convenient to use a user-defined boolean function
584 as a type guard. Such a function should use ``TypeGuard[...]`` as its
585 return type to alert static type checkers to this intention.
586
587 Using ``-> TypeGuard`` tells the static type checker that for a given
588 function:
589
590 1. The return value is a boolean.
591 2. If the return value is ``True``, the type of its argument
592 is the type inside ``TypeGuard``.
593
594 For example::
595
596 def is_str(val: Union[str, float]):
597 # "isinstance" type guard
598 if isinstance(val, str):
599 # Type of ``val`` is narrowed to ``str``
600 ...
601 else:
602 # Else, type of ``val`` is narrowed to ``float``.
603 ...
604
605 Strict type narrowing is not enforced -- ``TypeB`` need not be a narrower
606 form of ``TypeA`` (it can even be a wider form) and this may lead to
607 type-unsafe results. The main reason is to allow for things like
608 narrowing ``List[object]`` to ``List[str]`` even though the latter is not
609 a subtype of the former, since ``List`` is invariant. The responsibility of
610 writing type-safe type guards is left to the user.
611
612 ``TypeGuard`` also works with type variables. For more information, see
613 PEP 647 (User-Defined Type Guards).
614 """
615 item = _type_check(parameters, f'{self} accepts only single type.')
616 return _GenericAlias(self, (item,))
617
618
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000619class ForwardRef(_Final, _root=True):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800620 """Internal wrapper to hold a forward reference."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700621
Guido van Rossum4cefe742016-09-27 15:20:12 -0700622 __slots__ = ('__forward_arg__', '__forward_code__',
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400623 '__forward_evaluated__', '__forward_value__',
624 '__forward_is_argument__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700625
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700626 def __init__(self, arg, is_argument=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700627 if not isinstance(arg, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000628 raise TypeError(f"Forward reference must be a string -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700629 try:
630 code = compile(arg, '<string>', 'eval')
631 except SyntaxError:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000632 raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700633 self.__forward_arg__ = arg
634 self.__forward_code__ = code
635 self.__forward_evaluated__ = False
636 self.__forward_value__ = None
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400637 self.__forward_is_argument__ = is_argument
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700638
wyfo653f4202020-07-22 21:47:28 +0200639 def _evaluate(self, globalns, localns, recursive_guard):
640 if self.__forward_arg__ in recursive_guard:
641 return self
Guido van Rossumdad17902016-11-10 08:29:18 -0800642 if not self.__forward_evaluated__ or localns is not globalns:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700643 if globalns is None and localns is None:
644 globalns = localns = {}
645 elif globalns is None:
646 globalns = localns
647 elif localns is None:
648 localns = globalns
wyfo653f4202020-07-22 21:47:28 +0200649 type_ =_type_check(
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700650 eval(self.__forward_code__, globalns, localns),
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400651 "Forward references must evaluate to types.",
wyfo653f4202020-07-22 21:47:28 +0200652 is_argument=self.__forward_is_argument__,
653 )
654 self.__forward_value__ = _eval_type(
655 type_, globalns, localns, recursive_guard | {self.__forward_arg__}
656 )
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700657 self.__forward_evaluated__ = True
658 return self.__forward_value__
659
Guido van Rossum4cefe742016-09-27 15:20:12 -0700660 def __eq__(self, other):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000661 if not isinstance(other, ForwardRef):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700662 return NotImplemented
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100663 if self.__forward_evaluated__ and other.__forward_evaluated__:
664 return (self.__forward_arg__ == other.__forward_arg__ and
665 self.__forward_value__ == other.__forward_value__)
666 return self.__forward_arg__ == other.__forward_arg__
Guido van Rossum4cefe742016-09-27 15:20:12 -0700667
668 def __hash__(self):
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100669 return hash(self.__forward_arg__)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700670
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700671 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000672 return f'ForwardRef({self.__forward_arg__!r})'
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700673
kj73607be2020-12-24 12:33:48 +0800674class _TypeVarLike:
675 """Mixin for TypeVar-like types (TypeVar and ParamSpec)."""
676 def __init__(self, bound, covariant, contravariant):
677 """Used to setup TypeVars and ParamSpec's bound, covariant and
678 contravariant attributes.
679 """
680 if covariant and contravariant:
681 raise ValueError("Bivariant types are not supported.")
682 self.__covariant__ = bool(covariant)
683 self.__contravariant__ = bool(contravariant)
684 if bound:
685 self.__bound__ = _type_check(bound, "Bound must be a type.")
686 else:
687 self.__bound__ = None
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700688
kj73607be2020-12-24 12:33:48 +0800689 def __or__(self, right):
690 return Union[self, right]
691
Jelle Zijlstra90459192021-04-10 20:00:05 -0700692 def __ror__(self, left):
693 return Union[left, self]
kj73607be2020-12-24 12:33:48 +0800694
695 def __repr__(self):
696 if self.__covariant__:
697 prefix = '+'
698 elif self.__contravariant__:
699 prefix = '-'
700 else:
701 prefix = '~'
702 return prefix + self.__name__
703
704 def __reduce__(self):
705 return self.__name__
706
707
708class TypeVar( _Final, _Immutable, _TypeVarLike, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700709 """Type variable.
710
711 Usage::
712
713 T = TypeVar('T') # Can be anything
714 A = TypeVar('A', str, bytes) # Must be str or bytes
715
716 Type variables exist primarily for the benefit of static type
717 checkers. They serve as the parameters for generic types as well
718 as for generic function definitions. See class Generic for more
719 information on generic types. Generic functions work as follows:
720
Guido van Rossumb24569a2016-11-20 18:01:29 -0800721 def repeat(x: T, n: int) -> List[T]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700722 '''Return a list containing n references to x.'''
723 return [x]*n
724
725 def longest(x: A, y: A) -> A:
726 '''Return the longest of two strings.'''
727 return x if len(x) >= len(y) else y
728
729 The latter example's signature is essentially the overloading
730 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
731 that if the arguments are instances of some subclass of str,
732 the return type is still plain str.
733
Guido van Rossumb24569a2016-11-20 18:01:29 -0800734 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700735
Guido van Rossumefa798d2016-08-23 11:01:50 -0700736 Type variables defined with covariant=True or contravariant=True
João D. Ferreira86bfed32018-07-07 16:41:20 +0100737 can be used to declare covariant or contravariant generic types.
Guido van Rossumefa798d2016-08-23 11:01:50 -0700738 See PEP 484 for more details. By default generic types are invariant
739 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700740
741 Type variables can be introspected. e.g.:
742
743 T.__name__ == 'T'
744 T.__constraints__ == ()
745 T.__covariant__ == False
746 T.__contravariant__ = False
747 A.__constraints__ == (str, bytes)
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100748
749 Note that only type variables defined in global scope can be pickled.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700750 """
751
Guido van Rossum4cefe742016-09-27 15:20:12 -0700752 __slots__ = ('__name__', '__bound__', '__constraints__',
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300753 '__covariant__', '__contravariant__', '__dict__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700754
755 def __init__(self, name, *constraints, bound=None,
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800756 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700757 self.__name__ = name
kj73607be2020-12-24 12:33:48 +0800758 super().__init__(bound, covariant, contravariant)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700759 if constraints and bound is not None:
760 raise TypeError("Constraints cannot be combined with bound=...")
761 if constraints and len(constraints) == 1:
762 raise TypeError("A single constraint is not allowed")
763 msg = "TypeVar(name, constraint, ...): constraints must be types."
764 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
HongWeipenga25a04f2020-04-21 04:01:53 +0800765 try:
766 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling
767 except (AttributeError, ValueError):
768 def_mod = None
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300769 if def_mod != 'typing':
770 self.__module__ = def_mod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700771
Maggie Moss1b4552c2020-09-09 13:23:24 -0700772
Jelle Zijlstra52243362021-04-10 19:57:05 -0700773class ParamSpecArgs(_Final, _Immutable, _root=True):
774 """The args for a ParamSpec object.
775
776 Given a ParamSpec object P, P.args is an instance of ParamSpecArgs.
777
778 ParamSpecArgs objects have a reference back to their ParamSpec:
779
780 P.args.__origin__ is P
781
782 This type is meant for runtime introspection and has no special meaning to
783 static type checkers.
784 """
785 def __init__(self, origin):
786 self.__origin__ = origin
787
788 def __repr__(self):
789 return f"{self.__origin__.__name__}.args"
790
791
792class ParamSpecKwargs(_Final, _Immutable, _root=True):
793 """The kwargs for a ParamSpec object.
794
795 Given a ParamSpec object P, P.kwargs is an instance of ParamSpecKwargs.
796
797 ParamSpecKwargs objects have a reference back to their ParamSpec:
798
799 P.kwargs.__origin__ is P
800
801 This type is meant for runtime introspection and has no special meaning to
802 static type checkers.
803 """
804 def __init__(self, origin):
805 self.__origin__ = origin
806
807 def __repr__(self):
808 return f"{self.__origin__.__name__}.kwargs"
809
810
kj73607be2020-12-24 12:33:48 +0800811class ParamSpec(_Final, _Immutable, _TypeVarLike, _root=True):
812 """Parameter specification variable.
Maggie Moss1b4552c2020-09-09 13:23:24 -0700813
kj73607be2020-12-24 12:33:48 +0800814 Usage::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700815
kj73607be2020-12-24 12:33:48 +0800816 P = ParamSpec('P')
817
818 Parameter specification variables exist primarily for the benefit of static
819 type checkers. They are used to forward the parameter types of one
Ken Jin11276cd2021-01-02 08:45:50 +0800820 callable to another callable, a pattern commonly found in higher order
821 functions and decorators. They are only valid when used in ``Concatenate``,
822 or s the first argument to ``Callable``, or as parameters for user-defined
823 Generics. See class Generic for more information on generic types. An
824 example for annotating a decorator::
kj73607be2020-12-24 12:33:48 +0800825
826 T = TypeVar('T')
827 P = ParamSpec('P')
828
829 def add_logging(f: Callable[P, T]) -> Callable[P, T]:
830 '''A type-safe decorator to add logging to a function.'''
831 def inner(*args: P.args, **kwargs: P.kwargs) -> T:
832 logging.info(f'{f.__name__} was called')
833 return f(*args, **kwargs)
834 return inner
835
836 @add_logging
837 def add_two(x: float, y: float) -> float:
838 '''Add two numbers together.'''
839 return x + y
840
841 Parameter specification variables defined with covariant=True or
842 contravariant=True can be used to declare covariant or contravariant
843 generic types. These keyword arguments are valid, but their actual semantics
844 are yet to be decided. See PEP 612 for details.
845
846 Parameter specification variables can be introspected. e.g.:
847
848 P.__name__ == 'T'
849 P.__bound__ == None
850 P.__covariant__ == False
851 P.__contravariant__ == False
852
853 Note that only parameter specification variables defined in global scope can
854 be pickled.
855 """
856
857 __slots__ = ('__name__', '__bound__', '__covariant__', '__contravariant__',
858 '__dict__')
859
Jelle Zijlstra52243362021-04-10 19:57:05 -0700860 @property
861 def args(self):
862 return ParamSpecArgs(self)
863
864 @property
865 def kwargs(self):
866 return ParamSpecKwargs(self)
kj73607be2020-12-24 12:33:48 +0800867
Ken Jinace008c2021-01-11 08:11:41 +0800868 def __init__(self, name, *, bound=None, covariant=False, contravariant=False):
kj73607be2020-12-24 12:33:48 +0800869 self.__name__ = name
870 super().__init__(bound, covariant, contravariant)
871 try:
872 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__')
873 except (AttributeError, ValueError):
874 def_mod = None
875 if def_mod != 'typing':
876 self.__module__ = def_mod
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100877
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700878
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000879def _is_dunder(attr):
880 return attr.startswith('__') and attr.endswith('__')
Guido van Rossum83ec3022017-01-17 20:43:28 -0800881
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300882class _BaseGenericAlias(_Final, _root=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000883 """The central part of internal API.
884
885 This represents a generic version of type 'origin' with type arguments 'params'.
886 There are two kind of these aliases: user defined and special. The special ones
887 are wrappers around builtin collections and ABCs in collections.abc. These must
888 have 'name' always set. If 'inst' is False, then the alias can't be instantiated,
889 this is used by e.g. typing.List and typing.Dict.
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700890 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300891 def __init__(self, origin, *, inst=True, name=None):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000892 self._inst = inst
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000893 self._name = name
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700894 self.__origin__ = origin
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000895 self.__slots__ = None # This is not documented.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300896
897 def __call__(self, *args, **kwargs):
898 if not self._inst:
899 raise TypeError(f"Type {self._name} cannot be instantiated; "
900 f"use {self.__origin__.__name__}() instead")
901 result = self.__origin__(*args, **kwargs)
902 try:
903 result.__orig_class__ = self
904 except AttributeError:
905 pass
906 return result
907
908 def __mro_entries__(self, bases):
909 res = []
910 if self.__origin__ not in bases:
911 res.append(self.__origin__)
912 i = bases.index(self)
913 for b in bases[i+1:]:
914 if isinstance(b, _BaseGenericAlias) or issubclass(b, Generic):
915 break
916 else:
917 res.append(Generic)
918 return tuple(res)
919
920 def __getattr__(self, attr):
921 # We are careful for copy and pickle.
922 # Also for simplicity we just don't relay all dunder names
923 if '__origin__' in self.__dict__ and not _is_dunder(attr):
924 return getattr(self.__origin__, attr)
925 raise AttributeError(attr)
926
927 def __setattr__(self, attr, val):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300928 if _is_dunder(attr) or attr in ('_name', '_inst', '_nparams'):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300929 super().__setattr__(attr, val)
930 else:
931 setattr(self.__origin__, attr, val)
932
933 def __instancecheck__(self, obj):
934 return self.__subclasscheck__(type(obj))
935
936 def __subclasscheck__(self, cls):
937 raise TypeError("Subscripted generics cannot be used with"
938 " class and instance checks")
939
940
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300941# Special typing constructs Union, Optional, Generic, Callable and Tuple
942# use three special attributes for internal bookkeeping of generic types:
943# * __parameters__ is a tuple of unique free type parameters of a generic
944# type, for example, Dict[T, T].__parameters__ == (T,);
945# * __origin__ keeps a reference to a type that was subscripted,
946# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
947# the type.
948# * __args__ is a tuple of all arguments used in subscripting,
949# e.g., Dict[T, int].__args__ == (T, int).
950
951
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300952class _GenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300953 def __init__(self, origin, params, *, inst=True, name=None):
954 super().__init__(origin, inst=inst, name=name)
955 if not isinstance(params, tuple):
956 params = (params,)
957 self.__args__ = tuple(... if a is _TypingEllipsis else
958 () if a is _TypingEmpty else
959 a for a in params)
960 self.__parameters__ = _collect_type_vars(params)
961 if not name:
962 self.__module__ = origin.__module__
963
964 def __eq__(self, other):
965 if not isinstance(other, _GenericAlias):
966 return NotImplemented
967 return (self.__origin__ == other.__origin__
968 and self.__args__ == other.__args__)
969
970 def __hash__(self):
971 return hash((self.__origin__, self.__args__))
972
Maggie Moss1b4552c2020-09-09 13:23:24 -0700973 def __or__(self, right):
974 return Union[self, right]
975
976 def __ror__(self, right):
977 return Union[self, right]
978
Guido van Rossum4cefe742016-09-27 15:20:12 -0700979 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700980 def __getitem__(self, params):
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100981 if self.__origin__ in (Generic, Protocol):
982 # Can't subscript Generic[...] or Protocol[...].
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000983 raise TypeError(f"Cannot subscript already-subscripted {self}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700984 if not isinstance(params, tuple):
985 params = (params,)
kj73607be2020-12-24 12:33:48 +0800986 params = tuple(_type_convert(p) for p in params)
987 if any(isinstance(t, ParamSpec) for t in self.__parameters__):
988 params = _prepare_paramspec_params(self, params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300989 _check_generic(self, params, len(self.__parameters__))
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300990
991 subst = dict(zip(self.__parameters__, params))
992 new_args = []
993 for arg in self.__args__:
kj73607be2020-12-24 12:33:48 +0800994 if isinstance(arg, _TypeVarLike):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300995 arg = subst[arg]
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300996 elif isinstance(arg, (_GenericAlias, GenericAlias)):
Serhiy Storchaka0122d482020-05-10 13:39:40 +0300997 subparams = arg.__parameters__
998 if subparams:
999 subargs = tuple(subst[x] for x in subparams)
1000 arg = arg[subargs]
kj73607be2020-12-24 12:33:48 +08001001 # Required to flatten out the args for CallableGenericAlias
1002 if self.__origin__ == collections.abc.Callable and isinstance(arg, tuple):
1003 new_args.extend(arg)
1004 else:
1005 new_args.append(arg)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001006 return self.copy_with(tuple(new_args))
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001007
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001008 def copy_with(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001009 return self.__class__(self.__origin__, params, name=self._name, inst=self._inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001010
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001011 def __repr__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001012 if self._name:
1013 name = 'typing.' + self._name
1014 else:
1015 name = _type_repr(self.__origin__)
1016 args = ", ".join([_type_repr(a) for a in self.__args__])
1017 return f'{name}[{args}]'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001018
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001019 def __reduce__(self):
1020 if self._name:
1021 origin = globals()[self._name]
1022 else:
1023 origin = self.__origin__
1024 args = tuple(self.__args__)
1025 if len(args) == 1 and not isinstance(args[0], tuple):
1026 args, = args
1027 return operator.getitem, (origin, args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001028
1029 def __mro_entries__(self, bases):
1030 if self._name: # generic version of an ABC or built-in class
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001031 return super().__mro_entries__(bases)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001032 if self.__origin__ is Generic:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001033 if Protocol in bases:
1034 return ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001035 i = bases.index(self)
1036 for b in bases[i+1:]:
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001037 if isinstance(b, _BaseGenericAlias) and b is not self:
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001038 return ()
1039 return (self.__origin__,)
1040
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001041
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001042# _nparams is the number of accepted parameters, e.g. 0 for Hashable,
1043# 1 for List and 2 for Dict. It may be -1 if variable number of
1044# parameters are accepted (needs custom __getitem__).
1045
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001046class _SpecialGenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001047 def __init__(self, origin, nparams, *, inst=True, name=None):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001048 if name is None:
1049 name = origin.__name__
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001050 super().__init__(origin, inst=inst, name=name)
1051 self._nparams = nparams
Serhiy Storchaka2fbc57a2020-05-10 15:14:27 +03001052 if origin.__module__ == 'builtins':
1053 self.__doc__ = f'A generic version of {origin.__qualname__}.'
1054 else:
1055 self.__doc__ = f'A generic version of {origin.__module__}.{origin.__qualname__}.'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001056
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001057 @_tp_cache
1058 def __getitem__(self, params):
1059 if not isinstance(params, tuple):
1060 params = (params,)
1061 msg = "Parameters to generic types must be types."
1062 params = tuple(_type_check(p, msg) for p in params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001063 _check_generic(self, params, self._nparams)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001064 return self.copy_with(params)
1065
1066 def copy_with(self, params):
1067 return _GenericAlias(self.__origin__, params,
1068 name=self._name, inst=self._inst)
1069
1070 def __repr__(self):
1071 return 'typing.' + self._name
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001072
1073 def __subclasscheck__(self, cls):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001074 if isinstance(cls, _SpecialGenericAlias):
1075 return issubclass(cls.__origin__, self.__origin__)
1076 if not isinstance(cls, _GenericAlias):
1077 return issubclass(cls, self.__origin__)
1078 return super().__subclasscheck__(cls)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001079
Ivan Levkivskyi83494032018-03-26 23:01:12 +01001080 def __reduce__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001081 return self._name
Ivan Levkivskyi83494032018-03-26 23:01:12 +01001082
Maggie Moss1b4552c2020-09-09 13:23:24 -07001083 def __or__(self, right):
1084 return Union[self, right]
1085
1086 def __ror__(self, right):
1087 return Union[self, right]
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001088
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001089class _CallableGenericAlias(_GenericAlias, _root=True):
1090 def __repr__(self):
1091 assert self._name == 'Callable'
kj73607be2020-12-24 12:33:48 +08001092 args = self.__args__
1093 if len(args) == 2 and (args[0] is Ellipsis
1094 or isinstance(args[0], (ParamSpec, _ConcatenateGenericAlias))):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001095 return super().__repr__()
1096 return (f'typing.Callable'
kj73607be2020-12-24 12:33:48 +08001097 f'[[{", ".join([_type_repr(a) for a in args[:-1]])}], '
1098 f'{_type_repr(args[-1])}]')
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001099
1100 def __reduce__(self):
1101 args = self.__args__
kj73607be2020-12-24 12:33:48 +08001102 if not (len(args) == 2 and (args[0] is Ellipsis
1103 or isinstance(args[0], (ParamSpec, _ConcatenateGenericAlias)))):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001104 args = list(args[:-1]), args[-1]
1105 return operator.getitem, (Callable, args)
1106
1107
1108class _CallableType(_SpecialGenericAlias, _root=True):
1109 def copy_with(self, params):
1110 return _CallableGenericAlias(self.__origin__, params,
1111 name=self._name, inst=self._inst)
1112
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001113 def __getitem__(self, params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001114 if not isinstance(params, tuple) or len(params) != 2:
1115 raise TypeError("Callable must be used as "
1116 "Callable[[arg, ...], result].")
1117 args, result = params
kj463c7d32020-12-14 02:38:24 +08001118 # This relaxes what args can be on purpose to allow things like
1119 # PEP 612 ParamSpec. Responsibility for whether a user is using
1120 # Callable[...] properly is deferred to static type checkers.
1121 if isinstance(args, list):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001122 params = (tuple(args), result)
kj463c7d32020-12-14 02:38:24 +08001123 else:
1124 params = (args, result)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001125 return self.__getitem_inner__(params)
1126
1127 @_tp_cache
1128 def __getitem_inner__(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001129 args, result = params
1130 msg = "Callable[args, result]: result must be a type."
1131 result = _type_check(result, msg)
1132 if args is Ellipsis:
1133 return self.copy_with((_TypingEllipsis, result))
kj463c7d32020-12-14 02:38:24 +08001134 if not isinstance(args, tuple):
1135 args = (args,)
1136 args = tuple(_type_convert(arg) for arg in args)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001137 params = args + (result,)
1138 return self.copy_with(params)
1139
1140
1141class _TupleType(_SpecialGenericAlias, _root=True):
1142 @_tp_cache
1143 def __getitem__(self, params):
1144 if params == ():
1145 return self.copy_with((_TypingEmpty,))
1146 if not isinstance(params, tuple):
1147 params = (params,)
1148 if len(params) == 2 and params[1] is ...:
1149 msg = "Tuple[t, ...]: t must be a type."
1150 p = _type_check(params[0], msg)
1151 return self.copy_with((p, _TypingEllipsis))
1152 msg = "Tuple[t0, t1, ...]: each t must be a type."
1153 params = tuple(_type_check(p, msg) for p in params)
1154 return self.copy_with(params)
1155
1156
1157class _UnionGenericAlias(_GenericAlias, _root=True):
1158 def copy_with(self, params):
1159 return Union[params]
1160
1161 def __eq__(self, other):
1162 if not isinstance(other, _UnionGenericAlias):
1163 return NotImplemented
1164 return set(self.__args__) == set(other.__args__)
1165
1166 def __hash__(self):
1167 return hash(frozenset(self.__args__))
1168
1169 def __repr__(self):
1170 args = self.__args__
1171 if len(args) == 2:
1172 if args[0] is type(None):
1173 return f'typing.Optional[{_type_repr(args[1])}]'
1174 elif args[1] is type(None):
1175 return f'typing.Optional[{_type_repr(args[0])}]'
1176 return super().__repr__()
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001177
Maggie Moss1b4552c2020-09-09 13:23:24 -07001178 def __instancecheck__(self, obj):
1179 return self.__subclasscheck__(type(obj))
1180
1181 def __subclasscheck__(self, cls):
1182 for arg in self.__args__:
1183 if issubclass(cls, arg):
1184 return True
1185
1186
Yurii Karabasf03d3182020-11-17 04:23:19 +02001187def _value_and_type_iter(parameters):
1188 return ((p, type(p)) for p in parameters)
1189
1190
1191class _LiteralGenericAlias(_GenericAlias, _root=True):
1192
1193 def __eq__(self, other):
1194 if not isinstance(other, _LiteralGenericAlias):
1195 return NotImplemented
1196
1197 return set(_value_and_type_iter(self.__args__)) == set(_value_and_type_iter(other.__args__))
1198
1199 def __hash__(self):
Yurii Karabas1b540772020-11-19 18:17:38 +02001200 return hash(frozenset(_value_and_type_iter(self.__args__)))
Yurii Karabasf03d3182020-11-17 04:23:19 +02001201
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001202
kj73607be2020-12-24 12:33:48 +08001203class _ConcatenateGenericAlias(_GenericAlias, _root=True):
1204 pass
1205
1206
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001207class Generic:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001208 """Abstract base class for generic types.
1209
Guido van Rossumb24569a2016-11-20 18:01:29 -08001210 A generic type is typically declared by inheriting from
1211 this class parameterized with one or more type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001212 For example, a generic mapping type might be defined as::
1213
1214 class Mapping(Generic[KT, VT]):
1215 def __getitem__(self, key: KT) -> VT:
1216 ...
1217 # Etc.
1218
1219 This class can then be used as follows::
1220
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001221 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001222 try:
1223 return mapping[key]
1224 except KeyError:
1225 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001226 """
Guido van Rossumd70fe632015-08-05 12:11:06 +02001227 __slots__ = ()
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001228 _is_protocol = False
Guido van Rossumd70fe632015-08-05 12:11:06 +02001229
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001230 @_tp_cache
1231 def __class_getitem__(cls, params):
1232 if not isinstance(params, tuple):
1233 params = (params,)
1234 if not params and cls is not Tuple:
1235 raise TypeError(
1236 f"Parameter list to {cls.__qualname__}[...] cannot be empty")
kj73607be2020-12-24 12:33:48 +08001237 params = tuple(_type_convert(p) for p in params)
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001238 if cls in (Generic, Protocol):
1239 # Generic and Protocol can only be subscripted with unique type variables.
kj73607be2020-12-24 12:33:48 +08001240 if not all(isinstance(p, _TypeVarLike) for p in params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001241 raise TypeError(
kj73607be2020-12-24 12:33:48 +08001242 f"Parameters to {cls.__name__}[...] must all be type variables "
1243 f"or parameter specification variables.")
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001244 if len(set(params)) != len(params):
1245 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001246 f"Parameters to {cls.__name__}[...] must all be unique")
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001247 else:
1248 # Subscripting a regular Generic subclass.
kj73607be2020-12-24 12:33:48 +08001249 if any(isinstance(t, ParamSpec) for t in cls.__parameters__):
1250 params = _prepare_paramspec_params(cls, params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001251 _check_generic(cls, params, len(cls.__parameters__))
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001252 return _GenericAlias(cls, params)
1253
1254 def __init_subclass__(cls, *args, **kwargs):
Ivan Levkivskyiee566fe2018-04-04 17:00:15 +01001255 super().__init_subclass__(*args, **kwargs)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001256 tvars = []
1257 if '__orig_bases__' in cls.__dict__:
1258 error = Generic in cls.__orig_bases__
1259 else:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001260 error = Generic in cls.__bases__ and cls.__name__ != 'Protocol'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001261 if error:
1262 raise TypeError("Cannot inherit from plain Generic")
1263 if '__orig_bases__' in cls.__dict__:
1264 tvars = _collect_type_vars(cls.__orig_bases__)
1265 # Look for Generic[T1, ..., Tn].
1266 # If found, tvars must be a subset of it.
1267 # If not found, tvars is it.
1268 # Also check for and reject plain Generic,
1269 # and reject multiple Generic[...].
1270 gvars = None
1271 for base in cls.__orig_bases__:
1272 if (isinstance(base, _GenericAlias) and
1273 base.__origin__ is Generic):
1274 if gvars is not None:
1275 raise TypeError(
1276 "Cannot inherit from Generic[...] multiple types.")
1277 gvars = base.__parameters__
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001278 if gvars is not None:
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001279 tvarset = set(tvars)
1280 gvarset = set(gvars)
1281 if not tvarset <= gvarset:
1282 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
1283 s_args = ', '.join(str(g) for g in gvars)
1284 raise TypeError(f"Some type variables ({s_vars}) are"
1285 f" not listed in Generic[{s_args}]")
1286 tvars = gvars
1287 cls.__parameters__ = tuple(tvars)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001288
1289
1290class _TypingEmpty:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001291 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
1292 to allow empty list/tuple in specific places, without allowing them
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001293 to sneak in where prohibited.
1294 """
1295
1296
1297class _TypingEllipsis:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001298 """Internal placeholder for ... (ellipsis)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001299
1300
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001301_TYPING_INTERNALS = ['__parameters__', '__orig_bases__', '__orig_class__',
1302 '_is_protocol', '_is_runtime_protocol']
1303
1304_SPECIAL_NAMES = ['__abstractmethods__', '__annotations__', '__dict__', '__doc__',
1305 '__init__', '__module__', '__new__', '__slots__',
Guido van Rossum48b069a2020-04-07 09:50:06 -07001306 '__subclasshook__', '__weakref__', '__class_getitem__']
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001307
1308# These special attributes will be not collected as protocol members.
1309EXCLUDED_ATTRIBUTES = _TYPING_INTERNALS + _SPECIAL_NAMES + ['_MutableMapping__marker']
1310
1311
1312def _get_protocol_attrs(cls):
1313 """Collect protocol members from a protocol class objects.
1314
1315 This includes names actually defined in the class dictionary, as well
1316 as names that appear in annotations. Special names (above) are skipped.
1317 """
1318 attrs = set()
1319 for base in cls.__mro__[:-1]: # without object
1320 if base.__name__ in ('Protocol', 'Generic'):
1321 continue
1322 annotations = getattr(base, '__annotations__', {})
1323 for attr in list(base.__dict__.keys()) + list(annotations.keys()):
1324 if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
1325 attrs.add(attr)
1326 return attrs
1327
1328
1329def _is_callable_members_only(cls):
1330 # PEP 544 prohibits using issubclass() with protocols that have non-method members.
1331 return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls))
1332
1333
1334def _no_init(self, *args, **kwargs):
1335 if type(self)._is_protocol:
1336 raise TypeError('Protocols cannot be instantiated')
1337
1338
Rossc1af1282020-12-29 11:55:28 +00001339def _allow_reckless_class_checks():
Nickolena Fishercfaf4c02020-04-26 12:49:11 -05001340 """Allow instance and class checks for special stdlib modules.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001341
1342 The abc and functools modules indiscriminately call isinstance() and
1343 issubclass() on the whole MRO of a user class, which may contain protocols.
1344 """
1345 try:
1346 return sys._getframe(3).f_globals['__name__'] in ['abc', 'functools']
1347 except (AttributeError, ValueError): # For platforms without _getframe().
1348 return True
1349
1350
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001351_PROTO_ALLOWLIST = {
Divij Rajkumar692a0dc2019-09-12 11:13:51 +01001352 'collections.abc': [
1353 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
1354 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
1355 ],
1356 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
1357}
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001358
1359
1360class _ProtocolMeta(ABCMeta):
1361 # This metaclass is really unfortunate and exists only because of
1362 # the lack of __instancehook__.
1363 def __instancecheck__(cls, instance):
1364 # We need this method for situations where attributes are
1365 # assigned in __init__.
1366 if ((not getattr(cls, '_is_protocol', False) or
1367 _is_callable_members_only(cls)) and
1368 issubclass(instance.__class__, cls)):
1369 return True
1370 if cls._is_protocol:
1371 if all(hasattr(instance, attr) and
1372 # All *methods* can be blocked by setting them to None.
1373 (not callable(getattr(cls, attr, None)) or
1374 getattr(instance, attr) is not None)
1375 for attr in _get_protocol_attrs(cls)):
1376 return True
1377 return super().__instancecheck__(instance)
1378
1379
1380class Protocol(Generic, metaclass=_ProtocolMeta):
1381 """Base class for protocol classes.
1382
1383 Protocol classes are defined as::
1384
1385 class Proto(Protocol):
1386 def meth(self) -> int:
1387 ...
1388
1389 Such classes are primarily used with static type checkers that recognize
1390 structural subtyping (static duck-typing), for example::
1391
1392 class C:
1393 def meth(self) -> int:
1394 return 0
1395
1396 def func(x: Proto) -> int:
1397 return x.meth()
1398
1399 func(C()) # Passes static type check
1400
1401 See PEP 544 for details. Protocol classes decorated with
1402 @typing.runtime_checkable act as simple-minded runtime protocols that check
1403 only the presence of given attributes, ignoring their type signatures.
1404 Protocol classes can be generic, they are defined as::
1405
1406 class GenProto(Protocol[T]):
1407 def meth(self) -> T:
1408 ...
1409 """
1410 __slots__ = ()
1411 _is_protocol = True
1412 _is_runtime_protocol = False
1413
1414 def __init_subclass__(cls, *args, **kwargs):
1415 super().__init_subclass__(*args, **kwargs)
1416
1417 # Determine if this is a protocol or a concrete subclass.
1418 if not cls.__dict__.get('_is_protocol', False):
1419 cls._is_protocol = any(b is Protocol for b in cls.__bases__)
1420
1421 # Set (or override) the protocol subclass hook.
1422 def _proto_hook(other):
1423 if not cls.__dict__.get('_is_protocol', False):
1424 return NotImplemented
1425
1426 # First, perform various sanity checks.
1427 if not getattr(cls, '_is_runtime_protocol', False):
Rossc1af1282020-12-29 11:55:28 +00001428 if _allow_reckless_class_checks():
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001429 return NotImplemented
1430 raise TypeError("Instance and class checks can only be used with"
1431 " @runtime_checkable protocols")
1432 if not _is_callable_members_only(cls):
Rossc1af1282020-12-29 11:55:28 +00001433 if _allow_reckless_class_checks():
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001434 return NotImplemented
1435 raise TypeError("Protocols with non-method members"
1436 " don't support issubclass()")
1437 if not isinstance(other, type):
1438 # Same error message as for issubclass(1, int).
1439 raise TypeError('issubclass() arg 1 must be a class')
1440
1441 # Second, perform the actual structural compatibility check.
1442 for attr in _get_protocol_attrs(cls):
1443 for base in other.__mro__:
1444 # Check if the members appears in the class dictionary...
1445 if attr in base.__dict__:
1446 if base.__dict__[attr] is None:
1447 return NotImplemented
1448 break
1449
1450 # ...or in annotations, if it is a sub-protocol.
1451 annotations = getattr(base, '__annotations__', {})
1452 if (isinstance(annotations, collections.abc.Mapping) and
1453 attr in annotations and
1454 issubclass(other, Generic) and other._is_protocol):
1455 break
1456 else:
1457 return NotImplemented
1458 return True
1459
1460 if '__subclasshook__' not in cls.__dict__:
1461 cls.__subclasshook__ = _proto_hook
1462
1463 # We have nothing more to do for non-protocols...
1464 if not cls._is_protocol:
1465 return
1466
1467 # ... otherwise check consistency of bases, and prohibit instantiation.
1468 for base in cls.__bases__:
1469 if not (base in (object, Generic) or
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001470 base.__module__ in _PROTO_ALLOWLIST and
1471 base.__name__ in _PROTO_ALLOWLIST[base.__module__] or
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001472 issubclass(base, Generic) and base._is_protocol):
1473 raise TypeError('Protocols can only inherit from other'
1474 ' protocols, got %r' % base)
1475 cls.__init__ = _no_init
1476
1477
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001478class _AnnotatedAlias(_GenericAlias, _root=True):
1479 """Runtime representation of an annotated type.
1480
1481 At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't'
1482 with extra annotations. The alias behaves like a normal typing alias,
1483 instantiating is the same as instantiating the underlying type, binding
1484 it to types is also the same.
1485 """
1486 def __init__(self, origin, metadata):
1487 if isinstance(origin, _AnnotatedAlias):
1488 metadata = origin.__metadata__ + metadata
1489 origin = origin.__origin__
1490 super().__init__(origin, origin)
1491 self.__metadata__ = metadata
1492
1493 def copy_with(self, params):
1494 assert len(params) == 1
1495 new_type = params[0]
1496 return _AnnotatedAlias(new_type, self.__metadata__)
1497
1498 def __repr__(self):
1499 return "typing.Annotated[{}, {}]".format(
1500 _type_repr(self.__origin__),
1501 ", ".join(repr(a) for a in self.__metadata__)
1502 )
1503
1504 def __reduce__(self):
1505 return operator.getitem, (
1506 Annotated, (self.__origin__,) + self.__metadata__
1507 )
1508
1509 def __eq__(self, other):
1510 if not isinstance(other, _AnnotatedAlias):
1511 return NotImplemented
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001512 return (self.__origin__ == other.__origin__
1513 and self.__metadata__ == other.__metadata__)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001514
1515 def __hash__(self):
1516 return hash((self.__origin__, self.__metadata__))
1517
1518
1519class Annotated:
1520 """Add context specific metadata to a type.
1521
1522 Example: Annotated[int, runtime_check.Unsigned] indicates to the
1523 hypothetical runtime_check module that this type is an unsigned int.
1524 Every other consumer of this type can ignore this metadata and treat
1525 this type as int.
1526
1527 The first argument to Annotated must be a valid type.
1528
1529 Details:
1530
1531 - It's an error to call `Annotated` with less than two arguments.
1532 - Nested Annotated are flattened::
1533
1534 Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
1535
1536 - Instantiating an annotated type is equivalent to instantiating the
1537 underlying type::
1538
1539 Annotated[C, Ann1](5) == C(5)
1540
1541 - Annotated can be used as a generic type alias::
1542
1543 Optimized = Annotated[T, runtime.Optimize()]
1544 Optimized[int] == Annotated[int, runtime.Optimize()]
1545
1546 OptimizedList = Annotated[List[T], runtime.Optimize()]
1547 OptimizedList[int] == Annotated[List[int], runtime.Optimize()]
1548 """
1549
1550 __slots__ = ()
1551
1552 def __new__(cls, *args, **kwargs):
1553 raise TypeError("Type Annotated cannot be instantiated.")
1554
1555 @_tp_cache
1556 def __class_getitem__(cls, params):
1557 if not isinstance(params, tuple) or len(params) < 2:
1558 raise TypeError("Annotated[...] should be used "
1559 "with at least two arguments (a type and an "
1560 "annotation).")
1561 msg = "Annotated[t, ...]: t must be a type."
1562 origin = _type_check(params[0], msg)
1563 metadata = tuple(params[1:])
1564 return _AnnotatedAlias(origin, metadata)
1565
1566 def __init_subclass__(cls, *args, **kwargs):
1567 raise TypeError(
1568 "Cannot subclass {}.Annotated".format(cls.__module__)
1569 )
1570
1571
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001572def runtime_checkable(cls):
1573 """Mark a protocol class as a runtime protocol.
1574
1575 Such protocol can be used with isinstance() and issubclass().
1576 Raise TypeError if applied to a non-protocol class.
1577 This allows a simple-minded structural check very similar to
1578 one trick ponies in collections.abc such as Iterable.
1579 For example::
1580
1581 @runtime_checkable
1582 class Closable(Protocol):
1583 def close(self): ...
1584
1585 assert isinstance(open('/some/file'), Closable)
1586
1587 Warning: this will check only the presence of the required methods,
1588 not their type signatures!
1589 """
1590 if not issubclass(cls, Generic) or not cls._is_protocol:
1591 raise TypeError('@runtime_checkable can be only applied to protocol classes,'
1592 ' got %r' % cls)
1593 cls._is_runtime_protocol = True
1594 return cls
1595
1596
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001597def cast(typ, val):
1598 """Cast a value to a type.
1599
1600 This returns the value unchanged. To the type checker this
1601 signals that the return value has the designated type, but at
1602 runtime we intentionally don't check anything (we want this
1603 to be as fast as possible).
1604 """
1605 return val
1606
1607
1608def _get_defaults(func):
1609 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -08001610 try:
1611 code = func.__code__
1612 except AttributeError:
1613 # Some built-in functions don't have __code__, __defaults__, etc.
1614 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001615 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001616 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001617 arg_names = arg_names[:pos_count]
1618 defaults = func.__defaults__ or ()
1619 kwdefaults = func.__kwdefaults__
1620 res = dict(kwdefaults) if kwdefaults else {}
1621 pos_offset = pos_count - len(defaults)
1622 for name, value in zip(arg_names[pos_offset:], defaults):
1623 assert name not in res
1624 res[name] = value
1625 return res
1626
1627
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001628_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
1629 types.MethodType, types.ModuleType,
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02001630 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001631
1632
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001633def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
Guido van Rossum991d14f2016-11-09 13:12:51 -08001634 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001635
Guido van Rossum991d14f2016-11-09 13:12:51 -08001636 This is often the same as obj.__annotations__, but it handles
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001637 forward references encoded as string literals, adds Optional[t] if a
1638 default value equal to None is set and recursively replaces all
1639 'Annotated[T, ...]' with 'T' (unless 'include_extras=True').
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001640
Guido van Rossum991d14f2016-11-09 13:12:51 -08001641 The argument may be a module, class, method, or function. The annotations
1642 are returned as a dictionary. For classes, annotations include also
1643 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001644
Guido van Rossum991d14f2016-11-09 13:12:51 -08001645 TypeError is raised if the argument is not of a type that can contain
1646 annotations, and an empty dictionary is returned if no annotations are
1647 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001648
Guido van Rossum991d14f2016-11-09 13:12:51 -08001649 BEWARE -- the behavior of globalns and localns is counterintuitive
1650 (unless you are familiar with how eval() and exec() work). The
1651 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001652
Guido van Rossum991d14f2016-11-09 13:12:51 -08001653 - If no dict arguments are passed, an attempt is made to use the
Łukasz Langaf350a262017-09-14 14:33:00 -04001654 globals from obj (or the respective module's globals for classes),
1655 and these are also used as the locals. If the object does not appear
Ken Jin1b1f9852021-04-27 01:31:21 +08001656 to have globals, an empty dictionary is used. For classes, the search
1657 order is globals first then locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001658
Guido van Rossum991d14f2016-11-09 13:12:51 -08001659 - If one dict argument is passed, it is used for both globals and
1660 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001661
Guido van Rossum991d14f2016-11-09 13:12:51 -08001662 - If two dict arguments are passed, they specify globals and
1663 locals, respectively.
1664 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001665
Guido van Rossum991d14f2016-11-09 13:12:51 -08001666 if getattr(obj, '__no_type_check__', None):
1667 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001668 # Classes require a special treatment.
1669 if isinstance(obj, type):
1670 hints = {}
1671 for base in reversed(obj.__mro__):
Łukasz Langaf350a262017-09-14 14:33:00 -04001672 if globalns is None:
Karthikeyan Singaravelana9cf69d2021-04-12 23:47:25 +05301673 try:
1674 base_globals = sys.modules[base.__module__].__dict__
1675 except KeyError:
1676 continue
Łukasz Langaf350a262017-09-14 14:33:00 -04001677 else:
1678 base_globals = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001679 ann = base.__dict__.get('__annotations__', {})
larryhastings2f2b6982021-04-29 20:09:08 -07001680 if isinstance(ann, types.GetSetDescriptorType):
1681 ann = {}
Ken Jin852150d2021-04-13 01:23:12 +08001682 base_locals = dict(vars(base)) if localns is None else localns
Ken Jin1b1f9852021-04-27 01:31:21 +08001683 if localns is None and globalns is None:
1684 # This is surprising, but required. Before Python 3.10,
1685 # get_type_hints only evaluated the globalns of
1686 # a class. To maintain backwards compatibility, we reverse
1687 # the globalns and localns order so that eval() looks into
1688 # *base_globals* first rather than *base_locals*.
1689 # This only affects ForwardRefs.
1690 base_globals, base_locals = base_locals, base_globals
Guido van Rossum991d14f2016-11-09 13:12:51 -08001691 for name, value in ann.items():
1692 if value is None:
1693 value = type(None)
1694 if isinstance(value, str):
Nina Zakharenko0e61dff2018-05-22 20:32:10 -07001695 value = ForwardRef(value, is_argument=False)
Ken Jin852150d2021-04-13 01:23:12 +08001696 value = _eval_type(value, base_globals, base_locals)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001697 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001698 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
Łukasz Langaf350a262017-09-14 14:33:00 -04001699
1700 if globalns is None:
1701 if isinstance(obj, types.ModuleType):
1702 globalns = obj.__dict__
1703 else:
benedwards140aca3a32019-11-21 17:24:58 +00001704 nsobj = obj
1705 # Find globalns for the unwrapped object.
1706 while hasattr(nsobj, '__wrapped__'):
1707 nsobj = nsobj.__wrapped__
1708 globalns = getattr(nsobj, '__globals__', {})
Łukasz Langaf350a262017-09-14 14:33:00 -04001709 if localns is None:
1710 localns = globalns
1711 elif localns is None:
1712 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001713 hints = getattr(obj, '__annotations__', None)
1714 if hints is None:
1715 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001716 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001717 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001718 else:
1719 raise TypeError('{!r} is not a module, class, method, '
1720 'or function.'.format(obj))
1721 defaults = _get_defaults(obj)
1722 hints = dict(hints)
1723 for name, value in hints.items():
1724 if value is None:
1725 value = type(None)
1726 if isinstance(value, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001727 value = ForwardRef(value)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001728 value = _eval_type(value, globalns, localns)
1729 if name in defaults and defaults[name] is None:
1730 value = Optional[value]
1731 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001732 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
1733
1734
1735def _strip_annotations(t):
1736 """Strips the annotations from a given type.
1737 """
1738 if isinstance(t, _AnnotatedAlias):
1739 return _strip_annotations(t.__origin__)
1740 if isinstance(t, _GenericAlias):
1741 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1742 if stripped_args == t.__args__:
1743 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001744 return t.copy_with(stripped_args)
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001745 if isinstance(t, GenericAlias):
1746 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1747 if stripped_args == t.__args__:
1748 return t
1749 return GenericAlias(t.__origin__, stripped_args)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001750 return t
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001751
1752
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001753def get_origin(tp):
1754 """Get the unsubscripted version of a type.
1755
Jakub Stasiak38aaaaa2020-02-07 02:15:12 +01001756 This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
1757 and Annotated. Return None for unsupported types. Examples::
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001758
1759 get_origin(Literal[42]) is Literal
1760 get_origin(int) is None
1761 get_origin(ClassVar[int]) is ClassVar
1762 get_origin(Generic) is Generic
1763 get_origin(Generic[T]) is Generic
1764 get_origin(Union[T, int]) is Union
1765 get_origin(List[Tuple[T, T]][int]) == list
Jelle Zijlstra52243362021-04-10 19:57:05 -07001766 get_origin(P.args) is P
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001767 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001768 if isinstance(tp, _AnnotatedAlias):
1769 return Annotated
Jelle Zijlstra52243362021-04-10 19:57:05 -07001770 if isinstance(tp, (_BaseGenericAlias, GenericAlias,
1771 ParamSpecArgs, ParamSpecKwargs)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001772 return tp.__origin__
1773 if tp is Generic:
1774 return Generic
Ken Jinefb1f092020-12-29 10:26:19 +08001775 if isinstance(tp, types.Union):
1776 return types.Union
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001777 return None
1778
1779
1780def get_args(tp):
1781 """Get type arguments with all substitutions performed.
1782
1783 For unions, basic simplifications used by Union constructor are performed.
1784 Examples::
1785 get_args(Dict[str, int]) == (str, int)
1786 get_args(int) == ()
1787 get_args(Union[int, Union[T, int], str][int]) == (int, str)
1788 get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
1789 get_args(Callable[[], T][int]) == ([], int)
1790 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001791 if isinstance(tp, _AnnotatedAlias):
1792 return (tp.__origin__,) + tp.__metadata__
Ken Jin4140f102020-12-29 04:06:19 +08001793 if isinstance(tp, (_GenericAlias, GenericAlias)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001794 res = tp.__args__
Ken Jinefb1f092020-12-29 10:26:19 +08001795 if (tp.__origin__ is collections.abc.Callable
1796 and not (res[0] is Ellipsis
1797 or isinstance(res[0], (ParamSpec, _ConcatenateGenericAlias)))):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001798 res = (list(res[:-1]), res[-1])
1799 return res
Ken Jinefb1f092020-12-29 10:26:19 +08001800 if isinstance(tp, types.Union):
1801 return tp.__args__
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001802 return ()
1803
1804
Patrick Reader0705ec82020-09-16 05:58:32 +01001805def is_typeddict(tp):
1806 """Check if an annotation is a TypedDict class
1807
1808 For example::
1809 class Film(TypedDict):
1810 title: str
1811 year: int
1812
1813 is_typeddict(Film) # => True
1814 is_typeddict(Union[list, str]) # => False
1815 """
1816 return isinstance(tp, _TypedDictMeta)
1817
1818
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001819def no_type_check(arg):
1820 """Decorator to indicate that annotations are not type hints.
1821
1822 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001823 applies recursively to all methods and classes defined in that class
1824 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001825
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001826 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001827 """
1828 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001829 arg_attrs = arg.__dict__.copy()
1830 for attr, val in arg.__dict__.items():
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001831 if val in arg.__bases__ + (arg,):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001832 arg_attrs.pop(attr)
1833 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001834 if isinstance(obj, types.FunctionType):
1835 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001836 if isinstance(obj, type):
1837 no_type_check(obj)
1838 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001839 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001840 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001841 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001842 return arg
1843
1844
1845def no_type_check_decorator(decorator):
1846 """Decorator to give another decorator the @no_type_check effect.
1847
1848 This wraps the decorator with something that wraps the decorated
1849 function in @no_type_check.
1850 """
1851
1852 @functools.wraps(decorator)
1853 def wrapped_decorator(*args, **kwds):
1854 func = decorator(*args, **kwds)
1855 func = no_type_check(func)
1856 return func
1857
1858 return wrapped_decorator
1859
1860
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001861def _overload_dummy(*args, **kwds):
1862 """Helper for @overload to raise when called."""
1863 raise NotImplementedError(
1864 "You should not call an overloaded function. "
1865 "A series of @overload-decorated functions "
1866 "outside a stub module should always be followed "
1867 "by an implementation that is not @overload-ed.")
1868
1869
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001870def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001871 """Decorator for overloaded functions/methods.
1872
1873 In a stub file, place two or more stub definitions for the same
1874 function in a row, each decorated with @overload. For example:
1875
1876 @overload
1877 def utf8(value: None) -> None: ...
1878 @overload
1879 def utf8(value: bytes) -> bytes: ...
1880 @overload
1881 def utf8(value: str) -> bytes: ...
1882
1883 In a non-stub file (i.e. a regular .py file), do the same but
1884 follow it with an implementation. The implementation should *not*
1885 be decorated with @overload. For example:
1886
1887 @overload
1888 def utf8(value: None) -> None: ...
1889 @overload
1890 def utf8(value: bytes) -> bytes: ...
1891 @overload
1892 def utf8(value: str) -> bytes: ...
1893 def utf8(value):
1894 # implementation goes here
1895 """
1896 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001897
1898
Ivan Levkivskyif3672422019-05-26 09:37:07 +01001899def final(f):
1900 """A decorator to indicate final methods and final classes.
1901
1902 Use this decorator to indicate to type checkers that the decorated
1903 method cannot be overridden, and decorated class cannot be subclassed.
1904 For example:
1905
1906 class Base:
1907 @final
1908 def done(self) -> None:
1909 ...
1910 class Sub(Base):
1911 def done(self) -> None: # Error reported by type checker
1912 ...
1913
1914 @final
1915 class Leaf:
1916 ...
1917 class Other(Leaf): # Error reported by type checker
1918 ...
1919
1920 There is no runtime checking of these properties.
1921 """
1922 return f
1923
1924
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001925# Some unconstrained type variables. These are used by the container types.
1926# (These are not for export.)
1927T = TypeVar('T') # Any type.
1928KT = TypeVar('KT') # Key type.
1929VT = TypeVar('VT') # Value type.
1930T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
1931V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
1932VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
1933T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
1934# Internal type variable used for Type[].
1935CT_co = TypeVar('CT_co', covariant=True, bound=type)
1936
1937# A useful type variable with constraints. This represents string types.
1938# (This one *is* for export!)
1939AnyStr = TypeVar('AnyStr', bytes, str)
1940
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001941
1942# Various ABCs mimicking those in collections.abc.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001943_alias = _SpecialGenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001944
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001945Hashable = _alias(collections.abc.Hashable, 0) # Not generic.
1946Awaitable = _alias(collections.abc.Awaitable, 1)
1947Coroutine = _alias(collections.abc.Coroutine, 3)
1948AsyncIterable = _alias(collections.abc.AsyncIterable, 1)
1949AsyncIterator = _alias(collections.abc.AsyncIterator, 1)
1950Iterable = _alias(collections.abc.Iterable, 1)
1951Iterator = _alias(collections.abc.Iterator, 1)
1952Reversible = _alias(collections.abc.Reversible, 1)
1953Sized = _alias(collections.abc.Sized, 0) # Not generic.
1954Container = _alias(collections.abc.Container, 1)
1955Collection = _alias(collections.abc.Collection, 1)
1956Callable = _CallableType(collections.abc.Callable, 2)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001957Callable.__doc__ = \
1958 """Callable type; Callable[[int], str] is a function of (int) -> str.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001959
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001960 The subscription syntax must always be used with exactly two
1961 values: the argument list and the return type. The argument list
1962 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001963
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001964 There is no syntax to indicate optional or keyword arguments,
1965 such function types are rarely used as callback types.
1966 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001967AbstractSet = _alias(collections.abc.Set, 1, name='AbstractSet')
1968MutableSet = _alias(collections.abc.MutableSet, 1)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001969# NOTE: Mapping is only covariant in the value type.
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001970Mapping = _alias(collections.abc.Mapping, 2)
1971MutableMapping = _alias(collections.abc.MutableMapping, 2)
1972Sequence = _alias(collections.abc.Sequence, 1)
1973MutableSequence = _alias(collections.abc.MutableSequence, 1)
1974ByteString = _alias(collections.abc.ByteString, 0) # Not generic
1975# Tuple accepts variable number of parameters.
1976Tuple = _TupleType(tuple, -1, inst=False, name='Tuple')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001977Tuple.__doc__ = \
1978 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001979
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001980 Example: Tuple[T1, T2] is a tuple of two elements corresponding
1981 to type variables T1 and T2. Tuple[int, float, str] is a tuple
1982 of an int, a float and a string.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001983
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001984 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
1985 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001986List = _alias(list, 1, inst=False, name='List')
1987Deque = _alias(collections.deque, 1, name='Deque')
1988Set = _alias(set, 1, inst=False, name='Set')
1989FrozenSet = _alias(frozenset, 1, inst=False, name='FrozenSet')
1990MappingView = _alias(collections.abc.MappingView, 1)
1991KeysView = _alias(collections.abc.KeysView, 1)
1992ItemsView = _alias(collections.abc.ItemsView, 2)
1993ValuesView = _alias(collections.abc.ValuesView, 1)
1994ContextManager = _alias(contextlib.AbstractContextManager, 1, name='ContextManager')
1995AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, 1, name='AsyncContextManager')
1996Dict = _alias(dict, 2, inst=False, name='Dict')
1997DefaultDict = _alias(collections.defaultdict, 2, name='DefaultDict')
1998OrderedDict = _alias(collections.OrderedDict, 2)
1999Counter = _alias(collections.Counter, 1)
2000ChainMap = _alias(collections.ChainMap, 2)
2001Generator = _alias(collections.abc.Generator, 3)
2002AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2)
2003Type = _alias(type, 1, inst=False, name='Type')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002004Type.__doc__ = \
2005 """A special construct usable to annotate class objects.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07002006
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002007 For example, suppose we have the following classes::
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07002008
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002009 class User: ... # Abstract base for User classes
2010 class BasicUser(User): ...
2011 class ProUser(User): ...
2012 class TeamUser(User): ...
Guido van Rossumf17c2002015-12-03 17:31:24 -08002013
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002014 And a function that takes a class argument that's a subclass of
2015 User and returns an instance of the corresponding class::
Guido van Rossumf17c2002015-12-03 17:31:24 -08002016
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002017 U = TypeVar('U', bound=User)
2018 def new_user(user_class: Type[U]) -> U:
2019 user = user_class()
2020 # (Here we could write the user object to a database)
2021 return user
Guido van Rossumf17c2002015-12-03 17:31:24 -08002022
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002023 joe = new_user(BasicUser)
Guido van Rossumf17c2002015-12-03 17:31:24 -08002024
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002025 At this point the type checker knows that joe has type BasicUser.
2026 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002027
2028
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002029@runtime_checkable
2030class SupportsInt(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002031 """An ABC with one abstract method __int__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002032 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002033
2034 @abstractmethod
2035 def __int__(self) -> int:
2036 pass
2037
2038
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002039@runtime_checkable
2040class SupportsFloat(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002041 """An ABC with one abstract method __float__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002042 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002043
2044 @abstractmethod
2045 def __float__(self) -> float:
2046 pass
2047
2048
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002049@runtime_checkable
2050class SupportsComplex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002051 """An ABC with one abstract method __complex__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002052 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002053
2054 @abstractmethod
2055 def __complex__(self) -> complex:
2056 pass
2057
2058
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002059@runtime_checkable
2060class SupportsBytes(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002061 """An ABC with one abstract method __bytes__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002062 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002063
2064 @abstractmethod
2065 def __bytes__(self) -> bytes:
2066 pass
2067
2068
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002069@runtime_checkable
2070class SupportsIndex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002071 """An ABC with one abstract method __index__."""
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -07002072 __slots__ = ()
2073
2074 @abstractmethod
2075 def __index__(self) -> int:
2076 pass
2077
2078
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002079@runtime_checkable
2080class SupportsAbs(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002081 """An ABC with one abstract method __abs__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002082 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002083
2084 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02002085 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002086 pass
2087
2088
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002089@runtime_checkable
2090class SupportsRound(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002091 """An ABC with one abstract method __round__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002092 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002093
2094 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02002095 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002096 pass
2097
2098
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002099def _make_nmtuple(name, types, module, defaults = ()):
2100 fields = [n for n, t in types]
2101 types = {n: _type_check(t, f"field {n} annotation must be a type")
2102 for n, t in types}
2103 nm_tpl = collections.namedtuple(name, fields,
2104 defaults=defaults, module=module)
2105 nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002106 return nm_tpl
2107
2108
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002109# attributes prohibited to set in NamedTuple class syntax
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002110_prohibited = frozenset({'__new__', '__init__', '__slots__', '__getnewargs__',
2111 '_fields', '_field_defaults',
2112 '_make', '_replace', '_asdict', '_source'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002113
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002114_special = frozenset({'__module__', '__name__', '__annotations__'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002115
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002116
Guido van Rossum2f841442016-11-15 09:48:06 -08002117class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002118
Guido van Rossum2f841442016-11-15 09:48:06 -08002119 def __new__(cls, typename, bases, ns):
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002120 assert bases[0] is _NamedTuple
Guido van Rossum2f841442016-11-15 09:48:06 -08002121 types = ns.get('__annotations__', {})
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002122 default_names = []
Guido van Rossum3c268be2017-01-18 08:03:50 -08002123 for field_name in types:
2124 if field_name in ns:
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002125 default_names.append(field_name)
2126 elif default_names:
2127 raise TypeError(f"Non-default namedtuple field {field_name} "
2128 f"cannot follow default field"
2129 f"{'s' if len(default_names) > 1 else ''} "
2130 f"{', '.join(default_names)}")
2131 nm_tpl = _make_nmtuple(typename, types.items(),
2132 defaults=[ns[n] for n in default_names],
2133 module=ns['__module__'])
Guido van Rossum95919c02017-01-22 17:47:20 -08002134 # update from user namespace without overriding special namedtuple attributes
2135 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002136 if key in _prohibited:
2137 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
2138 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08002139 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08002140 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002141
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002142
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002143def NamedTuple(typename, fields=None, /, **kwargs):
Guido van Rossum2f841442016-11-15 09:48:06 -08002144 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002145
Guido van Rossum2f841442016-11-15 09:48:06 -08002146 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002147
Guido van Rossum2f841442016-11-15 09:48:06 -08002148 class Employee(NamedTuple):
2149 name: str
2150 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002151
Guido van Rossum2f841442016-11-15 09:48:06 -08002152 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002153
Guido van Rossum2f841442016-11-15 09:48:06 -08002154 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002155
Raymond Hettingerf7b57df2019-03-18 09:53:56 -07002156 The resulting class has an extra __annotations__ attribute, giving a
2157 dict that maps field names to types. (The field names are also in
2158 the _fields attribute, which is part of the namedtuple API.)
2159 Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002160
Guido van Rossum2f841442016-11-15 09:48:06 -08002161 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002162
Guido van Rossum2f841442016-11-15 09:48:06 -08002163 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002164
Guido van Rossum2f841442016-11-15 09:48:06 -08002165 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
2166 """
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002167 if fields is None:
2168 fields = kwargs.items()
2169 elif kwargs:
2170 raise TypeError("Either list of fields or keywords"
2171 " can be provided to NamedTuple, not both")
2172 try:
2173 module = sys._getframe(1).f_globals.get('__name__', '__main__')
2174 except (AttributeError, ValueError):
2175 module = None
2176 return _make_nmtuple(typename, fields, module=module)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002177
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002178_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})
2179
2180def _namedtuple_mro_entries(bases):
2181 if len(bases) > 1:
2182 raise TypeError("Multiple inheritance with NamedTuple is not supported")
2183 assert bases[0] is NamedTuple
2184 return (_NamedTuple,)
2185
2186NamedTuple.__mro_entries__ = _namedtuple_mro_entries
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002187
2188
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002189class _TypedDictMeta(type):
2190 def __new__(cls, name, bases, ns, total=True):
2191 """Create new typed dict class object.
2192
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002193 This method is called when TypedDict is subclassed,
2194 or when TypedDict is instantiated. This way
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002195 TypedDict supports all three syntax forms described in its docstring.
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002196 Subclasses and instances of TypedDict return actual dictionaries.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002197 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002198 for base in bases:
2199 if type(base) is not _TypedDictMeta:
2200 raise TypeError('cannot inherit from both a TypedDict type '
2201 'and a non-TypedDict base class')
2202 tp_dict = type.__new__(_TypedDictMeta, name, (dict,), ns)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002203
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002204 annotations = {}
2205 own_annotations = ns.get('__annotations__', {})
2206 own_annotation_keys = set(own_annotations.keys())
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002207 msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002208 own_annotations = {
2209 n: _type_check(tp, msg) for n, tp in own_annotations.items()
2210 }
2211 required_keys = set()
2212 optional_keys = set()
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002213
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002214 for base in bases:
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002215 annotations.update(base.__dict__.get('__annotations__', {}))
2216 required_keys.update(base.__dict__.get('__required_keys__', ()))
2217 optional_keys.update(base.__dict__.get('__optional_keys__', ()))
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002218
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002219 annotations.update(own_annotations)
2220 if total:
2221 required_keys.update(own_annotation_keys)
2222 else:
2223 optional_keys.update(own_annotation_keys)
2224
2225 tp_dict.__annotations__ = annotations
2226 tp_dict.__required_keys__ = frozenset(required_keys)
2227 tp_dict.__optional_keys__ = frozenset(optional_keys)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002228 if not hasattr(tp_dict, '__total__'):
2229 tp_dict.__total__ = total
2230 return tp_dict
2231
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002232 __call__ = dict # static method
2233
2234 def __subclasscheck__(cls, other):
2235 # Typed dicts are only for static structural subtyping.
2236 raise TypeError('TypedDict does not support instance and class checks')
2237
2238 __instancecheck__ = __subclasscheck__
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002239
2240
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002241def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002242 """A simple typed namespace. At runtime it is equivalent to a plain dict.
2243
2244 TypedDict creates a dictionary type that expects all of its
2245 instances to have a certain set of keys, where each key is
2246 associated with a value of a consistent type. This expectation
2247 is not checked at runtime but is only enforced by type checkers.
2248 Usage::
2249
2250 class Point2D(TypedDict):
2251 x: int
2252 y: int
2253 label: str
2254
2255 a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
2256 b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
2257
2258 assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
2259
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002260 The type info can be accessed via the Point2D.__annotations__ dict, and
2261 the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
2262 TypedDict supports two additional equivalent forms::
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002263
2264 Point2D = TypedDict('Point2D', x=int, y=int, label=str)
2265 Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
2266
ananthan-123ab6423f2020-02-19 10:03:05 +05302267 By default, all keys must be present in a TypedDict. It is possible
2268 to override this by specifying totality.
2269 Usage::
2270
2271 class point2D(TypedDict, total=False):
2272 x: int
2273 y: int
2274
2275 This means that a point2D TypedDict can have any of the keys omitted.A type
2276 checker is only expected to support a literal False or True as the value of
2277 the total argument. True is the default, and makes all items defined in the
2278 class body be required.
2279
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002280 The class syntax is only supported in Python 3.6+, while two other
2281 syntax forms work for Python 2.7 and 3.2+
2282 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002283 if fields is None:
2284 fields = kwargs
2285 elif kwargs:
2286 raise TypeError("TypedDict takes either a dict or keyword arguments,"
2287 " but not both")
2288
Alex Grönholm67b769f2020-12-10 23:49:05 +02002289 ns = {'__annotations__': dict(fields)}
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002290 try:
2291 # Setting correct module is necessary to make typed dict classes pickleable.
2292 ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
2293 except (AttributeError, ValueError):
2294 pass
2295
Alex Grönholm67b769f2020-12-10 23:49:05 +02002296 return _TypedDictMeta(typename, (), ns, total=total)
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002297
2298_TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {})
2299TypedDict.__mro_entries__ = lambda bases: (_TypedDict,)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002300
2301
Guido van Rossum91185fe2016-06-08 11:19:11 -07002302def NewType(name, tp):
2303 """NewType creates simple unique types with almost zero
2304 runtime overhead. NewType(name, tp) is considered a subtype of tp
2305 by static type checkers. At runtime, NewType(name, tp) returns
2306 a dummy function that simply returns its argument. Usage::
2307
2308 UserId = NewType('UserId', int)
2309
2310 def name_by_id(user_id: UserId) -> str:
2311 ...
2312
2313 UserId('user') # Fails type check
2314
2315 name_by_id(42) # Fails type check
2316 name_by_id(UserId(42)) # OK
2317
2318 num = UserId(5) + 1 # type: int
2319 """
2320
2321 def new_type(x):
2322 return x
2323
2324 new_type.__name__ = name
2325 new_type.__supertype__ = tp
2326 return new_type
2327
2328
Guido van Rossum0e0563c2016-04-05 14:54:25 -07002329# Python-version-specific alias (Python 2: unicode; Python 3: str)
2330Text = str
2331
2332
Guido van Rossum91185fe2016-06-08 11:19:11 -07002333# Constant that's True when type checking, but False here.
2334TYPE_CHECKING = False
2335
2336
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002337class IO(Generic[AnyStr]):
2338 """Generic base class for TextIO and BinaryIO.
2339
2340 This is an abstract, generic version of the return of open().
2341
2342 NOTE: This does not distinguish between the different possible
2343 classes (text vs. binary, read vs. write vs. read/write,
2344 append-only, unbuffered). The TextIO and BinaryIO subclasses
2345 below capture the distinctions between text vs. binary, which is
2346 pervasive in the interface; however we currently do not offer a
2347 way to track the other distinctions in the type system.
2348 """
2349
Guido van Rossumd70fe632015-08-05 12:11:06 +02002350 __slots__ = ()
2351
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002352 @property
2353 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002354 def mode(self) -> str:
2355 pass
2356
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002357 @property
2358 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002359 def name(self) -> str:
2360 pass
2361
2362 @abstractmethod
2363 def close(self) -> None:
2364 pass
2365
Shantanu2e6569b2020-01-29 18:52:36 -08002366 @property
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002367 @abstractmethod
2368 def closed(self) -> bool:
2369 pass
2370
2371 @abstractmethod
2372 def fileno(self) -> int:
2373 pass
2374
2375 @abstractmethod
2376 def flush(self) -> None:
2377 pass
2378
2379 @abstractmethod
2380 def isatty(self) -> bool:
2381 pass
2382
2383 @abstractmethod
2384 def read(self, n: int = -1) -> AnyStr:
2385 pass
2386
2387 @abstractmethod
2388 def readable(self) -> bool:
2389 pass
2390
2391 @abstractmethod
2392 def readline(self, limit: int = -1) -> AnyStr:
2393 pass
2394
2395 @abstractmethod
2396 def readlines(self, hint: int = -1) -> List[AnyStr]:
2397 pass
2398
2399 @abstractmethod
2400 def seek(self, offset: int, whence: int = 0) -> int:
2401 pass
2402
2403 @abstractmethod
2404 def seekable(self) -> bool:
2405 pass
2406
2407 @abstractmethod
2408 def tell(self) -> int:
2409 pass
2410
2411 @abstractmethod
2412 def truncate(self, size: int = None) -> int:
2413 pass
2414
2415 @abstractmethod
2416 def writable(self) -> bool:
2417 pass
2418
2419 @abstractmethod
2420 def write(self, s: AnyStr) -> int:
2421 pass
2422
2423 @abstractmethod
2424 def writelines(self, lines: List[AnyStr]) -> None:
2425 pass
2426
2427 @abstractmethod
2428 def __enter__(self) -> 'IO[AnyStr]':
2429 pass
2430
2431 @abstractmethod
2432 def __exit__(self, type, value, traceback) -> None:
2433 pass
2434
2435
2436class BinaryIO(IO[bytes]):
2437 """Typed version of the return of open() in binary mode."""
2438
Guido van Rossumd70fe632015-08-05 12:11:06 +02002439 __slots__ = ()
2440
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002441 @abstractmethod
2442 def write(self, s: Union[bytes, bytearray]) -> int:
2443 pass
2444
2445 @abstractmethod
2446 def __enter__(self) -> 'BinaryIO':
2447 pass
2448
2449
2450class TextIO(IO[str]):
2451 """Typed version of the return of open() in text mode."""
2452
Guido van Rossumd70fe632015-08-05 12:11:06 +02002453 __slots__ = ()
2454
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002455 @property
2456 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002457 def buffer(self) -> BinaryIO:
2458 pass
2459
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002460 @property
2461 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002462 def encoding(self) -> str:
2463 pass
2464
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002465 @property
2466 @abstractmethod
Guido van Rossum991d14f2016-11-09 13:12:51 -08002467 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002468 pass
2469
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002470 @property
2471 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002472 def line_buffering(self) -> bool:
2473 pass
2474
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002475 @property
2476 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002477 def newlines(self) -> Any:
2478 pass
2479
2480 @abstractmethod
2481 def __enter__(self) -> 'TextIO':
2482 pass
2483
2484
2485class io:
2486 """Wrapper namespace for IO generic classes."""
2487
2488 __all__ = ['IO', 'TextIO', 'BinaryIO']
2489 IO = IO
2490 TextIO = TextIO
2491 BinaryIO = BinaryIO
2492
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002493
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002494io.__name__ = __name__ + '.io'
2495sys.modules[io.__name__] = io
2496
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002497Pattern = _alias(stdlib_re.Pattern, 1)
2498Match = _alias(stdlib_re.Match, 1)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002499
2500class re:
2501 """Wrapper namespace for re type aliases."""
2502
2503 __all__ = ['Pattern', 'Match']
2504 Pattern = Pattern
2505 Match = Match
2506
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002507
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002508re.__name__ = __name__ + '.re'
2509sys.modules[re.__name__] = re