blob: 7f07321cda82a735b0426f84211dc13b29e3522c [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
kj463c7d32020-12-14 02:38:24 +0800123
124def _type_convert(arg):
125 """For converting None to type(None), and strings to ForwardRef."""
126 if arg is None:
127 return type(None)
128 if isinstance(arg, str):
129 return ForwardRef(arg)
130 return arg
131
132
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700133def _type_check(arg, msg, is_argument=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000134 """Check that the argument is a type, and return it (internal helper).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700135
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000136 As a special case, accept None and return type(None) instead. Also wrap strings
137 into ForwardRef instances. Consider several corner cases, for example plain
138 special forms like Union are not valid, while Union[int, str] is OK, etc.
139 The msg argument is a human-readable error message, e.g::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700140
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000141 "Union[arg, ...]: arg should be a type."
Guido van Rossum4cefe742016-09-27 15:20:12 -0700142
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000143 We append the repr() of the actual value (truncated to 100 chars).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700144 """
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100145 invalid_generic_forms = (Generic, Protocol)
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700146 if is_argument:
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100147 invalid_generic_forms = invalid_generic_forms + (ClassVar, Final)
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400148
kj463c7d32020-12-14 02:38:24 +0800149 arg = _type_convert(arg)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000150 if (isinstance(arg, _GenericAlias) and
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400151 arg.__origin__ in invalid_generic_forms):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000152 raise TypeError(f"{arg} is not valid as type argument")
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300153 if arg in (Any, NoReturn):
154 return arg
155 if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000156 raise TypeError(f"Plain {arg} is not valid as type argument")
Maggie Moss1b4552c2020-09-09 13:23:24 -0700157 if isinstance(arg, (type, TypeVar, ForwardRef, types.Union)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000158 return arg
159 if not callable(arg):
160 raise TypeError(f"{msg} Got {arg!r:.100}.")
161 return arg
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700162
163
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000164def _type_repr(obj):
165 """Return the repr() of an object, special-casing types (internal helper).
166
167 If obj is a type, we return a shorter version than the default
168 type.__repr__, based on the module and qualified name, which is
169 typically enough to uniquely identify a type. For everything
170 else, we fall back on repr(obj).
171 """
kj1f7dfb22020-11-02 02:13:38 +0800172 if isinstance(obj, types.GenericAlias):
173 return repr(obj)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000174 if isinstance(obj, type):
175 if obj.__module__ == 'builtins':
176 return obj.__qualname__
177 return f'{obj.__module__}.{obj.__qualname__}'
178 if obj is ...:
179 return('...')
180 if isinstance(obj, types.FunctionType):
181 return obj.__name__
182 return repr(obj)
183
184
185def _collect_type_vars(types):
186 """Collect all type variable contained in types in order of
187 first appearance (lexicographic order). For example::
188
189 _collect_type_vars((T, List[S, T])) == (T, S)
190 """
191 tvars = []
192 for t in types:
193 if isinstance(t, TypeVar) and t not in tvars:
194 tvars.append(t)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300195 if isinstance(t, (_GenericAlias, GenericAlias)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000196 tvars.extend([t for t in t.__parameters__ if t not in tvars])
197 return tuple(tvars)
198
199
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300200def _check_generic(cls, parameters, elen):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000201 """Check correct count for parameters of a generic cls (internal helper).
202 This gives a nice error message in case of count mismatch.
203 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300204 if not elen:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000205 raise TypeError(f"{cls} is not a generic class")
206 alen = len(parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000207 if alen != elen:
208 raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
209 f" actual {alen}, expected {elen}")
210
211
Yurii Karabasf03d3182020-11-17 04:23:19 +0200212def _deduplicate(params):
213 # Weed out strict duplicates, preserving the first of each occurrence.
214 all_params = set(params)
215 if len(all_params) < len(params):
216 new_params = []
217 for t in params:
218 if t in all_params:
219 new_params.append(t)
220 all_params.remove(t)
221 params = new_params
222 assert not all_params, all_params
223 return params
224
225
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000226def _remove_dups_flatten(parameters):
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700227 """An internal helper for Union creation and substitution: flatten Unions
228 among parameters, then remove duplicates.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000229 """
230 # Flatten out Union[Union[...], ...].
231 params = []
232 for p in parameters:
Maggie Moss1b4552c2020-09-09 13:23:24 -0700233 if isinstance(p, (_UnionGenericAlias, types.Union)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000234 params.extend(p.__args__)
235 elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
236 params.extend(p[1:])
237 else:
238 params.append(p)
Yurii Karabasf03d3182020-11-17 04:23:19 +0200239
240 return tuple(_deduplicate(params))
241
242
243def _flatten_literal_params(parameters):
244 """An internal helper for Literal creation: flatten Literals among parameters"""
245 params = []
246 for p in parameters:
247 if isinstance(p, _LiteralGenericAlias):
248 params.extend(p.__args__)
249 else:
250 params.append(p)
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700251 return tuple(params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000252
253
254_cleanups = []
255
256
Yurii Karabasf03d3182020-11-17 04:23:19 +0200257def _tp_cache(func=None, /, *, typed=False):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000258 """Internal wrapper caching __getitem__ of generic types with a fallback to
259 original function for non-hashable arguments.
260 """
Yurii Karabasf03d3182020-11-17 04:23:19 +0200261 def decorator(func):
262 cached = functools.lru_cache(typed=typed)(func)
263 _cleanups.append(cached.cache_clear)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000264
Yurii Karabasf03d3182020-11-17 04:23:19 +0200265 @functools.wraps(func)
266 def inner(*args, **kwds):
267 try:
268 return cached(*args, **kwds)
269 except TypeError:
270 pass # All real errors (not unhashable args) are raised below.
271 return func(*args, **kwds)
272 return inner
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000273
Yurii Karabasf03d3182020-11-17 04:23:19 +0200274 if func is not None:
275 return decorator(func)
276
277 return decorator
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000278
wyfo653f4202020-07-22 21:47:28 +0200279def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
Graham Bleaney84ef33c2020-09-08 18:41:10 -0400280 """Evaluate all forward references in the given type t.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000281 For use of globalns and localns see the docstring for get_type_hints().
wyfo653f4202020-07-22 21:47:28 +0200282 recursive_guard is used to prevent prevent infinite recursion
283 with recursive ForwardRef.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000284 """
285 if isinstance(t, ForwardRef):
wyfo653f4202020-07-22 21:47:28 +0200286 return t._evaluate(globalns, localns, recursive_guard)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300287 if isinstance(t, (_GenericAlias, GenericAlias)):
wyfo653f4202020-07-22 21:47:28 +0200288 ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000289 if ev_args == t.__args__:
290 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300291 if isinstance(t, GenericAlias):
292 return GenericAlias(t.__origin__, ev_args)
293 else:
294 return t.copy_with(ev_args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000295 return t
296
297
298class _Final:
299 """Mixin to prohibit subclassing"""
Guido van Rossum4cefe742016-09-27 15:20:12 -0700300
Guido van Rossum83ec3022017-01-17 20:43:28 -0800301 __slots__ = ('__weakref__',)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700302
Serhiy Storchaka2085bd02019-06-01 11:00:15 +0300303 def __init_subclass__(self, /, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000304 if '_root' not in kwds:
305 raise TypeError("Cannot subclass special typing classes")
306
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100307class _Immutable:
308 """Mixin to indicate that object should not be copied."""
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300309 __slots__ = ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000310
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100311 def __copy__(self):
312 return self
313
314 def __deepcopy__(self, memo):
315 return self
316
317
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300318# Internal indicator of special typing constructs.
319# See __doc__ instance attribute for specific docs.
320class _SpecialForm(_Final, _root=True):
321 __slots__ = ('_name', '__doc__', '_getitem')
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000322
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300323 def __init__(self, getitem):
324 self._getitem = getitem
325 self._name = getitem.__name__
326 self.__doc__ = getitem.__doc__
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000327
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300328 def __mro_entries__(self, bases):
329 raise TypeError(f"Cannot subclass {self!r}")
Guido van Rossum4cefe742016-09-27 15:20:12 -0700330
331 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000332 return 'typing.' + self._name
333
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100334 def __reduce__(self):
335 return self._name
Guido van Rossum4cefe742016-09-27 15:20:12 -0700336
337 def __call__(self, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000338 raise TypeError(f"Cannot instantiate {self!r}")
339
340 def __instancecheck__(self, obj):
341 raise TypeError(f"{self} cannot be used with isinstance()")
342
343 def __subclasscheck__(self, cls):
344 raise TypeError(f"{self} cannot be used with issubclass()")
345
346 @_tp_cache
347 def __getitem__(self, parameters):
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300348 return self._getitem(self, parameters)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700349
Yurii Karabasf03d3182020-11-17 04:23:19 +0200350
351class _LiteralSpecialForm(_SpecialForm, _root=True):
352 @_tp_cache(typed=True)
353 def __getitem__(self, parameters):
354 return self._getitem(self, parameters)
355
356
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300357@_SpecialForm
358def Any(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000359 """Special type indicating an unconstrained type.
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700360
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000361 - Any is compatible with every type.
362 - Any assumed to have all methods.
363 - All values assumed to be instances of Any.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700364
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000365 Note that all the above statements are true from the point of view of
366 static type checkers. At runtime, Any should not be used with instance
367 or class checks.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300368 """
369 raise TypeError(f"{self} is not subscriptable")
Guido van Rossumd70fe632015-08-05 12:11:06 +0200370
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300371@_SpecialForm
372def NoReturn(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000373 """Special type indicating functions that never return.
374 Example::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700375
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000376 from typing import NoReturn
377
378 def stop() -> NoReturn:
379 raise Exception('no way')
380
381 This type is invalid in other positions, e.g., ``List[NoReturn]``
382 will fail in static type checkers.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300383 """
384 raise TypeError(f"{self} is not subscriptable")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000385
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300386@_SpecialForm
387def ClassVar(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000388 """Special type construct to mark class variables.
389
390 An annotation wrapped in ClassVar indicates that a given
391 attribute is intended to be used as a class variable and
392 should not be set on instances of that class. Usage::
393
394 class Starship:
395 stats: ClassVar[Dict[str, int]] = {} # class variable
396 damage: int = 10 # instance variable
397
398 ClassVar accepts only types and cannot be further subscribed.
399
400 Note that ClassVar is not a class itself, and should not
401 be used with isinstance() or issubclass().
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300402 """
403 item = _type_check(parameters, f'{self} accepts only single type.')
404 return _GenericAlias(self, (item,))
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000405
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300406@_SpecialForm
407def Final(self, parameters):
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100408 """Special typing construct to indicate final names to type checkers.
409
410 A final name cannot be re-assigned or overridden in a subclass.
411 For example:
412
413 MAX_SIZE: Final = 9000
414 MAX_SIZE += 1 # Error reported by type checker
415
416 class Connection:
417 TIMEOUT: Final[int] = 10
418
419 class FastConnector(Connection):
420 TIMEOUT = 1 # Error reported by type checker
421
422 There is no runtime checking of these properties.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300423 """
424 item = _type_check(parameters, f'{self} accepts only single type.')
425 return _GenericAlias(self, (item,))
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100426
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300427@_SpecialForm
428def Union(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000429 """Union type; Union[X, Y] means either X or Y.
430
431 To define a union, use e.g. Union[int, str]. Details:
432 - The arguments must be types and there must be at least one.
433 - None as an argument is a special case and is replaced by
434 type(None).
435 - Unions of unions are flattened, e.g.::
436
437 Union[Union[int, str], float] == Union[int, str, float]
438
439 - Unions of a single argument vanish, e.g.::
440
441 Union[int] == int # The constructor actually returns int
442
443 - Redundant arguments are skipped, e.g.::
444
445 Union[int, str, int] == Union[int, str]
446
447 - When comparing unions, the argument order is ignored, e.g.::
448
449 Union[int, str] == Union[str, int]
450
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000451 - You cannot subclass or instantiate a union.
452 - You can use Optional[X] as a shorthand for Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300453 """
454 if parameters == ():
455 raise TypeError("Cannot take a Union of no types.")
456 if not isinstance(parameters, tuple):
457 parameters = (parameters,)
458 msg = "Union[arg, ...]: each arg must be a type."
459 parameters = tuple(_type_check(p, msg) for p in parameters)
460 parameters = _remove_dups_flatten(parameters)
461 if len(parameters) == 1:
462 return parameters[0]
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300463 return _UnionGenericAlias(self, parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000464
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300465@_SpecialForm
466def Optional(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000467 """Optional type.
468
469 Optional[X] is equivalent to Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300470 """
471 arg = _type_check(parameters, f"{self} requires a single type.")
472 return Union[arg, type(None)]
Guido van Rossumb7dedc82016-10-29 12:44:29 -0700473
Yurii Karabasf03d3182020-11-17 04:23:19 +0200474@_LiteralSpecialForm
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300475def Literal(self, parameters):
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100476 """Special typing form to define literal types (a.k.a. value types).
477
478 This form can be used to indicate to type checkers that the corresponding
479 variable or function parameter has a value equivalent to the provided
480 literal (or one of several literals):
481
482 def validate_simple(data: Any) -> Literal[True]: # always returns True
483 ...
484
485 MODE = Literal['r', 'rb', 'w', 'wb']
486 def open_helper(file: str, mode: MODE) -> str:
487 ...
488
489 open_helper('/some/path', 'r') # Passes type check
490 open_helper('/other/path', 'typo') # Error in type checker
491
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300492 Literal[...] cannot be subclassed. At runtime, an arbitrary value
493 is allowed as type argument to Literal[...], but type checkers may
494 impose restrictions.
495 """
496 # There is no '_type_check' call because arguments to Literal[...] are
497 # values, not types.
Yurii Karabasf03d3182020-11-17 04:23:19 +0200498 if not isinstance(parameters, tuple):
499 parameters = (parameters,)
500
501 parameters = _flatten_literal_params(parameters)
502
503 try:
504 parameters = tuple(p for p, _ in _deduplicate(list(_value_and_type_iter(parameters))))
505 except TypeError: # unhashable parameters
506 pass
507
508 return _LiteralGenericAlias(self, parameters)
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100509
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700510
Mikhail Golubev4f3c2502020-10-08 00:44:31 +0300511@_SpecialForm
512def TypeAlias(self, parameters):
513 """Special marker indicating that an assignment should
514 be recognized as a proper type alias definition by type
515 checkers.
516
517 For example::
518
519 Predicate: TypeAlias = Callable[..., bool]
520
521 It's invalid when used anywhere except as in the example above.
522 """
523 raise TypeError(f"{self} is not subscriptable")
524
525
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000526class ForwardRef(_Final, _root=True):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800527 """Internal wrapper to hold a forward reference."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700528
Guido van Rossum4cefe742016-09-27 15:20:12 -0700529 __slots__ = ('__forward_arg__', '__forward_code__',
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400530 '__forward_evaluated__', '__forward_value__',
531 '__forward_is_argument__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700532
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700533 def __init__(self, arg, is_argument=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700534 if not isinstance(arg, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000535 raise TypeError(f"Forward reference must be a string -- got {arg!r}")
Batuhan Taskaya044a1042020-10-06 23:03:02 +0300536
537 # Double-stringified forward references is a result of activating
538 # the 'annotations' future by default. This way, we eliminate them in
539 # the runtime.
540 if arg.startswith(("'", '\"')) and arg.endswith(("'", '"')):
541 arg = arg[1:-1]
542
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700543 try:
544 code = compile(arg, '<string>', 'eval')
545 except SyntaxError:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000546 raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700547 self.__forward_arg__ = arg
548 self.__forward_code__ = code
549 self.__forward_evaluated__ = False
550 self.__forward_value__ = None
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400551 self.__forward_is_argument__ = is_argument
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700552
wyfo653f4202020-07-22 21:47:28 +0200553 def _evaluate(self, globalns, localns, recursive_guard):
554 if self.__forward_arg__ in recursive_guard:
555 return self
Guido van Rossumdad17902016-11-10 08:29:18 -0800556 if not self.__forward_evaluated__ or localns is not globalns:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700557 if globalns is None and localns is None:
558 globalns = localns = {}
559 elif globalns is None:
560 globalns = localns
561 elif localns is None:
562 localns = globalns
wyfo653f4202020-07-22 21:47:28 +0200563 type_ =_type_check(
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700564 eval(self.__forward_code__, globalns, localns),
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400565 "Forward references must evaluate to types.",
wyfo653f4202020-07-22 21:47:28 +0200566 is_argument=self.__forward_is_argument__,
567 )
568 self.__forward_value__ = _eval_type(
569 type_, globalns, localns, recursive_guard | {self.__forward_arg__}
570 )
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700571 self.__forward_evaluated__ = True
572 return self.__forward_value__
573
Guido van Rossum4cefe742016-09-27 15:20:12 -0700574 def __eq__(self, other):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000575 if not isinstance(other, ForwardRef):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700576 return NotImplemented
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100577 if self.__forward_evaluated__ and other.__forward_evaluated__:
578 return (self.__forward_arg__ == other.__forward_arg__ and
579 self.__forward_value__ == other.__forward_value__)
580 return self.__forward_arg__ == other.__forward_arg__
Guido van Rossum4cefe742016-09-27 15:20:12 -0700581
582 def __hash__(self):
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100583 return hash(self.__forward_arg__)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700584
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700585 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000586 return f'ForwardRef({self.__forward_arg__!r})'
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700587
588
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100589class TypeVar(_Final, _Immutable, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700590 """Type variable.
591
592 Usage::
593
594 T = TypeVar('T') # Can be anything
595 A = TypeVar('A', str, bytes) # Must be str or bytes
596
597 Type variables exist primarily for the benefit of static type
598 checkers. They serve as the parameters for generic types as well
599 as for generic function definitions. See class Generic for more
600 information on generic types. Generic functions work as follows:
601
Guido van Rossumb24569a2016-11-20 18:01:29 -0800602 def repeat(x: T, n: int) -> List[T]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700603 '''Return a list containing n references to x.'''
604 return [x]*n
605
606 def longest(x: A, y: A) -> A:
607 '''Return the longest of two strings.'''
608 return x if len(x) >= len(y) else y
609
610 The latter example's signature is essentially the overloading
611 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
612 that if the arguments are instances of some subclass of str,
613 the return type is still plain str.
614
Guido van Rossumb24569a2016-11-20 18:01:29 -0800615 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700616
Guido van Rossumefa798d2016-08-23 11:01:50 -0700617 Type variables defined with covariant=True or contravariant=True
João D. Ferreira86bfed32018-07-07 16:41:20 +0100618 can be used to declare covariant or contravariant generic types.
Guido van Rossumefa798d2016-08-23 11:01:50 -0700619 See PEP 484 for more details. By default generic types are invariant
620 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700621
622 Type variables can be introspected. e.g.:
623
624 T.__name__ == 'T'
625 T.__constraints__ == ()
626 T.__covariant__ == False
627 T.__contravariant__ = False
628 A.__constraints__ == (str, bytes)
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100629
630 Note that only type variables defined in global scope can be pickled.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700631 """
632
Guido van Rossum4cefe742016-09-27 15:20:12 -0700633 __slots__ = ('__name__', '__bound__', '__constraints__',
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300634 '__covariant__', '__contravariant__', '__dict__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700635
636 def __init__(self, name, *constraints, bound=None,
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800637 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700638 self.__name__ = name
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700639 if covariant and contravariant:
Guido van Rossumefa798d2016-08-23 11:01:50 -0700640 raise ValueError("Bivariant types are not supported.")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700641 self.__covariant__ = bool(covariant)
642 self.__contravariant__ = bool(contravariant)
643 if constraints and bound is not None:
644 raise TypeError("Constraints cannot be combined with bound=...")
645 if constraints and len(constraints) == 1:
646 raise TypeError("A single constraint is not allowed")
647 msg = "TypeVar(name, constraint, ...): constraints must be types."
648 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
649 if bound:
650 self.__bound__ = _type_check(bound, "Bound must be a type.")
651 else:
652 self.__bound__ = None
HongWeipenga25a04f2020-04-21 04:01:53 +0800653 try:
654 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling
655 except (AttributeError, ValueError):
656 def_mod = None
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300657 if def_mod != 'typing':
658 self.__module__ = def_mod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700659
Maggie Moss1b4552c2020-09-09 13:23:24 -0700660 def __or__(self, right):
661 return Union[self, right]
662
663 def __ror__(self, right):
664 return Union[self, right]
665
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700666 def __repr__(self):
667 if self.__covariant__:
668 prefix = '+'
669 elif self.__contravariant__:
670 prefix = '-'
671 else:
672 prefix = '~'
673 return prefix + self.__name__
674
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100675 def __reduce__(self):
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300676 return self.__name__
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100677
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700678
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000679def _is_dunder(attr):
680 return attr.startswith('__') and attr.endswith('__')
Guido van Rossum83ec3022017-01-17 20:43:28 -0800681
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300682class _BaseGenericAlias(_Final, _root=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000683 """The central part of internal API.
684
685 This represents a generic version of type 'origin' with type arguments 'params'.
686 There are two kind of these aliases: user defined and special. The special ones
687 are wrappers around builtin collections and ABCs in collections.abc. These must
688 have 'name' always set. If 'inst' is False, then the alias can't be instantiated,
689 this is used by e.g. typing.List and typing.Dict.
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700690 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300691 def __init__(self, origin, *, inst=True, name=None):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000692 self._inst = inst
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000693 self._name = name
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700694 self.__origin__ = origin
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000695 self.__slots__ = None # This is not documented.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300696
697 def __call__(self, *args, **kwargs):
698 if not self._inst:
699 raise TypeError(f"Type {self._name} cannot be instantiated; "
700 f"use {self.__origin__.__name__}() instead")
701 result = self.__origin__(*args, **kwargs)
702 try:
703 result.__orig_class__ = self
704 except AttributeError:
705 pass
706 return result
707
708 def __mro_entries__(self, bases):
709 res = []
710 if self.__origin__ not in bases:
711 res.append(self.__origin__)
712 i = bases.index(self)
713 for b in bases[i+1:]:
714 if isinstance(b, _BaseGenericAlias) or issubclass(b, Generic):
715 break
716 else:
717 res.append(Generic)
718 return tuple(res)
719
720 def __getattr__(self, attr):
721 # We are careful for copy and pickle.
722 # Also for simplicity we just don't relay all dunder names
723 if '__origin__' in self.__dict__ and not _is_dunder(attr):
724 return getattr(self.__origin__, attr)
725 raise AttributeError(attr)
726
727 def __setattr__(self, attr, val):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300728 if _is_dunder(attr) or attr in ('_name', '_inst', '_nparams'):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300729 super().__setattr__(attr, val)
730 else:
731 setattr(self.__origin__, attr, val)
732
733 def __instancecheck__(self, obj):
734 return self.__subclasscheck__(type(obj))
735
736 def __subclasscheck__(self, cls):
737 raise TypeError("Subscripted generics cannot be used with"
738 " class and instance checks")
739
740
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300741# Special typing constructs Union, Optional, Generic, Callable and Tuple
742# use three special attributes for internal bookkeeping of generic types:
743# * __parameters__ is a tuple of unique free type parameters of a generic
744# type, for example, Dict[T, T].__parameters__ == (T,);
745# * __origin__ keeps a reference to a type that was subscripted,
746# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
747# the type.
748# * __args__ is a tuple of all arguments used in subscripting,
749# e.g., Dict[T, int].__args__ == (T, int).
750
751
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300752class _GenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300753 def __init__(self, origin, params, *, inst=True, name=None):
754 super().__init__(origin, inst=inst, name=name)
755 if not isinstance(params, tuple):
756 params = (params,)
757 self.__args__ = tuple(... if a is _TypingEllipsis else
758 () if a is _TypingEmpty else
759 a for a in params)
760 self.__parameters__ = _collect_type_vars(params)
761 if not name:
762 self.__module__ = origin.__module__
763
764 def __eq__(self, other):
765 if not isinstance(other, _GenericAlias):
766 return NotImplemented
767 return (self.__origin__ == other.__origin__
768 and self.__args__ == other.__args__)
769
770 def __hash__(self):
771 return hash((self.__origin__, self.__args__))
772
Maggie Moss1b4552c2020-09-09 13:23:24 -0700773 def __or__(self, right):
774 return Union[self, right]
775
776 def __ror__(self, right):
777 return Union[self, right]
778
Guido van Rossum4cefe742016-09-27 15:20:12 -0700779 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700780 def __getitem__(self, params):
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100781 if self.__origin__ in (Generic, Protocol):
782 # Can't subscript Generic[...] or Protocol[...].
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000783 raise TypeError(f"Cannot subscript already-subscripted {self}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700784 if not isinstance(params, tuple):
785 params = (params,)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700786 msg = "Parameters to generic types must be types."
787 params = tuple(_type_check(p, msg) for p in params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300788 _check_generic(self, params, len(self.__parameters__))
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300789
790 subst = dict(zip(self.__parameters__, params))
791 new_args = []
792 for arg in self.__args__:
793 if isinstance(arg, TypeVar):
794 arg = subst[arg]
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300795 elif isinstance(arg, (_GenericAlias, GenericAlias)):
Serhiy Storchaka0122d482020-05-10 13:39:40 +0300796 subparams = arg.__parameters__
797 if subparams:
798 subargs = tuple(subst[x] for x in subparams)
799 arg = arg[subargs]
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300800 new_args.append(arg)
801 return self.copy_with(tuple(new_args))
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100802
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000803 def copy_with(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300804 return self.__class__(self.__origin__, params, name=self._name, inst=self._inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700805
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000806 def __repr__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300807 if self._name:
808 name = 'typing.' + self._name
809 else:
810 name = _type_repr(self.__origin__)
811 args = ", ".join([_type_repr(a) for a in self.__args__])
812 return f'{name}[{args}]'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000813
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300814 def __reduce__(self):
815 if self._name:
816 origin = globals()[self._name]
817 else:
818 origin = self.__origin__
819 args = tuple(self.__args__)
820 if len(args) == 1 and not isinstance(args[0], tuple):
821 args, = args
822 return operator.getitem, (origin, args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000823
824 def __mro_entries__(self, bases):
825 if self._name: # generic version of an ABC or built-in class
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300826 return super().__mro_entries__(bases)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000827 if self.__origin__ is Generic:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100828 if Protocol in bases:
829 return ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000830 i = bases.index(self)
831 for b in bases[i+1:]:
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300832 if isinstance(b, _BaseGenericAlias) and b is not self:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000833 return ()
834 return (self.__origin__,)
835
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000836
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300837# _nparams is the number of accepted parameters, e.g. 0 for Hashable,
838# 1 for List and 2 for Dict. It may be -1 if variable number of
839# parameters are accepted (needs custom __getitem__).
840
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300841class _SpecialGenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300842 def __init__(self, origin, nparams, *, inst=True, name=None):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300843 if name is None:
844 name = origin.__name__
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300845 super().__init__(origin, inst=inst, name=name)
846 self._nparams = nparams
Serhiy Storchaka2fbc57a2020-05-10 15:14:27 +0300847 if origin.__module__ == 'builtins':
848 self.__doc__ = f'A generic version of {origin.__qualname__}.'
849 else:
850 self.__doc__ = f'A generic version of {origin.__module__}.{origin.__qualname__}.'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000851
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300852 @_tp_cache
853 def __getitem__(self, params):
854 if not isinstance(params, tuple):
855 params = (params,)
856 msg = "Parameters to generic types must be types."
857 params = tuple(_type_check(p, msg) for p in params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300858 _check_generic(self, params, self._nparams)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300859 return self.copy_with(params)
860
861 def copy_with(self, params):
862 return _GenericAlias(self.__origin__, params,
863 name=self._name, inst=self._inst)
864
865 def __repr__(self):
866 return 'typing.' + self._name
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000867
868 def __subclasscheck__(self, cls):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300869 if isinstance(cls, _SpecialGenericAlias):
870 return issubclass(cls.__origin__, self.__origin__)
871 if not isinstance(cls, _GenericAlias):
872 return issubclass(cls, self.__origin__)
873 return super().__subclasscheck__(cls)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700874
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100875 def __reduce__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300876 return self._name
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100877
Maggie Moss1b4552c2020-09-09 13:23:24 -0700878 def __or__(self, right):
879 return Union[self, right]
880
881 def __ror__(self, right):
882 return Union[self, right]
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700883
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300884class _CallableGenericAlias(_GenericAlias, _root=True):
885 def __repr__(self):
886 assert self._name == 'Callable'
887 if len(self.__args__) == 2 and self.__args__[0] is Ellipsis:
888 return super().__repr__()
889 return (f'typing.Callable'
890 f'[[{", ".join([_type_repr(a) for a in self.__args__[:-1]])}], '
891 f'{_type_repr(self.__args__[-1])}]')
892
893 def __reduce__(self):
894 args = self.__args__
895 if not (len(args) == 2 and args[0] is ...):
896 args = list(args[:-1]), args[-1]
897 return operator.getitem, (Callable, args)
898
899
900class _CallableType(_SpecialGenericAlias, _root=True):
901 def copy_with(self, params):
902 return _CallableGenericAlias(self.__origin__, params,
903 name=self._name, inst=self._inst)
904
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000905 def __getitem__(self, params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000906 if not isinstance(params, tuple) or len(params) != 2:
907 raise TypeError("Callable must be used as "
908 "Callable[[arg, ...], result].")
909 args, result = params
kj463c7d32020-12-14 02:38:24 +0800910 # This relaxes what args can be on purpose to allow things like
911 # PEP 612 ParamSpec. Responsibility for whether a user is using
912 # Callable[...] properly is deferred to static type checkers.
913 if isinstance(args, list):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000914 params = (tuple(args), result)
kj463c7d32020-12-14 02:38:24 +0800915 else:
916 params = (args, result)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000917 return self.__getitem_inner__(params)
918
919 @_tp_cache
920 def __getitem_inner__(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300921 args, result = params
922 msg = "Callable[args, result]: result must be a type."
923 result = _type_check(result, msg)
924 if args is Ellipsis:
925 return self.copy_with((_TypingEllipsis, result))
kj463c7d32020-12-14 02:38:24 +0800926 if not isinstance(args, tuple):
927 args = (args,)
928 args = tuple(_type_convert(arg) for arg in args)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300929 params = args + (result,)
930 return self.copy_with(params)
931
932
933class _TupleType(_SpecialGenericAlias, _root=True):
934 @_tp_cache
935 def __getitem__(self, params):
936 if params == ():
937 return self.copy_with((_TypingEmpty,))
938 if not isinstance(params, tuple):
939 params = (params,)
940 if len(params) == 2 and params[1] is ...:
941 msg = "Tuple[t, ...]: t must be a type."
942 p = _type_check(params[0], msg)
943 return self.copy_with((p, _TypingEllipsis))
944 msg = "Tuple[t0, t1, ...]: each t must be a type."
945 params = tuple(_type_check(p, msg) for p in params)
946 return self.copy_with(params)
947
948
949class _UnionGenericAlias(_GenericAlias, _root=True):
950 def copy_with(self, params):
951 return Union[params]
952
953 def __eq__(self, other):
954 if not isinstance(other, _UnionGenericAlias):
955 return NotImplemented
956 return set(self.__args__) == set(other.__args__)
957
958 def __hash__(self):
959 return hash(frozenset(self.__args__))
960
961 def __repr__(self):
962 args = self.__args__
963 if len(args) == 2:
964 if args[0] is type(None):
965 return f'typing.Optional[{_type_repr(args[1])}]'
966 elif args[1] is type(None):
967 return f'typing.Optional[{_type_repr(args[0])}]'
968 return super().__repr__()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000969
Maggie Moss1b4552c2020-09-09 13:23:24 -0700970 def __instancecheck__(self, obj):
971 return self.__subclasscheck__(type(obj))
972
973 def __subclasscheck__(self, cls):
974 for arg in self.__args__:
975 if issubclass(cls, arg):
976 return True
977
978
Yurii Karabasf03d3182020-11-17 04:23:19 +0200979def _value_and_type_iter(parameters):
980 return ((p, type(p)) for p in parameters)
981
982
983class _LiteralGenericAlias(_GenericAlias, _root=True):
984
985 def __eq__(self, other):
986 if not isinstance(other, _LiteralGenericAlias):
987 return NotImplemented
988
989 return set(_value_and_type_iter(self.__args__)) == set(_value_and_type_iter(other.__args__))
990
991 def __hash__(self):
Yurii Karabas1b540772020-11-19 18:17:38 +0200992 return hash(frozenset(_value_and_type_iter(self.__args__)))
Yurii Karabasf03d3182020-11-17 04:23:19 +0200993
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000994
995class Generic:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700996 """Abstract base class for generic types.
997
Guido van Rossumb24569a2016-11-20 18:01:29 -0800998 A generic type is typically declared by inheriting from
999 this class parameterized with one or more type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001000 For example, a generic mapping type might be defined as::
1001
1002 class Mapping(Generic[KT, VT]):
1003 def __getitem__(self, key: KT) -> VT:
1004 ...
1005 # Etc.
1006
1007 This class can then be used as follows::
1008
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001009 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001010 try:
1011 return mapping[key]
1012 except KeyError:
1013 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001014 """
Guido van Rossumd70fe632015-08-05 12:11:06 +02001015 __slots__ = ()
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001016 _is_protocol = False
Guido van Rossumd70fe632015-08-05 12:11:06 +02001017
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001018 @_tp_cache
1019 def __class_getitem__(cls, params):
1020 if not isinstance(params, tuple):
1021 params = (params,)
1022 if not params and cls is not Tuple:
1023 raise TypeError(
1024 f"Parameter list to {cls.__qualname__}[...] cannot be empty")
1025 msg = "Parameters to generic types must be types."
1026 params = tuple(_type_check(p, msg) for p in params)
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001027 if cls in (Generic, Protocol):
1028 # Generic and Protocol can only be subscripted with unique type variables.
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001029 if not all(isinstance(p, TypeVar) for p in params):
1030 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001031 f"Parameters to {cls.__name__}[...] must all be type variables")
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001032 if len(set(params)) != len(params):
1033 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001034 f"Parameters to {cls.__name__}[...] must all be unique")
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001035 else:
1036 # Subscripting a regular Generic subclass.
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001037 _check_generic(cls, params, len(cls.__parameters__))
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001038 return _GenericAlias(cls, params)
1039
1040 def __init_subclass__(cls, *args, **kwargs):
Ivan Levkivskyiee566fe2018-04-04 17:00:15 +01001041 super().__init_subclass__(*args, **kwargs)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001042 tvars = []
1043 if '__orig_bases__' in cls.__dict__:
1044 error = Generic in cls.__orig_bases__
1045 else:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001046 error = Generic in cls.__bases__ and cls.__name__ != 'Protocol'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001047 if error:
1048 raise TypeError("Cannot inherit from plain Generic")
1049 if '__orig_bases__' in cls.__dict__:
1050 tvars = _collect_type_vars(cls.__orig_bases__)
1051 # Look for Generic[T1, ..., Tn].
1052 # If found, tvars must be a subset of it.
1053 # If not found, tvars is it.
1054 # Also check for and reject plain Generic,
1055 # and reject multiple Generic[...].
1056 gvars = None
1057 for base in cls.__orig_bases__:
1058 if (isinstance(base, _GenericAlias) and
1059 base.__origin__ is Generic):
1060 if gvars is not None:
1061 raise TypeError(
1062 "Cannot inherit from Generic[...] multiple types.")
1063 gvars = base.__parameters__
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001064 if gvars is not None:
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001065 tvarset = set(tvars)
1066 gvarset = set(gvars)
1067 if not tvarset <= gvarset:
1068 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
1069 s_args = ', '.join(str(g) for g in gvars)
1070 raise TypeError(f"Some type variables ({s_vars}) are"
1071 f" not listed in Generic[{s_args}]")
1072 tvars = gvars
1073 cls.__parameters__ = tuple(tvars)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001074
1075
1076class _TypingEmpty:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001077 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
1078 to allow empty list/tuple in specific places, without allowing them
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001079 to sneak in where prohibited.
1080 """
1081
1082
1083class _TypingEllipsis:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001084 """Internal placeholder for ... (ellipsis)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001085
1086
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001087_TYPING_INTERNALS = ['__parameters__', '__orig_bases__', '__orig_class__',
1088 '_is_protocol', '_is_runtime_protocol']
1089
1090_SPECIAL_NAMES = ['__abstractmethods__', '__annotations__', '__dict__', '__doc__',
1091 '__init__', '__module__', '__new__', '__slots__',
Guido van Rossum48b069a2020-04-07 09:50:06 -07001092 '__subclasshook__', '__weakref__', '__class_getitem__']
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001093
1094# These special attributes will be not collected as protocol members.
1095EXCLUDED_ATTRIBUTES = _TYPING_INTERNALS + _SPECIAL_NAMES + ['_MutableMapping__marker']
1096
1097
1098def _get_protocol_attrs(cls):
1099 """Collect protocol members from a protocol class objects.
1100
1101 This includes names actually defined in the class dictionary, as well
1102 as names that appear in annotations. Special names (above) are skipped.
1103 """
1104 attrs = set()
1105 for base in cls.__mro__[:-1]: # without object
1106 if base.__name__ in ('Protocol', 'Generic'):
1107 continue
1108 annotations = getattr(base, '__annotations__', {})
1109 for attr in list(base.__dict__.keys()) + list(annotations.keys()):
1110 if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
1111 attrs.add(attr)
1112 return attrs
1113
1114
1115def _is_callable_members_only(cls):
1116 # PEP 544 prohibits using issubclass() with protocols that have non-method members.
1117 return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls))
1118
1119
1120def _no_init(self, *args, **kwargs):
1121 if type(self)._is_protocol:
1122 raise TypeError('Protocols cannot be instantiated')
1123
1124
1125def _allow_reckless_class_cheks():
Nickolena Fishercfaf4c02020-04-26 12:49:11 -05001126 """Allow instance and class checks for special stdlib modules.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001127
1128 The abc and functools modules indiscriminately call isinstance() and
1129 issubclass() on the whole MRO of a user class, which may contain protocols.
1130 """
1131 try:
1132 return sys._getframe(3).f_globals['__name__'] in ['abc', 'functools']
1133 except (AttributeError, ValueError): # For platforms without _getframe().
1134 return True
1135
1136
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001137_PROTO_ALLOWLIST = {
Divij Rajkumar692a0dc2019-09-12 11:13:51 +01001138 'collections.abc': [
1139 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
1140 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
1141 ],
1142 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
1143}
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001144
1145
1146class _ProtocolMeta(ABCMeta):
1147 # This metaclass is really unfortunate and exists only because of
1148 # the lack of __instancehook__.
1149 def __instancecheck__(cls, instance):
1150 # We need this method for situations where attributes are
1151 # assigned in __init__.
1152 if ((not getattr(cls, '_is_protocol', False) or
1153 _is_callable_members_only(cls)) and
1154 issubclass(instance.__class__, cls)):
1155 return True
1156 if cls._is_protocol:
1157 if all(hasattr(instance, attr) and
1158 # All *methods* can be blocked by setting them to None.
1159 (not callable(getattr(cls, attr, None)) or
1160 getattr(instance, attr) is not None)
1161 for attr in _get_protocol_attrs(cls)):
1162 return True
1163 return super().__instancecheck__(instance)
1164
1165
1166class Protocol(Generic, metaclass=_ProtocolMeta):
1167 """Base class for protocol classes.
1168
1169 Protocol classes are defined as::
1170
1171 class Proto(Protocol):
1172 def meth(self) -> int:
1173 ...
1174
1175 Such classes are primarily used with static type checkers that recognize
1176 structural subtyping (static duck-typing), for example::
1177
1178 class C:
1179 def meth(self) -> int:
1180 return 0
1181
1182 def func(x: Proto) -> int:
1183 return x.meth()
1184
1185 func(C()) # Passes static type check
1186
1187 See PEP 544 for details. Protocol classes decorated with
1188 @typing.runtime_checkable act as simple-minded runtime protocols that check
1189 only the presence of given attributes, ignoring their type signatures.
1190 Protocol classes can be generic, they are defined as::
1191
1192 class GenProto(Protocol[T]):
1193 def meth(self) -> T:
1194 ...
1195 """
1196 __slots__ = ()
1197 _is_protocol = True
1198 _is_runtime_protocol = False
1199
1200 def __init_subclass__(cls, *args, **kwargs):
1201 super().__init_subclass__(*args, **kwargs)
1202
1203 # Determine if this is a protocol or a concrete subclass.
1204 if not cls.__dict__.get('_is_protocol', False):
1205 cls._is_protocol = any(b is Protocol for b in cls.__bases__)
1206
1207 # Set (or override) the protocol subclass hook.
1208 def _proto_hook(other):
1209 if not cls.__dict__.get('_is_protocol', False):
1210 return NotImplemented
1211
1212 # First, perform various sanity checks.
1213 if not getattr(cls, '_is_runtime_protocol', False):
1214 if _allow_reckless_class_cheks():
1215 return NotImplemented
1216 raise TypeError("Instance and class checks can only be used with"
1217 " @runtime_checkable protocols")
1218 if not _is_callable_members_only(cls):
1219 if _allow_reckless_class_cheks():
1220 return NotImplemented
1221 raise TypeError("Protocols with non-method members"
1222 " don't support issubclass()")
1223 if not isinstance(other, type):
1224 # Same error message as for issubclass(1, int).
1225 raise TypeError('issubclass() arg 1 must be a class')
1226
1227 # Second, perform the actual structural compatibility check.
1228 for attr in _get_protocol_attrs(cls):
1229 for base in other.__mro__:
1230 # Check if the members appears in the class dictionary...
1231 if attr in base.__dict__:
1232 if base.__dict__[attr] is None:
1233 return NotImplemented
1234 break
1235
1236 # ...or in annotations, if it is a sub-protocol.
1237 annotations = getattr(base, '__annotations__', {})
1238 if (isinstance(annotations, collections.abc.Mapping) and
1239 attr in annotations and
1240 issubclass(other, Generic) and other._is_protocol):
1241 break
1242 else:
1243 return NotImplemented
1244 return True
1245
1246 if '__subclasshook__' not in cls.__dict__:
1247 cls.__subclasshook__ = _proto_hook
1248
1249 # We have nothing more to do for non-protocols...
1250 if not cls._is_protocol:
1251 return
1252
1253 # ... otherwise check consistency of bases, and prohibit instantiation.
1254 for base in cls.__bases__:
1255 if not (base in (object, Generic) or
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001256 base.__module__ in _PROTO_ALLOWLIST and
1257 base.__name__ in _PROTO_ALLOWLIST[base.__module__] or
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001258 issubclass(base, Generic) and base._is_protocol):
1259 raise TypeError('Protocols can only inherit from other'
1260 ' protocols, got %r' % base)
1261 cls.__init__ = _no_init
1262
1263
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001264class _AnnotatedAlias(_GenericAlias, _root=True):
1265 """Runtime representation of an annotated type.
1266
1267 At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't'
1268 with extra annotations. The alias behaves like a normal typing alias,
1269 instantiating is the same as instantiating the underlying type, binding
1270 it to types is also the same.
1271 """
1272 def __init__(self, origin, metadata):
1273 if isinstance(origin, _AnnotatedAlias):
1274 metadata = origin.__metadata__ + metadata
1275 origin = origin.__origin__
1276 super().__init__(origin, origin)
1277 self.__metadata__ = metadata
1278
1279 def copy_with(self, params):
1280 assert len(params) == 1
1281 new_type = params[0]
1282 return _AnnotatedAlias(new_type, self.__metadata__)
1283
1284 def __repr__(self):
1285 return "typing.Annotated[{}, {}]".format(
1286 _type_repr(self.__origin__),
1287 ", ".join(repr(a) for a in self.__metadata__)
1288 )
1289
1290 def __reduce__(self):
1291 return operator.getitem, (
1292 Annotated, (self.__origin__,) + self.__metadata__
1293 )
1294
1295 def __eq__(self, other):
1296 if not isinstance(other, _AnnotatedAlias):
1297 return NotImplemented
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001298 return (self.__origin__ == other.__origin__
1299 and self.__metadata__ == other.__metadata__)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001300
1301 def __hash__(self):
1302 return hash((self.__origin__, self.__metadata__))
1303
1304
1305class Annotated:
1306 """Add context specific metadata to a type.
1307
1308 Example: Annotated[int, runtime_check.Unsigned] indicates to the
1309 hypothetical runtime_check module that this type is an unsigned int.
1310 Every other consumer of this type can ignore this metadata and treat
1311 this type as int.
1312
1313 The first argument to Annotated must be a valid type.
1314
1315 Details:
1316
1317 - It's an error to call `Annotated` with less than two arguments.
1318 - Nested Annotated are flattened::
1319
1320 Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
1321
1322 - Instantiating an annotated type is equivalent to instantiating the
1323 underlying type::
1324
1325 Annotated[C, Ann1](5) == C(5)
1326
1327 - Annotated can be used as a generic type alias::
1328
1329 Optimized = Annotated[T, runtime.Optimize()]
1330 Optimized[int] == Annotated[int, runtime.Optimize()]
1331
1332 OptimizedList = Annotated[List[T], runtime.Optimize()]
1333 OptimizedList[int] == Annotated[List[int], runtime.Optimize()]
1334 """
1335
1336 __slots__ = ()
1337
1338 def __new__(cls, *args, **kwargs):
1339 raise TypeError("Type Annotated cannot be instantiated.")
1340
1341 @_tp_cache
1342 def __class_getitem__(cls, params):
1343 if not isinstance(params, tuple) or len(params) < 2:
1344 raise TypeError("Annotated[...] should be used "
1345 "with at least two arguments (a type and an "
1346 "annotation).")
1347 msg = "Annotated[t, ...]: t must be a type."
1348 origin = _type_check(params[0], msg)
1349 metadata = tuple(params[1:])
1350 return _AnnotatedAlias(origin, metadata)
1351
1352 def __init_subclass__(cls, *args, **kwargs):
1353 raise TypeError(
1354 "Cannot subclass {}.Annotated".format(cls.__module__)
1355 )
1356
1357
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001358def runtime_checkable(cls):
1359 """Mark a protocol class as a runtime protocol.
1360
1361 Such protocol can be used with isinstance() and issubclass().
1362 Raise TypeError if applied to a non-protocol class.
1363 This allows a simple-minded structural check very similar to
1364 one trick ponies in collections.abc such as Iterable.
1365 For example::
1366
1367 @runtime_checkable
1368 class Closable(Protocol):
1369 def close(self): ...
1370
1371 assert isinstance(open('/some/file'), Closable)
1372
1373 Warning: this will check only the presence of the required methods,
1374 not their type signatures!
1375 """
1376 if not issubclass(cls, Generic) or not cls._is_protocol:
1377 raise TypeError('@runtime_checkable can be only applied to protocol classes,'
1378 ' got %r' % cls)
1379 cls._is_runtime_protocol = True
1380 return cls
1381
1382
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001383def cast(typ, val):
1384 """Cast a value to a type.
1385
1386 This returns the value unchanged. To the type checker this
1387 signals that the return value has the designated type, but at
1388 runtime we intentionally don't check anything (we want this
1389 to be as fast as possible).
1390 """
1391 return val
1392
1393
1394def _get_defaults(func):
1395 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -08001396 try:
1397 code = func.__code__
1398 except AttributeError:
1399 # Some built-in functions don't have __code__, __defaults__, etc.
1400 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001401 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001402 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001403 arg_names = arg_names[:pos_count]
1404 defaults = func.__defaults__ or ()
1405 kwdefaults = func.__kwdefaults__
1406 res = dict(kwdefaults) if kwdefaults else {}
1407 pos_offset = pos_count - len(defaults)
1408 for name, value in zip(arg_names[pos_offset:], defaults):
1409 assert name not in res
1410 res[name] = value
1411 return res
1412
1413
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001414_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
1415 types.MethodType, types.ModuleType,
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02001416 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001417
1418
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001419def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
Guido van Rossum991d14f2016-11-09 13:12:51 -08001420 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001421
Guido van Rossum991d14f2016-11-09 13:12:51 -08001422 This is often the same as obj.__annotations__, but it handles
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001423 forward references encoded as string literals, adds Optional[t] if a
1424 default value equal to None is set and recursively replaces all
1425 'Annotated[T, ...]' with 'T' (unless 'include_extras=True').
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001426
Guido van Rossum991d14f2016-11-09 13:12:51 -08001427 The argument may be a module, class, method, or function. The annotations
1428 are returned as a dictionary. For classes, annotations include also
1429 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001430
Guido van Rossum991d14f2016-11-09 13:12:51 -08001431 TypeError is raised if the argument is not of a type that can contain
1432 annotations, and an empty dictionary is returned if no annotations are
1433 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001434
Guido van Rossum991d14f2016-11-09 13:12:51 -08001435 BEWARE -- the behavior of globalns and localns is counterintuitive
1436 (unless you are familiar with how eval() and exec() work). The
1437 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001438
Guido van Rossum991d14f2016-11-09 13:12:51 -08001439 - If no dict arguments are passed, an attempt is made to use the
Łukasz Langaf350a262017-09-14 14:33:00 -04001440 globals from obj (or the respective module's globals for classes),
1441 and these are also used as the locals. If the object does not appear
1442 to have globals, an empty dictionary is used.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001443
Guido van Rossum991d14f2016-11-09 13:12:51 -08001444 - If one dict argument is passed, it is used for both globals and
1445 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001446
Guido van Rossum991d14f2016-11-09 13:12:51 -08001447 - If two dict arguments are passed, they specify globals and
1448 locals, respectively.
1449 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001450
Guido van Rossum991d14f2016-11-09 13:12:51 -08001451 if getattr(obj, '__no_type_check__', None):
1452 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001453 # Classes require a special treatment.
1454 if isinstance(obj, type):
1455 hints = {}
1456 for base in reversed(obj.__mro__):
Łukasz Langaf350a262017-09-14 14:33:00 -04001457 if globalns is None:
1458 base_globals = sys.modules[base.__module__].__dict__
1459 else:
1460 base_globals = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001461 ann = base.__dict__.get('__annotations__', {})
1462 for name, value in ann.items():
1463 if value is None:
1464 value = type(None)
1465 if isinstance(value, str):
Nina Zakharenko0e61dff2018-05-22 20:32:10 -07001466 value = ForwardRef(value, is_argument=False)
Łukasz Langaf350a262017-09-14 14:33:00 -04001467 value = _eval_type(value, base_globals, localns)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001468 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001469 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
Łukasz Langaf350a262017-09-14 14:33:00 -04001470
1471 if globalns is None:
1472 if isinstance(obj, types.ModuleType):
1473 globalns = obj.__dict__
1474 else:
benedwards140aca3a32019-11-21 17:24:58 +00001475 nsobj = obj
1476 # Find globalns for the unwrapped object.
1477 while hasattr(nsobj, '__wrapped__'):
1478 nsobj = nsobj.__wrapped__
1479 globalns = getattr(nsobj, '__globals__', {})
Łukasz Langaf350a262017-09-14 14:33:00 -04001480 if localns is None:
1481 localns = globalns
1482 elif localns is None:
1483 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001484 hints = getattr(obj, '__annotations__', None)
1485 if hints is None:
1486 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001487 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001488 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001489 else:
1490 raise TypeError('{!r} is not a module, class, method, '
1491 'or function.'.format(obj))
1492 defaults = _get_defaults(obj)
1493 hints = dict(hints)
1494 for name, value in hints.items():
1495 if value is None:
1496 value = type(None)
1497 if isinstance(value, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001498 value = ForwardRef(value)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001499 value = _eval_type(value, globalns, localns)
1500 if name in defaults and defaults[name] is None:
1501 value = Optional[value]
1502 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001503 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
1504
1505
1506def _strip_annotations(t):
1507 """Strips the annotations from a given type.
1508 """
1509 if isinstance(t, _AnnotatedAlias):
1510 return _strip_annotations(t.__origin__)
1511 if isinstance(t, _GenericAlias):
1512 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1513 if stripped_args == t.__args__:
1514 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001515 return t.copy_with(stripped_args)
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001516 if isinstance(t, GenericAlias):
1517 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1518 if stripped_args == t.__args__:
1519 return t
1520 return GenericAlias(t.__origin__, stripped_args)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001521 return t
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001522
1523
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001524def get_origin(tp):
1525 """Get the unsubscripted version of a type.
1526
Jakub Stasiak38aaaaa2020-02-07 02:15:12 +01001527 This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
1528 and Annotated. Return None for unsupported types. Examples::
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001529
1530 get_origin(Literal[42]) is Literal
1531 get_origin(int) is None
1532 get_origin(ClassVar[int]) is ClassVar
1533 get_origin(Generic) is Generic
1534 get_origin(Generic[T]) is Generic
1535 get_origin(Union[T, int]) is Union
1536 get_origin(List[Tuple[T, T]][int]) == list
1537 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001538 if isinstance(tp, _AnnotatedAlias):
1539 return Annotated
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001540 if isinstance(tp, (_BaseGenericAlias, GenericAlias)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001541 return tp.__origin__
1542 if tp is Generic:
1543 return Generic
1544 return None
1545
1546
1547def get_args(tp):
1548 """Get type arguments with all substitutions performed.
1549
1550 For unions, basic simplifications used by Union constructor are performed.
1551 Examples::
1552 get_args(Dict[str, int]) == (str, int)
1553 get_args(int) == ()
1554 get_args(Union[int, Union[T, int], str][int]) == (int, str)
1555 get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
1556 get_args(Callable[[], T][int]) == ([], int)
1557 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001558 if isinstance(tp, _AnnotatedAlias):
1559 return (tp.__origin__,) + tp.__metadata__
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001560 if isinstance(tp, _GenericAlias):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001561 res = tp.__args__
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001562 if tp.__origin__ is collections.abc.Callable and res[0] is not Ellipsis:
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001563 res = (list(res[:-1]), res[-1])
1564 return res
Serhiy Storchaka6292be72020-04-27 10:27:21 +03001565 if isinstance(tp, GenericAlias):
1566 return tp.__args__
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001567 return ()
1568
1569
Patrick Reader0705ec82020-09-16 05:58:32 +01001570def is_typeddict(tp):
1571 """Check if an annotation is a TypedDict class
1572
1573 For example::
1574 class Film(TypedDict):
1575 title: str
1576 year: int
1577
1578 is_typeddict(Film) # => True
1579 is_typeddict(Union[list, str]) # => False
1580 """
1581 return isinstance(tp, _TypedDictMeta)
1582
1583
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001584def no_type_check(arg):
1585 """Decorator to indicate that annotations are not type hints.
1586
1587 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001588 applies recursively to all methods and classes defined in that class
1589 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001590
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001591 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001592 """
1593 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001594 arg_attrs = arg.__dict__.copy()
1595 for attr, val in arg.__dict__.items():
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001596 if val in arg.__bases__ + (arg,):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001597 arg_attrs.pop(attr)
1598 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001599 if isinstance(obj, types.FunctionType):
1600 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001601 if isinstance(obj, type):
1602 no_type_check(obj)
1603 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001604 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001605 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001606 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001607 return arg
1608
1609
1610def no_type_check_decorator(decorator):
1611 """Decorator to give another decorator the @no_type_check effect.
1612
1613 This wraps the decorator with something that wraps the decorated
1614 function in @no_type_check.
1615 """
1616
1617 @functools.wraps(decorator)
1618 def wrapped_decorator(*args, **kwds):
1619 func = decorator(*args, **kwds)
1620 func = no_type_check(func)
1621 return func
1622
1623 return wrapped_decorator
1624
1625
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001626def _overload_dummy(*args, **kwds):
1627 """Helper for @overload to raise when called."""
1628 raise NotImplementedError(
1629 "You should not call an overloaded function. "
1630 "A series of @overload-decorated functions "
1631 "outside a stub module should always be followed "
1632 "by an implementation that is not @overload-ed.")
1633
1634
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001635def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001636 """Decorator for overloaded functions/methods.
1637
1638 In a stub file, place two or more stub definitions for the same
1639 function in a row, each decorated with @overload. For example:
1640
1641 @overload
1642 def utf8(value: None) -> None: ...
1643 @overload
1644 def utf8(value: bytes) -> bytes: ...
1645 @overload
1646 def utf8(value: str) -> bytes: ...
1647
1648 In a non-stub file (i.e. a regular .py file), do the same but
1649 follow it with an implementation. The implementation should *not*
1650 be decorated with @overload. For example:
1651
1652 @overload
1653 def utf8(value: None) -> None: ...
1654 @overload
1655 def utf8(value: bytes) -> bytes: ...
1656 @overload
1657 def utf8(value: str) -> bytes: ...
1658 def utf8(value):
1659 # implementation goes here
1660 """
1661 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001662
1663
Ivan Levkivskyif3672422019-05-26 09:37:07 +01001664def final(f):
1665 """A decorator to indicate final methods and final classes.
1666
1667 Use this decorator to indicate to type checkers that the decorated
1668 method cannot be overridden, and decorated class cannot be subclassed.
1669 For example:
1670
1671 class Base:
1672 @final
1673 def done(self) -> None:
1674 ...
1675 class Sub(Base):
1676 def done(self) -> None: # Error reported by type checker
1677 ...
1678
1679 @final
1680 class Leaf:
1681 ...
1682 class Other(Leaf): # Error reported by type checker
1683 ...
1684
1685 There is no runtime checking of these properties.
1686 """
1687 return f
1688
1689
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001690# Some unconstrained type variables. These are used by the container types.
1691# (These are not for export.)
1692T = TypeVar('T') # Any type.
1693KT = TypeVar('KT') # Key type.
1694VT = TypeVar('VT') # Value type.
1695T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
1696V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
1697VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
1698T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
1699# Internal type variable used for Type[].
1700CT_co = TypeVar('CT_co', covariant=True, bound=type)
1701
1702# A useful type variable with constraints. This represents string types.
1703# (This one *is* for export!)
1704AnyStr = TypeVar('AnyStr', bytes, str)
1705
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001706
1707# Various ABCs mimicking those in collections.abc.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001708_alias = _SpecialGenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001709
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001710Hashable = _alias(collections.abc.Hashable, 0) # Not generic.
1711Awaitable = _alias(collections.abc.Awaitable, 1)
1712Coroutine = _alias(collections.abc.Coroutine, 3)
1713AsyncIterable = _alias(collections.abc.AsyncIterable, 1)
1714AsyncIterator = _alias(collections.abc.AsyncIterator, 1)
1715Iterable = _alias(collections.abc.Iterable, 1)
1716Iterator = _alias(collections.abc.Iterator, 1)
1717Reversible = _alias(collections.abc.Reversible, 1)
1718Sized = _alias(collections.abc.Sized, 0) # Not generic.
1719Container = _alias(collections.abc.Container, 1)
1720Collection = _alias(collections.abc.Collection, 1)
1721Callable = _CallableType(collections.abc.Callable, 2)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001722Callable.__doc__ = \
1723 """Callable type; Callable[[int], str] is a function of (int) -> str.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001724
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001725 The subscription syntax must always be used with exactly two
1726 values: the argument list and the return type. The argument list
1727 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001728
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001729 There is no syntax to indicate optional or keyword arguments,
1730 such function types are rarely used as callback types.
1731 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001732AbstractSet = _alias(collections.abc.Set, 1, name='AbstractSet')
1733MutableSet = _alias(collections.abc.MutableSet, 1)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001734# NOTE: Mapping is only covariant in the value type.
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001735Mapping = _alias(collections.abc.Mapping, 2)
1736MutableMapping = _alias(collections.abc.MutableMapping, 2)
1737Sequence = _alias(collections.abc.Sequence, 1)
1738MutableSequence = _alias(collections.abc.MutableSequence, 1)
1739ByteString = _alias(collections.abc.ByteString, 0) # Not generic
1740# Tuple accepts variable number of parameters.
1741Tuple = _TupleType(tuple, -1, inst=False, name='Tuple')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001742Tuple.__doc__ = \
1743 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001744
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001745 Example: Tuple[T1, T2] is a tuple of two elements corresponding
1746 to type variables T1 and T2. Tuple[int, float, str] is a tuple
1747 of an int, a float and a string.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001748
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001749 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
1750 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001751List = _alias(list, 1, inst=False, name='List')
1752Deque = _alias(collections.deque, 1, name='Deque')
1753Set = _alias(set, 1, inst=False, name='Set')
1754FrozenSet = _alias(frozenset, 1, inst=False, name='FrozenSet')
1755MappingView = _alias(collections.abc.MappingView, 1)
1756KeysView = _alias(collections.abc.KeysView, 1)
1757ItemsView = _alias(collections.abc.ItemsView, 2)
1758ValuesView = _alias(collections.abc.ValuesView, 1)
1759ContextManager = _alias(contextlib.AbstractContextManager, 1, name='ContextManager')
1760AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, 1, name='AsyncContextManager')
1761Dict = _alias(dict, 2, inst=False, name='Dict')
1762DefaultDict = _alias(collections.defaultdict, 2, name='DefaultDict')
1763OrderedDict = _alias(collections.OrderedDict, 2)
1764Counter = _alias(collections.Counter, 1)
1765ChainMap = _alias(collections.ChainMap, 2)
1766Generator = _alias(collections.abc.Generator, 3)
1767AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2)
1768Type = _alias(type, 1, inst=False, name='Type')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001769Type.__doc__ = \
1770 """A special construct usable to annotate class objects.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001771
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001772 For example, suppose we have the following classes::
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001773
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001774 class User: ... # Abstract base for User classes
1775 class BasicUser(User): ...
1776 class ProUser(User): ...
1777 class TeamUser(User): ...
Guido van Rossumf17c2002015-12-03 17:31:24 -08001778
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001779 And a function that takes a class argument that's a subclass of
1780 User and returns an instance of the corresponding class::
Guido van Rossumf17c2002015-12-03 17:31:24 -08001781
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001782 U = TypeVar('U', bound=User)
1783 def new_user(user_class: Type[U]) -> U:
1784 user = user_class()
1785 # (Here we could write the user object to a database)
1786 return user
Guido van Rossumf17c2002015-12-03 17:31:24 -08001787
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001788 joe = new_user(BasicUser)
Guido van Rossumf17c2002015-12-03 17:31:24 -08001789
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001790 At this point the type checker knows that joe has type BasicUser.
1791 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001792
1793
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001794@runtime_checkable
1795class SupportsInt(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001796 """An ABC with one abstract method __int__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001797 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001798
1799 @abstractmethod
1800 def __int__(self) -> int:
1801 pass
1802
1803
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001804@runtime_checkable
1805class SupportsFloat(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001806 """An ABC with one abstract method __float__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001807 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001808
1809 @abstractmethod
1810 def __float__(self) -> float:
1811 pass
1812
1813
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001814@runtime_checkable
1815class SupportsComplex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001816 """An ABC with one abstract method __complex__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001817 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001818
1819 @abstractmethod
1820 def __complex__(self) -> complex:
1821 pass
1822
1823
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001824@runtime_checkable
1825class SupportsBytes(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001826 """An ABC with one abstract method __bytes__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001827 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001828
1829 @abstractmethod
1830 def __bytes__(self) -> bytes:
1831 pass
1832
1833
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001834@runtime_checkable
1835class SupportsIndex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001836 """An ABC with one abstract method __index__."""
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -07001837 __slots__ = ()
1838
1839 @abstractmethod
1840 def __index__(self) -> int:
1841 pass
1842
1843
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001844@runtime_checkable
1845class SupportsAbs(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001846 """An ABC with one abstract method __abs__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001847 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001848
1849 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001850 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001851 pass
1852
1853
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001854@runtime_checkable
1855class SupportsRound(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001856 """An ABC with one abstract method __round__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001857 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001858
1859 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001860 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001861 pass
1862
1863
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001864def _make_nmtuple(name, types, module, defaults = ()):
1865 fields = [n for n, t in types]
1866 types = {n: _type_check(t, f"field {n} annotation must be a type")
1867 for n, t in types}
1868 nm_tpl = collections.namedtuple(name, fields,
1869 defaults=defaults, module=module)
1870 nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001871 return nm_tpl
1872
1873
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001874# attributes prohibited to set in NamedTuple class syntax
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001875_prohibited = frozenset({'__new__', '__init__', '__slots__', '__getnewargs__',
1876 '_fields', '_field_defaults',
1877 '_make', '_replace', '_asdict', '_source'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001878
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001879_special = frozenset({'__module__', '__name__', '__annotations__'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001880
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001881
Guido van Rossum2f841442016-11-15 09:48:06 -08001882class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001883
Guido van Rossum2f841442016-11-15 09:48:06 -08001884 def __new__(cls, typename, bases, ns):
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001885 assert bases[0] is _NamedTuple
Guido van Rossum2f841442016-11-15 09:48:06 -08001886 types = ns.get('__annotations__', {})
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001887 default_names = []
Guido van Rossum3c268be2017-01-18 08:03:50 -08001888 for field_name in types:
1889 if field_name in ns:
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001890 default_names.append(field_name)
1891 elif default_names:
1892 raise TypeError(f"Non-default namedtuple field {field_name} "
1893 f"cannot follow default field"
1894 f"{'s' if len(default_names) > 1 else ''} "
1895 f"{', '.join(default_names)}")
1896 nm_tpl = _make_nmtuple(typename, types.items(),
1897 defaults=[ns[n] for n in default_names],
1898 module=ns['__module__'])
Guido van Rossum95919c02017-01-22 17:47:20 -08001899 # update from user namespace without overriding special namedtuple attributes
1900 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001901 if key in _prohibited:
1902 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
1903 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08001904 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08001905 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001906
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001907
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001908def NamedTuple(typename, fields=None, /, **kwargs):
Guido van Rossum2f841442016-11-15 09:48:06 -08001909 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001910
Guido van Rossum2f841442016-11-15 09:48:06 -08001911 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001912
Guido van Rossum2f841442016-11-15 09:48:06 -08001913 class Employee(NamedTuple):
1914 name: str
1915 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001916
Guido van Rossum2f841442016-11-15 09:48:06 -08001917 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001918
Guido van Rossum2f841442016-11-15 09:48:06 -08001919 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001920
Raymond Hettingerf7b57df2019-03-18 09:53:56 -07001921 The resulting class has an extra __annotations__ attribute, giving a
1922 dict that maps field names to types. (The field names are also in
1923 the _fields attribute, which is part of the namedtuple API.)
1924 Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001925
Guido van Rossum2f841442016-11-15 09:48:06 -08001926 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001927
Guido van Rossum2f841442016-11-15 09:48:06 -08001928 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001929
Guido van Rossum2f841442016-11-15 09:48:06 -08001930 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
1931 """
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001932 if fields is None:
1933 fields = kwargs.items()
1934 elif kwargs:
1935 raise TypeError("Either list of fields or keywords"
1936 " can be provided to NamedTuple, not both")
1937 try:
1938 module = sys._getframe(1).f_globals.get('__name__', '__main__')
1939 except (AttributeError, ValueError):
1940 module = None
1941 return _make_nmtuple(typename, fields, module=module)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001942
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001943_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})
1944
1945def _namedtuple_mro_entries(bases):
1946 if len(bases) > 1:
1947 raise TypeError("Multiple inheritance with NamedTuple is not supported")
1948 assert bases[0] is NamedTuple
1949 return (_NamedTuple,)
1950
1951NamedTuple.__mro_entries__ = _namedtuple_mro_entries
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001952
1953
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001954class _TypedDictMeta(type):
1955 def __new__(cls, name, bases, ns, total=True):
1956 """Create new typed dict class object.
1957
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001958 This method is called when TypedDict is subclassed,
1959 or when TypedDict is instantiated. This way
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001960 TypedDict supports all three syntax forms described in its docstring.
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001961 Subclasses and instances of TypedDict return actual dictionaries.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001962 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001963 for base in bases:
1964 if type(base) is not _TypedDictMeta:
1965 raise TypeError('cannot inherit from both a TypedDict type '
1966 'and a non-TypedDict base class')
1967 tp_dict = type.__new__(_TypedDictMeta, name, (dict,), ns)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001968
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001969 annotations = {}
1970 own_annotations = ns.get('__annotations__', {})
1971 own_annotation_keys = set(own_annotations.keys())
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001972 msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001973 own_annotations = {
1974 n: _type_check(tp, msg) for n, tp in own_annotations.items()
1975 }
1976 required_keys = set()
1977 optional_keys = set()
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11001978
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001979 for base in bases:
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001980 annotations.update(base.__dict__.get('__annotations__', {}))
1981 required_keys.update(base.__dict__.get('__required_keys__', ()))
1982 optional_keys.update(base.__dict__.get('__optional_keys__', ()))
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11001983
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001984 annotations.update(own_annotations)
1985 if total:
1986 required_keys.update(own_annotation_keys)
1987 else:
1988 optional_keys.update(own_annotation_keys)
1989
1990 tp_dict.__annotations__ = annotations
1991 tp_dict.__required_keys__ = frozenset(required_keys)
1992 tp_dict.__optional_keys__ = frozenset(optional_keys)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001993 if not hasattr(tp_dict, '__total__'):
1994 tp_dict.__total__ = total
1995 return tp_dict
1996
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001997 __call__ = dict # static method
1998
1999 def __subclasscheck__(cls, other):
2000 # Typed dicts are only for static structural subtyping.
2001 raise TypeError('TypedDict does not support instance and class checks')
2002
2003 __instancecheck__ = __subclasscheck__
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002004
2005
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002006def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002007 """A simple typed namespace. At runtime it is equivalent to a plain dict.
2008
2009 TypedDict creates a dictionary type that expects all of its
2010 instances to have a certain set of keys, where each key is
2011 associated with a value of a consistent type. This expectation
2012 is not checked at runtime but is only enforced by type checkers.
2013 Usage::
2014
2015 class Point2D(TypedDict):
2016 x: int
2017 y: int
2018 label: str
2019
2020 a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
2021 b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
2022
2023 assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
2024
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002025 The type info can be accessed via the Point2D.__annotations__ dict, and
2026 the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
2027 TypedDict supports two additional equivalent forms::
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002028
2029 Point2D = TypedDict('Point2D', x=int, y=int, label=str)
2030 Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
2031
ananthan-123ab6423f2020-02-19 10:03:05 +05302032 By default, all keys must be present in a TypedDict. It is possible
2033 to override this by specifying totality.
2034 Usage::
2035
2036 class point2D(TypedDict, total=False):
2037 x: int
2038 y: int
2039
2040 This means that a point2D TypedDict can have any of the keys omitted.A type
2041 checker is only expected to support a literal False or True as the value of
2042 the total argument. True is the default, and makes all items defined in the
2043 class body be required.
2044
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002045 The class syntax is only supported in Python 3.6+, while two other
2046 syntax forms work for Python 2.7 and 3.2+
2047 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002048 if fields is None:
2049 fields = kwargs
2050 elif kwargs:
2051 raise TypeError("TypedDict takes either a dict or keyword arguments,"
2052 " but not both")
2053
Alex Grönholm67b769f2020-12-10 23:49:05 +02002054 ns = {'__annotations__': dict(fields)}
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002055 try:
2056 # Setting correct module is necessary to make typed dict classes pickleable.
2057 ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
2058 except (AttributeError, ValueError):
2059 pass
2060
Alex Grönholm67b769f2020-12-10 23:49:05 +02002061 return _TypedDictMeta(typename, (), ns, total=total)
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002062
2063_TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {})
2064TypedDict.__mro_entries__ = lambda bases: (_TypedDict,)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002065
2066
Guido van Rossum91185fe2016-06-08 11:19:11 -07002067def NewType(name, tp):
2068 """NewType creates simple unique types with almost zero
2069 runtime overhead. NewType(name, tp) is considered a subtype of tp
2070 by static type checkers. At runtime, NewType(name, tp) returns
2071 a dummy function that simply returns its argument. Usage::
2072
2073 UserId = NewType('UserId', int)
2074
2075 def name_by_id(user_id: UserId) -> str:
2076 ...
2077
2078 UserId('user') # Fails type check
2079
2080 name_by_id(42) # Fails type check
2081 name_by_id(UserId(42)) # OK
2082
2083 num = UserId(5) + 1 # type: int
2084 """
2085
2086 def new_type(x):
2087 return x
2088
2089 new_type.__name__ = name
2090 new_type.__supertype__ = tp
2091 return new_type
2092
2093
Guido van Rossum0e0563c2016-04-05 14:54:25 -07002094# Python-version-specific alias (Python 2: unicode; Python 3: str)
2095Text = str
2096
2097
Guido van Rossum91185fe2016-06-08 11:19:11 -07002098# Constant that's True when type checking, but False here.
2099TYPE_CHECKING = False
2100
2101
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002102class IO(Generic[AnyStr]):
2103 """Generic base class for TextIO and BinaryIO.
2104
2105 This is an abstract, generic version of the return of open().
2106
2107 NOTE: This does not distinguish between the different possible
2108 classes (text vs. binary, read vs. write vs. read/write,
2109 append-only, unbuffered). The TextIO and BinaryIO subclasses
2110 below capture the distinctions between text vs. binary, which is
2111 pervasive in the interface; however we currently do not offer a
2112 way to track the other distinctions in the type system.
2113 """
2114
Guido van Rossumd70fe632015-08-05 12:11:06 +02002115 __slots__ = ()
2116
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002117 @property
2118 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002119 def mode(self) -> str:
2120 pass
2121
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002122 @property
2123 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002124 def name(self) -> str:
2125 pass
2126
2127 @abstractmethod
2128 def close(self) -> None:
2129 pass
2130
Shantanu2e6569b2020-01-29 18:52:36 -08002131 @property
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002132 @abstractmethod
2133 def closed(self) -> bool:
2134 pass
2135
2136 @abstractmethod
2137 def fileno(self) -> int:
2138 pass
2139
2140 @abstractmethod
2141 def flush(self) -> None:
2142 pass
2143
2144 @abstractmethod
2145 def isatty(self) -> bool:
2146 pass
2147
2148 @abstractmethod
2149 def read(self, n: int = -1) -> AnyStr:
2150 pass
2151
2152 @abstractmethod
2153 def readable(self) -> bool:
2154 pass
2155
2156 @abstractmethod
2157 def readline(self, limit: int = -1) -> AnyStr:
2158 pass
2159
2160 @abstractmethod
2161 def readlines(self, hint: int = -1) -> List[AnyStr]:
2162 pass
2163
2164 @abstractmethod
2165 def seek(self, offset: int, whence: int = 0) -> int:
2166 pass
2167
2168 @abstractmethod
2169 def seekable(self) -> bool:
2170 pass
2171
2172 @abstractmethod
2173 def tell(self) -> int:
2174 pass
2175
2176 @abstractmethod
2177 def truncate(self, size: int = None) -> int:
2178 pass
2179
2180 @abstractmethod
2181 def writable(self) -> bool:
2182 pass
2183
2184 @abstractmethod
2185 def write(self, s: AnyStr) -> int:
2186 pass
2187
2188 @abstractmethod
2189 def writelines(self, lines: List[AnyStr]) -> None:
2190 pass
2191
2192 @abstractmethod
2193 def __enter__(self) -> 'IO[AnyStr]':
2194 pass
2195
2196 @abstractmethod
2197 def __exit__(self, type, value, traceback) -> None:
2198 pass
2199
2200
2201class BinaryIO(IO[bytes]):
2202 """Typed version of the return of open() in binary mode."""
2203
Guido van Rossumd70fe632015-08-05 12:11:06 +02002204 __slots__ = ()
2205
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002206 @abstractmethod
2207 def write(self, s: Union[bytes, bytearray]) -> int:
2208 pass
2209
2210 @abstractmethod
2211 def __enter__(self) -> 'BinaryIO':
2212 pass
2213
2214
2215class TextIO(IO[str]):
2216 """Typed version of the return of open() in text mode."""
2217
Guido van Rossumd70fe632015-08-05 12:11:06 +02002218 __slots__ = ()
2219
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002220 @property
2221 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002222 def buffer(self) -> BinaryIO:
2223 pass
2224
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002225 @property
2226 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002227 def encoding(self) -> str:
2228 pass
2229
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002230 @property
2231 @abstractmethod
Guido van Rossum991d14f2016-11-09 13:12:51 -08002232 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002233 pass
2234
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002235 @property
2236 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002237 def line_buffering(self) -> bool:
2238 pass
2239
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002240 @property
2241 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002242 def newlines(self) -> Any:
2243 pass
2244
2245 @abstractmethod
2246 def __enter__(self) -> 'TextIO':
2247 pass
2248
2249
2250class io:
2251 """Wrapper namespace for IO generic classes."""
2252
2253 __all__ = ['IO', 'TextIO', 'BinaryIO']
2254 IO = IO
2255 TextIO = TextIO
2256 BinaryIO = BinaryIO
2257
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002258
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002259io.__name__ = __name__ + '.io'
2260sys.modules[io.__name__] = io
2261
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002262Pattern = _alias(stdlib_re.Pattern, 1)
2263Match = _alias(stdlib_re.Match, 1)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002264
2265class re:
2266 """Wrapper namespace for re type aliases."""
2267
2268 __all__ = ['Pattern', 'Match']
2269 Pattern = Pattern
2270 Match = Match
2271
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002272
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002273re.__name__ = __name__ + '.re'
2274sys.modules[re.__name__] = re