blob: 6fd67b038834e1c61b38852d3277ca8c5086c907 [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 """
Miss Islington (bot)e81e09b2020-11-07 08:46:08 -0800163 if isinstance(obj, types.GenericAlias):
164 return repr(obj)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000165 if isinstance(obj, type):
166 if obj.__module__ == 'builtins':
167 return obj.__qualname__
168 return f'{obj.__module__}.{obj.__qualname__}'
169 if obj is ...:
170 return('...')
171 if isinstance(obj, types.FunctionType):
172 return obj.__name__
173 return repr(obj)
174
175
176def _collect_type_vars(types):
177 """Collect all type variable contained in types in order of
178 first appearance (lexicographic order). For example::
179
180 _collect_type_vars((T, List[S, T])) == (T, S)
181 """
182 tvars = []
183 for t in types:
184 if isinstance(t, TypeVar) and t not in tvars:
185 tvars.append(t)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300186 if isinstance(t, (_GenericAlias, GenericAlias)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000187 tvars.extend([t for t in t.__parameters__ if t not in tvars])
188 return tuple(tvars)
189
190
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300191def _check_generic(cls, parameters, elen):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000192 """Check correct count for parameters of a generic cls (internal helper).
193 This gives a nice error message in case of count mismatch.
194 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300195 if not elen:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000196 raise TypeError(f"{cls} is not a generic class")
197 alen = len(parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000198 if alen != elen:
199 raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
200 f" actual {alen}, expected {elen}")
201
202
203def _remove_dups_flatten(parameters):
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700204 """An internal helper for Union creation and substitution: flatten Unions
205 among parameters, then remove duplicates.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000206 """
207 # Flatten out Union[Union[...], ...].
208 params = []
209 for p in parameters:
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300210 if isinstance(p, _UnionGenericAlias):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000211 params.extend(p.__args__)
212 elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
213 params.extend(p[1:])
214 else:
215 params.append(p)
216 # Weed out strict duplicates, preserving the first of each occurrence.
217 all_params = set(params)
218 if len(all_params) < len(params):
219 new_params = []
220 for t in params:
221 if t in all_params:
222 new_params.append(t)
223 all_params.remove(t)
224 params = new_params
225 assert not all_params, all_params
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700226 return tuple(params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000227
228
229_cleanups = []
230
231
232def _tp_cache(func):
233 """Internal wrapper caching __getitem__ of generic types with a fallback to
234 original function for non-hashable arguments.
235 """
236 cached = functools.lru_cache()(func)
237 _cleanups.append(cached.cache_clear)
238
239 @functools.wraps(func)
240 def inner(*args, **kwds):
241 try:
242 return cached(*args, **kwds)
243 except TypeError:
244 pass # All real errors (not unhashable args) are raised below.
245 return func(*args, **kwds)
246 return inner
247
248
Miss Islington (bot)41d1c042020-07-26 08:31:24 -0700249def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
Miss Islington (bot)6e770fc2020-09-08 16:36:07 -0700250 """Evaluate all forward references in the given type t.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000251 For use of globalns and localns see the docstring for get_type_hints().
Miss Islington (bot)41d1c042020-07-26 08:31:24 -0700252 recursive_guard is used to prevent prevent infinite recursion
253 with recursive ForwardRef.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000254 """
255 if isinstance(t, ForwardRef):
Miss Islington (bot)41d1c042020-07-26 08:31:24 -0700256 return t._evaluate(globalns, localns, recursive_guard)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300257 if isinstance(t, (_GenericAlias, GenericAlias)):
Miss Islington (bot)41d1c042020-07-26 08:31:24 -0700258 ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000259 if ev_args == t.__args__:
260 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300261 if isinstance(t, GenericAlias):
262 return GenericAlias(t.__origin__, ev_args)
263 else:
264 return t.copy_with(ev_args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000265 return t
266
267
268class _Final:
269 """Mixin to prohibit subclassing"""
Guido van Rossum4cefe742016-09-27 15:20:12 -0700270
Guido van Rossum83ec3022017-01-17 20:43:28 -0800271 __slots__ = ('__weakref__',)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700272
Serhiy Storchaka2085bd02019-06-01 11:00:15 +0300273 def __init_subclass__(self, /, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000274 if '_root' not in kwds:
275 raise TypeError("Cannot subclass special typing classes")
276
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100277class _Immutable:
278 """Mixin to indicate that object should not be copied."""
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300279 __slots__ = ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000280
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100281 def __copy__(self):
282 return self
283
284 def __deepcopy__(self, memo):
285 return self
286
287
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300288# Internal indicator of special typing constructs.
289# See __doc__ instance attribute for specific docs.
290class _SpecialForm(_Final, _root=True):
291 __slots__ = ('_name', '__doc__', '_getitem')
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000292
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300293 def __init__(self, getitem):
294 self._getitem = getitem
295 self._name = getitem.__name__
296 self.__doc__ = getitem.__doc__
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000297
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300298 def __mro_entries__(self, bases):
299 raise TypeError(f"Cannot subclass {self!r}")
Guido van Rossum4cefe742016-09-27 15:20:12 -0700300
301 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000302 return 'typing.' + self._name
303
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100304 def __reduce__(self):
305 return self._name
Guido van Rossum4cefe742016-09-27 15:20:12 -0700306
307 def __call__(self, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000308 raise TypeError(f"Cannot instantiate {self!r}")
309
310 def __instancecheck__(self, obj):
311 raise TypeError(f"{self} cannot be used with isinstance()")
312
313 def __subclasscheck__(self, cls):
314 raise TypeError(f"{self} cannot be used with issubclass()")
315
316 @_tp_cache
317 def __getitem__(self, parameters):
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300318 return self._getitem(self, parameters)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700319
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300320@_SpecialForm
321def Any(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000322 """Special type indicating an unconstrained type.
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700323
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000324 - Any is compatible with every type.
325 - Any assumed to have all methods.
326 - All values assumed to be instances of Any.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700327
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000328 Note that all the above statements are true from the point of view of
329 static type checkers. At runtime, Any should not be used with instance
330 or class checks.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300331 """
332 raise TypeError(f"{self} is not subscriptable")
Guido van Rossumd70fe632015-08-05 12:11:06 +0200333
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300334@_SpecialForm
335def NoReturn(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000336 """Special type indicating functions that never return.
337 Example::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700338
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000339 from typing import NoReturn
340
341 def stop() -> NoReturn:
342 raise Exception('no way')
343
344 This type is invalid in other positions, e.g., ``List[NoReturn]``
345 will fail in static type checkers.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300346 """
347 raise TypeError(f"{self} is not subscriptable")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000348
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300349@_SpecialForm
350def ClassVar(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000351 """Special type construct to mark class variables.
352
353 An annotation wrapped in ClassVar indicates that a given
354 attribute is intended to be used as a class variable and
355 should not be set on instances of that class. Usage::
356
357 class Starship:
358 stats: ClassVar[Dict[str, int]] = {} # class variable
359 damage: int = 10 # instance variable
360
361 ClassVar accepts only types and cannot be further subscribed.
362
363 Note that ClassVar is not a class itself, and should not
364 be used with isinstance() or issubclass().
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300365 """
366 item = _type_check(parameters, f'{self} accepts only single type.')
367 return _GenericAlias(self, (item,))
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000368
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300369@_SpecialForm
370def Final(self, parameters):
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100371 """Special typing construct to indicate final names to type checkers.
372
373 A final name cannot be re-assigned or overridden in a subclass.
374 For example:
375
376 MAX_SIZE: Final = 9000
377 MAX_SIZE += 1 # Error reported by type checker
378
379 class Connection:
380 TIMEOUT: Final[int] = 10
381
382 class FastConnector(Connection):
383 TIMEOUT = 1 # Error reported by type checker
384
385 There is no runtime checking of these properties.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300386 """
387 item = _type_check(parameters, f'{self} accepts only single type.')
388 return _GenericAlias(self, (item,))
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100389
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300390@_SpecialForm
391def Union(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000392 """Union type; Union[X, Y] means either X or Y.
393
394 To define a union, use e.g. Union[int, str]. Details:
395 - The arguments must be types and there must be at least one.
396 - None as an argument is a special case and is replaced by
397 type(None).
398 - Unions of unions are flattened, e.g.::
399
400 Union[Union[int, str], float] == Union[int, str, float]
401
402 - Unions of a single argument vanish, e.g.::
403
404 Union[int] == int # The constructor actually returns int
405
406 - Redundant arguments are skipped, e.g.::
407
408 Union[int, str, int] == Union[int, str]
409
410 - When comparing unions, the argument order is ignored, e.g.::
411
412 Union[int, str] == Union[str, int]
413
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000414 - You cannot subclass or instantiate a union.
415 - You can use Optional[X] as a shorthand for Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300416 """
417 if parameters == ():
418 raise TypeError("Cannot take a Union of no types.")
419 if not isinstance(parameters, tuple):
420 parameters = (parameters,)
421 msg = "Union[arg, ...]: each arg must be a type."
422 parameters = tuple(_type_check(p, msg) for p in parameters)
423 parameters = _remove_dups_flatten(parameters)
424 if len(parameters) == 1:
425 return parameters[0]
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300426 return _UnionGenericAlias(self, parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000427
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300428@_SpecialForm
429def Optional(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000430 """Optional type.
431
432 Optional[X] is equivalent to Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300433 """
434 arg = _type_check(parameters, f"{self} requires a single type.")
435 return Union[arg, type(None)]
Guido van Rossumb7dedc82016-10-29 12:44:29 -0700436
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300437@_SpecialForm
438def Literal(self, parameters):
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100439 """Special typing form to define literal types (a.k.a. value types).
440
441 This form can be used to indicate to type checkers that the corresponding
442 variable or function parameter has a value equivalent to the provided
443 literal (or one of several literals):
444
445 def validate_simple(data: Any) -> Literal[True]: # always returns True
446 ...
447
448 MODE = Literal['r', 'rb', 'w', 'wb']
449 def open_helper(file: str, mode: MODE) -> str:
450 ...
451
452 open_helper('/some/path', 'r') # Passes type check
453 open_helper('/other/path', 'typo') # Error in type checker
454
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300455 Literal[...] cannot be subclassed. At runtime, an arbitrary value
456 is allowed as type argument to Literal[...], but type checkers may
457 impose restrictions.
458 """
459 # There is no '_type_check' call because arguments to Literal[...] are
460 # values, not types.
461 return _GenericAlias(self, parameters)
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100462
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700463
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000464class ForwardRef(_Final, _root=True):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800465 """Internal wrapper to hold a forward reference."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700466
Guido van Rossum4cefe742016-09-27 15:20:12 -0700467 __slots__ = ('__forward_arg__', '__forward_code__',
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400468 '__forward_evaluated__', '__forward_value__',
469 '__forward_is_argument__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700470
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700471 def __init__(self, arg, is_argument=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700472 if not isinstance(arg, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000473 raise TypeError(f"Forward reference must be a string -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700474 try:
475 code = compile(arg, '<string>', 'eval')
476 except SyntaxError:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000477 raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700478 self.__forward_arg__ = arg
479 self.__forward_code__ = code
480 self.__forward_evaluated__ = False
481 self.__forward_value__ = None
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400482 self.__forward_is_argument__ = is_argument
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700483
Miss Islington (bot)41d1c042020-07-26 08:31:24 -0700484 def _evaluate(self, globalns, localns, recursive_guard):
485 if self.__forward_arg__ in recursive_guard:
486 return self
Guido van Rossumdad17902016-11-10 08:29:18 -0800487 if not self.__forward_evaluated__ or localns is not globalns:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700488 if globalns is None and localns is None:
489 globalns = localns = {}
490 elif globalns is None:
491 globalns = localns
492 elif localns is None:
493 localns = globalns
Miss Islington (bot)41d1c042020-07-26 08:31:24 -0700494 type_ =_type_check(
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700495 eval(self.__forward_code__, globalns, localns),
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400496 "Forward references must evaluate to types.",
Miss Islington (bot)41d1c042020-07-26 08:31:24 -0700497 is_argument=self.__forward_is_argument__,
498 )
499 self.__forward_value__ = _eval_type(
500 type_, globalns, localns, recursive_guard | {self.__forward_arg__}
501 )
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700502 self.__forward_evaluated__ = True
503 return self.__forward_value__
504
Guido van Rossum4cefe742016-09-27 15:20:12 -0700505 def __eq__(self, other):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000506 if not isinstance(other, ForwardRef):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700507 return NotImplemented
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100508 if self.__forward_evaluated__ and other.__forward_evaluated__:
509 return (self.__forward_arg__ == other.__forward_arg__ and
510 self.__forward_value__ == other.__forward_value__)
511 return self.__forward_arg__ == other.__forward_arg__
Guido van Rossum4cefe742016-09-27 15:20:12 -0700512
513 def __hash__(self):
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100514 return hash(self.__forward_arg__)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700515
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700516 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000517 return f'ForwardRef({self.__forward_arg__!r})'
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700518
519
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100520class TypeVar(_Final, _Immutable, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700521 """Type variable.
522
523 Usage::
524
525 T = TypeVar('T') # Can be anything
526 A = TypeVar('A', str, bytes) # Must be str or bytes
527
528 Type variables exist primarily for the benefit of static type
529 checkers. They serve as the parameters for generic types as well
530 as for generic function definitions. See class Generic for more
531 information on generic types. Generic functions work as follows:
532
Guido van Rossumb24569a2016-11-20 18:01:29 -0800533 def repeat(x: T, n: int) -> List[T]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700534 '''Return a list containing n references to x.'''
535 return [x]*n
536
537 def longest(x: A, y: A) -> A:
538 '''Return the longest of two strings.'''
539 return x if len(x) >= len(y) else y
540
541 The latter example's signature is essentially the overloading
542 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
543 that if the arguments are instances of some subclass of str,
544 the return type is still plain str.
545
Guido van Rossumb24569a2016-11-20 18:01:29 -0800546 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700547
Guido van Rossumefa798d2016-08-23 11:01:50 -0700548 Type variables defined with covariant=True or contravariant=True
João D. Ferreira86bfed32018-07-07 16:41:20 +0100549 can be used to declare covariant or contravariant generic types.
Guido van Rossumefa798d2016-08-23 11:01:50 -0700550 See PEP 484 for more details. By default generic types are invariant
551 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700552
553 Type variables can be introspected. e.g.:
554
555 T.__name__ == 'T'
556 T.__constraints__ == ()
557 T.__covariant__ == False
558 T.__contravariant__ = False
559 A.__constraints__ == (str, bytes)
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100560
561 Note that only type variables defined in global scope can be pickled.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700562 """
563
Guido van Rossum4cefe742016-09-27 15:20:12 -0700564 __slots__ = ('__name__', '__bound__', '__constraints__',
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300565 '__covariant__', '__contravariant__', '__dict__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700566
567 def __init__(self, name, *constraints, bound=None,
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800568 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700569 self.__name__ = name
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700570 if covariant and contravariant:
Guido van Rossumefa798d2016-08-23 11:01:50 -0700571 raise ValueError("Bivariant types are not supported.")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700572 self.__covariant__ = bool(covariant)
573 self.__contravariant__ = bool(contravariant)
574 if constraints and bound is not None:
575 raise TypeError("Constraints cannot be combined with bound=...")
576 if constraints and len(constraints) == 1:
577 raise TypeError("A single constraint is not allowed")
578 msg = "TypeVar(name, constraint, ...): constraints must be types."
579 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
580 if bound:
581 self.__bound__ = _type_check(bound, "Bound must be a type.")
582 else:
583 self.__bound__ = None
HongWeipenga25a04f2020-04-21 04:01:53 +0800584 try:
585 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling
586 except (AttributeError, ValueError):
587 def_mod = None
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300588 if def_mod != 'typing':
589 self.__module__ = def_mod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700590
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700591 def __repr__(self):
592 if self.__covariant__:
593 prefix = '+'
594 elif self.__contravariant__:
595 prefix = '-'
596 else:
597 prefix = '~'
598 return prefix + self.__name__
599
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100600 def __reduce__(self):
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300601 return self.__name__
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100602
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700603
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000604def _is_dunder(attr):
605 return attr.startswith('__') and attr.endswith('__')
Guido van Rossum83ec3022017-01-17 20:43:28 -0800606
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300607class _BaseGenericAlias(_Final, _root=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000608 """The central part of internal API.
609
610 This represents a generic version of type 'origin' with type arguments 'params'.
611 There are two kind of these aliases: user defined and special. The special ones
612 are wrappers around builtin collections and ABCs in collections.abc. These must
613 have 'name' always set. If 'inst' is False, then the alias can't be instantiated,
614 this is used by e.g. typing.List and typing.Dict.
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700615 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300616 def __init__(self, origin, *, inst=True, name=None):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000617 self._inst = inst
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000618 self._name = name
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700619 self.__origin__ = origin
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000620 self.__slots__ = None # This is not documented.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300621
622 def __call__(self, *args, **kwargs):
623 if not self._inst:
624 raise TypeError(f"Type {self._name} cannot be instantiated; "
625 f"use {self.__origin__.__name__}() instead")
626 result = self.__origin__(*args, **kwargs)
627 try:
628 result.__orig_class__ = self
629 except AttributeError:
630 pass
631 return result
632
633 def __mro_entries__(self, bases):
634 res = []
635 if self.__origin__ not in bases:
636 res.append(self.__origin__)
637 i = bases.index(self)
638 for b in bases[i+1:]:
639 if isinstance(b, _BaseGenericAlias) or issubclass(b, Generic):
640 break
641 else:
642 res.append(Generic)
643 return tuple(res)
644
645 def __getattr__(self, attr):
646 # We are careful for copy and pickle.
647 # Also for simplicity we just don't relay all dunder names
648 if '__origin__' in self.__dict__ and not _is_dunder(attr):
649 return getattr(self.__origin__, attr)
650 raise AttributeError(attr)
651
652 def __setattr__(self, attr, val):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300653 if _is_dunder(attr) or attr in ('_name', '_inst', '_nparams'):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300654 super().__setattr__(attr, val)
655 else:
656 setattr(self.__origin__, attr, val)
657
658 def __instancecheck__(self, obj):
659 return self.__subclasscheck__(type(obj))
660
661 def __subclasscheck__(self, cls):
662 raise TypeError("Subscripted generics cannot be used with"
663 " class and instance checks")
664
665
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300666# Special typing constructs Union, Optional, Generic, Callable and Tuple
667# use three special attributes for internal bookkeeping of generic types:
668# * __parameters__ is a tuple of unique free type parameters of a generic
669# type, for example, Dict[T, T].__parameters__ == (T,);
670# * __origin__ keeps a reference to a type that was subscripted,
671# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
672# the type.
673# * __args__ is a tuple of all arguments used in subscripting,
674# e.g., Dict[T, int].__args__ == (T, int).
675
676
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300677class _GenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300678 def __init__(self, origin, params, *, inst=True, name=None):
679 super().__init__(origin, inst=inst, name=name)
680 if not isinstance(params, tuple):
681 params = (params,)
682 self.__args__ = tuple(... if a is _TypingEllipsis else
683 () if a is _TypingEmpty else
684 a for a in params)
685 self.__parameters__ = _collect_type_vars(params)
686 if not name:
687 self.__module__ = origin.__module__
688
689 def __eq__(self, other):
690 if not isinstance(other, _GenericAlias):
691 return NotImplemented
692 return (self.__origin__ == other.__origin__
693 and self.__args__ == other.__args__)
694
695 def __hash__(self):
696 return hash((self.__origin__, self.__args__))
697
Guido van Rossum4cefe742016-09-27 15:20:12 -0700698 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700699 def __getitem__(self, params):
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100700 if self.__origin__ in (Generic, Protocol):
701 # Can't subscript Generic[...] or Protocol[...].
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000702 raise TypeError(f"Cannot subscript already-subscripted {self}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700703 if not isinstance(params, tuple):
704 params = (params,)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700705 msg = "Parameters to generic types must be types."
706 params = tuple(_type_check(p, msg) for p in params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300707 _check_generic(self, params, len(self.__parameters__))
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300708
709 subst = dict(zip(self.__parameters__, params))
710 new_args = []
711 for arg in self.__args__:
712 if isinstance(arg, TypeVar):
713 arg = subst[arg]
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300714 elif isinstance(arg, (_GenericAlias, GenericAlias)):
Serhiy Storchaka0122d482020-05-10 13:39:40 +0300715 subparams = arg.__parameters__
716 if subparams:
717 subargs = tuple(subst[x] for x in subparams)
718 arg = arg[subargs]
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300719 new_args.append(arg)
720 return self.copy_with(tuple(new_args))
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100721
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000722 def copy_with(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300723 return self.__class__(self.__origin__, params, name=self._name, inst=self._inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700724
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000725 def __repr__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300726 if self._name:
727 name = 'typing.' + self._name
728 else:
729 name = _type_repr(self.__origin__)
730 args = ", ".join([_type_repr(a) for a in self.__args__])
731 return f'{name}[{args}]'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000732
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300733 def __reduce__(self):
734 if self._name:
735 origin = globals()[self._name]
736 else:
737 origin = self.__origin__
738 args = tuple(self.__args__)
739 if len(args) == 1 and not isinstance(args[0], tuple):
740 args, = args
741 return operator.getitem, (origin, args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000742
743 def __mro_entries__(self, bases):
744 if self._name: # generic version of an ABC or built-in class
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300745 return super().__mro_entries__(bases)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000746 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:]:
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300751 if isinstance(b, _BaseGenericAlias) and b is not self:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000752 return ()
753 return (self.__origin__,)
754
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000755
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300756# _nparams is the number of accepted parameters, e.g. 0 for Hashable,
757# 1 for List and 2 for Dict. It may be -1 if variable number of
758# parameters are accepted (needs custom __getitem__).
759
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300760class _SpecialGenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300761 def __init__(self, origin, nparams, *, inst=True, name=None):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300762 if name is None:
763 name = origin.__name__
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300764 super().__init__(origin, inst=inst, name=name)
765 self._nparams = nparams
Serhiy Storchaka2fbc57a2020-05-10 15:14:27 +0300766 if origin.__module__ == 'builtins':
767 self.__doc__ = f'A generic version of {origin.__qualname__}.'
768 else:
769 self.__doc__ = f'A generic version of {origin.__module__}.{origin.__qualname__}.'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000770
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300771 @_tp_cache
772 def __getitem__(self, params):
773 if not isinstance(params, tuple):
774 params = (params,)
775 msg = "Parameters to generic types must be types."
776 params = tuple(_type_check(p, msg) for p in params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300777 _check_generic(self, params, self._nparams)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300778 return self.copy_with(params)
779
780 def copy_with(self, params):
781 return _GenericAlias(self.__origin__, params,
782 name=self._name, inst=self._inst)
783
784 def __repr__(self):
785 return 'typing.' + self._name
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000786
787 def __subclasscheck__(self, cls):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300788 if isinstance(cls, _SpecialGenericAlias):
789 return issubclass(cls.__origin__, self.__origin__)
790 if not isinstance(cls, _GenericAlias):
791 return issubclass(cls, self.__origin__)
792 return super().__subclasscheck__(cls)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700793
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100794 def __reduce__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300795 return self._name
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100796
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700797
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300798class _CallableGenericAlias(_GenericAlias, _root=True):
799 def __repr__(self):
800 assert self._name == 'Callable'
801 if len(self.__args__) == 2 and self.__args__[0] is Ellipsis:
802 return super().__repr__()
803 return (f'typing.Callable'
804 f'[[{", ".join([_type_repr(a) for a in self.__args__[:-1]])}], '
805 f'{_type_repr(self.__args__[-1])}]')
806
807 def __reduce__(self):
808 args = self.__args__
809 if not (len(args) == 2 and args[0] is ...):
810 args = list(args[:-1]), args[-1]
811 return operator.getitem, (Callable, args)
812
813
814class _CallableType(_SpecialGenericAlias, _root=True):
815 def copy_with(self, params):
816 return _CallableGenericAlias(self.__origin__, params,
817 name=self._name, inst=self._inst)
818
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000819 def __getitem__(self, params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000820 if not isinstance(params, tuple) or len(params) != 2:
821 raise TypeError("Callable must be used as "
822 "Callable[[arg, ...], result].")
823 args, result = params
824 if args is Ellipsis:
825 params = (Ellipsis, result)
826 else:
827 if not isinstance(args, list):
828 raise TypeError(f"Callable[args, result]: args must be a list."
829 f" Got {args}")
830 params = (tuple(args), result)
831 return self.__getitem_inner__(params)
832
833 @_tp_cache
834 def __getitem_inner__(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300835 args, result = params
836 msg = "Callable[args, result]: result must be a type."
837 result = _type_check(result, msg)
838 if args is Ellipsis:
839 return self.copy_with((_TypingEllipsis, result))
840 msg = "Callable[[arg, ...], result]: each arg must be a type."
841 args = tuple(_type_check(arg, msg) for arg in args)
842 params = args + (result,)
843 return self.copy_with(params)
844
845
846class _TupleType(_SpecialGenericAlias, _root=True):
847 @_tp_cache
848 def __getitem__(self, params):
849 if params == ():
850 return self.copy_with((_TypingEmpty,))
851 if not isinstance(params, tuple):
852 params = (params,)
853 if len(params) == 2 and params[1] is ...:
854 msg = "Tuple[t, ...]: t must be a type."
855 p = _type_check(params[0], msg)
856 return self.copy_with((p, _TypingEllipsis))
857 msg = "Tuple[t0, t1, ...]: each t must be a type."
858 params = tuple(_type_check(p, msg) for p in params)
859 return self.copy_with(params)
860
861
862class _UnionGenericAlias(_GenericAlias, _root=True):
863 def copy_with(self, params):
864 return Union[params]
865
866 def __eq__(self, other):
867 if not isinstance(other, _UnionGenericAlias):
868 return NotImplemented
869 return set(self.__args__) == set(other.__args__)
870
871 def __hash__(self):
872 return hash(frozenset(self.__args__))
873
874 def __repr__(self):
875 args = self.__args__
876 if len(args) == 2:
877 if args[0] is type(None):
878 return f'typing.Optional[{_type_repr(args[1])}]'
879 elif args[1] is type(None):
880 return f'typing.Optional[{_type_repr(args[0])}]'
881 return super().__repr__()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000882
883
884class Generic:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700885 """Abstract base class for generic types.
886
Guido van Rossumb24569a2016-11-20 18:01:29 -0800887 A generic type is typically declared by inheriting from
888 this class parameterized with one or more type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700889 For example, a generic mapping type might be defined as::
890
891 class Mapping(Generic[KT, VT]):
892 def __getitem__(self, key: KT) -> VT:
893 ...
894 # Etc.
895
896 This class can then be used as follows::
897
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700898 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700899 try:
900 return mapping[key]
901 except KeyError:
902 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700903 """
Guido van Rossumd70fe632015-08-05 12:11:06 +0200904 __slots__ = ()
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100905 _is_protocol = False
Guido van Rossumd70fe632015-08-05 12:11:06 +0200906
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000907 @_tp_cache
908 def __class_getitem__(cls, params):
909 if not isinstance(params, tuple):
910 params = (params,)
911 if not params and cls is not Tuple:
912 raise TypeError(
913 f"Parameter list to {cls.__qualname__}[...] cannot be empty")
914 msg = "Parameters to generic types must be types."
915 params = tuple(_type_check(p, msg) for p in params)
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100916 if cls in (Generic, Protocol):
917 # Generic and Protocol can only be subscripted with unique type variables.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000918 if not all(isinstance(p, TypeVar) for p in params):
919 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100920 f"Parameters to {cls.__name__}[...] must all be type variables")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000921 if len(set(params)) != len(params):
922 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100923 f"Parameters to {cls.__name__}[...] must all be unique")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000924 else:
925 # Subscripting a regular Generic subclass.
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300926 _check_generic(cls, params, len(cls.__parameters__))
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000927 return _GenericAlias(cls, params)
928
929 def __init_subclass__(cls, *args, **kwargs):
Ivan Levkivskyiee566fe2018-04-04 17:00:15 +0100930 super().__init_subclass__(*args, **kwargs)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000931 tvars = []
932 if '__orig_bases__' in cls.__dict__:
933 error = Generic in cls.__orig_bases__
934 else:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100935 error = Generic in cls.__bases__ and cls.__name__ != 'Protocol'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000936 if error:
937 raise TypeError("Cannot inherit from plain Generic")
938 if '__orig_bases__' in cls.__dict__:
939 tvars = _collect_type_vars(cls.__orig_bases__)
940 # Look for Generic[T1, ..., Tn].
941 # If found, tvars must be a subset of it.
942 # If not found, tvars is it.
943 # Also check for and reject plain Generic,
944 # and reject multiple Generic[...].
945 gvars = None
946 for base in cls.__orig_bases__:
947 if (isinstance(base, _GenericAlias) and
948 base.__origin__ is Generic):
949 if gvars is not None:
950 raise TypeError(
951 "Cannot inherit from Generic[...] multiple types.")
952 gvars = base.__parameters__
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100953 if gvars is not None:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000954 tvarset = set(tvars)
955 gvarset = set(gvars)
956 if not tvarset <= gvarset:
957 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
958 s_args = ', '.join(str(g) for g in gvars)
959 raise TypeError(f"Some type variables ({s_vars}) are"
960 f" not listed in Generic[{s_args}]")
961 tvars = gvars
962 cls.__parameters__ = tuple(tvars)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700963
964
965class _TypingEmpty:
Guido van Rossumb24569a2016-11-20 18:01:29 -0800966 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
967 to allow empty list/tuple in specific places, without allowing them
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700968 to sneak in where prohibited.
969 """
970
971
972class _TypingEllipsis:
Guido van Rossumb24569a2016-11-20 18:01:29 -0800973 """Internal placeholder for ... (ellipsis)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700974
975
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100976_TYPING_INTERNALS = ['__parameters__', '__orig_bases__', '__orig_class__',
977 '_is_protocol', '_is_runtime_protocol']
978
979_SPECIAL_NAMES = ['__abstractmethods__', '__annotations__', '__dict__', '__doc__',
980 '__init__', '__module__', '__new__', '__slots__',
Guido van Rossum48b069a2020-04-07 09:50:06 -0700981 '__subclasshook__', '__weakref__', '__class_getitem__']
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100982
983# These special attributes will be not collected as protocol members.
984EXCLUDED_ATTRIBUTES = _TYPING_INTERNALS + _SPECIAL_NAMES + ['_MutableMapping__marker']
985
986
987def _get_protocol_attrs(cls):
988 """Collect protocol members from a protocol class objects.
989
990 This includes names actually defined in the class dictionary, as well
991 as names that appear in annotations. Special names (above) are skipped.
992 """
993 attrs = set()
994 for base in cls.__mro__[:-1]: # without object
995 if base.__name__ in ('Protocol', 'Generic'):
996 continue
997 annotations = getattr(base, '__annotations__', {})
998 for attr in list(base.__dict__.keys()) + list(annotations.keys()):
999 if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
1000 attrs.add(attr)
1001 return attrs
1002
1003
1004def _is_callable_members_only(cls):
1005 # PEP 544 prohibits using issubclass() with protocols that have non-method members.
1006 return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls))
1007
1008
1009def _no_init(self, *args, **kwargs):
1010 if type(self)._is_protocol:
1011 raise TypeError('Protocols cannot be instantiated')
1012
1013
1014def _allow_reckless_class_cheks():
Nickolena Fishercfaf4c02020-04-26 12:49:11 -05001015 """Allow instance and class checks for special stdlib modules.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001016
1017 The abc and functools modules indiscriminately call isinstance() and
1018 issubclass() on the whole MRO of a user class, which may contain protocols.
1019 """
1020 try:
1021 return sys._getframe(3).f_globals['__name__'] in ['abc', 'functools']
1022 except (AttributeError, ValueError): # For platforms without _getframe().
1023 return True
1024
1025
Divij Rajkumar692a0dc2019-09-12 11:13:51 +01001026_PROTO_WHITELIST = {
1027 'collections.abc': [
1028 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
1029 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
1030 ],
1031 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
1032}
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001033
1034
1035class _ProtocolMeta(ABCMeta):
1036 # This metaclass is really unfortunate and exists only because of
1037 # the lack of __instancehook__.
1038 def __instancecheck__(cls, instance):
1039 # We need this method for situations where attributes are
1040 # assigned in __init__.
1041 if ((not getattr(cls, '_is_protocol', False) or
1042 _is_callable_members_only(cls)) and
1043 issubclass(instance.__class__, cls)):
1044 return True
1045 if cls._is_protocol:
1046 if all(hasattr(instance, attr) and
1047 # All *methods* can be blocked by setting them to None.
1048 (not callable(getattr(cls, attr, None)) or
1049 getattr(instance, attr) is not None)
1050 for attr in _get_protocol_attrs(cls)):
1051 return True
1052 return super().__instancecheck__(instance)
1053
1054
1055class Protocol(Generic, metaclass=_ProtocolMeta):
1056 """Base class for protocol classes.
1057
1058 Protocol classes are defined as::
1059
1060 class Proto(Protocol):
1061 def meth(self) -> int:
1062 ...
1063
1064 Such classes are primarily used with static type checkers that recognize
1065 structural subtyping (static duck-typing), for example::
1066
1067 class C:
1068 def meth(self) -> int:
1069 return 0
1070
1071 def func(x: Proto) -> int:
1072 return x.meth()
1073
1074 func(C()) # Passes static type check
1075
1076 See PEP 544 for details. Protocol classes decorated with
1077 @typing.runtime_checkable act as simple-minded runtime protocols that check
1078 only the presence of given attributes, ignoring their type signatures.
1079 Protocol classes can be generic, they are defined as::
1080
1081 class GenProto(Protocol[T]):
1082 def meth(self) -> T:
1083 ...
1084 """
1085 __slots__ = ()
1086 _is_protocol = True
1087 _is_runtime_protocol = False
1088
1089 def __init_subclass__(cls, *args, **kwargs):
1090 super().__init_subclass__(*args, **kwargs)
1091
1092 # Determine if this is a protocol or a concrete subclass.
1093 if not cls.__dict__.get('_is_protocol', False):
1094 cls._is_protocol = any(b is Protocol for b in cls.__bases__)
1095
1096 # Set (or override) the protocol subclass hook.
1097 def _proto_hook(other):
1098 if not cls.__dict__.get('_is_protocol', False):
1099 return NotImplemented
1100
1101 # First, perform various sanity checks.
1102 if not getattr(cls, '_is_runtime_protocol', False):
1103 if _allow_reckless_class_cheks():
1104 return NotImplemented
1105 raise TypeError("Instance and class checks can only be used with"
1106 " @runtime_checkable protocols")
1107 if not _is_callable_members_only(cls):
1108 if _allow_reckless_class_cheks():
1109 return NotImplemented
1110 raise TypeError("Protocols with non-method members"
1111 " don't support issubclass()")
1112 if not isinstance(other, type):
1113 # Same error message as for issubclass(1, int).
1114 raise TypeError('issubclass() arg 1 must be a class')
1115
1116 # Second, perform the actual structural compatibility check.
1117 for attr in _get_protocol_attrs(cls):
1118 for base in other.__mro__:
1119 # Check if the members appears in the class dictionary...
1120 if attr in base.__dict__:
1121 if base.__dict__[attr] is None:
1122 return NotImplemented
1123 break
1124
1125 # ...or in annotations, if it is a sub-protocol.
1126 annotations = getattr(base, '__annotations__', {})
1127 if (isinstance(annotations, collections.abc.Mapping) and
1128 attr in annotations and
1129 issubclass(other, Generic) and other._is_protocol):
1130 break
1131 else:
1132 return NotImplemented
1133 return True
1134
1135 if '__subclasshook__' not in cls.__dict__:
1136 cls.__subclasshook__ = _proto_hook
1137
1138 # We have nothing more to do for non-protocols...
1139 if not cls._is_protocol:
1140 return
1141
1142 # ... otherwise check consistency of bases, and prohibit instantiation.
1143 for base in cls.__bases__:
1144 if not (base in (object, Generic) or
Divij Rajkumar692a0dc2019-09-12 11:13:51 +01001145 base.__module__ in _PROTO_WHITELIST and
1146 base.__name__ in _PROTO_WHITELIST[base.__module__] or
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001147 issubclass(base, Generic) and base._is_protocol):
1148 raise TypeError('Protocols can only inherit from other'
1149 ' protocols, got %r' % base)
1150 cls.__init__ = _no_init
1151
1152
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001153class _AnnotatedAlias(_GenericAlias, _root=True):
1154 """Runtime representation of an annotated type.
1155
1156 At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't'
1157 with extra annotations. The alias behaves like a normal typing alias,
1158 instantiating is the same as instantiating the underlying type, binding
1159 it to types is also the same.
1160 """
1161 def __init__(self, origin, metadata):
1162 if isinstance(origin, _AnnotatedAlias):
1163 metadata = origin.__metadata__ + metadata
1164 origin = origin.__origin__
1165 super().__init__(origin, origin)
1166 self.__metadata__ = metadata
1167
1168 def copy_with(self, params):
1169 assert len(params) == 1
1170 new_type = params[0]
1171 return _AnnotatedAlias(new_type, self.__metadata__)
1172
1173 def __repr__(self):
1174 return "typing.Annotated[{}, {}]".format(
1175 _type_repr(self.__origin__),
1176 ", ".join(repr(a) for a in self.__metadata__)
1177 )
1178
1179 def __reduce__(self):
1180 return operator.getitem, (
1181 Annotated, (self.__origin__,) + self.__metadata__
1182 )
1183
1184 def __eq__(self, other):
1185 if not isinstance(other, _AnnotatedAlias):
1186 return NotImplemented
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001187 return (self.__origin__ == other.__origin__
1188 and self.__metadata__ == other.__metadata__)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001189
1190 def __hash__(self):
1191 return hash((self.__origin__, self.__metadata__))
1192
1193
1194class Annotated:
1195 """Add context specific metadata to a type.
1196
1197 Example: Annotated[int, runtime_check.Unsigned] indicates to the
1198 hypothetical runtime_check module that this type is an unsigned int.
1199 Every other consumer of this type can ignore this metadata and treat
1200 this type as int.
1201
1202 The first argument to Annotated must be a valid type.
1203
1204 Details:
1205
1206 - It's an error to call `Annotated` with less than two arguments.
1207 - Nested Annotated are flattened::
1208
1209 Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
1210
1211 - Instantiating an annotated type is equivalent to instantiating the
1212 underlying type::
1213
1214 Annotated[C, Ann1](5) == C(5)
1215
1216 - Annotated can be used as a generic type alias::
1217
1218 Optimized = Annotated[T, runtime.Optimize()]
1219 Optimized[int] == Annotated[int, runtime.Optimize()]
1220
1221 OptimizedList = Annotated[List[T], runtime.Optimize()]
1222 OptimizedList[int] == Annotated[List[int], runtime.Optimize()]
1223 """
1224
1225 __slots__ = ()
1226
1227 def __new__(cls, *args, **kwargs):
1228 raise TypeError("Type Annotated cannot be instantiated.")
1229
1230 @_tp_cache
1231 def __class_getitem__(cls, params):
1232 if not isinstance(params, tuple) or len(params) < 2:
1233 raise TypeError("Annotated[...] should be used "
1234 "with at least two arguments (a type and an "
1235 "annotation).")
1236 msg = "Annotated[t, ...]: t must be a type."
1237 origin = _type_check(params[0], msg)
1238 metadata = tuple(params[1:])
1239 return _AnnotatedAlias(origin, metadata)
1240
1241 def __init_subclass__(cls, *args, **kwargs):
1242 raise TypeError(
1243 "Cannot subclass {}.Annotated".format(cls.__module__)
1244 )
1245
1246
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001247def runtime_checkable(cls):
1248 """Mark a protocol class as a runtime protocol.
1249
1250 Such protocol can be used with isinstance() and issubclass().
1251 Raise TypeError if applied to a non-protocol class.
1252 This allows a simple-minded structural check very similar to
1253 one trick ponies in collections.abc such as Iterable.
1254 For example::
1255
1256 @runtime_checkable
1257 class Closable(Protocol):
1258 def close(self): ...
1259
1260 assert isinstance(open('/some/file'), Closable)
1261
1262 Warning: this will check only the presence of the required methods,
1263 not their type signatures!
1264 """
1265 if not issubclass(cls, Generic) or not cls._is_protocol:
1266 raise TypeError('@runtime_checkable can be only applied to protocol classes,'
1267 ' got %r' % cls)
1268 cls._is_runtime_protocol = True
1269 return cls
1270
1271
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001272def cast(typ, val):
1273 """Cast a value to a type.
1274
1275 This returns the value unchanged. To the type checker this
1276 signals that the return value has the designated type, but at
1277 runtime we intentionally don't check anything (we want this
1278 to be as fast as possible).
1279 """
1280 return val
1281
1282
1283def _get_defaults(func):
1284 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -08001285 try:
1286 code = func.__code__
1287 except AttributeError:
1288 # Some built-in functions don't have __code__, __defaults__, etc.
1289 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001290 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001291 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001292 arg_names = arg_names[:pos_count]
1293 defaults = func.__defaults__ or ()
1294 kwdefaults = func.__kwdefaults__
1295 res = dict(kwdefaults) if kwdefaults else {}
1296 pos_offset = pos_count - len(defaults)
1297 for name, value in zip(arg_names[pos_offset:], defaults):
1298 assert name not in res
1299 res[name] = value
1300 return res
1301
1302
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001303_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
1304 types.MethodType, types.ModuleType,
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02001305 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001306
1307
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001308def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
Guido van Rossum991d14f2016-11-09 13:12:51 -08001309 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001310
Guido van Rossum991d14f2016-11-09 13:12:51 -08001311 This is often the same as obj.__annotations__, but it handles
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001312 forward references encoded as string literals, adds Optional[t] if a
1313 default value equal to None is set and recursively replaces all
1314 'Annotated[T, ...]' with 'T' (unless 'include_extras=True').
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001315
Guido van Rossum991d14f2016-11-09 13:12:51 -08001316 The argument may be a module, class, method, or function. The annotations
1317 are returned as a dictionary. For classes, annotations include also
1318 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001319
Guido van Rossum991d14f2016-11-09 13:12:51 -08001320 TypeError is raised if the argument is not of a type that can contain
1321 annotations, and an empty dictionary is returned if no annotations are
1322 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001323
Guido van Rossum991d14f2016-11-09 13:12:51 -08001324 BEWARE -- the behavior of globalns and localns is counterintuitive
1325 (unless you are familiar with how eval() and exec() work). The
1326 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001327
Guido van Rossum991d14f2016-11-09 13:12:51 -08001328 - If no dict arguments are passed, an attempt is made to use the
Łukasz Langaf350a262017-09-14 14:33:00 -04001329 globals from obj (or the respective module's globals for classes),
1330 and these are also used as the locals. If the object does not appear
1331 to have globals, an empty dictionary is used.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001332
Guido van Rossum991d14f2016-11-09 13:12:51 -08001333 - If one dict argument is passed, it is used for both globals and
1334 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001335
Guido van Rossum991d14f2016-11-09 13:12:51 -08001336 - If two dict arguments are passed, they specify globals and
1337 locals, respectively.
1338 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001339
Guido van Rossum991d14f2016-11-09 13:12:51 -08001340 if getattr(obj, '__no_type_check__', None):
1341 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001342 # Classes require a special treatment.
1343 if isinstance(obj, type):
1344 hints = {}
1345 for base in reversed(obj.__mro__):
Łukasz Langaf350a262017-09-14 14:33:00 -04001346 if globalns is None:
1347 base_globals = sys.modules[base.__module__].__dict__
1348 else:
1349 base_globals = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001350 ann = base.__dict__.get('__annotations__', {})
1351 for name, value in ann.items():
1352 if value is None:
1353 value = type(None)
1354 if isinstance(value, str):
Nina Zakharenko0e61dff2018-05-22 20:32:10 -07001355 value = ForwardRef(value, is_argument=False)
Łukasz Langaf350a262017-09-14 14:33:00 -04001356 value = _eval_type(value, base_globals, localns)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001357 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001358 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
Łukasz Langaf350a262017-09-14 14:33:00 -04001359
1360 if globalns is None:
1361 if isinstance(obj, types.ModuleType):
1362 globalns = obj.__dict__
1363 else:
benedwards140aca3a32019-11-21 17:24:58 +00001364 nsobj = obj
1365 # Find globalns for the unwrapped object.
1366 while hasattr(nsobj, '__wrapped__'):
1367 nsobj = nsobj.__wrapped__
1368 globalns = getattr(nsobj, '__globals__', {})
Łukasz Langaf350a262017-09-14 14:33:00 -04001369 if localns is None:
1370 localns = globalns
1371 elif localns is None:
1372 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001373 hints = getattr(obj, '__annotations__', None)
1374 if hints is None:
1375 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001376 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001377 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001378 else:
1379 raise TypeError('{!r} is not a module, class, method, '
1380 'or function.'.format(obj))
1381 defaults = _get_defaults(obj)
1382 hints = dict(hints)
1383 for name, value in hints.items():
1384 if value is None:
1385 value = type(None)
1386 if isinstance(value, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001387 value = ForwardRef(value)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001388 value = _eval_type(value, globalns, localns)
1389 if name in defaults and defaults[name] is None:
1390 value = Optional[value]
1391 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001392 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
1393
1394
1395def _strip_annotations(t):
1396 """Strips the annotations from a given type.
1397 """
1398 if isinstance(t, _AnnotatedAlias):
1399 return _strip_annotations(t.__origin__)
1400 if isinstance(t, _GenericAlias):
1401 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1402 if stripped_args == t.__args__:
1403 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001404 return t.copy_with(stripped_args)
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001405 if isinstance(t, GenericAlias):
1406 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1407 if stripped_args == t.__args__:
1408 return t
1409 return GenericAlias(t.__origin__, stripped_args)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001410 return t
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001411
1412
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001413def get_origin(tp):
1414 """Get the unsubscripted version of a type.
1415
Jakub Stasiak38aaaaa2020-02-07 02:15:12 +01001416 This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
1417 and Annotated. Return None for unsupported types. Examples::
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001418
1419 get_origin(Literal[42]) is Literal
1420 get_origin(int) is None
1421 get_origin(ClassVar[int]) is ClassVar
1422 get_origin(Generic) is Generic
1423 get_origin(Generic[T]) is Generic
1424 get_origin(Union[T, int]) is Union
1425 get_origin(List[Tuple[T, T]][int]) == list
1426 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001427 if isinstance(tp, _AnnotatedAlias):
1428 return Annotated
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001429 if isinstance(tp, (_BaseGenericAlias, GenericAlias)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001430 return tp.__origin__
1431 if tp is Generic:
1432 return Generic
1433 return None
1434
1435
1436def get_args(tp):
1437 """Get type arguments with all substitutions performed.
1438
1439 For unions, basic simplifications used by Union constructor are performed.
1440 Examples::
1441 get_args(Dict[str, int]) == (str, int)
1442 get_args(int) == ()
1443 get_args(Union[int, Union[T, int], str][int]) == (int, str)
1444 get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
1445 get_args(Callable[[], T][int]) == ([], int)
1446 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001447 if isinstance(tp, _AnnotatedAlias):
1448 return (tp.__origin__,) + tp.__metadata__
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001449 if isinstance(tp, _GenericAlias):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001450 res = tp.__args__
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001451 if tp.__origin__ is collections.abc.Callable and res[0] is not Ellipsis:
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001452 res = (list(res[:-1]), res[-1])
1453 return res
Serhiy Storchaka6292be72020-04-27 10:27:21 +03001454 if isinstance(tp, GenericAlias):
1455 return tp.__args__
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001456 return ()
1457
1458
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001459def no_type_check(arg):
1460 """Decorator to indicate that annotations are not type hints.
1461
1462 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001463 applies recursively to all methods and classes defined in that class
1464 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001465
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001466 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001467 """
1468 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001469 arg_attrs = arg.__dict__.copy()
1470 for attr, val in arg.__dict__.items():
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001471 if val in arg.__bases__ + (arg,):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001472 arg_attrs.pop(attr)
1473 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001474 if isinstance(obj, types.FunctionType):
1475 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001476 if isinstance(obj, type):
1477 no_type_check(obj)
1478 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001479 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001480 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001481 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001482 return arg
1483
1484
1485def no_type_check_decorator(decorator):
1486 """Decorator to give another decorator the @no_type_check effect.
1487
1488 This wraps the decorator with something that wraps the decorated
1489 function in @no_type_check.
1490 """
1491
1492 @functools.wraps(decorator)
1493 def wrapped_decorator(*args, **kwds):
1494 func = decorator(*args, **kwds)
1495 func = no_type_check(func)
1496 return func
1497
1498 return wrapped_decorator
1499
1500
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001501def _overload_dummy(*args, **kwds):
1502 """Helper for @overload to raise when called."""
1503 raise NotImplementedError(
1504 "You should not call an overloaded function. "
1505 "A series of @overload-decorated functions "
1506 "outside a stub module should always be followed "
1507 "by an implementation that is not @overload-ed.")
1508
1509
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001510def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001511 """Decorator for overloaded functions/methods.
1512
1513 In a stub file, place two or more stub definitions for the same
1514 function in a row, each decorated with @overload. For example:
1515
1516 @overload
1517 def utf8(value: None) -> None: ...
1518 @overload
1519 def utf8(value: bytes) -> bytes: ...
1520 @overload
1521 def utf8(value: str) -> bytes: ...
1522
1523 In a non-stub file (i.e. a regular .py file), do the same but
1524 follow it with an implementation. The implementation should *not*
1525 be decorated with @overload. For example:
1526
1527 @overload
1528 def utf8(value: None) -> None: ...
1529 @overload
1530 def utf8(value: bytes) -> bytes: ...
1531 @overload
1532 def utf8(value: str) -> bytes: ...
1533 def utf8(value):
1534 # implementation goes here
1535 """
1536 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001537
1538
Ivan Levkivskyif3672422019-05-26 09:37:07 +01001539def final(f):
1540 """A decorator to indicate final methods and final classes.
1541
1542 Use this decorator to indicate to type checkers that the decorated
1543 method cannot be overridden, and decorated class cannot be subclassed.
1544 For example:
1545
1546 class Base:
1547 @final
1548 def done(self) -> None:
1549 ...
1550 class Sub(Base):
1551 def done(self) -> None: # Error reported by type checker
1552 ...
1553
1554 @final
1555 class Leaf:
1556 ...
1557 class Other(Leaf): # Error reported by type checker
1558 ...
1559
1560 There is no runtime checking of these properties.
1561 """
1562 return f
1563
1564
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001565# Some unconstrained type variables. These are used by the container types.
1566# (These are not for export.)
1567T = TypeVar('T') # Any type.
1568KT = TypeVar('KT') # Key type.
1569VT = TypeVar('VT') # Value type.
1570T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
1571V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
1572VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
1573T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
1574# Internal type variable used for Type[].
1575CT_co = TypeVar('CT_co', covariant=True, bound=type)
1576
1577# A useful type variable with constraints. This represents string types.
1578# (This one *is* for export!)
1579AnyStr = TypeVar('AnyStr', bytes, str)
1580
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001581
1582# Various ABCs mimicking those in collections.abc.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001583_alias = _SpecialGenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001584
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001585Hashable = _alias(collections.abc.Hashable, 0) # Not generic.
1586Awaitable = _alias(collections.abc.Awaitable, 1)
1587Coroutine = _alias(collections.abc.Coroutine, 3)
1588AsyncIterable = _alias(collections.abc.AsyncIterable, 1)
1589AsyncIterator = _alias(collections.abc.AsyncIterator, 1)
1590Iterable = _alias(collections.abc.Iterable, 1)
1591Iterator = _alias(collections.abc.Iterator, 1)
1592Reversible = _alias(collections.abc.Reversible, 1)
1593Sized = _alias(collections.abc.Sized, 0) # Not generic.
1594Container = _alias(collections.abc.Container, 1)
1595Collection = _alias(collections.abc.Collection, 1)
1596Callable = _CallableType(collections.abc.Callable, 2)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001597Callable.__doc__ = \
1598 """Callable type; Callable[[int], str] is a function of (int) -> str.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001599
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001600 The subscription syntax must always be used with exactly two
1601 values: the argument list and the return type. The argument list
1602 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001603
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001604 There is no syntax to indicate optional or keyword arguments,
1605 such function types are rarely used as callback types.
1606 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001607AbstractSet = _alias(collections.abc.Set, 1, name='AbstractSet')
1608MutableSet = _alias(collections.abc.MutableSet, 1)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001609# NOTE: Mapping is only covariant in the value type.
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001610Mapping = _alias(collections.abc.Mapping, 2)
1611MutableMapping = _alias(collections.abc.MutableMapping, 2)
1612Sequence = _alias(collections.abc.Sequence, 1)
1613MutableSequence = _alias(collections.abc.MutableSequence, 1)
1614ByteString = _alias(collections.abc.ByteString, 0) # Not generic
1615# Tuple accepts variable number of parameters.
1616Tuple = _TupleType(tuple, -1, inst=False, name='Tuple')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001617Tuple.__doc__ = \
1618 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001619
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001620 Example: Tuple[T1, T2] is a tuple of two elements corresponding
1621 to type variables T1 and T2. Tuple[int, float, str] is a tuple
1622 of an int, a float and a string.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001623
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001624 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
1625 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001626List = _alias(list, 1, inst=False, name='List')
1627Deque = _alias(collections.deque, 1, name='Deque')
1628Set = _alias(set, 1, inst=False, name='Set')
1629FrozenSet = _alias(frozenset, 1, inst=False, name='FrozenSet')
1630MappingView = _alias(collections.abc.MappingView, 1)
1631KeysView = _alias(collections.abc.KeysView, 1)
1632ItemsView = _alias(collections.abc.ItemsView, 2)
1633ValuesView = _alias(collections.abc.ValuesView, 1)
1634ContextManager = _alias(contextlib.AbstractContextManager, 1, name='ContextManager')
1635AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, 1, name='AsyncContextManager')
1636Dict = _alias(dict, 2, inst=False, name='Dict')
1637DefaultDict = _alias(collections.defaultdict, 2, name='DefaultDict')
1638OrderedDict = _alias(collections.OrderedDict, 2)
1639Counter = _alias(collections.Counter, 1)
1640ChainMap = _alias(collections.ChainMap, 2)
1641Generator = _alias(collections.abc.Generator, 3)
1642AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2)
1643Type = _alias(type, 1, inst=False, name='Type')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001644Type.__doc__ = \
1645 """A special construct usable to annotate class objects.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001646
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001647 For example, suppose we have the following classes::
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001648
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001649 class User: ... # Abstract base for User classes
1650 class BasicUser(User): ...
1651 class ProUser(User): ...
1652 class TeamUser(User): ...
Guido van Rossumf17c2002015-12-03 17:31:24 -08001653
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001654 And a function that takes a class argument that's a subclass of
1655 User and returns an instance of the corresponding class::
Guido van Rossumf17c2002015-12-03 17:31:24 -08001656
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001657 U = TypeVar('U', bound=User)
1658 def new_user(user_class: Type[U]) -> U:
1659 user = user_class()
1660 # (Here we could write the user object to a database)
1661 return user
Guido van Rossumf17c2002015-12-03 17:31:24 -08001662
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001663 joe = new_user(BasicUser)
Guido van Rossumf17c2002015-12-03 17:31:24 -08001664
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001665 At this point the type checker knows that joe has type BasicUser.
1666 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001667
1668
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001669@runtime_checkable
1670class SupportsInt(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001671 """An ABC with one abstract method __int__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001672 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001673
1674 @abstractmethod
1675 def __int__(self) -> int:
1676 pass
1677
1678
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001679@runtime_checkable
1680class SupportsFloat(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001681 """An ABC with one abstract method __float__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001682 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001683
1684 @abstractmethod
1685 def __float__(self) -> float:
1686 pass
1687
1688
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001689@runtime_checkable
1690class SupportsComplex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001691 """An ABC with one abstract method __complex__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001692 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001693
1694 @abstractmethod
1695 def __complex__(self) -> complex:
1696 pass
1697
1698
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001699@runtime_checkable
1700class SupportsBytes(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001701 """An ABC with one abstract method __bytes__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001702 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001703
1704 @abstractmethod
1705 def __bytes__(self) -> bytes:
1706 pass
1707
1708
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001709@runtime_checkable
1710class SupportsIndex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001711 """An ABC with one abstract method __index__."""
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -07001712 __slots__ = ()
1713
1714 @abstractmethod
1715 def __index__(self) -> int:
1716 pass
1717
1718
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001719@runtime_checkable
1720class SupportsAbs(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001721 """An ABC with one abstract method __abs__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001722 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001723
1724 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001725 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001726 pass
1727
1728
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001729@runtime_checkable
1730class SupportsRound(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001731 """An ABC with one abstract method __round__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001732 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001733
1734 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001735 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001736 pass
1737
1738
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001739def _make_nmtuple(name, types, module, defaults = ()):
1740 fields = [n for n, t in types]
1741 types = {n: _type_check(t, f"field {n} annotation must be a type")
1742 for n, t in types}
1743 nm_tpl = collections.namedtuple(name, fields,
1744 defaults=defaults, module=module)
1745 nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001746 return nm_tpl
1747
1748
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001749# attributes prohibited to set in NamedTuple class syntax
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001750_prohibited = frozenset({'__new__', '__init__', '__slots__', '__getnewargs__',
1751 '_fields', '_field_defaults',
1752 '_make', '_replace', '_asdict', '_source'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001753
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001754_special = frozenset({'__module__', '__name__', '__annotations__'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001755
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001756
Guido van Rossum2f841442016-11-15 09:48:06 -08001757class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001758
Guido van Rossum2f841442016-11-15 09:48:06 -08001759 def __new__(cls, typename, bases, ns):
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001760 assert bases[0] is _NamedTuple
Guido van Rossum2f841442016-11-15 09:48:06 -08001761 types = ns.get('__annotations__', {})
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001762 default_names = []
Guido van Rossum3c268be2017-01-18 08:03:50 -08001763 for field_name in types:
1764 if field_name in ns:
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001765 default_names.append(field_name)
1766 elif default_names:
1767 raise TypeError(f"Non-default namedtuple field {field_name} "
1768 f"cannot follow default field"
1769 f"{'s' if len(default_names) > 1 else ''} "
1770 f"{', '.join(default_names)}")
1771 nm_tpl = _make_nmtuple(typename, types.items(),
1772 defaults=[ns[n] for n in default_names],
1773 module=ns['__module__'])
Guido van Rossum95919c02017-01-22 17:47:20 -08001774 # update from user namespace without overriding special namedtuple attributes
1775 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001776 if key in _prohibited:
1777 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
1778 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08001779 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08001780 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001781
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001782
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001783def NamedTuple(typename, fields=None, /, **kwargs):
Guido van Rossum2f841442016-11-15 09:48:06 -08001784 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001785
Guido van Rossum2f841442016-11-15 09:48:06 -08001786 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001787
Guido van Rossum2f841442016-11-15 09:48:06 -08001788 class Employee(NamedTuple):
1789 name: str
1790 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001791
Guido van Rossum2f841442016-11-15 09:48:06 -08001792 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001793
Guido van Rossum2f841442016-11-15 09:48:06 -08001794 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001795
Raymond Hettingerf7b57df2019-03-18 09:53:56 -07001796 The resulting class has an extra __annotations__ attribute, giving a
1797 dict that maps field names to types. (The field names are also in
1798 the _fields attribute, which is part of the namedtuple API.)
1799 Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001800
Guido van Rossum2f841442016-11-15 09:48:06 -08001801 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001802
Guido van Rossum2f841442016-11-15 09:48:06 -08001803 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001804
Guido van Rossum2f841442016-11-15 09:48:06 -08001805 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
1806 """
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001807 if fields is None:
1808 fields = kwargs.items()
1809 elif kwargs:
1810 raise TypeError("Either list of fields or keywords"
1811 " can be provided to NamedTuple, not both")
1812 try:
1813 module = sys._getframe(1).f_globals.get('__name__', '__main__')
1814 except (AttributeError, ValueError):
1815 module = None
1816 return _make_nmtuple(typename, fields, module=module)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001817
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001818_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})
1819
1820def _namedtuple_mro_entries(bases):
1821 if len(bases) > 1:
1822 raise TypeError("Multiple inheritance with NamedTuple is not supported")
1823 assert bases[0] is NamedTuple
1824 return (_NamedTuple,)
1825
1826NamedTuple.__mro_entries__ = _namedtuple_mro_entries
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001827
1828
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001829class _TypedDictMeta(type):
1830 def __new__(cls, name, bases, ns, total=True):
1831 """Create new typed dict class object.
1832
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001833 This method is called when TypedDict is subclassed,
1834 or when TypedDict is instantiated. This way
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001835 TypedDict supports all three syntax forms described in its docstring.
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001836 Subclasses and instances of TypedDict return actual dictionaries.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001837 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001838 for base in bases:
1839 if type(base) is not _TypedDictMeta:
1840 raise TypeError('cannot inherit from both a TypedDict type '
1841 'and a non-TypedDict base class')
1842 tp_dict = type.__new__(_TypedDictMeta, name, (dict,), ns)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001843
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001844 annotations = {}
1845 own_annotations = ns.get('__annotations__', {})
1846 own_annotation_keys = set(own_annotations.keys())
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001847 msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001848 own_annotations = {
1849 n: _type_check(tp, msg) for n, tp in own_annotations.items()
1850 }
1851 required_keys = set()
1852 optional_keys = set()
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11001853
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001854 for base in bases:
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001855 annotations.update(base.__dict__.get('__annotations__', {}))
1856 required_keys.update(base.__dict__.get('__required_keys__', ()))
1857 optional_keys.update(base.__dict__.get('__optional_keys__', ()))
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11001858
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001859 annotations.update(own_annotations)
1860 if total:
1861 required_keys.update(own_annotation_keys)
1862 else:
1863 optional_keys.update(own_annotation_keys)
1864
1865 tp_dict.__annotations__ = annotations
1866 tp_dict.__required_keys__ = frozenset(required_keys)
1867 tp_dict.__optional_keys__ = frozenset(optional_keys)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001868 if not hasattr(tp_dict, '__total__'):
1869 tp_dict.__total__ = total
1870 return tp_dict
1871
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001872 __call__ = dict # static method
1873
1874 def __subclasscheck__(cls, other):
1875 # Typed dicts are only for static structural subtyping.
1876 raise TypeError('TypedDict does not support instance and class checks')
1877
1878 __instancecheck__ = __subclasscheck__
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001879
1880
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001881def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001882 """A simple typed namespace. At runtime it is equivalent to a plain dict.
1883
1884 TypedDict creates a dictionary type that expects all of its
1885 instances to have a certain set of keys, where each key is
1886 associated with a value of a consistent type. This expectation
1887 is not checked at runtime but is only enforced by type checkers.
1888 Usage::
1889
1890 class Point2D(TypedDict):
1891 x: int
1892 y: int
1893 label: str
1894
1895 a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
1896 b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
1897
1898 assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
1899
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11001900 The type info can be accessed via the Point2D.__annotations__ dict, and
1901 the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
1902 TypedDict supports two additional equivalent forms::
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001903
1904 Point2D = TypedDict('Point2D', x=int, y=int, label=str)
1905 Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
1906
ananthan-123ab6423f2020-02-19 10:03:05 +05301907 By default, all keys must be present in a TypedDict. It is possible
1908 to override this by specifying totality.
1909 Usage::
1910
1911 class point2D(TypedDict, total=False):
1912 x: int
1913 y: int
1914
1915 This means that a point2D TypedDict can have any of the keys omitted.A type
1916 checker is only expected to support a literal False or True as the value of
1917 the total argument. True is the default, and makes all items defined in the
1918 class body be required.
1919
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001920 The class syntax is only supported in Python 3.6+, while two other
1921 syntax forms work for Python 2.7 and 3.2+
1922 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001923 if fields is None:
1924 fields = kwargs
1925 elif kwargs:
1926 raise TypeError("TypedDict takes either a dict or keyword arguments,"
1927 " but not both")
1928
1929 ns = {'__annotations__': dict(fields), '__total__': total}
1930 try:
1931 # Setting correct module is necessary to make typed dict classes pickleable.
1932 ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
1933 except (AttributeError, ValueError):
1934 pass
1935
1936 return _TypedDictMeta(typename, (), ns)
1937
1938_TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {})
1939TypedDict.__mro_entries__ = lambda bases: (_TypedDict,)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001940
1941
Guido van Rossum91185fe2016-06-08 11:19:11 -07001942def NewType(name, tp):
1943 """NewType creates simple unique types with almost zero
1944 runtime overhead. NewType(name, tp) is considered a subtype of tp
1945 by static type checkers. At runtime, NewType(name, tp) returns
1946 a dummy function that simply returns its argument. Usage::
1947
1948 UserId = NewType('UserId', int)
1949
1950 def name_by_id(user_id: UserId) -> str:
1951 ...
1952
1953 UserId('user') # Fails type check
1954
1955 name_by_id(42) # Fails type check
1956 name_by_id(UserId(42)) # OK
1957
1958 num = UserId(5) + 1 # type: int
1959 """
1960
1961 def new_type(x):
1962 return x
1963
1964 new_type.__name__ = name
1965 new_type.__supertype__ = tp
1966 return new_type
1967
1968
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001969# Python-version-specific alias (Python 2: unicode; Python 3: str)
1970Text = str
1971
1972
Guido van Rossum91185fe2016-06-08 11:19:11 -07001973# Constant that's True when type checking, but False here.
1974TYPE_CHECKING = False
1975
1976
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001977class IO(Generic[AnyStr]):
1978 """Generic base class for TextIO and BinaryIO.
1979
1980 This is an abstract, generic version of the return of open().
1981
1982 NOTE: This does not distinguish between the different possible
1983 classes (text vs. binary, read vs. write vs. read/write,
1984 append-only, unbuffered). The TextIO and BinaryIO subclasses
1985 below capture the distinctions between text vs. binary, which is
1986 pervasive in the interface; however we currently do not offer a
1987 way to track the other distinctions in the type system.
1988 """
1989
Guido van Rossumd70fe632015-08-05 12:11:06 +02001990 __slots__ = ()
1991
HongWeipeng6ce03ec2019-09-27 15:54:26 +08001992 @property
1993 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001994 def mode(self) -> str:
1995 pass
1996
HongWeipeng6ce03ec2019-09-27 15:54:26 +08001997 @property
1998 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001999 def name(self) -> str:
2000 pass
2001
2002 @abstractmethod
2003 def close(self) -> None:
2004 pass
2005
Shantanu2e6569b2020-01-29 18:52:36 -08002006 @property
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002007 @abstractmethod
2008 def closed(self) -> bool:
2009 pass
2010
2011 @abstractmethod
2012 def fileno(self) -> int:
2013 pass
2014
2015 @abstractmethod
2016 def flush(self) -> None:
2017 pass
2018
2019 @abstractmethod
2020 def isatty(self) -> bool:
2021 pass
2022
2023 @abstractmethod
2024 def read(self, n: int = -1) -> AnyStr:
2025 pass
2026
2027 @abstractmethod
2028 def readable(self) -> bool:
2029 pass
2030
2031 @abstractmethod
2032 def readline(self, limit: int = -1) -> AnyStr:
2033 pass
2034
2035 @abstractmethod
2036 def readlines(self, hint: int = -1) -> List[AnyStr]:
2037 pass
2038
2039 @abstractmethod
2040 def seek(self, offset: int, whence: int = 0) -> int:
2041 pass
2042
2043 @abstractmethod
2044 def seekable(self) -> bool:
2045 pass
2046
2047 @abstractmethod
2048 def tell(self) -> int:
2049 pass
2050
2051 @abstractmethod
2052 def truncate(self, size: int = None) -> int:
2053 pass
2054
2055 @abstractmethod
2056 def writable(self) -> bool:
2057 pass
2058
2059 @abstractmethod
2060 def write(self, s: AnyStr) -> int:
2061 pass
2062
2063 @abstractmethod
2064 def writelines(self, lines: List[AnyStr]) -> None:
2065 pass
2066
2067 @abstractmethod
2068 def __enter__(self) -> 'IO[AnyStr]':
2069 pass
2070
2071 @abstractmethod
2072 def __exit__(self, type, value, traceback) -> None:
2073 pass
2074
2075
2076class BinaryIO(IO[bytes]):
2077 """Typed version of the return of open() in binary mode."""
2078
Guido van Rossumd70fe632015-08-05 12:11:06 +02002079 __slots__ = ()
2080
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002081 @abstractmethod
2082 def write(self, s: Union[bytes, bytearray]) -> int:
2083 pass
2084
2085 @abstractmethod
2086 def __enter__(self) -> 'BinaryIO':
2087 pass
2088
2089
2090class TextIO(IO[str]):
2091 """Typed version of the return of open() in text mode."""
2092
Guido van Rossumd70fe632015-08-05 12:11:06 +02002093 __slots__ = ()
2094
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002095 @property
2096 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002097 def buffer(self) -> BinaryIO:
2098 pass
2099
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002100 @property
2101 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002102 def encoding(self) -> str:
2103 pass
2104
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002105 @property
2106 @abstractmethod
Guido van Rossum991d14f2016-11-09 13:12:51 -08002107 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002108 pass
2109
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002110 @property
2111 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002112 def line_buffering(self) -> bool:
2113 pass
2114
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002115 @property
2116 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002117 def newlines(self) -> Any:
2118 pass
2119
2120 @abstractmethod
2121 def __enter__(self) -> 'TextIO':
2122 pass
2123
2124
2125class io:
2126 """Wrapper namespace for IO generic classes."""
2127
2128 __all__ = ['IO', 'TextIO', 'BinaryIO']
2129 IO = IO
2130 TextIO = TextIO
2131 BinaryIO = BinaryIO
2132
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002133
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002134io.__name__ = __name__ + '.io'
2135sys.modules[io.__name__] = io
2136
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002137Pattern = _alias(stdlib_re.Pattern, 1)
2138Match = _alias(stdlib_re.Match, 1)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002139
2140class re:
2141 """Wrapper namespace for re type aliases."""
2142
2143 __all__ = ['Pattern', 'Match']
2144 Pattern = Pattern
2145 Match = Match
2146
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002147
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002148re.__name__ = __name__ + '.re'
2149sys.modules[re.__name__] = re