blob: 46c54c406992f748fa4b61ecfa75aea07a7400b6 [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.
7* _SpecialForm and its instances (special forms): Any, NoReturn, ClassVar, Union, Optional
8* Two classes whose instances can be type arguments in addition to types: ForwardRef and TypeVar
9* The core of internal generics API: _GenericAlias and _VariadicGenericAlias, the latter is
10 currently only used by Tuple and Callable. All subscripted types like X[int], Union[int, str],
11 etc., are instances of either of these classes.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +010012* The public counterpart of the generics API consists of two classes: Generic and Protocol.
Ivan Levkivskyid911e402018-01-20 11:23:59 +000013* Public helper functions: get_type_hints, overload, cast, no_type_check,
14 no_type_check_decorator.
15* Generic aliases for collections.abc ABCs and few additional protocols.
ananthan-123ab6423f2020-02-19 10:03:05 +053016* Special types: NewType, NamedTuple, TypedDict.
Ivan Levkivskyid911e402018-01-20 11:23:59 +000017* Wrapper submodules for re and io related types.
18"""
19
HongWeipeng6ce03ec2019-09-27 15:54:26 +080020from abc import abstractmethod, ABCMeta
Batuhan Taskaya044a1042020-10-06 23:03:02 +030021import ast
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070022import collections
Ivan Levkivskyid911e402018-01-20 11:23:59 +000023import collections.abc
Brett Cannonf3ad0422016-04-15 10:51:30 -070024import contextlib
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070025import functools
Serhiy Storchaka09f32212018-05-26 21:19:26 +030026import operator
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070027import re as stdlib_re # Avoid confusion with the re we export.
28import sys
29import types
Guido van Rossum48b069a2020-04-07 09:50:06 -070030from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType, GenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070031
32# Please keep __all__ alphabetized within each category.
33__all__ = [
34 # Super-special typing primitives.
Jakub Stasiakcf5b1092020-02-05 02:10:19 +010035 'Annotated',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070036 'Any',
37 'Callable',
Guido van Rossum0a6976d2016-09-11 15:34:56 -070038 'ClassVar',
Ivan Levkivskyif3672422019-05-26 09:37:07 +010039 'Final',
Anthony Sottiled30da5d2019-05-29 11:19:38 -070040 'ForwardRef',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070041 'Generic',
Ivan Levkivskyib891c462019-05-26 09:37:48 +010042 'Literal',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070043 'Optional',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +010044 'Protocol',
Guido van Rossumeb9aca32016-05-24 16:38:22 -070045 'Tuple',
46 'Type',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070047 'TypeVar',
48 'Union',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070049
50 # ABCs (from collections.abc).
51 'AbstractSet', # collections.abc.Set.
52 'ByteString',
53 'Container',
Ivan Levkivskyi29fda8d2017-06-10 21:57:56 +020054 'ContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070055 'Hashable',
56 'ItemsView',
57 'Iterable',
58 'Iterator',
59 'KeysView',
60 'Mapping',
61 'MappingView',
62 'MutableMapping',
63 'MutableSequence',
64 'MutableSet',
65 'Sequence',
66 'Sized',
67 'ValuesView',
Ivan Levkivskyid911e402018-01-20 11:23:59 +000068 'Awaitable',
69 'AsyncIterator',
70 'AsyncIterable',
71 'Coroutine',
72 'Collection',
73 'AsyncGenerator',
74 'AsyncContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070075
76 # Structural checks, a.k.a. protocols.
77 'Reversible',
78 'SupportsAbs',
Ivan Levkivskyif06e0212017-05-02 19:14:07 +020079 'SupportsBytes',
80 'SupportsComplex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070081 'SupportsFloat',
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -070082 'SupportsIndex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070083 'SupportsInt',
84 'SupportsRound',
85
86 # Concrete collection types.
Anthony Sottiled30da5d2019-05-29 11:19:38 -070087 'ChainMap',
Ivan Levkivskyib692dc82017-02-13 22:50:14 +010088 'Counter',
Raymond Hettinger80490522017-01-16 22:42:37 -080089 'Deque',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070090 'Dict',
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070091 'DefaultDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070092 'List',
Anthony Sottiled30da5d2019-05-29 11:19:38 -070093 'OrderedDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070094 'Set',
Guido van Rossumefa798d2016-08-23 11:01:50 -070095 'FrozenSet',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070096 'NamedTuple', # Not really a type.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +010097 'TypedDict', # Not really a type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070098 'Generator',
99
100 # One-off things.
101 'AnyStr',
102 'cast',
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100103 'final',
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +0100104 'get_args',
105 'get_origin',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700106 'get_type_hints',
Patrick Reader0705ec82020-09-16 05:58:32 +0100107 'is_typeddict',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700108 'NewType',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700109 'no_type_check',
110 'no_type_check_decorator',
aetracht45738202018-03-19 14:41:32 -0400111 'NoReturn',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700112 'overload',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100113 'runtime_checkable',
Guido van Rossum0e0563c2016-04-05 14:54:25 -0700114 'Text',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700115 'TYPE_CHECKING',
Mikhail Golubev4f3c2502020-10-08 00:44:31 +0300116 'TypeAlias',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700117]
118
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700119# The pseudo-submodules 're' and 'io' are part of the public
120# namespace, but excluded from __all__ because they might stomp on
121# legitimate imports of those modules.
122
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700123def _type_check(arg, msg, is_argument=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000124 """Check that the argument is a type, and return it (internal helper).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700125
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000126 As a special case, accept None and return type(None) instead. Also wrap strings
127 into ForwardRef instances. Consider several corner cases, for example plain
128 special forms like Union are not valid, while Union[int, str] is OK, etc.
129 The msg argument is a human-readable error message, e.g::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700130
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000131 "Union[arg, ...]: arg should be a type."
Guido van Rossum4cefe742016-09-27 15:20:12 -0700132
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000133 We append the repr() of the actual value (truncated to 100 chars).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700134 """
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100135 invalid_generic_forms = (Generic, Protocol)
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700136 if is_argument:
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100137 invalid_generic_forms = invalid_generic_forms + (ClassVar, Final)
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400138
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000139 if arg is None:
140 return type(None)
141 if isinstance(arg, str):
142 return ForwardRef(arg)
143 if (isinstance(arg, _GenericAlias) and
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400144 arg.__origin__ in invalid_generic_forms):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000145 raise TypeError(f"{arg} is not valid as type argument")
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300146 if arg in (Any, NoReturn):
147 return arg
148 if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000149 raise TypeError(f"Plain {arg} is not valid as type argument")
Maggie Moss1b4552c2020-09-09 13:23:24 -0700150 if isinstance(arg, (type, TypeVar, ForwardRef, types.Union)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000151 return arg
152 if not callable(arg):
153 raise TypeError(f"{msg} Got {arg!r:.100}.")
154 return arg
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700155
156
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000157def _type_repr(obj):
158 """Return the repr() of an object, special-casing types (internal helper).
159
160 If obj is a type, we return a shorter version than the default
161 type.__repr__, based on the module and qualified name, which is
162 typically enough to uniquely identify a type. For everything
163 else, we fall back on repr(obj).
164 """
kj1f7dfb22020-11-02 02:13:38 +0800165 if isinstance(obj, types.GenericAlias):
166 return repr(obj)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000167 if isinstance(obj, type):
168 if obj.__module__ == 'builtins':
169 return obj.__qualname__
170 return f'{obj.__module__}.{obj.__qualname__}'
171 if obj is ...:
172 return('...')
173 if isinstance(obj, types.FunctionType):
174 return obj.__name__
175 return repr(obj)
176
177
178def _collect_type_vars(types):
179 """Collect all type variable contained in types in order of
180 first appearance (lexicographic order). For example::
181
182 _collect_type_vars((T, List[S, T])) == (T, S)
183 """
184 tvars = []
185 for t in types:
186 if isinstance(t, TypeVar) and t not in tvars:
187 tvars.append(t)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300188 if isinstance(t, (_GenericAlias, GenericAlias)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000189 tvars.extend([t for t in t.__parameters__ if t not in tvars])
190 return tuple(tvars)
191
192
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300193def _check_generic(cls, parameters, elen):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000194 """Check correct count for parameters of a generic cls (internal helper).
195 This gives a nice error message in case of count mismatch.
196 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300197 if not elen:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000198 raise TypeError(f"{cls} is not a generic class")
199 alen = len(parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000200 if alen != elen:
201 raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
202 f" actual {alen}, expected {elen}")
203
204
Yurii Karabasf03d3182020-11-17 04:23:19 +0200205def _deduplicate(params):
206 # Weed out strict duplicates, preserving the first of each occurrence.
207 all_params = set(params)
208 if len(all_params) < len(params):
209 new_params = []
210 for t in params:
211 if t in all_params:
212 new_params.append(t)
213 all_params.remove(t)
214 params = new_params
215 assert not all_params, all_params
216 return params
217
218
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000219def _remove_dups_flatten(parameters):
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700220 """An internal helper for Union creation and substitution: flatten Unions
221 among parameters, then remove duplicates.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000222 """
223 # Flatten out Union[Union[...], ...].
224 params = []
225 for p in parameters:
Maggie Moss1b4552c2020-09-09 13:23:24 -0700226 if isinstance(p, (_UnionGenericAlias, types.Union)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000227 params.extend(p.__args__)
228 elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
229 params.extend(p[1:])
230 else:
231 params.append(p)
Yurii Karabasf03d3182020-11-17 04:23:19 +0200232
233 return tuple(_deduplicate(params))
234
235
236def _flatten_literal_params(parameters):
237 """An internal helper for Literal creation: flatten Literals among parameters"""
238 params = []
239 for p in parameters:
240 if isinstance(p, _LiteralGenericAlias):
241 params.extend(p.__args__)
242 else:
243 params.append(p)
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700244 return tuple(params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000245
246
247_cleanups = []
248
249
Yurii Karabasf03d3182020-11-17 04:23:19 +0200250def _tp_cache(func=None, /, *, typed=False):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000251 """Internal wrapper caching __getitem__ of generic types with a fallback to
252 original function for non-hashable arguments.
253 """
Yurii Karabasf03d3182020-11-17 04:23:19 +0200254 def decorator(func):
255 cached = functools.lru_cache(typed=typed)(func)
256 _cleanups.append(cached.cache_clear)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000257
Yurii Karabasf03d3182020-11-17 04:23:19 +0200258 @functools.wraps(func)
259 def inner(*args, **kwds):
260 try:
261 return cached(*args, **kwds)
262 except TypeError:
263 pass # All real errors (not unhashable args) are raised below.
264 return func(*args, **kwds)
265 return inner
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000266
Yurii Karabasf03d3182020-11-17 04:23:19 +0200267 if func is not None:
268 return decorator(func)
269
270 return decorator
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000271
wyfo653f4202020-07-22 21:47:28 +0200272def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
Graham Bleaney84ef33c2020-09-08 18:41:10 -0400273 """Evaluate all forward references in the given type t.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000274 For use of globalns and localns see the docstring for get_type_hints().
wyfo653f4202020-07-22 21:47:28 +0200275 recursive_guard is used to prevent prevent infinite recursion
276 with recursive ForwardRef.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000277 """
278 if isinstance(t, ForwardRef):
wyfo653f4202020-07-22 21:47:28 +0200279 return t._evaluate(globalns, localns, recursive_guard)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300280 if isinstance(t, (_GenericAlias, GenericAlias)):
wyfo653f4202020-07-22 21:47:28 +0200281 ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000282 if ev_args == t.__args__:
283 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300284 if isinstance(t, GenericAlias):
285 return GenericAlias(t.__origin__, ev_args)
286 else:
287 return t.copy_with(ev_args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000288 return t
289
290
291class _Final:
292 """Mixin to prohibit subclassing"""
Guido van Rossum4cefe742016-09-27 15:20:12 -0700293
Guido van Rossum83ec3022017-01-17 20:43:28 -0800294 __slots__ = ('__weakref__',)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700295
Serhiy Storchaka2085bd02019-06-01 11:00:15 +0300296 def __init_subclass__(self, /, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000297 if '_root' not in kwds:
298 raise TypeError("Cannot subclass special typing classes")
299
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100300class _Immutable:
301 """Mixin to indicate that object should not be copied."""
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300302 __slots__ = ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000303
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100304 def __copy__(self):
305 return self
306
307 def __deepcopy__(self, memo):
308 return self
309
310
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300311# Internal indicator of special typing constructs.
312# See __doc__ instance attribute for specific docs.
313class _SpecialForm(_Final, _root=True):
314 __slots__ = ('_name', '__doc__', '_getitem')
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000315
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300316 def __init__(self, getitem):
317 self._getitem = getitem
318 self._name = getitem.__name__
319 self.__doc__ = getitem.__doc__
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000320
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300321 def __mro_entries__(self, bases):
322 raise TypeError(f"Cannot subclass {self!r}")
Guido van Rossum4cefe742016-09-27 15:20:12 -0700323
324 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000325 return 'typing.' + self._name
326
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100327 def __reduce__(self):
328 return self._name
Guido van Rossum4cefe742016-09-27 15:20:12 -0700329
330 def __call__(self, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000331 raise TypeError(f"Cannot instantiate {self!r}")
332
333 def __instancecheck__(self, obj):
334 raise TypeError(f"{self} cannot be used with isinstance()")
335
336 def __subclasscheck__(self, cls):
337 raise TypeError(f"{self} cannot be used with issubclass()")
338
339 @_tp_cache
340 def __getitem__(self, parameters):
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300341 return self._getitem(self, parameters)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700342
Yurii Karabasf03d3182020-11-17 04:23:19 +0200343
344class _LiteralSpecialForm(_SpecialForm, _root=True):
345 @_tp_cache(typed=True)
346 def __getitem__(self, parameters):
347 return self._getitem(self, parameters)
348
349
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300350@_SpecialForm
351def Any(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000352 """Special type indicating an unconstrained type.
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700353
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000354 - Any is compatible with every type.
355 - Any assumed to have all methods.
356 - All values assumed to be instances of Any.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700357
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000358 Note that all the above statements are true from the point of view of
359 static type checkers. At runtime, Any should not be used with instance
360 or class checks.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300361 """
362 raise TypeError(f"{self} is not subscriptable")
Guido van Rossumd70fe632015-08-05 12:11:06 +0200363
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300364@_SpecialForm
365def NoReturn(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000366 """Special type indicating functions that never return.
367 Example::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700368
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000369 from typing import NoReturn
370
371 def stop() -> NoReturn:
372 raise Exception('no way')
373
374 This type is invalid in other positions, e.g., ``List[NoReturn]``
375 will fail in static type checkers.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300376 """
377 raise TypeError(f"{self} is not subscriptable")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000378
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300379@_SpecialForm
380def ClassVar(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000381 """Special type construct to mark class variables.
382
383 An annotation wrapped in ClassVar indicates that a given
384 attribute is intended to be used as a class variable and
385 should not be set on instances of that class. Usage::
386
387 class Starship:
388 stats: ClassVar[Dict[str, int]] = {} # class variable
389 damage: int = 10 # instance variable
390
391 ClassVar accepts only types and cannot be further subscribed.
392
393 Note that ClassVar is not a class itself, and should not
394 be used with isinstance() or issubclass().
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300395 """
396 item = _type_check(parameters, f'{self} accepts only single type.')
397 return _GenericAlias(self, (item,))
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000398
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300399@_SpecialForm
400def Final(self, parameters):
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100401 """Special typing construct to indicate final names to type checkers.
402
403 A final name cannot be re-assigned or overridden in a subclass.
404 For example:
405
406 MAX_SIZE: Final = 9000
407 MAX_SIZE += 1 # Error reported by type checker
408
409 class Connection:
410 TIMEOUT: Final[int] = 10
411
412 class FastConnector(Connection):
413 TIMEOUT = 1 # Error reported by type checker
414
415 There is no runtime checking of these properties.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300416 """
417 item = _type_check(parameters, f'{self} accepts only single type.')
418 return _GenericAlias(self, (item,))
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100419
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300420@_SpecialForm
421def Union(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000422 """Union type; Union[X, Y] means either X or Y.
423
424 To define a union, use e.g. Union[int, str]. Details:
425 - The arguments must be types and there must be at least one.
426 - None as an argument is a special case and is replaced by
427 type(None).
428 - Unions of unions are flattened, e.g.::
429
430 Union[Union[int, str], float] == Union[int, str, float]
431
432 - Unions of a single argument vanish, e.g.::
433
434 Union[int] == int # The constructor actually returns int
435
436 - Redundant arguments are skipped, e.g.::
437
438 Union[int, str, int] == Union[int, str]
439
440 - When comparing unions, the argument order is ignored, e.g.::
441
442 Union[int, str] == Union[str, int]
443
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000444 - You cannot subclass or instantiate a union.
445 - You can use Optional[X] as a shorthand for Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300446 """
447 if parameters == ():
448 raise TypeError("Cannot take a Union of no types.")
449 if not isinstance(parameters, tuple):
450 parameters = (parameters,)
451 msg = "Union[arg, ...]: each arg must be a type."
452 parameters = tuple(_type_check(p, msg) for p in parameters)
453 parameters = _remove_dups_flatten(parameters)
454 if len(parameters) == 1:
455 return parameters[0]
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300456 return _UnionGenericAlias(self, parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000457
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300458@_SpecialForm
459def Optional(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000460 """Optional type.
461
462 Optional[X] is equivalent to Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300463 """
464 arg = _type_check(parameters, f"{self} requires a single type.")
465 return Union[arg, type(None)]
Guido van Rossumb7dedc82016-10-29 12:44:29 -0700466
Yurii Karabasf03d3182020-11-17 04:23:19 +0200467@_LiteralSpecialForm
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300468def Literal(self, parameters):
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100469 """Special typing form to define literal types (a.k.a. value types).
470
471 This form can be used to indicate to type checkers that the corresponding
472 variable or function parameter has a value equivalent to the provided
473 literal (or one of several literals):
474
475 def validate_simple(data: Any) -> Literal[True]: # always returns True
476 ...
477
478 MODE = Literal['r', 'rb', 'w', 'wb']
479 def open_helper(file: str, mode: MODE) -> str:
480 ...
481
482 open_helper('/some/path', 'r') # Passes type check
483 open_helper('/other/path', 'typo') # Error in type checker
484
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300485 Literal[...] cannot be subclassed. At runtime, an arbitrary value
486 is allowed as type argument to Literal[...], but type checkers may
487 impose restrictions.
488 """
489 # There is no '_type_check' call because arguments to Literal[...] are
490 # values, not types.
Yurii Karabasf03d3182020-11-17 04:23:19 +0200491 if not isinstance(parameters, tuple):
492 parameters = (parameters,)
493
494 parameters = _flatten_literal_params(parameters)
495
496 try:
497 parameters = tuple(p for p, _ in _deduplicate(list(_value_and_type_iter(parameters))))
498 except TypeError: # unhashable parameters
499 pass
500
501 return _LiteralGenericAlias(self, parameters)
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100502
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700503
Mikhail Golubev4f3c2502020-10-08 00:44:31 +0300504@_SpecialForm
505def TypeAlias(self, parameters):
506 """Special marker indicating that an assignment should
507 be recognized as a proper type alias definition by type
508 checkers.
509
510 For example::
511
512 Predicate: TypeAlias = Callable[..., bool]
513
514 It's invalid when used anywhere except as in the example above.
515 """
516 raise TypeError(f"{self} is not subscriptable")
517
518
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000519class ForwardRef(_Final, _root=True):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800520 """Internal wrapper to hold a forward reference."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700521
Guido van Rossum4cefe742016-09-27 15:20:12 -0700522 __slots__ = ('__forward_arg__', '__forward_code__',
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400523 '__forward_evaluated__', '__forward_value__',
524 '__forward_is_argument__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700525
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700526 def __init__(self, arg, is_argument=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700527 if not isinstance(arg, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000528 raise TypeError(f"Forward reference must be a string -- got {arg!r}")
Batuhan Taskaya044a1042020-10-06 23:03:02 +0300529
530 # Double-stringified forward references is a result of activating
531 # the 'annotations' future by default. This way, we eliminate them in
532 # the runtime.
533 if arg.startswith(("'", '\"')) and arg.endswith(("'", '"')):
534 arg = arg[1:-1]
535
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700536 try:
537 code = compile(arg, '<string>', 'eval')
538 except SyntaxError:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000539 raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700540 self.__forward_arg__ = arg
541 self.__forward_code__ = code
542 self.__forward_evaluated__ = False
543 self.__forward_value__ = None
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400544 self.__forward_is_argument__ = is_argument
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700545
wyfo653f4202020-07-22 21:47:28 +0200546 def _evaluate(self, globalns, localns, recursive_guard):
547 if self.__forward_arg__ in recursive_guard:
548 return self
Guido van Rossumdad17902016-11-10 08:29:18 -0800549 if not self.__forward_evaluated__ or localns is not globalns:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700550 if globalns is None and localns is None:
551 globalns = localns = {}
552 elif globalns is None:
553 globalns = localns
554 elif localns is None:
555 localns = globalns
wyfo653f4202020-07-22 21:47:28 +0200556 type_ =_type_check(
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700557 eval(self.__forward_code__, globalns, localns),
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400558 "Forward references must evaluate to types.",
wyfo653f4202020-07-22 21:47:28 +0200559 is_argument=self.__forward_is_argument__,
560 )
561 self.__forward_value__ = _eval_type(
562 type_, globalns, localns, recursive_guard | {self.__forward_arg__}
563 )
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700564 self.__forward_evaluated__ = True
565 return self.__forward_value__
566
Guido van Rossum4cefe742016-09-27 15:20:12 -0700567 def __eq__(self, other):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000568 if not isinstance(other, ForwardRef):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700569 return NotImplemented
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100570 if self.__forward_evaluated__ and other.__forward_evaluated__:
571 return (self.__forward_arg__ == other.__forward_arg__ and
572 self.__forward_value__ == other.__forward_value__)
573 return self.__forward_arg__ == other.__forward_arg__
Guido van Rossum4cefe742016-09-27 15:20:12 -0700574
575 def __hash__(self):
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100576 return hash(self.__forward_arg__)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700577
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700578 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000579 return f'ForwardRef({self.__forward_arg__!r})'
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700580
581
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100582class TypeVar(_Final, _Immutable, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700583 """Type variable.
584
585 Usage::
586
587 T = TypeVar('T') # Can be anything
588 A = TypeVar('A', str, bytes) # Must be str or bytes
589
590 Type variables exist primarily for the benefit of static type
591 checkers. They serve as the parameters for generic types as well
592 as for generic function definitions. See class Generic for more
593 information on generic types. Generic functions work as follows:
594
Guido van Rossumb24569a2016-11-20 18:01:29 -0800595 def repeat(x: T, n: int) -> List[T]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700596 '''Return a list containing n references to x.'''
597 return [x]*n
598
599 def longest(x: A, y: A) -> A:
600 '''Return the longest of two strings.'''
601 return x if len(x) >= len(y) else y
602
603 The latter example's signature is essentially the overloading
604 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
605 that if the arguments are instances of some subclass of str,
606 the return type is still plain str.
607
Guido van Rossumb24569a2016-11-20 18:01:29 -0800608 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700609
Guido van Rossumefa798d2016-08-23 11:01:50 -0700610 Type variables defined with covariant=True or contravariant=True
João D. Ferreira86bfed32018-07-07 16:41:20 +0100611 can be used to declare covariant or contravariant generic types.
Guido van Rossumefa798d2016-08-23 11:01:50 -0700612 See PEP 484 for more details. By default generic types are invariant
613 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700614
615 Type variables can be introspected. e.g.:
616
617 T.__name__ == 'T'
618 T.__constraints__ == ()
619 T.__covariant__ == False
620 T.__contravariant__ = False
621 A.__constraints__ == (str, bytes)
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100622
623 Note that only type variables defined in global scope can be pickled.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700624 """
625
Guido van Rossum4cefe742016-09-27 15:20:12 -0700626 __slots__ = ('__name__', '__bound__', '__constraints__',
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300627 '__covariant__', '__contravariant__', '__dict__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700628
629 def __init__(self, name, *constraints, bound=None,
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800630 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700631 self.__name__ = name
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700632 if covariant and contravariant:
Guido van Rossumefa798d2016-08-23 11:01:50 -0700633 raise ValueError("Bivariant types are not supported.")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700634 self.__covariant__ = bool(covariant)
635 self.__contravariant__ = bool(contravariant)
636 if constraints and bound is not None:
637 raise TypeError("Constraints cannot be combined with bound=...")
638 if constraints and len(constraints) == 1:
639 raise TypeError("A single constraint is not allowed")
640 msg = "TypeVar(name, constraint, ...): constraints must be types."
641 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
642 if bound:
643 self.__bound__ = _type_check(bound, "Bound must be a type.")
644 else:
645 self.__bound__ = None
HongWeipenga25a04f2020-04-21 04:01:53 +0800646 try:
647 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling
648 except (AttributeError, ValueError):
649 def_mod = None
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300650 if def_mod != 'typing':
651 self.__module__ = def_mod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700652
Maggie Moss1b4552c2020-09-09 13:23:24 -0700653 def __or__(self, right):
654 return Union[self, right]
655
656 def __ror__(self, right):
657 return Union[self, right]
658
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700659 def __repr__(self):
660 if self.__covariant__:
661 prefix = '+'
662 elif self.__contravariant__:
663 prefix = '-'
664 else:
665 prefix = '~'
666 return prefix + self.__name__
667
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100668 def __reduce__(self):
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300669 return self.__name__
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100670
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700671
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000672def _is_dunder(attr):
673 return attr.startswith('__') and attr.endswith('__')
Guido van Rossum83ec3022017-01-17 20:43:28 -0800674
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300675class _BaseGenericAlias(_Final, _root=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000676 """The central part of internal API.
677
678 This represents a generic version of type 'origin' with type arguments 'params'.
679 There are two kind of these aliases: user defined and special. The special ones
680 are wrappers around builtin collections and ABCs in collections.abc. These must
681 have 'name' always set. If 'inst' is False, then the alias can't be instantiated,
682 this is used by e.g. typing.List and typing.Dict.
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700683 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300684 def __init__(self, origin, *, inst=True, name=None):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000685 self._inst = inst
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000686 self._name = name
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700687 self.__origin__ = origin
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000688 self.__slots__ = None # This is not documented.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300689
690 def __call__(self, *args, **kwargs):
691 if not self._inst:
692 raise TypeError(f"Type {self._name} cannot be instantiated; "
693 f"use {self.__origin__.__name__}() instead")
694 result = self.__origin__(*args, **kwargs)
695 try:
696 result.__orig_class__ = self
697 except AttributeError:
698 pass
699 return result
700
701 def __mro_entries__(self, bases):
702 res = []
703 if self.__origin__ not in bases:
704 res.append(self.__origin__)
705 i = bases.index(self)
706 for b in bases[i+1:]:
707 if isinstance(b, _BaseGenericAlias) or issubclass(b, Generic):
708 break
709 else:
710 res.append(Generic)
711 return tuple(res)
712
713 def __getattr__(self, attr):
714 # We are careful for copy and pickle.
715 # Also for simplicity we just don't relay all dunder names
716 if '__origin__' in self.__dict__ and not _is_dunder(attr):
717 return getattr(self.__origin__, attr)
718 raise AttributeError(attr)
719
720 def __setattr__(self, attr, val):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300721 if _is_dunder(attr) or attr in ('_name', '_inst', '_nparams'):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300722 super().__setattr__(attr, val)
723 else:
724 setattr(self.__origin__, attr, val)
725
726 def __instancecheck__(self, obj):
727 return self.__subclasscheck__(type(obj))
728
729 def __subclasscheck__(self, cls):
730 raise TypeError("Subscripted generics cannot be used with"
731 " class and instance checks")
732
733
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300734# Special typing constructs Union, Optional, Generic, Callable and Tuple
735# use three special attributes for internal bookkeeping of generic types:
736# * __parameters__ is a tuple of unique free type parameters of a generic
737# type, for example, Dict[T, T].__parameters__ == (T,);
738# * __origin__ keeps a reference to a type that was subscripted,
739# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
740# the type.
741# * __args__ is a tuple of all arguments used in subscripting,
742# e.g., Dict[T, int].__args__ == (T, int).
743
744
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300745class _GenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300746 def __init__(self, origin, params, *, inst=True, name=None):
747 super().__init__(origin, inst=inst, name=name)
748 if not isinstance(params, tuple):
749 params = (params,)
750 self.__args__ = tuple(... if a is _TypingEllipsis else
751 () if a is _TypingEmpty else
752 a for a in params)
753 self.__parameters__ = _collect_type_vars(params)
754 if not name:
755 self.__module__ = origin.__module__
756
757 def __eq__(self, other):
758 if not isinstance(other, _GenericAlias):
759 return NotImplemented
760 return (self.__origin__ == other.__origin__
761 and self.__args__ == other.__args__)
762
763 def __hash__(self):
764 return hash((self.__origin__, self.__args__))
765
Maggie Moss1b4552c2020-09-09 13:23:24 -0700766 def __or__(self, right):
767 return Union[self, right]
768
769 def __ror__(self, right):
770 return Union[self, right]
771
Guido van Rossum4cefe742016-09-27 15:20:12 -0700772 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700773 def __getitem__(self, params):
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100774 if self.__origin__ in (Generic, Protocol):
775 # Can't subscript Generic[...] or Protocol[...].
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000776 raise TypeError(f"Cannot subscript already-subscripted {self}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700777 if not isinstance(params, tuple):
778 params = (params,)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700779 msg = "Parameters to generic types must be types."
780 params = tuple(_type_check(p, msg) for p in params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300781 _check_generic(self, params, len(self.__parameters__))
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300782
783 subst = dict(zip(self.__parameters__, params))
784 new_args = []
785 for arg in self.__args__:
786 if isinstance(arg, TypeVar):
787 arg = subst[arg]
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300788 elif isinstance(arg, (_GenericAlias, GenericAlias)):
Serhiy Storchaka0122d482020-05-10 13:39:40 +0300789 subparams = arg.__parameters__
790 if subparams:
791 subargs = tuple(subst[x] for x in subparams)
792 arg = arg[subargs]
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300793 new_args.append(arg)
794 return self.copy_with(tuple(new_args))
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100795
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000796 def copy_with(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300797 return self.__class__(self.__origin__, params, name=self._name, inst=self._inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700798
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000799 def __repr__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300800 if self._name:
801 name = 'typing.' + self._name
802 else:
803 name = _type_repr(self.__origin__)
804 args = ", ".join([_type_repr(a) for a in self.__args__])
805 return f'{name}[{args}]'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000806
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300807 def __reduce__(self):
808 if self._name:
809 origin = globals()[self._name]
810 else:
811 origin = self.__origin__
812 args = tuple(self.__args__)
813 if len(args) == 1 and not isinstance(args[0], tuple):
814 args, = args
815 return operator.getitem, (origin, args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000816
817 def __mro_entries__(self, bases):
818 if self._name: # generic version of an ABC or built-in class
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300819 return super().__mro_entries__(bases)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000820 if self.__origin__ is Generic:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100821 if Protocol in bases:
822 return ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000823 i = bases.index(self)
824 for b in bases[i+1:]:
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300825 if isinstance(b, _BaseGenericAlias) and b is not self:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000826 return ()
827 return (self.__origin__,)
828
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000829
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300830# _nparams is the number of accepted parameters, e.g. 0 for Hashable,
831# 1 for List and 2 for Dict. It may be -1 if variable number of
832# parameters are accepted (needs custom __getitem__).
833
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300834class _SpecialGenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300835 def __init__(self, origin, nparams, *, inst=True, name=None):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300836 if name is None:
837 name = origin.__name__
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300838 super().__init__(origin, inst=inst, name=name)
839 self._nparams = nparams
Serhiy Storchaka2fbc57a2020-05-10 15:14:27 +0300840 if origin.__module__ == 'builtins':
841 self.__doc__ = f'A generic version of {origin.__qualname__}.'
842 else:
843 self.__doc__ = f'A generic version of {origin.__module__}.{origin.__qualname__}.'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000844
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300845 @_tp_cache
846 def __getitem__(self, params):
847 if not isinstance(params, tuple):
848 params = (params,)
849 msg = "Parameters to generic types must be types."
850 params = tuple(_type_check(p, msg) for p in params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300851 _check_generic(self, params, self._nparams)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300852 return self.copy_with(params)
853
854 def copy_with(self, params):
855 return _GenericAlias(self.__origin__, params,
856 name=self._name, inst=self._inst)
857
858 def __repr__(self):
859 return 'typing.' + self._name
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000860
861 def __subclasscheck__(self, cls):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300862 if isinstance(cls, _SpecialGenericAlias):
863 return issubclass(cls.__origin__, self.__origin__)
864 if not isinstance(cls, _GenericAlias):
865 return issubclass(cls, self.__origin__)
866 return super().__subclasscheck__(cls)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700867
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100868 def __reduce__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300869 return self._name
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100870
Maggie Moss1b4552c2020-09-09 13:23:24 -0700871 def __or__(self, right):
872 return Union[self, right]
873
874 def __ror__(self, right):
875 return Union[self, right]
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700876
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300877class _CallableGenericAlias(_GenericAlias, _root=True):
878 def __repr__(self):
879 assert self._name == 'Callable'
880 if len(self.__args__) == 2 and self.__args__[0] is Ellipsis:
881 return super().__repr__()
882 return (f'typing.Callable'
883 f'[[{", ".join([_type_repr(a) for a in self.__args__[:-1]])}], '
884 f'{_type_repr(self.__args__[-1])}]')
885
886 def __reduce__(self):
887 args = self.__args__
888 if not (len(args) == 2 and args[0] is ...):
889 args = list(args[:-1]), args[-1]
890 return operator.getitem, (Callable, args)
891
892
893class _CallableType(_SpecialGenericAlias, _root=True):
894 def copy_with(self, params):
895 return _CallableGenericAlias(self.__origin__, params,
896 name=self._name, inst=self._inst)
897
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000898 def __getitem__(self, params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000899 if not isinstance(params, tuple) or len(params) != 2:
900 raise TypeError("Callable must be used as "
901 "Callable[[arg, ...], result].")
902 args, result = params
903 if args is Ellipsis:
904 params = (Ellipsis, result)
905 else:
906 if not isinstance(args, list):
907 raise TypeError(f"Callable[args, result]: args must be a list."
908 f" Got {args}")
909 params = (tuple(args), result)
910 return self.__getitem_inner__(params)
911
912 @_tp_cache
913 def __getitem_inner__(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300914 args, result = params
915 msg = "Callable[args, result]: result must be a type."
916 result = _type_check(result, msg)
917 if args is Ellipsis:
918 return self.copy_with((_TypingEllipsis, result))
919 msg = "Callable[[arg, ...], result]: each arg must be a type."
920 args = tuple(_type_check(arg, msg) for arg in args)
921 params = args + (result,)
922 return self.copy_with(params)
923
924
925class _TupleType(_SpecialGenericAlias, _root=True):
926 @_tp_cache
927 def __getitem__(self, params):
928 if params == ():
929 return self.copy_with((_TypingEmpty,))
930 if not isinstance(params, tuple):
931 params = (params,)
932 if len(params) == 2 and params[1] is ...:
933 msg = "Tuple[t, ...]: t must be a type."
934 p = _type_check(params[0], msg)
935 return self.copy_with((p, _TypingEllipsis))
936 msg = "Tuple[t0, t1, ...]: each t must be a type."
937 params = tuple(_type_check(p, msg) for p in params)
938 return self.copy_with(params)
939
940
941class _UnionGenericAlias(_GenericAlias, _root=True):
942 def copy_with(self, params):
943 return Union[params]
944
945 def __eq__(self, other):
946 if not isinstance(other, _UnionGenericAlias):
947 return NotImplemented
948 return set(self.__args__) == set(other.__args__)
949
950 def __hash__(self):
951 return hash(frozenset(self.__args__))
952
953 def __repr__(self):
954 args = self.__args__
955 if len(args) == 2:
956 if args[0] is type(None):
957 return f'typing.Optional[{_type_repr(args[1])}]'
958 elif args[1] is type(None):
959 return f'typing.Optional[{_type_repr(args[0])}]'
960 return super().__repr__()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000961
Maggie Moss1b4552c2020-09-09 13:23:24 -0700962 def __instancecheck__(self, obj):
963 return self.__subclasscheck__(type(obj))
964
965 def __subclasscheck__(self, cls):
966 for arg in self.__args__:
967 if issubclass(cls, arg):
968 return True
969
970
Yurii Karabasf03d3182020-11-17 04:23:19 +0200971def _value_and_type_iter(parameters):
972 return ((p, type(p)) for p in parameters)
973
974
975class _LiteralGenericAlias(_GenericAlias, _root=True):
976
977 def __eq__(self, other):
978 if not isinstance(other, _LiteralGenericAlias):
979 return NotImplemented
980
981 return set(_value_and_type_iter(self.__args__)) == set(_value_and_type_iter(other.__args__))
982
983 def __hash__(self):
Yurii Karabas1b540772020-11-19 18:17:38 +0200984 return hash(frozenset(_value_and_type_iter(self.__args__)))
Yurii Karabasf03d3182020-11-17 04:23:19 +0200985
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000986
987class Generic:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700988 """Abstract base class for generic types.
989
Guido van Rossumb24569a2016-11-20 18:01:29 -0800990 A generic type is typically declared by inheriting from
991 this class parameterized with one or more type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700992 For example, a generic mapping type might be defined as::
993
994 class Mapping(Generic[KT, VT]):
995 def __getitem__(self, key: KT) -> VT:
996 ...
997 # Etc.
998
999 This class can then be used as follows::
1000
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001001 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001002 try:
1003 return mapping[key]
1004 except KeyError:
1005 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001006 """
Guido van Rossumd70fe632015-08-05 12:11:06 +02001007 __slots__ = ()
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001008 _is_protocol = False
Guido van Rossumd70fe632015-08-05 12:11:06 +02001009
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001010 @_tp_cache
1011 def __class_getitem__(cls, params):
1012 if not isinstance(params, tuple):
1013 params = (params,)
1014 if not params and cls is not Tuple:
1015 raise TypeError(
1016 f"Parameter list to {cls.__qualname__}[...] cannot be empty")
1017 msg = "Parameters to generic types must be types."
1018 params = tuple(_type_check(p, msg) for p in params)
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001019 if cls in (Generic, Protocol):
1020 # Generic and Protocol can only be subscripted with unique type variables.
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001021 if not all(isinstance(p, TypeVar) for p in params):
1022 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001023 f"Parameters to {cls.__name__}[...] must all be type variables")
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001024 if len(set(params)) != len(params):
1025 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001026 f"Parameters to {cls.__name__}[...] must all be unique")
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001027 else:
1028 # Subscripting a regular Generic subclass.
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001029 _check_generic(cls, params, len(cls.__parameters__))
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001030 return _GenericAlias(cls, params)
1031
1032 def __init_subclass__(cls, *args, **kwargs):
Ivan Levkivskyiee566fe2018-04-04 17:00:15 +01001033 super().__init_subclass__(*args, **kwargs)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001034 tvars = []
1035 if '__orig_bases__' in cls.__dict__:
1036 error = Generic in cls.__orig_bases__
1037 else:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001038 error = Generic in cls.__bases__ and cls.__name__ != 'Protocol'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001039 if error:
1040 raise TypeError("Cannot inherit from plain Generic")
1041 if '__orig_bases__' in cls.__dict__:
1042 tvars = _collect_type_vars(cls.__orig_bases__)
1043 # Look for Generic[T1, ..., Tn].
1044 # If found, tvars must be a subset of it.
1045 # If not found, tvars is it.
1046 # Also check for and reject plain Generic,
1047 # and reject multiple Generic[...].
1048 gvars = None
1049 for base in cls.__orig_bases__:
1050 if (isinstance(base, _GenericAlias) and
1051 base.__origin__ is Generic):
1052 if gvars is not None:
1053 raise TypeError(
1054 "Cannot inherit from Generic[...] multiple types.")
1055 gvars = base.__parameters__
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001056 if gvars is not None:
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001057 tvarset = set(tvars)
1058 gvarset = set(gvars)
1059 if not tvarset <= gvarset:
1060 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
1061 s_args = ', '.join(str(g) for g in gvars)
1062 raise TypeError(f"Some type variables ({s_vars}) are"
1063 f" not listed in Generic[{s_args}]")
1064 tvars = gvars
1065 cls.__parameters__ = tuple(tvars)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001066
1067
1068class _TypingEmpty:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001069 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
1070 to allow empty list/tuple in specific places, without allowing them
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001071 to sneak in where prohibited.
1072 """
1073
1074
1075class _TypingEllipsis:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001076 """Internal placeholder for ... (ellipsis)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001077
1078
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001079_TYPING_INTERNALS = ['__parameters__', '__orig_bases__', '__orig_class__',
1080 '_is_protocol', '_is_runtime_protocol']
1081
1082_SPECIAL_NAMES = ['__abstractmethods__', '__annotations__', '__dict__', '__doc__',
1083 '__init__', '__module__', '__new__', '__slots__',
Guido van Rossum48b069a2020-04-07 09:50:06 -07001084 '__subclasshook__', '__weakref__', '__class_getitem__']
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001085
1086# These special attributes will be not collected as protocol members.
1087EXCLUDED_ATTRIBUTES = _TYPING_INTERNALS + _SPECIAL_NAMES + ['_MutableMapping__marker']
1088
1089
1090def _get_protocol_attrs(cls):
1091 """Collect protocol members from a protocol class objects.
1092
1093 This includes names actually defined in the class dictionary, as well
1094 as names that appear in annotations. Special names (above) are skipped.
1095 """
1096 attrs = set()
1097 for base in cls.__mro__[:-1]: # without object
1098 if base.__name__ in ('Protocol', 'Generic'):
1099 continue
1100 annotations = getattr(base, '__annotations__', {})
1101 for attr in list(base.__dict__.keys()) + list(annotations.keys()):
1102 if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
1103 attrs.add(attr)
1104 return attrs
1105
1106
1107def _is_callable_members_only(cls):
1108 # PEP 544 prohibits using issubclass() with protocols that have non-method members.
1109 return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls))
1110
1111
1112def _no_init(self, *args, **kwargs):
1113 if type(self)._is_protocol:
1114 raise TypeError('Protocols cannot be instantiated')
1115
1116
1117def _allow_reckless_class_cheks():
Nickolena Fishercfaf4c02020-04-26 12:49:11 -05001118 """Allow instance and class checks for special stdlib modules.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001119
1120 The abc and functools modules indiscriminately call isinstance() and
1121 issubclass() on the whole MRO of a user class, which may contain protocols.
1122 """
1123 try:
1124 return sys._getframe(3).f_globals['__name__'] in ['abc', 'functools']
1125 except (AttributeError, ValueError): # For platforms without _getframe().
1126 return True
1127
1128
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001129_PROTO_ALLOWLIST = {
Divij Rajkumar692a0dc2019-09-12 11:13:51 +01001130 'collections.abc': [
1131 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
1132 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
1133 ],
1134 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
1135}
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001136
1137
1138class _ProtocolMeta(ABCMeta):
1139 # This metaclass is really unfortunate and exists only because of
1140 # the lack of __instancehook__.
1141 def __instancecheck__(cls, instance):
1142 # We need this method for situations where attributes are
1143 # assigned in __init__.
1144 if ((not getattr(cls, '_is_protocol', False) or
1145 _is_callable_members_only(cls)) and
1146 issubclass(instance.__class__, cls)):
1147 return True
1148 if cls._is_protocol:
1149 if all(hasattr(instance, attr) and
1150 # All *methods* can be blocked by setting them to None.
1151 (not callable(getattr(cls, attr, None)) or
1152 getattr(instance, attr) is not None)
1153 for attr in _get_protocol_attrs(cls)):
1154 return True
1155 return super().__instancecheck__(instance)
1156
1157
1158class Protocol(Generic, metaclass=_ProtocolMeta):
1159 """Base class for protocol classes.
1160
1161 Protocol classes are defined as::
1162
1163 class Proto(Protocol):
1164 def meth(self) -> int:
1165 ...
1166
1167 Such classes are primarily used with static type checkers that recognize
1168 structural subtyping (static duck-typing), for example::
1169
1170 class C:
1171 def meth(self) -> int:
1172 return 0
1173
1174 def func(x: Proto) -> int:
1175 return x.meth()
1176
1177 func(C()) # Passes static type check
1178
1179 See PEP 544 for details. Protocol classes decorated with
1180 @typing.runtime_checkable act as simple-minded runtime protocols that check
1181 only the presence of given attributes, ignoring their type signatures.
1182 Protocol classes can be generic, they are defined as::
1183
1184 class GenProto(Protocol[T]):
1185 def meth(self) -> T:
1186 ...
1187 """
1188 __slots__ = ()
1189 _is_protocol = True
1190 _is_runtime_protocol = False
1191
1192 def __init_subclass__(cls, *args, **kwargs):
1193 super().__init_subclass__(*args, **kwargs)
1194
1195 # Determine if this is a protocol or a concrete subclass.
1196 if not cls.__dict__.get('_is_protocol', False):
1197 cls._is_protocol = any(b is Protocol for b in cls.__bases__)
1198
1199 # Set (or override) the protocol subclass hook.
1200 def _proto_hook(other):
1201 if not cls.__dict__.get('_is_protocol', False):
1202 return NotImplemented
1203
1204 # First, perform various sanity checks.
1205 if not getattr(cls, '_is_runtime_protocol', False):
1206 if _allow_reckless_class_cheks():
1207 return NotImplemented
1208 raise TypeError("Instance and class checks can only be used with"
1209 " @runtime_checkable protocols")
1210 if not _is_callable_members_only(cls):
1211 if _allow_reckless_class_cheks():
1212 return NotImplemented
1213 raise TypeError("Protocols with non-method members"
1214 " don't support issubclass()")
1215 if not isinstance(other, type):
1216 # Same error message as for issubclass(1, int).
1217 raise TypeError('issubclass() arg 1 must be a class')
1218
1219 # Second, perform the actual structural compatibility check.
1220 for attr in _get_protocol_attrs(cls):
1221 for base in other.__mro__:
1222 # Check if the members appears in the class dictionary...
1223 if attr in base.__dict__:
1224 if base.__dict__[attr] is None:
1225 return NotImplemented
1226 break
1227
1228 # ...or in annotations, if it is a sub-protocol.
1229 annotations = getattr(base, '__annotations__', {})
1230 if (isinstance(annotations, collections.abc.Mapping) and
1231 attr in annotations and
1232 issubclass(other, Generic) and other._is_protocol):
1233 break
1234 else:
1235 return NotImplemented
1236 return True
1237
1238 if '__subclasshook__' not in cls.__dict__:
1239 cls.__subclasshook__ = _proto_hook
1240
1241 # We have nothing more to do for non-protocols...
1242 if not cls._is_protocol:
1243 return
1244
1245 # ... otherwise check consistency of bases, and prohibit instantiation.
1246 for base in cls.__bases__:
1247 if not (base in (object, Generic) or
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001248 base.__module__ in _PROTO_ALLOWLIST and
1249 base.__name__ in _PROTO_ALLOWLIST[base.__module__] or
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001250 issubclass(base, Generic) and base._is_protocol):
1251 raise TypeError('Protocols can only inherit from other'
1252 ' protocols, got %r' % base)
1253 cls.__init__ = _no_init
1254
1255
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001256class _AnnotatedAlias(_GenericAlias, _root=True):
1257 """Runtime representation of an annotated type.
1258
1259 At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't'
1260 with extra annotations. The alias behaves like a normal typing alias,
1261 instantiating is the same as instantiating the underlying type, binding
1262 it to types is also the same.
1263 """
1264 def __init__(self, origin, metadata):
1265 if isinstance(origin, _AnnotatedAlias):
1266 metadata = origin.__metadata__ + metadata
1267 origin = origin.__origin__
1268 super().__init__(origin, origin)
1269 self.__metadata__ = metadata
1270
1271 def copy_with(self, params):
1272 assert len(params) == 1
1273 new_type = params[0]
1274 return _AnnotatedAlias(new_type, self.__metadata__)
1275
1276 def __repr__(self):
1277 return "typing.Annotated[{}, {}]".format(
1278 _type_repr(self.__origin__),
1279 ", ".join(repr(a) for a in self.__metadata__)
1280 )
1281
1282 def __reduce__(self):
1283 return operator.getitem, (
1284 Annotated, (self.__origin__,) + self.__metadata__
1285 )
1286
1287 def __eq__(self, other):
1288 if not isinstance(other, _AnnotatedAlias):
1289 return NotImplemented
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001290 return (self.__origin__ == other.__origin__
1291 and self.__metadata__ == other.__metadata__)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001292
1293 def __hash__(self):
1294 return hash((self.__origin__, self.__metadata__))
1295
1296
1297class Annotated:
1298 """Add context specific metadata to a type.
1299
1300 Example: Annotated[int, runtime_check.Unsigned] indicates to the
1301 hypothetical runtime_check module that this type is an unsigned int.
1302 Every other consumer of this type can ignore this metadata and treat
1303 this type as int.
1304
1305 The first argument to Annotated must be a valid type.
1306
1307 Details:
1308
1309 - It's an error to call `Annotated` with less than two arguments.
1310 - Nested Annotated are flattened::
1311
1312 Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
1313
1314 - Instantiating an annotated type is equivalent to instantiating the
1315 underlying type::
1316
1317 Annotated[C, Ann1](5) == C(5)
1318
1319 - Annotated can be used as a generic type alias::
1320
1321 Optimized = Annotated[T, runtime.Optimize()]
1322 Optimized[int] == Annotated[int, runtime.Optimize()]
1323
1324 OptimizedList = Annotated[List[T], runtime.Optimize()]
1325 OptimizedList[int] == Annotated[List[int], runtime.Optimize()]
1326 """
1327
1328 __slots__ = ()
1329
1330 def __new__(cls, *args, **kwargs):
1331 raise TypeError("Type Annotated cannot be instantiated.")
1332
1333 @_tp_cache
1334 def __class_getitem__(cls, params):
1335 if not isinstance(params, tuple) or len(params) < 2:
1336 raise TypeError("Annotated[...] should be used "
1337 "with at least two arguments (a type and an "
1338 "annotation).")
1339 msg = "Annotated[t, ...]: t must be a type."
1340 origin = _type_check(params[0], msg)
1341 metadata = tuple(params[1:])
1342 return _AnnotatedAlias(origin, metadata)
1343
1344 def __init_subclass__(cls, *args, **kwargs):
1345 raise TypeError(
1346 "Cannot subclass {}.Annotated".format(cls.__module__)
1347 )
1348
1349
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001350def runtime_checkable(cls):
1351 """Mark a protocol class as a runtime protocol.
1352
1353 Such protocol can be used with isinstance() and issubclass().
1354 Raise TypeError if applied to a non-protocol class.
1355 This allows a simple-minded structural check very similar to
1356 one trick ponies in collections.abc such as Iterable.
1357 For example::
1358
1359 @runtime_checkable
1360 class Closable(Protocol):
1361 def close(self): ...
1362
1363 assert isinstance(open('/some/file'), Closable)
1364
1365 Warning: this will check only the presence of the required methods,
1366 not their type signatures!
1367 """
1368 if not issubclass(cls, Generic) or not cls._is_protocol:
1369 raise TypeError('@runtime_checkable can be only applied to protocol classes,'
1370 ' got %r' % cls)
1371 cls._is_runtime_protocol = True
1372 return cls
1373
1374
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001375def cast(typ, val):
1376 """Cast a value to a type.
1377
1378 This returns the value unchanged. To the type checker this
1379 signals that the return value has the designated type, but at
1380 runtime we intentionally don't check anything (we want this
1381 to be as fast as possible).
1382 """
1383 return val
1384
1385
1386def _get_defaults(func):
1387 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -08001388 try:
1389 code = func.__code__
1390 except AttributeError:
1391 # Some built-in functions don't have __code__, __defaults__, etc.
1392 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001393 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001394 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001395 arg_names = arg_names[:pos_count]
1396 defaults = func.__defaults__ or ()
1397 kwdefaults = func.__kwdefaults__
1398 res = dict(kwdefaults) if kwdefaults else {}
1399 pos_offset = pos_count - len(defaults)
1400 for name, value in zip(arg_names[pos_offset:], defaults):
1401 assert name not in res
1402 res[name] = value
1403 return res
1404
1405
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001406_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
1407 types.MethodType, types.ModuleType,
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02001408 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001409
1410
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001411def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
Guido van Rossum991d14f2016-11-09 13:12:51 -08001412 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001413
Guido van Rossum991d14f2016-11-09 13:12:51 -08001414 This is often the same as obj.__annotations__, but it handles
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001415 forward references encoded as string literals, adds Optional[t] if a
1416 default value equal to None is set and recursively replaces all
1417 'Annotated[T, ...]' with 'T' (unless 'include_extras=True').
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001418
Guido van Rossum991d14f2016-11-09 13:12:51 -08001419 The argument may be a module, class, method, or function. The annotations
1420 are returned as a dictionary. For classes, annotations include also
1421 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001422
Guido van Rossum991d14f2016-11-09 13:12:51 -08001423 TypeError is raised if the argument is not of a type that can contain
1424 annotations, and an empty dictionary is returned if no annotations are
1425 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001426
Guido van Rossum991d14f2016-11-09 13:12:51 -08001427 BEWARE -- the behavior of globalns and localns is counterintuitive
1428 (unless you are familiar with how eval() and exec() work). The
1429 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001430
Guido van Rossum991d14f2016-11-09 13:12:51 -08001431 - If no dict arguments are passed, an attempt is made to use the
Łukasz Langaf350a262017-09-14 14:33:00 -04001432 globals from obj (or the respective module's globals for classes),
1433 and these are also used as the locals. If the object does not appear
1434 to have globals, an empty dictionary is used.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001435
Guido van Rossum991d14f2016-11-09 13:12:51 -08001436 - If one dict argument is passed, it is used for both globals and
1437 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001438
Guido van Rossum991d14f2016-11-09 13:12:51 -08001439 - If two dict arguments are passed, they specify globals and
1440 locals, respectively.
1441 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001442
Guido van Rossum991d14f2016-11-09 13:12:51 -08001443 if getattr(obj, '__no_type_check__', None):
1444 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001445 # Classes require a special treatment.
1446 if isinstance(obj, type):
1447 hints = {}
1448 for base in reversed(obj.__mro__):
Łukasz Langaf350a262017-09-14 14:33:00 -04001449 if globalns is None:
1450 base_globals = sys.modules[base.__module__].__dict__
1451 else:
1452 base_globals = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001453 ann = base.__dict__.get('__annotations__', {})
1454 for name, value in ann.items():
1455 if value is None:
1456 value = type(None)
1457 if isinstance(value, str):
Nina Zakharenko0e61dff2018-05-22 20:32:10 -07001458 value = ForwardRef(value, is_argument=False)
Łukasz Langaf350a262017-09-14 14:33:00 -04001459 value = _eval_type(value, base_globals, localns)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001460 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001461 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
Łukasz Langaf350a262017-09-14 14:33:00 -04001462
1463 if globalns is None:
1464 if isinstance(obj, types.ModuleType):
1465 globalns = obj.__dict__
1466 else:
benedwards140aca3a32019-11-21 17:24:58 +00001467 nsobj = obj
1468 # Find globalns for the unwrapped object.
1469 while hasattr(nsobj, '__wrapped__'):
1470 nsobj = nsobj.__wrapped__
1471 globalns = getattr(nsobj, '__globals__', {})
Łukasz Langaf350a262017-09-14 14:33:00 -04001472 if localns is None:
1473 localns = globalns
1474 elif localns is None:
1475 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001476 hints = getattr(obj, '__annotations__', None)
1477 if hints is None:
1478 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001479 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001480 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001481 else:
1482 raise TypeError('{!r} is not a module, class, method, '
1483 'or function.'.format(obj))
1484 defaults = _get_defaults(obj)
1485 hints = dict(hints)
1486 for name, value in hints.items():
1487 if value is None:
1488 value = type(None)
1489 if isinstance(value, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001490 value = ForwardRef(value)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001491 value = _eval_type(value, globalns, localns)
1492 if name in defaults and defaults[name] is None:
1493 value = Optional[value]
1494 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001495 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
1496
1497
1498def _strip_annotations(t):
1499 """Strips the annotations from a given type.
1500 """
1501 if isinstance(t, _AnnotatedAlias):
1502 return _strip_annotations(t.__origin__)
1503 if isinstance(t, _GenericAlias):
1504 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1505 if stripped_args == t.__args__:
1506 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001507 return t.copy_with(stripped_args)
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001508 if isinstance(t, GenericAlias):
1509 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1510 if stripped_args == t.__args__:
1511 return t
1512 return GenericAlias(t.__origin__, stripped_args)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001513 return t
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001514
1515
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001516def get_origin(tp):
1517 """Get the unsubscripted version of a type.
1518
Jakub Stasiak38aaaaa2020-02-07 02:15:12 +01001519 This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
1520 and Annotated. Return None for unsupported types. Examples::
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001521
1522 get_origin(Literal[42]) is Literal
1523 get_origin(int) is None
1524 get_origin(ClassVar[int]) is ClassVar
1525 get_origin(Generic) is Generic
1526 get_origin(Generic[T]) is Generic
1527 get_origin(Union[T, int]) is Union
1528 get_origin(List[Tuple[T, T]][int]) == list
1529 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001530 if isinstance(tp, _AnnotatedAlias):
1531 return Annotated
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001532 if isinstance(tp, (_BaseGenericAlias, GenericAlias)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001533 return tp.__origin__
1534 if tp is Generic:
1535 return Generic
1536 return None
1537
1538
1539def get_args(tp):
1540 """Get type arguments with all substitutions performed.
1541
1542 For unions, basic simplifications used by Union constructor are performed.
1543 Examples::
1544 get_args(Dict[str, int]) == (str, int)
1545 get_args(int) == ()
1546 get_args(Union[int, Union[T, int], str][int]) == (int, str)
1547 get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
1548 get_args(Callable[[], T][int]) == ([], int)
1549 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001550 if isinstance(tp, _AnnotatedAlias):
1551 return (tp.__origin__,) + tp.__metadata__
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001552 if isinstance(tp, _GenericAlias):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001553 res = tp.__args__
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001554 if tp.__origin__ is collections.abc.Callable and res[0] is not Ellipsis:
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001555 res = (list(res[:-1]), res[-1])
1556 return res
Serhiy Storchaka6292be72020-04-27 10:27:21 +03001557 if isinstance(tp, GenericAlias):
1558 return tp.__args__
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001559 return ()
1560
1561
Patrick Reader0705ec82020-09-16 05:58:32 +01001562def is_typeddict(tp):
1563 """Check if an annotation is a TypedDict class
1564
1565 For example::
1566 class Film(TypedDict):
1567 title: str
1568 year: int
1569
1570 is_typeddict(Film) # => True
1571 is_typeddict(Union[list, str]) # => False
1572 """
1573 return isinstance(tp, _TypedDictMeta)
1574
1575
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001576def no_type_check(arg):
1577 """Decorator to indicate that annotations are not type hints.
1578
1579 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001580 applies recursively to all methods and classes defined in that class
1581 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001582
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001583 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001584 """
1585 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001586 arg_attrs = arg.__dict__.copy()
1587 for attr, val in arg.__dict__.items():
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001588 if val in arg.__bases__ + (arg,):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001589 arg_attrs.pop(attr)
1590 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001591 if isinstance(obj, types.FunctionType):
1592 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001593 if isinstance(obj, type):
1594 no_type_check(obj)
1595 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001596 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001597 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001598 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001599 return arg
1600
1601
1602def no_type_check_decorator(decorator):
1603 """Decorator to give another decorator the @no_type_check effect.
1604
1605 This wraps the decorator with something that wraps the decorated
1606 function in @no_type_check.
1607 """
1608
1609 @functools.wraps(decorator)
1610 def wrapped_decorator(*args, **kwds):
1611 func = decorator(*args, **kwds)
1612 func = no_type_check(func)
1613 return func
1614
1615 return wrapped_decorator
1616
1617
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001618def _overload_dummy(*args, **kwds):
1619 """Helper for @overload to raise when called."""
1620 raise NotImplementedError(
1621 "You should not call an overloaded function. "
1622 "A series of @overload-decorated functions "
1623 "outside a stub module should always be followed "
1624 "by an implementation that is not @overload-ed.")
1625
1626
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001627def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001628 """Decorator for overloaded functions/methods.
1629
1630 In a stub file, place two or more stub definitions for the same
1631 function in a row, each decorated with @overload. For example:
1632
1633 @overload
1634 def utf8(value: None) -> None: ...
1635 @overload
1636 def utf8(value: bytes) -> bytes: ...
1637 @overload
1638 def utf8(value: str) -> bytes: ...
1639
1640 In a non-stub file (i.e. a regular .py file), do the same but
1641 follow it with an implementation. The implementation should *not*
1642 be decorated with @overload. For example:
1643
1644 @overload
1645 def utf8(value: None) -> None: ...
1646 @overload
1647 def utf8(value: bytes) -> bytes: ...
1648 @overload
1649 def utf8(value: str) -> bytes: ...
1650 def utf8(value):
1651 # implementation goes here
1652 """
1653 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001654
1655
Ivan Levkivskyif3672422019-05-26 09:37:07 +01001656def final(f):
1657 """A decorator to indicate final methods and final classes.
1658
1659 Use this decorator to indicate to type checkers that the decorated
1660 method cannot be overridden, and decorated class cannot be subclassed.
1661 For example:
1662
1663 class Base:
1664 @final
1665 def done(self) -> None:
1666 ...
1667 class Sub(Base):
1668 def done(self) -> None: # Error reported by type checker
1669 ...
1670
1671 @final
1672 class Leaf:
1673 ...
1674 class Other(Leaf): # Error reported by type checker
1675 ...
1676
1677 There is no runtime checking of these properties.
1678 """
1679 return f
1680
1681
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001682# Some unconstrained type variables. These are used by the container types.
1683# (These are not for export.)
1684T = TypeVar('T') # Any type.
1685KT = TypeVar('KT') # Key type.
1686VT = TypeVar('VT') # Value type.
1687T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
1688V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
1689VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
1690T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
1691# Internal type variable used for Type[].
1692CT_co = TypeVar('CT_co', covariant=True, bound=type)
1693
1694# A useful type variable with constraints. This represents string types.
1695# (This one *is* for export!)
1696AnyStr = TypeVar('AnyStr', bytes, str)
1697
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001698
1699# Various ABCs mimicking those in collections.abc.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001700_alias = _SpecialGenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001701
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001702Hashable = _alias(collections.abc.Hashable, 0) # Not generic.
1703Awaitable = _alias(collections.abc.Awaitable, 1)
1704Coroutine = _alias(collections.abc.Coroutine, 3)
1705AsyncIterable = _alias(collections.abc.AsyncIterable, 1)
1706AsyncIterator = _alias(collections.abc.AsyncIterator, 1)
1707Iterable = _alias(collections.abc.Iterable, 1)
1708Iterator = _alias(collections.abc.Iterator, 1)
1709Reversible = _alias(collections.abc.Reversible, 1)
1710Sized = _alias(collections.abc.Sized, 0) # Not generic.
1711Container = _alias(collections.abc.Container, 1)
1712Collection = _alias(collections.abc.Collection, 1)
1713Callable = _CallableType(collections.abc.Callable, 2)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001714Callable.__doc__ = \
1715 """Callable type; Callable[[int], str] is a function of (int) -> str.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001716
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001717 The subscription syntax must always be used with exactly two
1718 values: the argument list and the return type. The argument list
1719 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001720
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001721 There is no syntax to indicate optional or keyword arguments,
1722 such function types are rarely used as callback types.
1723 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001724AbstractSet = _alias(collections.abc.Set, 1, name='AbstractSet')
1725MutableSet = _alias(collections.abc.MutableSet, 1)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001726# NOTE: Mapping is only covariant in the value type.
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001727Mapping = _alias(collections.abc.Mapping, 2)
1728MutableMapping = _alias(collections.abc.MutableMapping, 2)
1729Sequence = _alias(collections.abc.Sequence, 1)
1730MutableSequence = _alias(collections.abc.MutableSequence, 1)
1731ByteString = _alias(collections.abc.ByteString, 0) # Not generic
1732# Tuple accepts variable number of parameters.
1733Tuple = _TupleType(tuple, -1, inst=False, name='Tuple')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001734Tuple.__doc__ = \
1735 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001736
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001737 Example: Tuple[T1, T2] is a tuple of two elements corresponding
1738 to type variables T1 and T2. Tuple[int, float, str] is a tuple
1739 of an int, a float and a string.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001740
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001741 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
1742 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001743List = _alias(list, 1, inst=False, name='List')
1744Deque = _alias(collections.deque, 1, name='Deque')
1745Set = _alias(set, 1, inst=False, name='Set')
1746FrozenSet = _alias(frozenset, 1, inst=False, name='FrozenSet')
1747MappingView = _alias(collections.abc.MappingView, 1)
1748KeysView = _alias(collections.abc.KeysView, 1)
1749ItemsView = _alias(collections.abc.ItemsView, 2)
1750ValuesView = _alias(collections.abc.ValuesView, 1)
1751ContextManager = _alias(contextlib.AbstractContextManager, 1, name='ContextManager')
1752AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, 1, name='AsyncContextManager')
1753Dict = _alias(dict, 2, inst=False, name='Dict')
1754DefaultDict = _alias(collections.defaultdict, 2, name='DefaultDict')
1755OrderedDict = _alias(collections.OrderedDict, 2)
1756Counter = _alias(collections.Counter, 1)
1757ChainMap = _alias(collections.ChainMap, 2)
1758Generator = _alias(collections.abc.Generator, 3)
1759AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2)
1760Type = _alias(type, 1, inst=False, name='Type')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001761Type.__doc__ = \
1762 """A special construct usable to annotate class objects.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001763
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001764 For example, suppose we have the following classes::
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001765
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001766 class User: ... # Abstract base for User classes
1767 class BasicUser(User): ...
1768 class ProUser(User): ...
1769 class TeamUser(User): ...
Guido van Rossumf17c2002015-12-03 17:31:24 -08001770
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001771 And a function that takes a class argument that's a subclass of
1772 User and returns an instance of the corresponding class::
Guido van Rossumf17c2002015-12-03 17:31:24 -08001773
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001774 U = TypeVar('U', bound=User)
1775 def new_user(user_class: Type[U]) -> U:
1776 user = user_class()
1777 # (Here we could write the user object to a database)
1778 return user
Guido van Rossumf17c2002015-12-03 17:31:24 -08001779
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001780 joe = new_user(BasicUser)
Guido van Rossumf17c2002015-12-03 17:31:24 -08001781
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001782 At this point the type checker knows that joe has type BasicUser.
1783 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001784
1785
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001786@runtime_checkable
1787class SupportsInt(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001788 """An ABC with one abstract method __int__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001789 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001790
1791 @abstractmethod
1792 def __int__(self) -> int:
1793 pass
1794
1795
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001796@runtime_checkable
1797class SupportsFloat(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001798 """An ABC with one abstract method __float__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001799 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001800
1801 @abstractmethod
1802 def __float__(self) -> float:
1803 pass
1804
1805
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001806@runtime_checkable
1807class SupportsComplex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001808 """An ABC with one abstract method __complex__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001809 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001810
1811 @abstractmethod
1812 def __complex__(self) -> complex:
1813 pass
1814
1815
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001816@runtime_checkable
1817class SupportsBytes(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001818 """An ABC with one abstract method __bytes__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001819 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001820
1821 @abstractmethod
1822 def __bytes__(self) -> bytes:
1823 pass
1824
1825
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001826@runtime_checkable
1827class SupportsIndex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001828 """An ABC with one abstract method __index__."""
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -07001829 __slots__ = ()
1830
1831 @abstractmethod
1832 def __index__(self) -> int:
1833 pass
1834
1835
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001836@runtime_checkable
1837class SupportsAbs(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001838 """An ABC with one abstract method __abs__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001839 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001840
1841 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001842 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001843 pass
1844
1845
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001846@runtime_checkable
1847class SupportsRound(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001848 """An ABC with one abstract method __round__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001849 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001850
1851 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001852 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001853 pass
1854
1855
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001856def _make_nmtuple(name, types, module, defaults = ()):
1857 fields = [n for n, t in types]
1858 types = {n: _type_check(t, f"field {n} annotation must be a type")
1859 for n, t in types}
1860 nm_tpl = collections.namedtuple(name, fields,
1861 defaults=defaults, module=module)
1862 nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001863 return nm_tpl
1864
1865
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001866# attributes prohibited to set in NamedTuple class syntax
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001867_prohibited = frozenset({'__new__', '__init__', '__slots__', '__getnewargs__',
1868 '_fields', '_field_defaults',
1869 '_make', '_replace', '_asdict', '_source'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001870
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001871_special = frozenset({'__module__', '__name__', '__annotations__'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001872
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001873
Guido van Rossum2f841442016-11-15 09:48:06 -08001874class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001875
Guido van Rossum2f841442016-11-15 09:48:06 -08001876 def __new__(cls, typename, bases, ns):
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001877 assert bases[0] is _NamedTuple
Guido van Rossum2f841442016-11-15 09:48:06 -08001878 types = ns.get('__annotations__', {})
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001879 default_names = []
Guido van Rossum3c268be2017-01-18 08:03:50 -08001880 for field_name in types:
1881 if field_name in ns:
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001882 default_names.append(field_name)
1883 elif default_names:
1884 raise TypeError(f"Non-default namedtuple field {field_name} "
1885 f"cannot follow default field"
1886 f"{'s' if len(default_names) > 1 else ''} "
1887 f"{', '.join(default_names)}")
1888 nm_tpl = _make_nmtuple(typename, types.items(),
1889 defaults=[ns[n] for n in default_names],
1890 module=ns['__module__'])
Guido van Rossum95919c02017-01-22 17:47:20 -08001891 # update from user namespace without overriding special namedtuple attributes
1892 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001893 if key in _prohibited:
1894 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
1895 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08001896 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08001897 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001898
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001899
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001900def NamedTuple(typename, fields=None, /, **kwargs):
Guido van Rossum2f841442016-11-15 09:48:06 -08001901 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001902
Guido van Rossum2f841442016-11-15 09:48:06 -08001903 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001904
Guido van Rossum2f841442016-11-15 09:48:06 -08001905 class Employee(NamedTuple):
1906 name: str
1907 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001908
Guido van Rossum2f841442016-11-15 09:48:06 -08001909 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001910
Guido van Rossum2f841442016-11-15 09:48:06 -08001911 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001912
Raymond Hettingerf7b57df2019-03-18 09:53:56 -07001913 The resulting class has an extra __annotations__ attribute, giving a
1914 dict that maps field names to types. (The field names are also in
1915 the _fields attribute, which is part of the namedtuple API.)
1916 Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001917
Guido van Rossum2f841442016-11-15 09:48:06 -08001918 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001919
Guido van Rossum2f841442016-11-15 09:48:06 -08001920 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001921
Guido van Rossum2f841442016-11-15 09:48:06 -08001922 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
1923 """
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001924 if fields is None:
1925 fields = kwargs.items()
1926 elif kwargs:
1927 raise TypeError("Either list of fields or keywords"
1928 " can be provided to NamedTuple, not both")
1929 try:
1930 module = sys._getframe(1).f_globals.get('__name__', '__main__')
1931 except (AttributeError, ValueError):
1932 module = None
1933 return _make_nmtuple(typename, fields, module=module)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001934
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001935_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})
1936
1937def _namedtuple_mro_entries(bases):
1938 if len(bases) > 1:
1939 raise TypeError("Multiple inheritance with NamedTuple is not supported")
1940 assert bases[0] is NamedTuple
1941 return (_NamedTuple,)
1942
1943NamedTuple.__mro_entries__ = _namedtuple_mro_entries
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001944
1945
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001946class _TypedDictMeta(type):
1947 def __new__(cls, name, bases, ns, total=True):
1948 """Create new typed dict class object.
1949
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001950 This method is called when TypedDict is subclassed,
1951 or when TypedDict is instantiated. This way
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001952 TypedDict supports all three syntax forms described in its docstring.
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001953 Subclasses and instances of TypedDict return actual dictionaries.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001954 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001955 for base in bases:
1956 if type(base) is not _TypedDictMeta:
1957 raise TypeError('cannot inherit from both a TypedDict type '
1958 'and a non-TypedDict base class')
1959 tp_dict = type.__new__(_TypedDictMeta, name, (dict,), ns)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001960
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001961 annotations = {}
1962 own_annotations = ns.get('__annotations__', {})
1963 own_annotation_keys = set(own_annotations.keys())
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001964 msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001965 own_annotations = {
1966 n: _type_check(tp, msg) for n, tp in own_annotations.items()
1967 }
1968 required_keys = set()
1969 optional_keys = set()
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11001970
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001971 for base in bases:
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001972 annotations.update(base.__dict__.get('__annotations__', {}))
1973 required_keys.update(base.__dict__.get('__required_keys__', ()))
1974 optional_keys.update(base.__dict__.get('__optional_keys__', ()))
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11001975
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001976 annotations.update(own_annotations)
1977 if total:
1978 required_keys.update(own_annotation_keys)
1979 else:
1980 optional_keys.update(own_annotation_keys)
1981
1982 tp_dict.__annotations__ = annotations
1983 tp_dict.__required_keys__ = frozenset(required_keys)
1984 tp_dict.__optional_keys__ = frozenset(optional_keys)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001985 if not hasattr(tp_dict, '__total__'):
1986 tp_dict.__total__ = total
1987 return tp_dict
1988
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001989 __call__ = dict # static method
1990
1991 def __subclasscheck__(cls, other):
1992 # Typed dicts are only for static structural subtyping.
1993 raise TypeError('TypedDict does not support instance and class checks')
1994
1995 __instancecheck__ = __subclasscheck__
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001996
1997
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001998def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001999 """A simple typed namespace. At runtime it is equivalent to a plain dict.
2000
2001 TypedDict creates a dictionary type that expects all of its
2002 instances to have a certain set of keys, where each key is
2003 associated with a value of a consistent type. This expectation
2004 is not checked at runtime but is only enforced by type checkers.
2005 Usage::
2006
2007 class Point2D(TypedDict):
2008 x: int
2009 y: int
2010 label: str
2011
2012 a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
2013 b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
2014
2015 assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
2016
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002017 The type info can be accessed via the Point2D.__annotations__ dict, and
2018 the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
2019 TypedDict supports two additional equivalent forms::
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002020
2021 Point2D = TypedDict('Point2D', x=int, y=int, label=str)
2022 Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
2023
ananthan-123ab6423f2020-02-19 10:03:05 +05302024 By default, all keys must be present in a TypedDict. It is possible
2025 to override this by specifying totality.
2026 Usage::
2027
2028 class point2D(TypedDict, total=False):
2029 x: int
2030 y: int
2031
2032 This means that a point2D TypedDict can have any of the keys omitted.A type
2033 checker is only expected to support a literal False or True as the value of
2034 the total argument. True is the default, and makes all items defined in the
2035 class body be required.
2036
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002037 The class syntax is only supported in Python 3.6+, while two other
2038 syntax forms work for Python 2.7 and 3.2+
2039 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002040 if fields is None:
2041 fields = kwargs
2042 elif kwargs:
2043 raise TypeError("TypedDict takes either a dict or keyword arguments,"
2044 " but not both")
2045
2046 ns = {'__annotations__': dict(fields), '__total__': total}
2047 try:
2048 # Setting correct module is necessary to make typed dict classes pickleable.
2049 ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
2050 except (AttributeError, ValueError):
2051 pass
2052
2053 return _TypedDictMeta(typename, (), ns)
2054
2055_TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {})
2056TypedDict.__mro_entries__ = lambda bases: (_TypedDict,)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002057
2058
Guido van Rossum91185fe2016-06-08 11:19:11 -07002059def NewType(name, tp):
2060 """NewType creates simple unique types with almost zero
2061 runtime overhead. NewType(name, tp) is considered a subtype of tp
2062 by static type checkers. At runtime, NewType(name, tp) returns
2063 a dummy function that simply returns its argument. Usage::
2064
2065 UserId = NewType('UserId', int)
2066
2067 def name_by_id(user_id: UserId) -> str:
2068 ...
2069
2070 UserId('user') # Fails type check
2071
2072 name_by_id(42) # Fails type check
2073 name_by_id(UserId(42)) # OK
2074
2075 num = UserId(5) + 1 # type: int
2076 """
2077
2078 def new_type(x):
2079 return x
2080
2081 new_type.__name__ = name
2082 new_type.__supertype__ = tp
2083 return new_type
2084
2085
Guido van Rossum0e0563c2016-04-05 14:54:25 -07002086# Python-version-specific alias (Python 2: unicode; Python 3: str)
2087Text = str
2088
2089
Guido van Rossum91185fe2016-06-08 11:19:11 -07002090# Constant that's True when type checking, but False here.
2091TYPE_CHECKING = False
2092
2093
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002094class IO(Generic[AnyStr]):
2095 """Generic base class for TextIO and BinaryIO.
2096
2097 This is an abstract, generic version of the return of open().
2098
2099 NOTE: This does not distinguish between the different possible
2100 classes (text vs. binary, read vs. write vs. read/write,
2101 append-only, unbuffered). The TextIO and BinaryIO subclasses
2102 below capture the distinctions between text vs. binary, which is
2103 pervasive in the interface; however we currently do not offer a
2104 way to track the other distinctions in the type system.
2105 """
2106
Guido van Rossumd70fe632015-08-05 12:11:06 +02002107 __slots__ = ()
2108
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002109 @property
2110 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002111 def mode(self) -> str:
2112 pass
2113
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002114 @property
2115 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002116 def name(self) -> str:
2117 pass
2118
2119 @abstractmethod
2120 def close(self) -> None:
2121 pass
2122
Shantanu2e6569b2020-01-29 18:52:36 -08002123 @property
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002124 @abstractmethod
2125 def closed(self) -> bool:
2126 pass
2127
2128 @abstractmethod
2129 def fileno(self) -> int:
2130 pass
2131
2132 @abstractmethod
2133 def flush(self) -> None:
2134 pass
2135
2136 @abstractmethod
2137 def isatty(self) -> bool:
2138 pass
2139
2140 @abstractmethod
2141 def read(self, n: int = -1) -> AnyStr:
2142 pass
2143
2144 @abstractmethod
2145 def readable(self) -> bool:
2146 pass
2147
2148 @abstractmethod
2149 def readline(self, limit: int = -1) -> AnyStr:
2150 pass
2151
2152 @abstractmethod
2153 def readlines(self, hint: int = -1) -> List[AnyStr]:
2154 pass
2155
2156 @abstractmethod
2157 def seek(self, offset: int, whence: int = 0) -> int:
2158 pass
2159
2160 @abstractmethod
2161 def seekable(self) -> bool:
2162 pass
2163
2164 @abstractmethod
2165 def tell(self) -> int:
2166 pass
2167
2168 @abstractmethod
2169 def truncate(self, size: int = None) -> int:
2170 pass
2171
2172 @abstractmethod
2173 def writable(self) -> bool:
2174 pass
2175
2176 @abstractmethod
2177 def write(self, s: AnyStr) -> int:
2178 pass
2179
2180 @abstractmethod
2181 def writelines(self, lines: List[AnyStr]) -> None:
2182 pass
2183
2184 @abstractmethod
2185 def __enter__(self) -> 'IO[AnyStr]':
2186 pass
2187
2188 @abstractmethod
2189 def __exit__(self, type, value, traceback) -> None:
2190 pass
2191
2192
2193class BinaryIO(IO[bytes]):
2194 """Typed version of the return of open() in binary mode."""
2195
Guido van Rossumd70fe632015-08-05 12:11:06 +02002196 __slots__ = ()
2197
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002198 @abstractmethod
2199 def write(self, s: Union[bytes, bytearray]) -> int:
2200 pass
2201
2202 @abstractmethod
2203 def __enter__(self) -> 'BinaryIO':
2204 pass
2205
2206
2207class TextIO(IO[str]):
2208 """Typed version of the return of open() in text mode."""
2209
Guido van Rossumd70fe632015-08-05 12:11:06 +02002210 __slots__ = ()
2211
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002212 @property
2213 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002214 def buffer(self) -> BinaryIO:
2215 pass
2216
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002217 @property
2218 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002219 def encoding(self) -> str:
2220 pass
2221
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002222 @property
2223 @abstractmethod
Guido van Rossum991d14f2016-11-09 13:12:51 -08002224 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002225 pass
2226
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002227 @property
2228 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002229 def line_buffering(self) -> bool:
2230 pass
2231
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002232 @property
2233 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002234 def newlines(self) -> Any:
2235 pass
2236
2237 @abstractmethod
2238 def __enter__(self) -> 'TextIO':
2239 pass
2240
2241
2242class io:
2243 """Wrapper namespace for IO generic classes."""
2244
2245 __all__ = ['IO', 'TextIO', 'BinaryIO']
2246 IO = IO
2247 TextIO = TextIO
2248 BinaryIO = BinaryIO
2249
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002250
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002251io.__name__ = __name__ + '.io'
2252sys.modules[io.__name__] = io
2253
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002254Pattern = _alias(stdlib_re.Pattern, 1)
2255Match = _alias(stdlib_re.Match, 1)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002256
2257class re:
2258 """Wrapper namespace for re type aliases."""
2259
2260 __all__ = ['Pattern', 'Match']
2261 Pattern = Pattern
2262 Match = Match
2263
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002264
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002265re.__name__ = __name__ + '.re'
2266sys.modules[re.__name__] = re