blob: c82989861927dad7de6e9eb0e36605db11f169de [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
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070021import collections
Ivan Levkivskyid911e402018-01-20 11:23:59 +000022import collections.abc
Brett Cannonf3ad0422016-04-15 10:51:30 -070023import contextlib
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070024import functools
Serhiy Storchaka09f32212018-05-26 21:19:26 +030025import operator
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070026import re as stdlib_re # Avoid confusion with the re we export.
27import sys
28import types
Guido van Rossum48b069a2020-04-07 09:50:06 -070029from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType, GenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070030
31# Please keep __all__ alphabetized within each category.
32__all__ = [
33 # Super-special typing primitives.
Jakub Stasiakcf5b1092020-02-05 02:10:19 +010034 'Annotated',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070035 'Any',
36 'Callable',
Guido van Rossum0a6976d2016-09-11 15:34:56 -070037 'ClassVar',
Ivan Levkivskyif3672422019-05-26 09:37:07 +010038 'Final',
Anthony Sottiled30da5d2019-05-29 11:19:38 -070039 'ForwardRef',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070040 'Generic',
Ivan Levkivskyib891c462019-05-26 09:37:48 +010041 'Literal',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070042 'Optional',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +010043 'Protocol',
Guido van Rossumeb9aca32016-05-24 16:38:22 -070044 'Tuple',
45 'Type',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070046 'TypeVar',
47 'Union',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070048
49 # ABCs (from collections.abc).
50 'AbstractSet', # collections.abc.Set.
51 'ByteString',
52 'Container',
Ivan Levkivskyi29fda8d2017-06-10 21:57:56 +020053 'ContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070054 'Hashable',
55 'ItemsView',
56 'Iterable',
57 'Iterator',
58 'KeysView',
59 'Mapping',
60 'MappingView',
61 'MutableMapping',
62 'MutableSequence',
63 'MutableSet',
64 'Sequence',
65 'Sized',
66 'ValuesView',
Ivan Levkivskyid911e402018-01-20 11:23:59 +000067 'Awaitable',
68 'AsyncIterator',
69 'AsyncIterable',
70 'Coroutine',
71 'Collection',
72 'AsyncGenerator',
73 'AsyncContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070074
75 # Structural checks, a.k.a. protocols.
76 'Reversible',
77 'SupportsAbs',
Ivan Levkivskyif06e0212017-05-02 19:14:07 +020078 'SupportsBytes',
79 'SupportsComplex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070080 'SupportsFloat',
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -070081 'SupportsIndex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070082 'SupportsInt',
83 'SupportsRound',
84
85 # Concrete collection types.
Anthony Sottiled30da5d2019-05-29 11:19:38 -070086 'ChainMap',
Ivan Levkivskyib692dc82017-02-13 22:50:14 +010087 'Counter',
Raymond Hettinger80490522017-01-16 22:42:37 -080088 'Deque',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070089 'Dict',
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070090 'DefaultDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070091 'List',
Anthony Sottiled30da5d2019-05-29 11:19:38 -070092 'OrderedDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070093 'Set',
Guido van Rossumefa798d2016-08-23 11:01:50 -070094 'FrozenSet',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070095 'NamedTuple', # Not really a type.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +010096 'TypedDict', # Not really a type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070097 'Generator',
98
99 # One-off things.
100 'AnyStr',
101 'cast',
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100102 'final',
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +0100103 'get_args',
104 'get_origin',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700105 'get_type_hints',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700106 'NewType',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700107 'no_type_check',
108 'no_type_check_decorator',
aetracht45738202018-03-19 14:41:32 -0400109 'NoReturn',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700110 'overload',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100111 'runtime_checkable',
Guido van Rossum0e0563c2016-04-05 14:54:25 -0700112 'Text',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700113 'TYPE_CHECKING',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700114]
115
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700116# The pseudo-submodules 're' and 'io' are part of the public
117# namespace, but excluded from __all__ because they might stomp on
118# legitimate imports of those modules.
119
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700120
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700121def _type_check(arg, msg, is_argument=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000122 """Check that the argument is a type, and return it (internal helper).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700123
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000124 As a special case, accept None and return type(None) instead. Also wrap strings
125 into ForwardRef instances. Consider several corner cases, for example plain
126 special forms like Union are not valid, while Union[int, str] is OK, etc.
127 The msg argument is a human-readable error message, e.g::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700128
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000129 "Union[arg, ...]: arg should be a type."
Guido van Rossum4cefe742016-09-27 15:20:12 -0700130
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000131 We append the repr() of the actual value (truncated to 100 chars).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700132 """
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100133 invalid_generic_forms = (Generic, Protocol)
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700134 if is_argument:
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100135 invalid_generic_forms = invalid_generic_forms + (ClassVar, Final)
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400136
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000137 if arg is None:
138 return type(None)
139 if isinstance(arg, str):
140 return ForwardRef(arg)
141 if (isinstance(arg, _GenericAlias) and
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400142 arg.__origin__ in invalid_generic_forms):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000143 raise TypeError(f"{arg} is not valid as type argument")
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300144 if arg in (Any, NoReturn):
145 return arg
146 if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000147 raise TypeError(f"Plain {arg} is not valid as type argument")
148 if isinstance(arg, (type, TypeVar, ForwardRef)):
149 return arg
150 if not callable(arg):
151 raise TypeError(f"{msg} Got {arg!r:.100}.")
152 return arg
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700153
154
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000155def _type_repr(obj):
156 """Return the repr() of an object, special-casing types (internal helper).
157
158 If obj is a type, we return a shorter version than the default
159 type.__repr__, based on the module and qualified name, which is
160 typically enough to uniquely identify a type. For everything
161 else, we fall back on repr(obj).
162 """
163 if isinstance(obj, type):
164 if obj.__module__ == 'builtins':
165 return obj.__qualname__
166 return f'{obj.__module__}.{obj.__qualname__}'
167 if obj is ...:
168 return('...')
169 if isinstance(obj, types.FunctionType):
170 return obj.__name__
171 return repr(obj)
172
173
174def _collect_type_vars(types):
175 """Collect all type variable contained in types in order of
176 first appearance (lexicographic order). For example::
177
178 _collect_type_vars((T, List[S, T])) == (T, S)
179 """
180 tvars = []
181 for t in types:
182 if isinstance(t, TypeVar) and t not in tvars:
183 tvars.append(t)
Guido van Rossum48b069a2020-04-07 09:50:06 -0700184 if ((isinstance(t, _GenericAlias) and not t._special)
185 or isinstance(t, GenericAlias)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000186 tvars.extend([t for t in t.__parameters__ if t not in tvars])
187 return tuple(tvars)
188
189
190def _subs_tvars(tp, tvars, subs):
191 """Substitute type variables 'tvars' with substitutions 'subs'.
192 These two must have the same length.
193 """
Serhiy Storchaka68b352a2020-04-26 21:21:08 +0300194 if not isinstance(tp, (_GenericAlias, GenericAlias)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000195 return tp
196 new_args = list(tp.__args__)
197 for a, arg in enumerate(tp.__args__):
198 if isinstance(arg, TypeVar):
199 for i, tvar in enumerate(tvars):
200 if arg == tvar:
201 new_args[a] = subs[i]
202 else:
203 new_args[a] = _subs_tvars(arg, tvars, subs)
204 if tp.__origin__ is Union:
205 return Union[tuple(new_args)]
Serhiy Storchaka68b352a2020-04-26 21:21:08 +0300206 if isinstance(tp, GenericAlias):
207 return GenericAlias(tp.__origin__, tuple(new_args))
208 else:
209 return tp.copy_with(tuple(new_args))
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000210
211
212def _check_generic(cls, parameters):
213 """Check correct count for parameters of a generic cls (internal helper).
214 This gives a nice error message in case of count mismatch.
215 """
216 if not cls.__parameters__:
217 raise TypeError(f"{cls} is not a generic class")
218 alen = len(parameters)
219 elen = len(cls.__parameters__)
220 if alen != elen:
221 raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
222 f" actual {alen}, expected {elen}")
223
224
225def _remove_dups_flatten(parameters):
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700226 """An internal helper for Union creation and substitution: flatten Unions
227 among parameters, then remove duplicates.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000228 """
229 # Flatten out Union[Union[...], ...].
230 params = []
231 for p in parameters:
232 if isinstance(p, _GenericAlias) and p.__origin__ is Union:
233 params.extend(p.__args__)
234 elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
235 params.extend(p[1:])
236 else:
237 params.append(p)
238 # Weed out strict duplicates, preserving the first of each occurrence.
239 all_params = set(params)
240 if len(all_params) < len(params):
241 new_params = []
242 for t in params:
243 if t in all_params:
244 new_params.append(t)
245 all_params.remove(t)
246 params = new_params
247 assert not all_params, all_params
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700248 return tuple(params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000249
250
251_cleanups = []
252
253
254def _tp_cache(func):
255 """Internal wrapper caching __getitem__ of generic types with a fallback to
256 original function for non-hashable arguments.
257 """
258 cached = functools.lru_cache()(func)
259 _cleanups.append(cached.cache_clear)
260
261 @functools.wraps(func)
262 def inner(*args, **kwds):
263 try:
264 return cached(*args, **kwds)
265 except TypeError:
266 pass # All real errors (not unhashable args) are raised below.
267 return func(*args, **kwds)
268 return inner
269
270
271def _eval_type(t, globalns, localns):
272 """Evaluate all forward reverences in the given type t.
273 For use of globalns and localns see the docstring for get_type_hints().
274 """
275 if isinstance(t, ForwardRef):
276 return t._evaluate(globalns, localns)
277 if isinstance(t, _GenericAlias):
278 ev_args = tuple(_eval_type(a, globalns, localns) for a in t.__args__)
279 if ev_args == t.__args__:
280 return t
281 res = t.copy_with(ev_args)
282 res._special = t._special
283 return res
Serhiy Storchaka68b352a2020-04-26 21:21:08 +0300284 if isinstance(t, GenericAlias):
285 ev_args = tuple(_eval_type(a, globalns, localns) for a in t.__args__)
286 if ev_args == t.__args__:
287 return t
288 return GenericAlias(t.__origin__, ev_args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000289 return t
290
291
292class _Final:
293 """Mixin to prohibit subclassing"""
Guido van Rossum4cefe742016-09-27 15:20:12 -0700294
Guido van Rossum83ec3022017-01-17 20:43:28 -0800295 __slots__ = ('__weakref__',)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700296
Serhiy Storchaka2085bd02019-06-01 11:00:15 +0300297 def __init_subclass__(self, /, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000298 if '_root' not in kwds:
299 raise TypeError("Cannot subclass special typing classes")
300
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100301class _Immutable:
302 """Mixin to indicate that object should not be copied."""
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000303
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100304 def __copy__(self):
305 return self
306
307 def __deepcopy__(self, memo):
308 return self
309
310
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300311# Internal indicator of special typing constructs.
312# See __doc__ instance attribute for specific docs.
313class _SpecialForm(_Final, _root=True):
314 __slots__ = ('_name', '__doc__', '_getitem')
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000315
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300316 def __init__(self, getitem):
317 self._getitem = getitem
318 self._name = getitem.__name__
319 self.__doc__ = getitem.__doc__
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000320
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300321 def __mro_entries__(self, bases):
322 raise TypeError(f"Cannot subclass {self!r}")
Guido van Rossum4cefe742016-09-27 15:20:12 -0700323
324 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000325 return 'typing.' + self._name
326
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100327 def __reduce__(self):
328 return self._name
Guido van Rossum4cefe742016-09-27 15:20:12 -0700329
330 def __call__(self, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000331 raise TypeError(f"Cannot instantiate {self!r}")
332
333 def __instancecheck__(self, obj):
334 raise TypeError(f"{self} cannot be used with isinstance()")
335
336 def __subclasscheck__(self, cls):
337 raise TypeError(f"{self} cannot be used with issubclass()")
338
339 @_tp_cache
340 def __getitem__(self, parameters):
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300341 return self._getitem(self, parameters)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700342
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300343@_SpecialForm
344def Any(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000345 """Special type indicating an unconstrained type.
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700346
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000347 - Any is compatible with every type.
348 - Any assumed to have all methods.
349 - All values assumed to be instances of Any.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700350
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000351 Note that all the above statements are true from the point of view of
352 static type checkers. At runtime, Any should not be used with instance
353 or class checks.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300354 """
355 raise TypeError(f"{self} is not subscriptable")
Guido van Rossumd70fe632015-08-05 12:11:06 +0200356
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300357@_SpecialForm
358def NoReturn(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000359 """Special type indicating functions that never return.
360 Example::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700361
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000362 from typing import NoReturn
363
364 def stop() -> NoReturn:
365 raise Exception('no way')
366
367 This type is invalid in other positions, e.g., ``List[NoReturn]``
368 will fail in static type checkers.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300369 """
370 raise TypeError(f"{self} is not subscriptable")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000371
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300372@_SpecialForm
373def ClassVar(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000374 """Special type construct to mark class variables.
375
376 An annotation wrapped in ClassVar indicates that a given
377 attribute is intended to be used as a class variable and
378 should not be set on instances of that class. Usage::
379
380 class Starship:
381 stats: ClassVar[Dict[str, int]] = {} # class variable
382 damage: int = 10 # instance variable
383
384 ClassVar accepts only types and cannot be further subscribed.
385
386 Note that ClassVar is not a class itself, and should not
387 be used with isinstance() or issubclass().
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300388 """
389 item = _type_check(parameters, f'{self} accepts only single type.')
390 return _GenericAlias(self, (item,))
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000391
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300392@_SpecialForm
393def Final(self, parameters):
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100394 """Special typing construct to indicate final names to type checkers.
395
396 A final name cannot be re-assigned or overridden in a subclass.
397 For example:
398
399 MAX_SIZE: Final = 9000
400 MAX_SIZE += 1 # Error reported by type checker
401
402 class Connection:
403 TIMEOUT: Final[int] = 10
404
405 class FastConnector(Connection):
406 TIMEOUT = 1 # Error reported by type checker
407
408 There is no runtime checking of these properties.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300409 """
410 item = _type_check(parameters, f'{self} accepts only single type.')
411 return _GenericAlias(self, (item,))
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100412
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300413@_SpecialForm
414def Union(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000415 """Union type; Union[X, Y] means either X or Y.
416
417 To define a union, use e.g. Union[int, str]. Details:
418 - The arguments must be types and there must be at least one.
419 - None as an argument is a special case and is replaced by
420 type(None).
421 - Unions of unions are flattened, e.g.::
422
423 Union[Union[int, str], float] == Union[int, str, float]
424
425 - Unions of a single argument vanish, e.g.::
426
427 Union[int] == int # The constructor actually returns int
428
429 - Redundant arguments are skipped, e.g.::
430
431 Union[int, str, int] == Union[int, str]
432
433 - When comparing unions, the argument order is ignored, e.g.::
434
435 Union[int, str] == Union[str, int]
436
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000437 - You cannot subclass or instantiate a union.
438 - You can use Optional[X] as a shorthand for Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300439 """
440 if parameters == ():
441 raise TypeError("Cannot take a Union of no types.")
442 if not isinstance(parameters, tuple):
443 parameters = (parameters,)
444 msg = "Union[arg, ...]: each arg must be a type."
445 parameters = tuple(_type_check(p, msg) for p in parameters)
446 parameters = _remove_dups_flatten(parameters)
447 if len(parameters) == 1:
448 return parameters[0]
449 return _GenericAlias(self, parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000450
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300451@_SpecialForm
452def Optional(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000453 """Optional type.
454
455 Optional[X] is equivalent to Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300456 """
457 arg = _type_check(parameters, f"{self} requires a single type.")
458 return Union[arg, type(None)]
Guido van Rossumb7dedc82016-10-29 12:44:29 -0700459
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300460@_SpecialForm
461def Literal(self, parameters):
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100462 """Special typing form to define literal types (a.k.a. value types).
463
464 This form can be used to indicate to type checkers that the corresponding
465 variable or function parameter has a value equivalent to the provided
466 literal (or one of several literals):
467
468 def validate_simple(data: Any) -> Literal[True]: # always returns True
469 ...
470
471 MODE = Literal['r', 'rb', 'w', 'wb']
472 def open_helper(file: str, mode: MODE) -> str:
473 ...
474
475 open_helper('/some/path', 'r') # Passes type check
476 open_helper('/other/path', 'typo') # Error in type checker
477
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300478 Literal[...] cannot be subclassed. At runtime, an arbitrary value
479 is allowed as type argument to Literal[...], but type checkers may
480 impose restrictions.
481 """
482 # There is no '_type_check' call because arguments to Literal[...] are
483 # values, not types.
484 return _GenericAlias(self, parameters)
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100485
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700486
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000487class ForwardRef(_Final, _root=True):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800488 """Internal wrapper to hold a forward reference."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700489
Guido van Rossum4cefe742016-09-27 15:20:12 -0700490 __slots__ = ('__forward_arg__', '__forward_code__',
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400491 '__forward_evaluated__', '__forward_value__',
492 '__forward_is_argument__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700493
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700494 def __init__(self, arg, is_argument=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700495 if not isinstance(arg, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000496 raise TypeError(f"Forward reference must be a string -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700497 try:
498 code = compile(arg, '<string>', 'eval')
499 except SyntaxError:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000500 raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700501 self.__forward_arg__ = arg
502 self.__forward_code__ = code
503 self.__forward_evaluated__ = False
504 self.__forward_value__ = None
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400505 self.__forward_is_argument__ = is_argument
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700506
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000507 def _evaluate(self, globalns, localns):
Guido van Rossumdad17902016-11-10 08:29:18 -0800508 if not self.__forward_evaluated__ or localns is not globalns:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700509 if globalns is None and localns is None:
510 globalns = localns = {}
511 elif globalns is None:
512 globalns = localns
513 elif localns is None:
514 localns = globalns
515 self.__forward_value__ = _type_check(
516 eval(self.__forward_code__, globalns, localns),
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400517 "Forward references must evaluate to types.",
518 is_argument=self.__forward_is_argument__)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700519 self.__forward_evaluated__ = True
520 return self.__forward_value__
521
Guido van Rossum4cefe742016-09-27 15:20:12 -0700522 def __eq__(self, other):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000523 if not isinstance(other, ForwardRef):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700524 return NotImplemented
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100525 if self.__forward_evaluated__ and other.__forward_evaluated__:
526 return (self.__forward_arg__ == other.__forward_arg__ and
527 self.__forward_value__ == other.__forward_value__)
528 return self.__forward_arg__ == other.__forward_arg__
Guido van Rossum4cefe742016-09-27 15:20:12 -0700529
530 def __hash__(self):
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100531 return hash(self.__forward_arg__)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700532
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700533 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000534 return f'ForwardRef({self.__forward_arg__!r})'
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700535
536
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100537class TypeVar(_Final, _Immutable, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700538 """Type variable.
539
540 Usage::
541
542 T = TypeVar('T') # Can be anything
543 A = TypeVar('A', str, bytes) # Must be str or bytes
544
545 Type variables exist primarily for the benefit of static type
546 checkers. They serve as the parameters for generic types as well
547 as for generic function definitions. See class Generic for more
548 information on generic types. Generic functions work as follows:
549
Guido van Rossumb24569a2016-11-20 18:01:29 -0800550 def repeat(x: T, n: int) -> List[T]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700551 '''Return a list containing n references to x.'''
552 return [x]*n
553
554 def longest(x: A, y: A) -> A:
555 '''Return the longest of two strings.'''
556 return x if len(x) >= len(y) else y
557
558 The latter example's signature is essentially the overloading
559 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
560 that if the arguments are instances of some subclass of str,
561 the return type is still plain str.
562
Guido van Rossumb24569a2016-11-20 18:01:29 -0800563 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700564
Guido van Rossumefa798d2016-08-23 11:01:50 -0700565 Type variables defined with covariant=True or contravariant=True
João D. Ferreira86bfed32018-07-07 16:41:20 +0100566 can be used to declare covariant or contravariant generic types.
Guido van Rossumefa798d2016-08-23 11:01:50 -0700567 See PEP 484 for more details. By default generic types are invariant
568 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700569
570 Type variables can be introspected. e.g.:
571
572 T.__name__ == 'T'
573 T.__constraints__ == ()
574 T.__covariant__ == False
575 T.__contravariant__ = False
576 A.__constraints__ == (str, bytes)
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100577
578 Note that only type variables defined in global scope can be pickled.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700579 """
580
Guido van Rossum4cefe742016-09-27 15:20:12 -0700581 __slots__ = ('__name__', '__bound__', '__constraints__',
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300582 '__covariant__', '__contravariant__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700583
584 def __init__(self, name, *constraints, bound=None,
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800585 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700586 self.__name__ = name
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700587 if covariant and contravariant:
Guido van Rossumefa798d2016-08-23 11:01:50 -0700588 raise ValueError("Bivariant types are not supported.")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700589 self.__covariant__ = bool(covariant)
590 self.__contravariant__ = bool(contravariant)
591 if constraints and bound is not None:
592 raise TypeError("Constraints cannot be combined with bound=...")
593 if constraints and len(constraints) == 1:
594 raise TypeError("A single constraint is not allowed")
595 msg = "TypeVar(name, constraint, ...): constraints must be types."
596 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
597 if bound:
598 self.__bound__ = _type_check(bound, "Bound must be a type.")
599 else:
600 self.__bound__ = None
HongWeipenga25a04f2020-04-21 04:01:53 +0800601 try:
602 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling
603 except (AttributeError, ValueError):
604 def_mod = None
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300605 if def_mod != 'typing':
606 self.__module__ = def_mod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700607
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700608 def __repr__(self):
609 if self.__covariant__:
610 prefix = '+'
611 elif self.__contravariant__:
612 prefix = '-'
613 else:
614 prefix = '~'
615 return prefix + self.__name__
616
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100617 def __reduce__(self):
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300618 return self.__name__
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100619
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700620
Guido van Rossum83ec3022017-01-17 20:43:28 -0800621# Special typing constructs Union, Optional, Generic, Callable and Tuple
622# use three special attributes for internal bookkeeping of generic types:
623# * __parameters__ is a tuple of unique free type parameters of a generic
624# type, for example, Dict[T, T].__parameters__ == (T,);
625# * __origin__ keeps a reference to a type that was subscripted,
Ivan Levkivskyi43d12a62018-05-09 02:23:46 +0100626# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
627# the type.
Guido van Rossum83ec3022017-01-17 20:43:28 -0800628# * __args__ is a tuple of all arguments used in subscripting,
629# e.g., Dict[T, int].__args__ == (T, int).
630
Ivan Levkivskyi2a363d22018-04-05 01:25:15 +0100631
632# Mapping from non-generic type names that have a generic alias in typing
633# but with a different name.
634_normalize_alias = {'list': 'List',
635 'tuple': 'Tuple',
636 'dict': 'Dict',
637 'set': 'Set',
638 'frozenset': 'FrozenSet',
639 'deque': 'Deque',
640 'defaultdict': 'DefaultDict',
641 'type': 'Type',
642 'Set': 'AbstractSet'}
643
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000644def _is_dunder(attr):
645 return attr.startswith('__') and attr.endswith('__')
Guido van Rossum83ec3022017-01-17 20:43:28 -0800646
Guido van Rossumb24569a2016-11-20 18:01:29 -0800647
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000648class _GenericAlias(_Final, _root=True):
649 """The central part of internal API.
650
651 This represents a generic version of type 'origin' with type arguments 'params'.
652 There are two kind of these aliases: user defined and special. The special ones
653 are wrappers around builtin collections and ABCs in collections.abc. These must
654 have 'name' always set. If 'inst' is False, then the alias can't be instantiated,
655 this is used by e.g. typing.List and typing.Dict.
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700656 """
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000657 def __init__(self, origin, params, *, inst=True, special=False, name=None):
658 self._inst = inst
659 self._special = special
660 if special and name is None:
661 orig_name = origin.__name__
Ivan Levkivskyi2a363d22018-04-05 01:25:15 +0100662 name = _normalize_alias.get(orig_name, orig_name)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000663 self._name = name
664 if not isinstance(params, tuple):
665 params = (params,)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700666 self.__origin__ = origin
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700667 self.__args__ = tuple(... if a is _TypingEllipsis else
668 () if a is _TypingEmpty else
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000669 a for a in params)
670 self.__parameters__ = _collect_type_vars(params)
671 self.__slots__ = None # This is not documented.
672 if not name:
673 self.__module__ = origin.__module__
Serhiy Storchaka7e644142020-04-18 17:13:21 +0300674 if special:
675 self.__doc__ = f'A generic version of {origin.__module__}.{origin.__qualname__}'
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700676
Guido van Rossum4cefe742016-09-27 15:20:12 -0700677 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700678 def __getitem__(self, params):
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100679 if self.__origin__ in (Generic, Protocol):
680 # Can't subscript Generic[...] or Protocol[...].
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000681 raise TypeError(f"Cannot subscript already-subscripted {self}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700682 if not isinstance(params, tuple):
683 params = (params,)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700684 msg = "Parameters to generic types must be types."
685 params = tuple(_type_check(p, msg) for p in params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000686 _check_generic(self, params)
687 return _subs_tvars(self, self.__parameters__, params)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100688
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000689 def copy_with(self, params):
690 # We don't copy self._special.
691 return _GenericAlias(self.__origin__, params, name=self._name, inst=self._inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700692
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000693 def __repr__(self):
694 if (self._name != 'Callable' or
695 len(self.__args__) == 2 and self.__args__[0] is Ellipsis):
696 if self._name:
697 name = 'typing.' + self._name
698 else:
699 name = _type_repr(self.__origin__)
700 if not self._special:
701 args = f'[{", ".join([_type_repr(a) for a in self.__args__])}]'
702 else:
703 args = ''
704 return (f'{name}{args}')
705 if self._special:
706 return 'typing.Callable'
707 return (f'typing.Callable'
708 f'[[{", ".join([_type_repr(a) for a in self.__args__[:-1]])}], '
709 f'{_type_repr(self.__args__[-1])}]')
710
711 def __eq__(self, other):
712 if not isinstance(other, _GenericAlias):
713 return NotImplemented
714 if self.__origin__ != other.__origin__:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100715 return False
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000716 if self.__origin__ is Union and other.__origin__ is Union:
717 return frozenset(self.__args__) == frozenset(other.__args__)
718 return self.__args__ == other.__args__
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100719
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000720 def __hash__(self):
721 if self.__origin__ is Union:
722 return hash((Union, frozenset(self.__args__)))
723 return hash((self.__origin__, self.__args__))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700724
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000725 def __call__(self, *args, **kwargs):
726 if not self._inst:
727 raise TypeError(f"Type {self._name} cannot be instantiated; "
728 f"use {self._name.lower()}() instead")
729 result = self.__origin__(*args, **kwargs)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700730 try:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000731 result.__orig_class__ = self
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700732 except AttributeError:
733 pass
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000734 return result
735
736 def __mro_entries__(self, bases):
737 if self._name: # generic version of an ABC or built-in class
738 res = []
739 if self.__origin__ not in bases:
740 res.append(self.__origin__)
741 i = bases.index(self)
742 if not any(isinstance(b, _GenericAlias) or issubclass(b, Generic)
743 for b in bases[i+1:]):
744 res.append(Generic)
745 return tuple(res)
746 if self.__origin__ is Generic:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100747 if Protocol in bases:
748 return ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000749 i = bases.index(self)
750 for b in bases[i+1:]:
751 if isinstance(b, _GenericAlias) and b is not self:
752 return ()
753 return (self.__origin__,)
754
755 def __getattr__(self, attr):
Ville Skyttä61f82e02018-04-20 23:08:45 +0300756 # We are careful for copy and pickle.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000757 # Also for simplicity we just don't relay all dunder names
758 if '__origin__' in self.__dict__ and not _is_dunder(attr):
759 return getattr(self.__origin__, attr)
760 raise AttributeError(attr)
761
762 def __setattr__(self, attr, val):
763 if _is_dunder(attr) or attr in ('_name', '_inst', '_special'):
764 super().__setattr__(attr, val)
765 else:
766 setattr(self.__origin__, attr, val)
767
768 def __instancecheck__(self, obj):
769 return self.__subclasscheck__(type(obj))
770
771 def __subclasscheck__(self, cls):
772 if self._special:
773 if not isinstance(cls, _GenericAlias):
774 return issubclass(cls, self.__origin__)
775 if cls._special:
776 return issubclass(cls.__origin__, self.__origin__)
777 raise TypeError("Subscripted generics cannot be used with"
778 " class and instance checks")
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700779
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100780 def __reduce__(self):
781 if self._special:
782 return self._name
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300783
784 if self._name:
785 origin = globals()[self._name]
786 else:
787 origin = self.__origin__
788 if (origin is Callable and
789 not (len(self.__args__) == 2 and self.__args__[0] is Ellipsis)):
790 args = list(self.__args__[:-1]), self.__args__[-1]
791 else:
792 args = tuple(self.__args__)
793 if len(args) == 1 and not isinstance(args[0], tuple):
794 args, = args
795 return operator.getitem, (origin, args)
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100796
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700797
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000798class _VariadicGenericAlias(_GenericAlias, _root=True):
799 """Same as _GenericAlias above but for variadic aliases. Currently,
800 this is used only by special internal aliases: Tuple and Callable.
801 """
802 def __getitem__(self, params):
803 if self._name != 'Callable' or not self._special:
804 return self.__getitem_inner__(params)
805 if not isinstance(params, tuple) or len(params) != 2:
806 raise TypeError("Callable must be used as "
807 "Callable[[arg, ...], result].")
808 args, result = params
809 if args is Ellipsis:
810 params = (Ellipsis, result)
811 else:
812 if not isinstance(args, list):
813 raise TypeError(f"Callable[args, result]: args must be a list."
814 f" Got {args}")
815 params = (tuple(args), result)
816 return self.__getitem_inner__(params)
817
818 @_tp_cache
819 def __getitem_inner__(self, params):
820 if self.__origin__ is tuple and self._special:
821 if params == ():
822 return self.copy_with((_TypingEmpty,))
823 if not isinstance(params, tuple):
824 params = (params,)
825 if len(params) == 2 and params[1] is ...:
826 msg = "Tuple[t, ...]: t must be a type."
827 p = _type_check(params[0], msg)
828 return self.copy_with((p, _TypingEllipsis))
829 msg = "Tuple[t0, t1, ...]: each t must be a type."
830 params = tuple(_type_check(p, msg) for p in params)
831 return self.copy_with(params)
832 if self.__origin__ is collections.abc.Callable and self._special:
833 args, result = params
834 msg = "Callable[args, result]: result must be a type."
835 result = _type_check(result, msg)
836 if args is Ellipsis:
837 return self.copy_with((_TypingEllipsis, result))
838 msg = "Callable[[arg, ...], result]: each arg must be a type."
839 args = tuple(_type_check(arg, msg) for arg in args)
840 params = args + (result,)
841 return self.copy_with(params)
842 return super().__getitem__(params)
843
844
845class Generic:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700846 """Abstract base class for generic types.
847
Guido van Rossumb24569a2016-11-20 18:01:29 -0800848 A generic type is typically declared by inheriting from
849 this class parameterized with one or more type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700850 For example, a generic mapping type might be defined as::
851
852 class Mapping(Generic[KT, VT]):
853 def __getitem__(self, key: KT) -> VT:
854 ...
855 # Etc.
856
857 This class can then be used as follows::
858
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700859 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700860 try:
861 return mapping[key]
862 except KeyError:
863 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700864 """
Guido van Rossumd70fe632015-08-05 12:11:06 +0200865 __slots__ = ()
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100866 _is_protocol = False
Guido van Rossumd70fe632015-08-05 12:11:06 +0200867
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700868 def __new__(cls, *args, **kwds):
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100869 if cls in (Generic, Protocol):
870 raise TypeError(f"Type {cls.__name__} cannot be instantiated; "
Guido van Rossum62fe1bb2016-10-29 16:05:26 -0700871 "it can be used only as a base class")
Ivan Levkivskyib551e9f2018-05-10 23:10:10 -0400872 if super().__new__ is object.__new__ and cls.__init__ is not object.__init__:
Ivan Levkivskyi43d12a62018-05-09 02:23:46 +0100873 obj = super().__new__(cls)
874 else:
875 obj = super().__new__(cls, *args, **kwds)
876 return obj
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000877
878 @_tp_cache
879 def __class_getitem__(cls, params):
880 if not isinstance(params, tuple):
881 params = (params,)
882 if not params and cls is not Tuple:
883 raise TypeError(
884 f"Parameter list to {cls.__qualname__}[...] cannot be empty")
885 msg = "Parameters to generic types must be types."
886 params = tuple(_type_check(p, msg) for p in params)
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100887 if cls in (Generic, Protocol):
888 # Generic and Protocol can only be subscripted with unique type variables.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000889 if not all(isinstance(p, TypeVar) for p in params):
890 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100891 f"Parameters to {cls.__name__}[...] must all be type variables")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000892 if len(set(params)) != len(params):
893 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100894 f"Parameters to {cls.__name__}[...] must all be unique")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000895 else:
896 # Subscripting a regular Generic subclass.
897 _check_generic(cls, params)
898 return _GenericAlias(cls, params)
899
900 def __init_subclass__(cls, *args, **kwargs):
Ivan Levkivskyiee566fe2018-04-04 17:00:15 +0100901 super().__init_subclass__(*args, **kwargs)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000902 tvars = []
903 if '__orig_bases__' in cls.__dict__:
904 error = Generic in cls.__orig_bases__
905 else:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100906 error = Generic in cls.__bases__ and cls.__name__ != 'Protocol'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000907 if error:
908 raise TypeError("Cannot inherit from plain Generic")
909 if '__orig_bases__' in cls.__dict__:
910 tvars = _collect_type_vars(cls.__orig_bases__)
911 # Look for Generic[T1, ..., Tn].
912 # If found, tvars must be a subset of it.
913 # If not found, tvars is it.
914 # Also check for and reject plain Generic,
915 # and reject multiple Generic[...].
916 gvars = None
917 for base in cls.__orig_bases__:
918 if (isinstance(base, _GenericAlias) and
919 base.__origin__ is Generic):
920 if gvars is not None:
921 raise TypeError(
922 "Cannot inherit from Generic[...] multiple types.")
923 gvars = base.__parameters__
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100924 if gvars is not None:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000925 tvarset = set(tvars)
926 gvarset = set(gvars)
927 if not tvarset <= gvarset:
928 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
929 s_args = ', '.join(str(g) for g in gvars)
930 raise TypeError(f"Some type variables ({s_vars}) are"
931 f" not listed in Generic[{s_args}]")
932 tvars = gvars
933 cls.__parameters__ = tuple(tvars)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700934
935
936class _TypingEmpty:
Guido van Rossumb24569a2016-11-20 18:01:29 -0800937 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
938 to allow empty list/tuple in specific places, without allowing them
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700939 to sneak in where prohibited.
940 """
941
942
943class _TypingEllipsis:
Guido van Rossumb24569a2016-11-20 18:01:29 -0800944 """Internal placeholder for ... (ellipsis)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700945
946
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100947_TYPING_INTERNALS = ['__parameters__', '__orig_bases__', '__orig_class__',
948 '_is_protocol', '_is_runtime_protocol']
949
950_SPECIAL_NAMES = ['__abstractmethods__', '__annotations__', '__dict__', '__doc__',
951 '__init__', '__module__', '__new__', '__slots__',
Guido van Rossum48b069a2020-04-07 09:50:06 -0700952 '__subclasshook__', '__weakref__', '__class_getitem__']
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100953
954# These special attributes will be not collected as protocol members.
955EXCLUDED_ATTRIBUTES = _TYPING_INTERNALS + _SPECIAL_NAMES + ['_MutableMapping__marker']
956
957
958def _get_protocol_attrs(cls):
959 """Collect protocol members from a protocol class objects.
960
961 This includes names actually defined in the class dictionary, as well
962 as names that appear in annotations. Special names (above) are skipped.
963 """
964 attrs = set()
965 for base in cls.__mro__[:-1]: # without object
966 if base.__name__ in ('Protocol', 'Generic'):
967 continue
968 annotations = getattr(base, '__annotations__', {})
969 for attr in list(base.__dict__.keys()) + list(annotations.keys()):
970 if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
971 attrs.add(attr)
972 return attrs
973
974
975def _is_callable_members_only(cls):
976 # PEP 544 prohibits using issubclass() with protocols that have non-method members.
977 return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls))
978
979
980def _no_init(self, *args, **kwargs):
981 if type(self)._is_protocol:
982 raise TypeError('Protocols cannot be instantiated')
983
984
985def _allow_reckless_class_cheks():
Nickolena Fishercfaf4c02020-04-26 12:49:11 -0500986 """Allow instance and class checks for special stdlib modules.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100987
988 The abc and functools modules indiscriminately call isinstance() and
989 issubclass() on the whole MRO of a user class, which may contain protocols.
990 """
991 try:
992 return sys._getframe(3).f_globals['__name__'] in ['abc', 'functools']
993 except (AttributeError, ValueError): # For platforms without _getframe().
994 return True
995
996
Divij Rajkumar692a0dc2019-09-12 11:13:51 +0100997_PROTO_WHITELIST = {
998 'collections.abc': [
999 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
1000 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
1001 ],
1002 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
1003}
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001004
1005
1006class _ProtocolMeta(ABCMeta):
1007 # This metaclass is really unfortunate and exists only because of
1008 # the lack of __instancehook__.
1009 def __instancecheck__(cls, instance):
1010 # We need this method for situations where attributes are
1011 # assigned in __init__.
1012 if ((not getattr(cls, '_is_protocol', False) or
1013 _is_callable_members_only(cls)) and
1014 issubclass(instance.__class__, cls)):
1015 return True
1016 if cls._is_protocol:
1017 if all(hasattr(instance, attr) and
1018 # All *methods* can be blocked by setting them to None.
1019 (not callable(getattr(cls, attr, None)) or
1020 getattr(instance, attr) is not None)
1021 for attr in _get_protocol_attrs(cls)):
1022 return True
1023 return super().__instancecheck__(instance)
1024
1025
1026class Protocol(Generic, metaclass=_ProtocolMeta):
1027 """Base class for protocol classes.
1028
1029 Protocol classes are defined as::
1030
1031 class Proto(Protocol):
1032 def meth(self) -> int:
1033 ...
1034
1035 Such classes are primarily used with static type checkers that recognize
1036 structural subtyping (static duck-typing), for example::
1037
1038 class C:
1039 def meth(self) -> int:
1040 return 0
1041
1042 def func(x: Proto) -> int:
1043 return x.meth()
1044
1045 func(C()) # Passes static type check
1046
1047 See PEP 544 for details. Protocol classes decorated with
1048 @typing.runtime_checkable act as simple-minded runtime protocols that check
1049 only the presence of given attributes, ignoring their type signatures.
1050 Protocol classes can be generic, they are defined as::
1051
1052 class GenProto(Protocol[T]):
1053 def meth(self) -> T:
1054 ...
1055 """
1056 __slots__ = ()
1057 _is_protocol = True
1058 _is_runtime_protocol = False
1059
1060 def __init_subclass__(cls, *args, **kwargs):
1061 super().__init_subclass__(*args, **kwargs)
1062
1063 # Determine if this is a protocol or a concrete subclass.
1064 if not cls.__dict__.get('_is_protocol', False):
1065 cls._is_protocol = any(b is Protocol for b in cls.__bases__)
1066
1067 # Set (or override) the protocol subclass hook.
1068 def _proto_hook(other):
1069 if not cls.__dict__.get('_is_protocol', False):
1070 return NotImplemented
1071
1072 # First, perform various sanity checks.
1073 if not getattr(cls, '_is_runtime_protocol', False):
1074 if _allow_reckless_class_cheks():
1075 return NotImplemented
1076 raise TypeError("Instance and class checks can only be used with"
1077 " @runtime_checkable protocols")
1078 if not _is_callable_members_only(cls):
1079 if _allow_reckless_class_cheks():
1080 return NotImplemented
1081 raise TypeError("Protocols with non-method members"
1082 " don't support issubclass()")
1083 if not isinstance(other, type):
1084 # Same error message as for issubclass(1, int).
1085 raise TypeError('issubclass() arg 1 must be a class')
1086
1087 # Second, perform the actual structural compatibility check.
1088 for attr in _get_protocol_attrs(cls):
1089 for base in other.__mro__:
1090 # Check if the members appears in the class dictionary...
1091 if attr in base.__dict__:
1092 if base.__dict__[attr] is None:
1093 return NotImplemented
1094 break
1095
1096 # ...or in annotations, if it is a sub-protocol.
1097 annotations = getattr(base, '__annotations__', {})
1098 if (isinstance(annotations, collections.abc.Mapping) and
1099 attr in annotations and
1100 issubclass(other, Generic) and other._is_protocol):
1101 break
1102 else:
1103 return NotImplemented
1104 return True
1105
1106 if '__subclasshook__' not in cls.__dict__:
1107 cls.__subclasshook__ = _proto_hook
1108
1109 # We have nothing more to do for non-protocols...
1110 if not cls._is_protocol:
1111 return
1112
1113 # ... otherwise check consistency of bases, and prohibit instantiation.
1114 for base in cls.__bases__:
1115 if not (base in (object, Generic) or
Divij Rajkumar692a0dc2019-09-12 11:13:51 +01001116 base.__module__ in _PROTO_WHITELIST and
1117 base.__name__ in _PROTO_WHITELIST[base.__module__] or
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001118 issubclass(base, Generic) and base._is_protocol):
1119 raise TypeError('Protocols can only inherit from other'
1120 ' protocols, got %r' % base)
1121 cls.__init__ = _no_init
1122
1123
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001124class _AnnotatedAlias(_GenericAlias, _root=True):
1125 """Runtime representation of an annotated type.
1126
1127 At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't'
1128 with extra annotations. The alias behaves like a normal typing alias,
1129 instantiating is the same as instantiating the underlying type, binding
1130 it to types is also the same.
1131 """
1132 def __init__(self, origin, metadata):
1133 if isinstance(origin, _AnnotatedAlias):
1134 metadata = origin.__metadata__ + metadata
1135 origin = origin.__origin__
1136 super().__init__(origin, origin)
1137 self.__metadata__ = metadata
1138
1139 def copy_with(self, params):
1140 assert len(params) == 1
1141 new_type = params[0]
1142 return _AnnotatedAlias(new_type, self.__metadata__)
1143
1144 def __repr__(self):
1145 return "typing.Annotated[{}, {}]".format(
1146 _type_repr(self.__origin__),
1147 ", ".join(repr(a) for a in self.__metadata__)
1148 )
1149
1150 def __reduce__(self):
1151 return operator.getitem, (
1152 Annotated, (self.__origin__,) + self.__metadata__
1153 )
1154
1155 def __eq__(self, other):
1156 if not isinstance(other, _AnnotatedAlias):
1157 return NotImplemented
1158 if self.__origin__ != other.__origin__:
1159 return False
1160 return self.__metadata__ == other.__metadata__
1161
1162 def __hash__(self):
1163 return hash((self.__origin__, self.__metadata__))
1164
1165
1166class Annotated:
1167 """Add context specific metadata to a type.
1168
1169 Example: Annotated[int, runtime_check.Unsigned] indicates to the
1170 hypothetical runtime_check module that this type is an unsigned int.
1171 Every other consumer of this type can ignore this metadata and treat
1172 this type as int.
1173
1174 The first argument to Annotated must be a valid type.
1175
1176 Details:
1177
1178 - It's an error to call `Annotated` with less than two arguments.
1179 - Nested Annotated are flattened::
1180
1181 Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
1182
1183 - Instantiating an annotated type is equivalent to instantiating the
1184 underlying type::
1185
1186 Annotated[C, Ann1](5) == C(5)
1187
1188 - Annotated can be used as a generic type alias::
1189
1190 Optimized = Annotated[T, runtime.Optimize()]
1191 Optimized[int] == Annotated[int, runtime.Optimize()]
1192
1193 OptimizedList = Annotated[List[T], runtime.Optimize()]
1194 OptimizedList[int] == Annotated[List[int], runtime.Optimize()]
1195 """
1196
1197 __slots__ = ()
1198
1199 def __new__(cls, *args, **kwargs):
1200 raise TypeError("Type Annotated cannot be instantiated.")
1201
1202 @_tp_cache
1203 def __class_getitem__(cls, params):
1204 if not isinstance(params, tuple) or len(params) < 2:
1205 raise TypeError("Annotated[...] should be used "
1206 "with at least two arguments (a type and an "
1207 "annotation).")
1208 msg = "Annotated[t, ...]: t must be a type."
1209 origin = _type_check(params[0], msg)
1210 metadata = tuple(params[1:])
1211 return _AnnotatedAlias(origin, metadata)
1212
1213 def __init_subclass__(cls, *args, **kwargs):
1214 raise TypeError(
1215 "Cannot subclass {}.Annotated".format(cls.__module__)
1216 )
1217
1218
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001219def runtime_checkable(cls):
1220 """Mark a protocol class as a runtime protocol.
1221
1222 Such protocol can be used with isinstance() and issubclass().
1223 Raise TypeError if applied to a non-protocol class.
1224 This allows a simple-minded structural check very similar to
1225 one trick ponies in collections.abc such as Iterable.
1226 For example::
1227
1228 @runtime_checkable
1229 class Closable(Protocol):
1230 def close(self): ...
1231
1232 assert isinstance(open('/some/file'), Closable)
1233
1234 Warning: this will check only the presence of the required methods,
1235 not their type signatures!
1236 """
1237 if not issubclass(cls, Generic) or not cls._is_protocol:
1238 raise TypeError('@runtime_checkable can be only applied to protocol classes,'
1239 ' got %r' % cls)
1240 cls._is_runtime_protocol = True
1241 return cls
1242
1243
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001244def cast(typ, val):
1245 """Cast a value to a type.
1246
1247 This returns the value unchanged. To the type checker this
1248 signals that the return value has the designated type, but at
1249 runtime we intentionally don't check anything (we want this
1250 to be as fast as possible).
1251 """
1252 return val
1253
1254
1255def _get_defaults(func):
1256 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -08001257 try:
1258 code = func.__code__
1259 except AttributeError:
1260 # Some built-in functions don't have __code__, __defaults__, etc.
1261 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001262 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001263 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001264 arg_names = arg_names[:pos_count]
1265 defaults = func.__defaults__ or ()
1266 kwdefaults = func.__kwdefaults__
1267 res = dict(kwdefaults) if kwdefaults else {}
1268 pos_offset = pos_count - len(defaults)
1269 for name, value in zip(arg_names[pos_offset:], defaults):
1270 assert name not in res
1271 res[name] = value
1272 return res
1273
1274
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001275_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
1276 types.MethodType, types.ModuleType,
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02001277 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001278
1279
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001280def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
Guido van Rossum991d14f2016-11-09 13:12:51 -08001281 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001282
Guido van Rossum991d14f2016-11-09 13:12:51 -08001283 This is often the same as obj.__annotations__, but it handles
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001284 forward references encoded as string literals, adds Optional[t] if a
1285 default value equal to None is set and recursively replaces all
1286 'Annotated[T, ...]' with 'T' (unless 'include_extras=True').
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001287
Guido van Rossum991d14f2016-11-09 13:12:51 -08001288 The argument may be a module, class, method, or function. The annotations
1289 are returned as a dictionary. For classes, annotations include also
1290 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001291
Guido van Rossum991d14f2016-11-09 13:12:51 -08001292 TypeError is raised if the argument is not of a type that can contain
1293 annotations, and an empty dictionary is returned if no annotations are
1294 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001295
Guido van Rossum991d14f2016-11-09 13:12:51 -08001296 BEWARE -- the behavior of globalns and localns is counterintuitive
1297 (unless you are familiar with how eval() and exec() work). The
1298 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001299
Guido van Rossum991d14f2016-11-09 13:12:51 -08001300 - If no dict arguments are passed, an attempt is made to use the
Łukasz Langaf350a262017-09-14 14:33:00 -04001301 globals from obj (or the respective module's globals for classes),
1302 and these are also used as the locals. If the object does not appear
1303 to have globals, an empty dictionary is used.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001304
Guido van Rossum991d14f2016-11-09 13:12:51 -08001305 - If one dict argument is passed, it is used for both globals and
1306 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001307
Guido van Rossum991d14f2016-11-09 13:12:51 -08001308 - If two dict arguments are passed, they specify globals and
1309 locals, respectively.
1310 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001311
Guido van Rossum991d14f2016-11-09 13:12:51 -08001312 if getattr(obj, '__no_type_check__', None):
1313 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001314 # Classes require a special treatment.
1315 if isinstance(obj, type):
1316 hints = {}
1317 for base in reversed(obj.__mro__):
Łukasz Langaf350a262017-09-14 14:33:00 -04001318 if globalns is None:
1319 base_globals = sys.modules[base.__module__].__dict__
1320 else:
1321 base_globals = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001322 ann = base.__dict__.get('__annotations__', {})
1323 for name, value in ann.items():
1324 if value is None:
1325 value = type(None)
1326 if isinstance(value, str):
Nina Zakharenko0e61dff2018-05-22 20:32:10 -07001327 value = ForwardRef(value, is_argument=False)
Łukasz Langaf350a262017-09-14 14:33:00 -04001328 value = _eval_type(value, base_globals, localns)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001329 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001330 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
Łukasz Langaf350a262017-09-14 14:33:00 -04001331
1332 if globalns is None:
1333 if isinstance(obj, types.ModuleType):
1334 globalns = obj.__dict__
1335 else:
benedwards140aca3a32019-11-21 17:24:58 +00001336 nsobj = obj
1337 # Find globalns for the unwrapped object.
1338 while hasattr(nsobj, '__wrapped__'):
1339 nsobj = nsobj.__wrapped__
1340 globalns = getattr(nsobj, '__globals__', {})
Łukasz Langaf350a262017-09-14 14:33:00 -04001341 if localns is None:
1342 localns = globalns
1343 elif localns is None:
1344 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001345 hints = getattr(obj, '__annotations__', None)
1346 if hints is None:
1347 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001348 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001349 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001350 else:
1351 raise TypeError('{!r} is not a module, class, method, '
1352 'or function.'.format(obj))
1353 defaults = _get_defaults(obj)
1354 hints = dict(hints)
1355 for name, value in hints.items():
1356 if value is None:
1357 value = type(None)
1358 if isinstance(value, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001359 value = ForwardRef(value)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001360 value = _eval_type(value, globalns, localns)
1361 if name in defaults and defaults[name] is None:
1362 value = Optional[value]
1363 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001364 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
1365
1366
1367def _strip_annotations(t):
1368 """Strips the annotations from a given type.
1369 """
1370 if isinstance(t, _AnnotatedAlias):
1371 return _strip_annotations(t.__origin__)
1372 if isinstance(t, _GenericAlias):
1373 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1374 if stripped_args == t.__args__:
1375 return t
1376 res = t.copy_with(stripped_args)
1377 res._special = t._special
1378 return res
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001379 if isinstance(t, GenericAlias):
1380 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1381 if stripped_args == t.__args__:
1382 return t
1383 return GenericAlias(t.__origin__, stripped_args)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001384 return t
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001385
1386
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001387def get_origin(tp):
1388 """Get the unsubscripted version of a type.
1389
Jakub Stasiak38aaaaa2020-02-07 02:15:12 +01001390 This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
1391 and Annotated. Return None for unsupported types. Examples::
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001392
1393 get_origin(Literal[42]) is Literal
1394 get_origin(int) is None
1395 get_origin(ClassVar[int]) is ClassVar
1396 get_origin(Generic) is Generic
1397 get_origin(Generic[T]) is Generic
1398 get_origin(Union[T, int]) is Union
1399 get_origin(List[Tuple[T, T]][int]) == list
1400 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001401 if isinstance(tp, _AnnotatedAlias):
1402 return Annotated
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001403 if isinstance(tp, (_GenericAlias, GenericAlias)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001404 return tp.__origin__
1405 if tp is Generic:
1406 return Generic
1407 return None
1408
1409
1410def get_args(tp):
1411 """Get type arguments with all substitutions performed.
1412
1413 For unions, basic simplifications used by Union constructor are performed.
1414 Examples::
1415 get_args(Dict[str, int]) == (str, int)
1416 get_args(int) == ()
1417 get_args(Union[int, Union[T, int], str][int]) == (int, str)
1418 get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
1419 get_args(Callable[[], T][int]) == ([], int)
1420 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001421 if isinstance(tp, _AnnotatedAlias):
1422 return (tp.__origin__,) + tp.__metadata__
Serhiy Storchaka6292be72020-04-27 10:27:21 +03001423 if isinstance(tp, _GenericAlias) and not tp._special:
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001424 res = tp.__args__
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001425 if tp.__origin__ is collections.abc.Callable and res[0] is not Ellipsis:
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001426 res = (list(res[:-1]), res[-1])
1427 return res
Serhiy Storchaka6292be72020-04-27 10:27:21 +03001428 if isinstance(tp, GenericAlias):
1429 return tp.__args__
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001430 return ()
1431
1432
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001433def no_type_check(arg):
1434 """Decorator to indicate that annotations are not type hints.
1435
1436 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001437 applies recursively to all methods and classes defined in that class
1438 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001439
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001440 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001441 """
1442 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001443 arg_attrs = arg.__dict__.copy()
1444 for attr, val in arg.__dict__.items():
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001445 if val in arg.__bases__ + (arg,):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001446 arg_attrs.pop(attr)
1447 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001448 if isinstance(obj, types.FunctionType):
1449 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001450 if isinstance(obj, type):
1451 no_type_check(obj)
1452 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001453 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001454 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001455 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001456 return arg
1457
1458
1459def no_type_check_decorator(decorator):
1460 """Decorator to give another decorator the @no_type_check effect.
1461
1462 This wraps the decorator with something that wraps the decorated
1463 function in @no_type_check.
1464 """
1465
1466 @functools.wraps(decorator)
1467 def wrapped_decorator(*args, **kwds):
1468 func = decorator(*args, **kwds)
1469 func = no_type_check(func)
1470 return func
1471
1472 return wrapped_decorator
1473
1474
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001475def _overload_dummy(*args, **kwds):
1476 """Helper for @overload to raise when called."""
1477 raise NotImplementedError(
1478 "You should not call an overloaded function. "
1479 "A series of @overload-decorated functions "
1480 "outside a stub module should always be followed "
1481 "by an implementation that is not @overload-ed.")
1482
1483
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001484def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001485 """Decorator for overloaded functions/methods.
1486
1487 In a stub file, place two or more stub definitions for the same
1488 function in a row, each decorated with @overload. For example:
1489
1490 @overload
1491 def utf8(value: None) -> None: ...
1492 @overload
1493 def utf8(value: bytes) -> bytes: ...
1494 @overload
1495 def utf8(value: str) -> bytes: ...
1496
1497 In a non-stub file (i.e. a regular .py file), do the same but
1498 follow it with an implementation. The implementation should *not*
1499 be decorated with @overload. For example:
1500
1501 @overload
1502 def utf8(value: None) -> None: ...
1503 @overload
1504 def utf8(value: bytes) -> bytes: ...
1505 @overload
1506 def utf8(value: str) -> bytes: ...
1507 def utf8(value):
1508 # implementation goes here
1509 """
1510 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001511
1512
Ivan Levkivskyif3672422019-05-26 09:37:07 +01001513def final(f):
1514 """A decorator to indicate final methods and final classes.
1515
1516 Use this decorator to indicate to type checkers that the decorated
1517 method cannot be overridden, and decorated class cannot be subclassed.
1518 For example:
1519
1520 class Base:
1521 @final
1522 def done(self) -> None:
1523 ...
1524 class Sub(Base):
1525 def done(self) -> None: # Error reported by type checker
1526 ...
1527
1528 @final
1529 class Leaf:
1530 ...
1531 class Other(Leaf): # Error reported by type checker
1532 ...
1533
1534 There is no runtime checking of these properties.
1535 """
1536 return f
1537
1538
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001539# Some unconstrained type variables. These are used by the container types.
1540# (These are not for export.)
1541T = TypeVar('T') # Any type.
1542KT = TypeVar('KT') # Key type.
1543VT = TypeVar('VT') # Value type.
1544T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
1545V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
1546VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
1547T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
1548# Internal type variable used for Type[].
1549CT_co = TypeVar('CT_co', covariant=True, bound=type)
1550
1551# A useful type variable with constraints. This represents string types.
1552# (This one *is* for export!)
1553AnyStr = TypeVar('AnyStr', bytes, str)
1554
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001555
1556# Various ABCs mimicking those in collections.abc.
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001557def _alias(origin, params, inst=True):
1558 return _GenericAlias(origin, params, special=True, inst=inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001559
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001560Hashable = _alias(collections.abc.Hashable, ()) # Not generic.
1561Awaitable = _alias(collections.abc.Awaitable, T_co)
1562Coroutine = _alias(collections.abc.Coroutine, (T_co, T_contra, V_co))
1563AsyncIterable = _alias(collections.abc.AsyncIterable, T_co)
1564AsyncIterator = _alias(collections.abc.AsyncIterator, T_co)
1565Iterable = _alias(collections.abc.Iterable, T_co)
1566Iterator = _alias(collections.abc.Iterator, T_co)
1567Reversible = _alias(collections.abc.Reversible, T_co)
1568Sized = _alias(collections.abc.Sized, ()) # Not generic.
1569Container = _alias(collections.abc.Container, T_co)
1570Collection = _alias(collections.abc.Collection, T_co)
1571Callable = _VariadicGenericAlias(collections.abc.Callable, (), special=True)
1572Callable.__doc__ = \
1573 """Callable type; Callable[[int], str] is a function of (int) -> str.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001574
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001575 The subscription syntax must always be used with exactly two
1576 values: the argument list and the return type. The argument list
1577 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001578
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001579 There is no syntax to indicate optional or keyword arguments,
1580 such function types are rarely used as callback types.
1581 """
1582AbstractSet = _alias(collections.abc.Set, T_co)
1583MutableSet = _alias(collections.abc.MutableSet, T)
1584# NOTE: Mapping is only covariant in the value type.
1585Mapping = _alias(collections.abc.Mapping, (KT, VT_co))
1586MutableMapping = _alias(collections.abc.MutableMapping, (KT, VT))
1587Sequence = _alias(collections.abc.Sequence, T_co)
1588MutableSequence = _alias(collections.abc.MutableSequence, T)
1589ByteString = _alias(collections.abc.ByteString, ()) # Not generic
1590Tuple = _VariadicGenericAlias(tuple, (), inst=False, special=True)
1591Tuple.__doc__ = \
1592 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001593
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001594 Example: Tuple[T1, T2] is a tuple of two elements corresponding
1595 to type variables T1 and T2. Tuple[int, float, str] is a tuple
1596 of an int, a float and a string.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001597
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001598 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
1599 """
1600List = _alias(list, T, inst=False)
1601Deque = _alias(collections.deque, T)
1602Set = _alias(set, T, inst=False)
1603FrozenSet = _alias(frozenset, T_co, inst=False)
1604MappingView = _alias(collections.abc.MappingView, T_co)
1605KeysView = _alias(collections.abc.KeysView, KT)
1606ItemsView = _alias(collections.abc.ItemsView, (KT, VT_co))
1607ValuesView = _alias(collections.abc.ValuesView, VT_co)
1608ContextManager = _alias(contextlib.AbstractContextManager, T_co)
1609AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, T_co)
1610Dict = _alias(dict, (KT, VT), inst=False)
1611DefaultDict = _alias(collections.defaultdict, (KT, VT))
Ismo Toijala68b56d02018-12-02 17:53:14 +02001612OrderedDict = _alias(collections.OrderedDict, (KT, VT))
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001613Counter = _alias(collections.Counter, T)
1614ChainMap = _alias(collections.ChainMap, (KT, VT))
1615Generator = _alias(collections.abc.Generator, (T_co, T_contra, V_co))
1616AsyncGenerator = _alias(collections.abc.AsyncGenerator, (T_co, T_contra))
1617Type = _alias(type, CT_co, inst=False)
1618Type.__doc__ = \
1619 """A special construct usable to annotate class objects.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001620
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001621 For example, suppose we have the following classes::
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001622
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001623 class User: ... # Abstract base for User classes
1624 class BasicUser(User): ...
1625 class ProUser(User): ...
1626 class TeamUser(User): ...
Guido van Rossumf17c2002015-12-03 17:31:24 -08001627
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001628 And a function that takes a class argument that's a subclass of
1629 User and returns an instance of the corresponding class::
Guido van Rossumf17c2002015-12-03 17:31:24 -08001630
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001631 U = TypeVar('U', bound=User)
1632 def new_user(user_class: Type[U]) -> U:
1633 user = user_class()
1634 # (Here we could write the user object to a database)
1635 return user
Guido van Rossumf17c2002015-12-03 17:31:24 -08001636
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001637 joe = new_user(BasicUser)
Guido van Rossumf17c2002015-12-03 17:31:24 -08001638
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001639 At this point the type checker knows that joe has type BasicUser.
1640 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001641
1642
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001643@runtime_checkable
1644class SupportsInt(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001645 """An ABC with one abstract method __int__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001646 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001647
1648 @abstractmethod
1649 def __int__(self) -> int:
1650 pass
1651
1652
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001653@runtime_checkable
1654class SupportsFloat(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001655 """An ABC with one abstract method __float__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001656 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001657
1658 @abstractmethod
1659 def __float__(self) -> float:
1660 pass
1661
1662
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001663@runtime_checkable
1664class SupportsComplex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001665 """An ABC with one abstract method __complex__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001666 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001667
1668 @abstractmethod
1669 def __complex__(self) -> complex:
1670 pass
1671
1672
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001673@runtime_checkable
1674class SupportsBytes(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001675 """An ABC with one abstract method __bytes__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001676 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001677
1678 @abstractmethod
1679 def __bytes__(self) -> bytes:
1680 pass
1681
1682
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001683@runtime_checkable
1684class SupportsIndex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001685 """An ABC with one abstract method __index__."""
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -07001686 __slots__ = ()
1687
1688 @abstractmethod
1689 def __index__(self) -> int:
1690 pass
1691
1692
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001693@runtime_checkable
1694class SupportsAbs(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001695 """An ABC with one abstract method __abs__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001696 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001697
1698 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001699 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001700 pass
1701
1702
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001703@runtime_checkable
1704class SupportsRound(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001705 """An ABC with one abstract method __round__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001706 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001707
1708 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001709 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001710 pass
1711
1712
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001713def _make_nmtuple(name, types, module, defaults = ()):
1714 fields = [n for n, t in types]
1715 types = {n: _type_check(t, f"field {n} annotation must be a type")
1716 for n, t in types}
1717 nm_tpl = collections.namedtuple(name, fields,
1718 defaults=defaults, module=module)
1719 nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001720 return nm_tpl
1721
1722
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001723# attributes prohibited to set in NamedTuple class syntax
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001724_prohibited = frozenset({'__new__', '__init__', '__slots__', '__getnewargs__',
1725 '_fields', '_field_defaults',
1726 '_make', '_replace', '_asdict', '_source'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001727
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001728_special = frozenset({'__module__', '__name__', '__annotations__'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001729
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001730
Guido van Rossum2f841442016-11-15 09:48:06 -08001731class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001732
Guido van Rossum2f841442016-11-15 09:48:06 -08001733 def __new__(cls, typename, bases, ns):
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001734 assert bases[0] is _NamedTuple
Guido van Rossum2f841442016-11-15 09:48:06 -08001735 types = ns.get('__annotations__', {})
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001736 default_names = []
Guido van Rossum3c268be2017-01-18 08:03:50 -08001737 for field_name in types:
1738 if field_name in ns:
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001739 default_names.append(field_name)
1740 elif default_names:
1741 raise TypeError(f"Non-default namedtuple field {field_name} "
1742 f"cannot follow default field"
1743 f"{'s' if len(default_names) > 1 else ''} "
1744 f"{', '.join(default_names)}")
1745 nm_tpl = _make_nmtuple(typename, types.items(),
1746 defaults=[ns[n] for n in default_names],
1747 module=ns['__module__'])
Guido van Rossum95919c02017-01-22 17:47:20 -08001748 # update from user namespace without overriding special namedtuple attributes
1749 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001750 if key in _prohibited:
1751 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
1752 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08001753 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08001754 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001755
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001756
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001757def NamedTuple(typename, fields=None, /, **kwargs):
Guido van Rossum2f841442016-11-15 09:48:06 -08001758 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001759
Guido van Rossum2f841442016-11-15 09:48:06 -08001760 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001761
Guido van Rossum2f841442016-11-15 09:48:06 -08001762 class Employee(NamedTuple):
1763 name: str
1764 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001765
Guido van Rossum2f841442016-11-15 09:48:06 -08001766 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001767
Guido van Rossum2f841442016-11-15 09:48:06 -08001768 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001769
Raymond Hettingerf7b57df2019-03-18 09:53:56 -07001770 The resulting class has an extra __annotations__ attribute, giving a
1771 dict that maps field names to types. (The field names are also in
1772 the _fields attribute, which is part of the namedtuple API.)
1773 Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001774
Guido van Rossum2f841442016-11-15 09:48:06 -08001775 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001776
Guido van Rossum2f841442016-11-15 09:48:06 -08001777 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001778
Guido van Rossum2f841442016-11-15 09:48:06 -08001779 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
1780 """
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001781 if fields is None:
1782 fields = kwargs.items()
1783 elif kwargs:
1784 raise TypeError("Either list of fields or keywords"
1785 " can be provided to NamedTuple, not both")
1786 try:
1787 module = sys._getframe(1).f_globals.get('__name__', '__main__')
1788 except (AttributeError, ValueError):
1789 module = None
1790 return _make_nmtuple(typename, fields, module=module)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001791
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001792_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})
1793
1794def _namedtuple_mro_entries(bases):
1795 if len(bases) > 1:
1796 raise TypeError("Multiple inheritance with NamedTuple is not supported")
1797 assert bases[0] is NamedTuple
1798 return (_NamedTuple,)
1799
1800NamedTuple.__mro_entries__ = _namedtuple_mro_entries
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001801
1802
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001803class _TypedDictMeta(type):
1804 def __new__(cls, name, bases, ns, total=True):
1805 """Create new typed dict class object.
1806
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001807 This method is called when TypedDict is subclassed,
1808 or when TypedDict is instantiated. This way
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001809 TypedDict supports all three syntax forms described in its docstring.
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001810 Subclasses and instances of TypedDict return actual dictionaries.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001811 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001812 for base in bases:
1813 if type(base) is not _TypedDictMeta:
1814 raise TypeError('cannot inherit from both a TypedDict type '
1815 'and a non-TypedDict base class')
1816 tp_dict = type.__new__(_TypedDictMeta, name, (dict,), ns)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001817
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001818 annotations = {}
1819 own_annotations = ns.get('__annotations__', {})
1820 own_annotation_keys = set(own_annotations.keys())
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001821 msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001822 own_annotations = {
1823 n: _type_check(tp, msg) for n, tp in own_annotations.items()
1824 }
1825 required_keys = set()
1826 optional_keys = set()
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11001827
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001828 for base in bases:
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001829 annotations.update(base.__dict__.get('__annotations__', {}))
1830 required_keys.update(base.__dict__.get('__required_keys__', ()))
1831 optional_keys.update(base.__dict__.get('__optional_keys__', ()))
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11001832
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001833 annotations.update(own_annotations)
1834 if total:
1835 required_keys.update(own_annotation_keys)
1836 else:
1837 optional_keys.update(own_annotation_keys)
1838
1839 tp_dict.__annotations__ = annotations
1840 tp_dict.__required_keys__ = frozenset(required_keys)
1841 tp_dict.__optional_keys__ = frozenset(optional_keys)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001842 if not hasattr(tp_dict, '__total__'):
1843 tp_dict.__total__ = total
1844 return tp_dict
1845
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001846 __call__ = dict # static method
1847
1848 def __subclasscheck__(cls, other):
1849 # Typed dicts are only for static structural subtyping.
1850 raise TypeError('TypedDict does not support instance and class checks')
1851
1852 __instancecheck__ = __subclasscheck__
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001853
1854
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001855def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001856 """A simple typed namespace. At runtime it is equivalent to a plain dict.
1857
1858 TypedDict creates a dictionary type that expects all of its
1859 instances to have a certain set of keys, where each key is
1860 associated with a value of a consistent type. This expectation
1861 is not checked at runtime but is only enforced by type checkers.
1862 Usage::
1863
1864 class Point2D(TypedDict):
1865 x: int
1866 y: int
1867 label: str
1868
1869 a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
1870 b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
1871
1872 assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
1873
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11001874 The type info can be accessed via the Point2D.__annotations__ dict, and
1875 the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
1876 TypedDict supports two additional equivalent forms::
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001877
1878 Point2D = TypedDict('Point2D', x=int, y=int, label=str)
1879 Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
1880
ananthan-123ab6423f2020-02-19 10:03:05 +05301881 By default, all keys must be present in a TypedDict. It is possible
1882 to override this by specifying totality.
1883 Usage::
1884
1885 class point2D(TypedDict, total=False):
1886 x: int
1887 y: int
1888
1889 This means that a point2D TypedDict can have any of the keys omitted.A type
1890 checker is only expected to support a literal False or True as the value of
1891 the total argument. True is the default, and makes all items defined in the
1892 class body be required.
1893
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001894 The class syntax is only supported in Python 3.6+, while two other
1895 syntax forms work for Python 2.7 and 3.2+
1896 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001897 if fields is None:
1898 fields = kwargs
1899 elif kwargs:
1900 raise TypeError("TypedDict takes either a dict or keyword arguments,"
1901 " but not both")
1902
1903 ns = {'__annotations__': dict(fields), '__total__': total}
1904 try:
1905 # Setting correct module is necessary to make typed dict classes pickleable.
1906 ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
1907 except (AttributeError, ValueError):
1908 pass
1909
1910 return _TypedDictMeta(typename, (), ns)
1911
1912_TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {})
1913TypedDict.__mro_entries__ = lambda bases: (_TypedDict,)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001914
1915
Guido van Rossum91185fe2016-06-08 11:19:11 -07001916def NewType(name, tp):
1917 """NewType creates simple unique types with almost zero
1918 runtime overhead. NewType(name, tp) is considered a subtype of tp
1919 by static type checkers. At runtime, NewType(name, tp) returns
1920 a dummy function that simply returns its argument. Usage::
1921
1922 UserId = NewType('UserId', int)
1923
1924 def name_by_id(user_id: UserId) -> str:
1925 ...
1926
1927 UserId('user') # Fails type check
1928
1929 name_by_id(42) # Fails type check
1930 name_by_id(UserId(42)) # OK
1931
1932 num = UserId(5) + 1 # type: int
1933 """
1934
1935 def new_type(x):
1936 return x
1937
1938 new_type.__name__ = name
1939 new_type.__supertype__ = tp
1940 return new_type
1941
1942
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001943# Python-version-specific alias (Python 2: unicode; Python 3: str)
1944Text = str
1945
1946
Guido van Rossum91185fe2016-06-08 11:19:11 -07001947# Constant that's True when type checking, but False here.
1948TYPE_CHECKING = False
1949
1950
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001951class IO(Generic[AnyStr]):
1952 """Generic base class for TextIO and BinaryIO.
1953
1954 This is an abstract, generic version of the return of open().
1955
1956 NOTE: This does not distinguish between the different possible
1957 classes (text vs. binary, read vs. write vs. read/write,
1958 append-only, unbuffered). The TextIO and BinaryIO subclasses
1959 below capture the distinctions between text vs. binary, which is
1960 pervasive in the interface; however we currently do not offer a
1961 way to track the other distinctions in the type system.
1962 """
1963
Guido van Rossumd70fe632015-08-05 12:11:06 +02001964 __slots__ = ()
1965
HongWeipeng6ce03ec2019-09-27 15:54:26 +08001966 @property
1967 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001968 def mode(self) -> str:
1969 pass
1970
HongWeipeng6ce03ec2019-09-27 15:54:26 +08001971 @property
1972 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001973 def name(self) -> str:
1974 pass
1975
1976 @abstractmethod
1977 def close(self) -> None:
1978 pass
1979
Shantanu2e6569b2020-01-29 18:52:36 -08001980 @property
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001981 @abstractmethod
1982 def closed(self) -> bool:
1983 pass
1984
1985 @abstractmethod
1986 def fileno(self) -> int:
1987 pass
1988
1989 @abstractmethod
1990 def flush(self) -> None:
1991 pass
1992
1993 @abstractmethod
1994 def isatty(self) -> bool:
1995 pass
1996
1997 @abstractmethod
1998 def read(self, n: int = -1) -> AnyStr:
1999 pass
2000
2001 @abstractmethod
2002 def readable(self) -> bool:
2003 pass
2004
2005 @abstractmethod
2006 def readline(self, limit: int = -1) -> AnyStr:
2007 pass
2008
2009 @abstractmethod
2010 def readlines(self, hint: int = -1) -> List[AnyStr]:
2011 pass
2012
2013 @abstractmethod
2014 def seek(self, offset: int, whence: int = 0) -> int:
2015 pass
2016
2017 @abstractmethod
2018 def seekable(self) -> bool:
2019 pass
2020
2021 @abstractmethod
2022 def tell(self) -> int:
2023 pass
2024
2025 @abstractmethod
2026 def truncate(self, size: int = None) -> int:
2027 pass
2028
2029 @abstractmethod
2030 def writable(self) -> bool:
2031 pass
2032
2033 @abstractmethod
2034 def write(self, s: AnyStr) -> int:
2035 pass
2036
2037 @abstractmethod
2038 def writelines(self, lines: List[AnyStr]) -> None:
2039 pass
2040
2041 @abstractmethod
2042 def __enter__(self) -> 'IO[AnyStr]':
2043 pass
2044
2045 @abstractmethod
2046 def __exit__(self, type, value, traceback) -> None:
2047 pass
2048
2049
2050class BinaryIO(IO[bytes]):
2051 """Typed version of the return of open() in binary mode."""
2052
Guido van Rossumd70fe632015-08-05 12:11:06 +02002053 __slots__ = ()
2054
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002055 @abstractmethod
2056 def write(self, s: Union[bytes, bytearray]) -> int:
2057 pass
2058
2059 @abstractmethod
2060 def __enter__(self) -> 'BinaryIO':
2061 pass
2062
2063
2064class TextIO(IO[str]):
2065 """Typed version of the return of open() in text mode."""
2066
Guido van Rossumd70fe632015-08-05 12:11:06 +02002067 __slots__ = ()
2068
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002069 @property
2070 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002071 def buffer(self) -> BinaryIO:
2072 pass
2073
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002074 @property
2075 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002076 def encoding(self) -> str:
2077 pass
2078
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002079 @property
2080 @abstractmethod
Guido van Rossum991d14f2016-11-09 13:12:51 -08002081 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002082 pass
2083
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002084 @property
2085 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002086 def line_buffering(self) -> bool:
2087 pass
2088
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002089 @property
2090 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002091 def newlines(self) -> Any:
2092 pass
2093
2094 @abstractmethod
2095 def __enter__(self) -> 'TextIO':
2096 pass
2097
2098
2099class io:
2100 """Wrapper namespace for IO generic classes."""
2101
2102 __all__ = ['IO', 'TextIO', 'BinaryIO']
2103 IO = IO
2104 TextIO = TextIO
2105 BinaryIO = BinaryIO
2106
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002107
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002108io.__name__ = __name__ + '.io'
2109sys.modules[io.__name__] = io
2110
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002111Pattern = _alias(stdlib_re.Pattern, AnyStr)
2112Match = _alias(stdlib_re.Match, AnyStr)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002113
2114class re:
2115 """Wrapper namespace for re type aliases."""
2116
2117 __all__ = ['Pattern', 'Match']
2118 Pattern = Pattern
2119 Match = Match
2120
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002121
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002122re.__name__ = __name__ + '.re'
2123sys.modules[re.__name__] = re