blob: d409517ff58e9ae22d0ce7a27d2873d3a52d3e3b [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__', {})
Ken Jin852150d2021-04-13 01:23:12 +08001680 base_locals = dict(vars(base)) if localns is None else localns
Ken Jin1b1f9852021-04-27 01:31:21 +08001681 if localns is None and globalns is None:
1682 # This is surprising, but required. Before Python 3.10,
1683 # get_type_hints only evaluated the globalns of
1684 # a class. To maintain backwards compatibility, we reverse
1685 # the globalns and localns order so that eval() looks into
1686 # *base_globals* first rather than *base_locals*.
1687 # This only affects ForwardRefs.
1688 base_globals, base_locals = base_locals, base_globals
Guido van Rossum991d14f2016-11-09 13:12:51 -08001689 for name, value in ann.items():
1690 if value is None:
1691 value = type(None)
1692 if isinstance(value, str):
Nina Zakharenko0e61dff2018-05-22 20:32:10 -07001693 value = ForwardRef(value, is_argument=False)
Ken Jin852150d2021-04-13 01:23:12 +08001694 value = _eval_type(value, base_globals, base_locals)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001695 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001696 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
Łukasz Langaf350a262017-09-14 14:33:00 -04001697
1698 if globalns is None:
1699 if isinstance(obj, types.ModuleType):
1700 globalns = obj.__dict__
1701 else:
benedwards140aca3a32019-11-21 17:24:58 +00001702 nsobj = obj
1703 # Find globalns for the unwrapped object.
1704 while hasattr(nsobj, '__wrapped__'):
1705 nsobj = nsobj.__wrapped__
1706 globalns = getattr(nsobj, '__globals__', {})
Łukasz Langaf350a262017-09-14 14:33:00 -04001707 if localns is None:
1708 localns = globalns
1709 elif localns is None:
1710 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001711 hints = getattr(obj, '__annotations__', None)
1712 if hints is None:
1713 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001714 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001715 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001716 else:
1717 raise TypeError('{!r} is not a module, class, method, '
1718 'or function.'.format(obj))
1719 defaults = _get_defaults(obj)
1720 hints = dict(hints)
1721 for name, value in hints.items():
1722 if value is None:
1723 value = type(None)
1724 if isinstance(value, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001725 value = ForwardRef(value)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001726 value = _eval_type(value, globalns, localns)
1727 if name in defaults and defaults[name] is None:
1728 value = Optional[value]
1729 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001730 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
1731
1732
1733def _strip_annotations(t):
1734 """Strips the annotations from a given type.
1735 """
1736 if isinstance(t, _AnnotatedAlias):
1737 return _strip_annotations(t.__origin__)
1738 if isinstance(t, _GenericAlias):
1739 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1740 if stripped_args == t.__args__:
1741 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001742 return t.copy_with(stripped_args)
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001743 if isinstance(t, GenericAlias):
1744 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1745 if stripped_args == t.__args__:
1746 return t
1747 return GenericAlias(t.__origin__, stripped_args)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001748 return t
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001749
1750
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001751def get_origin(tp):
1752 """Get the unsubscripted version of a type.
1753
Jakub Stasiak38aaaaa2020-02-07 02:15:12 +01001754 This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
1755 and Annotated. Return None for unsupported types. Examples::
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001756
1757 get_origin(Literal[42]) is Literal
1758 get_origin(int) is None
1759 get_origin(ClassVar[int]) is ClassVar
1760 get_origin(Generic) is Generic
1761 get_origin(Generic[T]) is Generic
1762 get_origin(Union[T, int]) is Union
1763 get_origin(List[Tuple[T, T]][int]) == list
Jelle Zijlstra52243362021-04-10 19:57:05 -07001764 get_origin(P.args) is P
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001765 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001766 if isinstance(tp, _AnnotatedAlias):
1767 return Annotated
Jelle Zijlstra52243362021-04-10 19:57:05 -07001768 if isinstance(tp, (_BaseGenericAlias, GenericAlias,
1769 ParamSpecArgs, ParamSpecKwargs)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001770 return tp.__origin__
1771 if tp is Generic:
1772 return Generic
Ken Jinefb1f092020-12-29 10:26:19 +08001773 if isinstance(tp, types.Union):
1774 return types.Union
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001775 return None
1776
1777
1778def get_args(tp):
1779 """Get type arguments with all substitutions performed.
1780
1781 For unions, basic simplifications used by Union constructor are performed.
1782 Examples::
1783 get_args(Dict[str, int]) == (str, int)
1784 get_args(int) == ()
1785 get_args(Union[int, Union[T, int], str][int]) == (int, str)
1786 get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
1787 get_args(Callable[[], T][int]) == ([], int)
1788 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001789 if isinstance(tp, _AnnotatedAlias):
1790 return (tp.__origin__,) + tp.__metadata__
Ken Jin4140f102020-12-29 04:06:19 +08001791 if isinstance(tp, (_GenericAlias, GenericAlias)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001792 res = tp.__args__
Ken Jinefb1f092020-12-29 10:26:19 +08001793 if (tp.__origin__ is collections.abc.Callable
1794 and not (res[0] is Ellipsis
1795 or isinstance(res[0], (ParamSpec, _ConcatenateGenericAlias)))):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001796 res = (list(res[:-1]), res[-1])
1797 return res
Ken Jinefb1f092020-12-29 10:26:19 +08001798 if isinstance(tp, types.Union):
1799 return tp.__args__
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001800 return ()
1801
1802
Patrick Reader0705ec82020-09-16 05:58:32 +01001803def is_typeddict(tp):
1804 """Check if an annotation is a TypedDict class
1805
1806 For example::
1807 class Film(TypedDict):
1808 title: str
1809 year: int
1810
1811 is_typeddict(Film) # => True
1812 is_typeddict(Union[list, str]) # => False
1813 """
1814 return isinstance(tp, _TypedDictMeta)
1815
1816
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001817def no_type_check(arg):
1818 """Decorator to indicate that annotations are not type hints.
1819
1820 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001821 applies recursively to all methods and classes defined in that class
1822 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001823
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001824 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001825 """
1826 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001827 arg_attrs = arg.__dict__.copy()
1828 for attr, val in arg.__dict__.items():
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001829 if val in arg.__bases__ + (arg,):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001830 arg_attrs.pop(attr)
1831 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001832 if isinstance(obj, types.FunctionType):
1833 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001834 if isinstance(obj, type):
1835 no_type_check(obj)
1836 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001837 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001838 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001839 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001840 return arg
1841
1842
1843def no_type_check_decorator(decorator):
1844 """Decorator to give another decorator the @no_type_check effect.
1845
1846 This wraps the decorator with something that wraps the decorated
1847 function in @no_type_check.
1848 """
1849
1850 @functools.wraps(decorator)
1851 def wrapped_decorator(*args, **kwds):
1852 func = decorator(*args, **kwds)
1853 func = no_type_check(func)
1854 return func
1855
1856 return wrapped_decorator
1857
1858
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001859def _overload_dummy(*args, **kwds):
1860 """Helper for @overload to raise when called."""
1861 raise NotImplementedError(
1862 "You should not call an overloaded function. "
1863 "A series of @overload-decorated functions "
1864 "outside a stub module should always be followed "
1865 "by an implementation that is not @overload-ed.")
1866
1867
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001868def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001869 """Decorator for overloaded functions/methods.
1870
1871 In a stub file, place two or more stub definitions for the same
1872 function in a row, each decorated with @overload. For example:
1873
1874 @overload
1875 def utf8(value: None) -> None: ...
1876 @overload
1877 def utf8(value: bytes) -> bytes: ...
1878 @overload
1879 def utf8(value: str) -> bytes: ...
1880
1881 In a non-stub file (i.e. a regular .py file), do the same but
1882 follow it with an implementation. The implementation should *not*
1883 be decorated with @overload. For example:
1884
1885 @overload
1886 def utf8(value: None) -> None: ...
1887 @overload
1888 def utf8(value: bytes) -> bytes: ...
1889 @overload
1890 def utf8(value: str) -> bytes: ...
1891 def utf8(value):
1892 # implementation goes here
1893 """
1894 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001895
1896
Ivan Levkivskyif3672422019-05-26 09:37:07 +01001897def final(f):
1898 """A decorator to indicate final methods and final classes.
1899
1900 Use this decorator to indicate to type checkers that the decorated
1901 method cannot be overridden, and decorated class cannot be subclassed.
1902 For example:
1903
1904 class Base:
1905 @final
1906 def done(self) -> None:
1907 ...
1908 class Sub(Base):
1909 def done(self) -> None: # Error reported by type checker
1910 ...
1911
1912 @final
1913 class Leaf:
1914 ...
1915 class Other(Leaf): # Error reported by type checker
1916 ...
1917
1918 There is no runtime checking of these properties.
1919 """
1920 return f
1921
1922
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001923# Some unconstrained type variables. These are used by the container types.
1924# (These are not for export.)
1925T = TypeVar('T') # Any type.
1926KT = TypeVar('KT') # Key type.
1927VT = TypeVar('VT') # Value type.
1928T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
1929V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
1930VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
1931T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
1932# Internal type variable used for Type[].
1933CT_co = TypeVar('CT_co', covariant=True, bound=type)
1934
1935# A useful type variable with constraints. This represents string types.
1936# (This one *is* for export!)
1937AnyStr = TypeVar('AnyStr', bytes, str)
1938
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001939
1940# Various ABCs mimicking those in collections.abc.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001941_alias = _SpecialGenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001942
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001943Hashable = _alias(collections.abc.Hashable, 0) # Not generic.
1944Awaitable = _alias(collections.abc.Awaitable, 1)
1945Coroutine = _alias(collections.abc.Coroutine, 3)
1946AsyncIterable = _alias(collections.abc.AsyncIterable, 1)
1947AsyncIterator = _alias(collections.abc.AsyncIterator, 1)
1948Iterable = _alias(collections.abc.Iterable, 1)
1949Iterator = _alias(collections.abc.Iterator, 1)
1950Reversible = _alias(collections.abc.Reversible, 1)
1951Sized = _alias(collections.abc.Sized, 0) # Not generic.
1952Container = _alias(collections.abc.Container, 1)
1953Collection = _alias(collections.abc.Collection, 1)
1954Callable = _CallableType(collections.abc.Callable, 2)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001955Callable.__doc__ = \
1956 """Callable type; Callable[[int], str] is a function of (int) -> str.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001957
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001958 The subscription syntax must always be used with exactly two
1959 values: the argument list and the return type. The argument list
1960 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001961
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001962 There is no syntax to indicate optional or keyword arguments,
1963 such function types are rarely used as callback types.
1964 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001965AbstractSet = _alias(collections.abc.Set, 1, name='AbstractSet')
1966MutableSet = _alias(collections.abc.MutableSet, 1)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001967# NOTE: Mapping is only covariant in the value type.
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001968Mapping = _alias(collections.abc.Mapping, 2)
1969MutableMapping = _alias(collections.abc.MutableMapping, 2)
1970Sequence = _alias(collections.abc.Sequence, 1)
1971MutableSequence = _alias(collections.abc.MutableSequence, 1)
1972ByteString = _alias(collections.abc.ByteString, 0) # Not generic
1973# Tuple accepts variable number of parameters.
1974Tuple = _TupleType(tuple, -1, inst=False, name='Tuple')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001975Tuple.__doc__ = \
1976 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001977
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001978 Example: Tuple[T1, T2] is a tuple of two elements corresponding
1979 to type variables T1 and T2. Tuple[int, float, str] is a tuple
1980 of an int, a float and a string.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001981
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001982 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
1983 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001984List = _alias(list, 1, inst=False, name='List')
1985Deque = _alias(collections.deque, 1, name='Deque')
1986Set = _alias(set, 1, inst=False, name='Set')
1987FrozenSet = _alias(frozenset, 1, inst=False, name='FrozenSet')
1988MappingView = _alias(collections.abc.MappingView, 1)
1989KeysView = _alias(collections.abc.KeysView, 1)
1990ItemsView = _alias(collections.abc.ItemsView, 2)
1991ValuesView = _alias(collections.abc.ValuesView, 1)
1992ContextManager = _alias(contextlib.AbstractContextManager, 1, name='ContextManager')
1993AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, 1, name='AsyncContextManager')
1994Dict = _alias(dict, 2, inst=False, name='Dict')
1995DefaultDict = _alias(collections.defaultdict, 2, name='DefaultDict')
1996OrderedDict = _alias(collections.OrderedDict, 2)
1997Counter = _alias(collections.Counter, 1)
1998ChainMap = _alias(collections.ChainMap, 2)
1999Generator = _alias(collections.abc.Generator, 3)
2000AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2)
2001Type = _alias(type, 1, inst=False, name='Type')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002002Type.__doc__ = \
2003 """A special construct usable to annotate class objects.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07002004
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002005 For example, suppose we have the following classes::
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07002006
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002007 class User: ... # Abstract base for User classes
2008 class BasicUser(User): ...
2009 class ProUser(User): ...
2010 class TeamUser(User): ...
Guido van Rossumf17c2002015-12-03 17:31:24 -08002011
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002012 And a function that takes a class argument that's a subclass of
2013 User and returns an instance of the corresponding class::
Guido van Rossumf17c2002015-12-03 17:31:24 -08002014
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002015 U = TypeVar('U', bound=User)
2016 def new_user(user_class: Type[U]) -> U:
2017 user = user_class()
2018 # (Here we could write the user object to a database)
2019 return user
Guido van Rossumf17c2002015-12-03 17:31:24 -08002020
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002021 joe = new_user(BasicUser)
Guido van Rossumf17c2002015-12-03 17:31:24 -08002022
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002023 At this point the type checker knows that joe has type BasicUser.
2024 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002025
2026
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002027@runtime_checkable
2028class SupportsInt(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002029 """An ABC with one abstract method __int__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002030 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002031
2032 @abstractmethod
2033 def __int__(self) -> int:
2034 pass
2035
2036
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002037@runtime_checkable
2038class SupportsFloat(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002039 """An ABC with one abstract method __float__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002040 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002041
2042 @abstractmethod
2043 def __float__(self) -> float:
2044 pass
2045
2046
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002047@runtime_checkable
2048class SupportsComplex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002049 """An ABC with one abstract method __complex__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002050 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002051
2052 @abstractmethod
2053 def __complex__(self) -> complex:
2054 pass
2055
2056
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002057@runtime_checkable
2058class SupportsBytes(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002059 """An ABC with one abstract method __bytes__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002060 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002061
2062 @abstractmethod
2063 def __bytes__(self) -> bytes:
2064 pass
2065
2066
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002067@runtime_checkable
2068class SupportsIndex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002069 """An ABC with one abstract method __index__."""
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -07002070 __slots__ = ()
2071
2072 @abstractmethod
2073 def __index__(self) -> int:
2074 pass
2075
2076
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002077@runtime_checkable
2078class SupportsAbs(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002079 """An ABC with one abstract method __abs__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002080 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002081
2082 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02002083 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002084 pass
2085
2086
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002087@runtime_checkable
2088class SupportsRound(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002089 """An ABC with one abstract method __round__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002090 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002091
2092 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02002093 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002094 pass
2095
2096
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002097def _make_nmtuple(name, types, module, defaults = ()):
2098 fields = [n for n, t in types]
2099 types = {n: _type_check(t, f"field {n} annotation must be a type")
2100 for n, t in types}
2101 nm_tpl = collections.namedtuple(name, fields,
2102 defaults=defaults, module=module)
2103 nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002104 return nm_tpl
2105
2106
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002107# attributes prohibited to set in NamedTuple class syntax
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002108_prohibited = frozenset({'__new__', '__init__', '__slots__', '__getnewargs__',
2109 '_fields', '_field_defaults',
2110 '_make', '_replace', '_asdict', '_source'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002111
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002112_special = frozenset({'__module__', '__name__', '__annotations__'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002113
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002114
Guido van Rossum2f841442016-11-15 09:48:06 -08002115class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002116
Guido van Rossum2f841442016-11-15 09:48:06 -08002117 def __new__(cls, typename, bases, ns):
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002118 assert bases[0] is _NamedTuple
Guido van Rossum2f841442016-11-15 09:48:06 -08002119 types = ns.get('__annotations__', {})
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002120 default_names = []
Guido van Rossum3c268be2017-01-18 08:03:50 -08002121 for field_name in types:
2122 if field_name in ns:
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002123 default_names.append(field_name)
2124 elif default_names:
2125 raise TypeError(f"Non-default namedtuple field {field_name} "
2126 f"cannot follow default field"
2127 f"{'s' if len(default_names) > 1 else ''} "
2128 f"{', '.join(default_names)}")
2129 nm_tpl = _make_nmtuple(typename, types.items(),
2130 defaults=[ns[n] for n in default_names],
2131 module=ns['__module__'])
Guido van Rossum95919c02017-01-22 17:47:20 -08002132 # update from user namespace without overriding special namedtuple attributes
2133 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002134 if key in _prohibited:
2135 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
2136 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08002137 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08002138 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002139
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002140
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002141def NamedTuple(typename, fields=None, /, **kwargs):
Guido van Rossum2f841442016-11-15 09:48:06 -08002142 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002143
Guido van Rossum2f841442016-11-15 09:48:06 -08002144 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002145
Guido van Rossum2f841442016-11-15 09:48:06 -08002146 class Employee(NamedTuple):
2147 name: str
2148 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002149
Guido van Rossum2f841442016-11-15 09:48:06 -08002150 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002151
Guido van Rossum2f841442016-11-15 09:48:06 -08002152 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002153
Raymond Hettingerf7b57df2019-03-18 09:53:56 -07002154 The resulting class has an extra __annotations__ attribute, giving a
2155 dict that maps field names to types. (The field names are also in
2156 the _fields attribute, which is part of the namedtuple API.)
2157 Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002158
Guido van Rossum2f841442016-11-15 09:48:06 -08002159 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002160
Guido van Rossum2f841442016-11-15 09:48:06 -08002161 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002162
Guido van Rossum2f841442016-11-15 09:48:06 -08002163 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
2164 """
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002165 if fields is None:
2166 fields = kwargs.items()
2167 elif kwargs:
2168 raise TypeError("Either list of fields or keywords"
2169 " can be provided to NamedTuple, not both")
2170 try:
2171 module = sys._getframe(1).f_globals.get('__name__', '__main__')
2172 except (AttributeError, ValueError):
2173 module = None
2174 return _make_nmtuple(typename, fields, module=module)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002175
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002176_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})
2177
2178def _namedtuple_mro_entries(bases):
2179 if len(bases) > 1:
2180 raise TypeError("Multiple inheritance with NamedTuple is not supported")
2181 assert bases[0] is NamedTuple
2182 return (_NamedTuple,)
2183
2184NamedTuple.__mro_entries__ = _namedtuple_mro_entries
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002185
2186
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002187class _TypedDictMeta(type):
2188 def __new__(cls, name, bases, ns, total=True):
2189 """Create new typed dict class object.
2190
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002191 This method is called when TypedDict is subclassed,
2192 or when TypedDict is instantiated. This way
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002193 TypedDict supports all three syntax forms described in its docstring.
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002194 Subclasses and instances of TypedDict return actual dictionaries.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002195 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002196 for base in bases:
2197 if type(base) is not _TypedDictMeta:
2198 raise TypeError('cannot inherit from both a TypedDict type '
2199 'and a non-TypedDict base class')
2200 tp_dict = type.__new__(_TypedDictMeta, name, (dict,), ns)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002201
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002202 annotations = {}
2203 own_annotations = ns.get('__annotations__', {})
2204 own_annotation_keys = set(own_annotations.keys())
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002205 msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002206 own_annotations = {
2207 n: _type_check(tp, msg) for n, tp in own_annotations.items()
2208 }
2209 required_keys = set()
2210 optional_keys = set()
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002211
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002212 for base in bases:
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002213 annotations.update(base.__dict__.get('__annotations__', {}))
2214 required_keys.update(base.__dict__.get('__required_keys__', ()))
2215 optional_keys.update(base.__dict__.get('__optional_keys__', ()))
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002216
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002217 annotations.update(own_annotations)
2218 if total:
2219 required_keys.update(own_annotation_keys)
2220 else:
2221 optional_keys.update(own_annotation_keys)
2222
2223 tp_dict.__annotations__ = annotations
2224 tp_dict.__required_keys__ = frozenset(required_keys)
2225 tp_dict.__optional_keys__ = frozenset(optional_keys)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002226 if not hasattr(tp_dict, '__total__'):
2227 tp_dict.__total__ = total
2228 return tp_dict
2229
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002230 __call__ = dict # static method
2231
2232 def __subclasscheck__(cls, other):
2233 # Typed dicts are only for static structural subtyping.
2234 raise TypeError('TypedDict does not support instance and class checks')
2235
2236 __instancecheck__ = __subclasscheck__
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002237
2238
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002239def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002240 """A simple typed namespace. At runtime it is equivalent to a plain dict.
2241
2242 TypedDict creates a dictionary type that expects all of its
2243 instances to have a certain set of keys, where each key is
2244 associated with a value of a consistent type. This expectation
2245 is not checked at runtime but is only enforced by type checkers.
2246 Usage::
2247
2248 class Point2D(TypedDict):
2249 x: int
2250 y: int
2251 label: str
2252
2253 a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
2254 b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
2255
2256 assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
2257
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002258 The type info can be accessed via the Point2D.__annotations__ dict, and
2259 the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
2260 TypedDict supports two additional equivalent forms::
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002261
2262 Point2D = TypedDict('Point2D', x=int, y=int, label=str)
2263 Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
2264
ananthan-123ab6423f2020-02-19 10:03:05 +05302265 By default, all keys must be present in a TypedDict. It is possible
2266 to override this by specifying totality.
2267 Usage::
2268
2269 class point2D(TypedDict, total=False):
2270 x: int
2271 y: int
2272
2273 This means that a point2D TypedDict can have any of the keys omitted.A type
2274 checker is only expected to support a literal False or True as the value of
2275 the total argument. True is the default, and makes all items defined in the
2276 class body be required.
2277
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002278 The class syntax is only supported in Python 3.6+, while two other
2279 syntax forms work for Python 2.7 and 3.2+
2280 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002281 if fields is None:
2282 fields = kwargs
2283 elif kwargs:
2284 raise TypeError("TypedDict takes either a dict or keyword arguments,"
2285 " but not both")
2286
Alex Grönholm67b769f2020-12-10 23:49:05 +02002287 ns = {'__annotations__': dict(fields)}
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002288 try:
2289 # Setting correct module is necessary to make typed dict classes pickleable.
2290 ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
2291 except (AttributeError, ValueError):
2292 pass
2293
Alex Grönholm67b769f2020-12-10 23:49:05 +02002294 return _TypedDictMeta(typename, (), ns, total=total)
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002295
2296_TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {})
2297TypedDict.__mro_entries__ = lambda bases: (_TypedDict,)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002298
2299
Guido van Rossum91185fe2016-06-08 11:19:11 -07002300def NewType(name, tp):
2301 """NewType creates simple unique types with almost zero
2302 runtime overhead. NewType(name, tp) is considered a subtype of tp
2303 by static type checkers. At runtime, NewType(name, tp) returns
2304 a dummy function that simply returns its argument. Usage::
2305
2306 UserId = NewType('UserId', int)
2307
2308 def name_by_id(user_id: UserId) -> str:
2309 ...
2310
2311 UserId('user') # Fails type check
2312
2313 name_by_id(42) # Fails type check
2314 name_by_id(UserId(42)) # OK
2315
2316 num = UserId(5) + 1 # type: int
2317 """
2318
2319 def new_type(x):
2320 return x
2321
2322 new_type.__name__ = name
2323 new_type.__supertype__ = tp
2324 return new_type
2325
2326
Guido van Rossum0e0563c2016-04-05 14:54:25 -07002327# Python-version-specific alias (Python 2: unicode; Python 3: str)
2328Text = str
2329
2330
Guido van Rossum91185fe2016-06-08 11:19:11 -07002331# Constant that's True when type checking, but False here.
2332TYPE_CHECKING = False
2333
2334
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002335class IO(Generic[AnyStr]):
2336 """Generic base class for TextIO and BinaryIO.
2337
2338 This is an abstract, generic version of the return of open().
2339
2340 NOTE: This does not distinguish between the different possible
2341 classes (text vs. binary, read vs. write vs. read/write,
2342 append-only, unbuffered). The TextIO and BinaryIO subclasses
2343 below capture the distinctions between text vs. binary, which is
2344 pervasive in the interface; however we currently do not offer a
2345 way to track the other distinctions in the type system.
2346 """
2347
Guido van Rossumd70fe632015-08-05 12:11:06 +02002348 __slots__ = ()
2349
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002350 @property
2351 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002352 def mode(self) -> str:
2353 pass
2354
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002355 @property
2356 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002357 def name(self) -> str:
2358 pass
2359
2360 @abstractmethod
2361 def close(self) -> None:
2362 pass
2363
Shantanu2e6569b2020-01-29 18:52:36 -08002364 @property
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002365 @abstractmethod
2366 def closed(self) -> bool:
2367 pass
2368
2369 @abstractmethod
2370 def fileno(self) -> int:
2371 pass
2372
2373 @abstractmethod
2374 def flush(self) -> None:
2375 pass
2376
2377 @abstractmethod
2378 def isatty(self) -> bool:
2379 pass
2380
2381 @abstractmethod
2382 def read(self, n: int = -1) -> AnyStr:
2383 pass
2384
2385 @abstractmethod
2386 def readable(self) -> bool:
2387 pass
2388
2389 @abstractmethod
2390 def readline(self, limit: int = -1) -> AnyStr:
2391 pass
2392
2393 @abstractmethod
2394 def readlines(self, hint: int = -1) -> List[AnyStr]:
2395 pass
2396
2397 @abstractmethod
2398 def seek(self, offset: int, whence: int = 0) -> int:
2399 pass
2400
2401 @abstractmethod
2402 def seekable(self) -> bool:
2403 pass
2404
2405 @abstractmethod
2406 def tell(self) -> int:
2407 pass
2408
2409 @abstractmethod
2410 def truncate(self, size: int = None) -> int:
2411 pass
2412
2413 @abstractmethod
2414 def writable(self) -> bool:
2415 pass
2416
2417 @abstractmethod
2418 def write(self, s: AnyStr) -> int:
2419 pass
2420
2421 @abstractmethod
2422 def writelines(self, lines: List[AnyStr]) -> None:
2423 pass
2424
2425 @abstractmethod
2426 def __enter__(self) -> 'IO[AnyStr]':
2427 pass
2428
2429 @abstractmethod
2430 def __exit__(self, type, value, traceback) -> None:
2431 pass
2432
2433
2434class BinaryIO(IO[bytes]):
2435 """Typed version of the return of open() in binary mode."""
2436
Guido van Rossumd70fe632015-08-05 12:11:06 +02002437 __slots__ = ()
2438
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002439 @abstractmethod
2440 def write(self, s: Union[bytes, bytearray]) -> int:
2441 pass
2442
2443 @abstractmethod
2444 def __enter__(self) -> 'BinaryIO':
2445 pass
2446
2447
2448class TextIO(IO[str]):
2449 """Typed version of the return of open() in text mode."""
2450
Guido van Rossumd70fe632015-08-05 12:11:06 +02002451 __slots__ = ()
2452
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002453 @property
2454 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002455 def buffer(self) -> BinaryIO:
2456 pass
2457
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002458 @property
2459 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002460 def encoding(self) -> str:
2461 pass
2462
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002463 @property
2464 @abstractmethod
Guido van Rossum991d14f2016-11-09 13:12:51 -08002465 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002466 pass
2467
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002468 @property
2469 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002470 def line_buffering(self) -> bool:
2471 pass
2472
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002473 @property
2474 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002475 def newlines(self) -> Any:
2476 pass
2477
2478 @abstractmethod
2479 def __enter__(self) -> 'TextIO':
2480 pass
2481
2482
2483class io:
2484 """Wrapper namespace for IO generic classes."""
2485
2486 __all__ = ['IO', 'TextIO', 'BinaryIO']
2487 IO = IO
2488 TextIO = TextIO
2489 BinaryIO = BinaryIO
2490
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002491
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002492io.__name__ = __name__ + '.io'
2493sys.modules[io.__name__] = io
2494
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002495Pattern = _alias(stdlib_re.Pattern, 1)
2496Match = _alias(stdlib_re.Match, 1)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002497
2498class re:
2499 """Wrapper namespace for re type aliases."""
2500
2501 __all__ = ['Pattern', 'Match']
2502 Pattern = Pattern
2503 Match = Match
2504
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002505
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002506re.__name__ = __name__ + '.re'
2507sys.modules[re.__name__] = re