blob: 0f457ab1f56dfcd1907a4451fb0fd308a6db0e66 [file] [log] [blame]
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001"""
2The typing module: Support for gradual typing as defined by PEP 484.
3
4At large scale, the structure of the module is following:
Tim McNamara5265b3a2018-09-01 20:56:58 +12005* Imports and exports, all public names should be explicitly added to __all__.
Ivan Levkivskyid911e402018-01-20 11:23:59 +00006* Internal helper functions: these should never be used in code outside this module.
7* _SpecialForm and its instances (special forms): Any, NoReturn, ClassVar, Union, Optional
8* Two classes whose instances can be type arguments in addition to types: ForwardRef and TypeVar
9* The core of internal generics API: _GenericAlias and _VariadicGenericAlias, the latter is
10 currently only used by Tuple and Callable. All subscripted types like X[int], Union[int, str],
11 etc., are instances of either of these classes.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +010012* The public counterpart of the generics API consists of two classes: Generic and Protocol.
Ivan Levkivskyid911e402018-01-20 11:23:59 +000013* Public helper functions: get_type_hints, overload, cast, no_type_check,
14 no_type_check_decorator.
15* Generic aliases for collections.abc ABCs and few additional protocols.
ananthan-123ab6423f2020-02-19 10:03:05 +053016* Special types: NewType, NamedTuple, TypedDict.
Ivan Levkivskyid911e402018-01-20 11:23:59 +000017* Wrapper submodules for re and io related types.
18"""
19
HongWeipeng6ce03ec2019-09-27 15:54:26 +080020from abc import abstractmethod, ABCMeta
Batuhan Taskaya044a1042020-10-06 23:03:02 +030021import ast
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070022import collections
Ivan Levkivskyid911e402018-01-20 11:23:59 +000023import collections.abc
Brett Cannonf3ad0422016-04-15 10:51:30 -070024import contextlib
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070025import functools
Serhiy Storchaka09f32212018-05-26 21:19:26 +030026import operator
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070027import re as stdlib_re # Avoid confusion with the re we export.
28import sys
29import types
Guido van Rossum48b069a2020-04-07 09:50:06 -070030from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType, GenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070031
32# Please keep __all__ alphabetized within each category.
33__all__ = [
34 # Super-special typing primitives.
Jakub Stasiakcf5b1092020-02-05 02:10:19 +010035 'Annotated',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070036 'Any',
37 'Callable',
Guido van Rossum0a6976d2016-09-11 15:34:56 -070038 'ClassVar',
Ivan Levkivskyif3672422019-05-26 09:37:07 +010039 'Final',
Anthony Sottiled30da5d2019-05-29 11:19:38 -070040 'ForwardRef',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070041 'Generic',
Ivan Levkivskyib891c462019-05-26 09:37:48 +010042 'Literal',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070043 'Optional',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +010044 'Protocol',
Guido van Rossumeb9aca32016-05-24 16:38:22 -070045 'Tuple',
46 'Type',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070047 'TypeVar',
48 'Union',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070049
50 # ABCs (from collections.abc).
51 'AbstractSet', # collections.abc.Set.
52 'ByteString',
53 'Container',
Ivan Levkivskyi29fda8d2017-06-10 21:57:56 +020054 'ContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070055 'Hashable',
56 'ItemsView',
57 'Iterable',
58 'Iterator',
59 'KeysView',
60 'Mapping',
61 'MappingView',
62 'MutableMapping',
63 'MutableSequence',
64 'MutableSet',
65 'Sequence',
66 'Sized',
67 'ValuesView',
Ivan Levkivskyid911e402018-01-20 11:23:59 +000068 'Awaitable',
69 'AsyncIterator',
70 'AsyncIterable',
71 'Coroutine',
72 'Collection',
73 'AsyncGenerator',
74 'AsyncContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070075
76 # Structural checks, a.k.a. protocols.
77 'Reversible',
78 'SupportsAbs',
Ivan Levkivskyif06e0212017-05-02 19:14:07 +020079 'SupportsBytes',
80 'SupportsComplex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070081 'SupportsFloat',
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -070082 'SupportsIndex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070083 'SupportsInt',
84 'SupportsRound',
85
86 # Concrete collection types.
Anthony Sottiled30da5d2019-05-29 11:19:38 -070087 'ChainMap',
Ivan Levkivskyib692dc82017-02-13 22:50:14 +010088 'Counter',
Raymond Hettinger80490522017-01-16 22:42:37 -080089 'Deque',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070090 'Dict',
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070091 'DefaultDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070092 'List',
Anthony Sottiled30da5d2019-05-29 11:19:38 -070093 'OrderedDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070094 'Set',
Guido van Rossumefa798d2016-08-23 11:01:50 -070095 'FrozenSet',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070096 'NamedTuple', # Not really a type.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +010097 'TypedDict', # Not really a type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070098 'Generator',
99
100 # One-off things.
101 'AnyStr',
102 'cast',
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100103 'final',
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +0100104 'get_args',
105 'get_origin',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700106 'get_type_hints',
Patrick Reader0705ec82020-09-16 05:58:32 +0100107 'is_typeddict',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700108 'NewType',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700109 'no_type_check',
110 'no_type_check_decorator',
aetracht45738202018-03-19 14:41:32 -0400111 'NoReturn',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700112 'overload',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100113 'runtime_checkable',
Guido van Rossum0e0563c2016-04-05 14:54:25 -0700114 'Text',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700115 'TYPE_CHECKING',
Mikhail Golubev4f3c2502020-10-08 00:44:31 +0300116 'TypeAlias',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700117]
118
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700119# The pseudo-submodules 're' and 'io' are part of the public
120# namespace, but excluded from __all__ because they might stomp on
121# legitimate imports of those modules.
122
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700123def _type_check(arg, msg, is_argument=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000124 """Check that the argument is a type, and return it (internal helper).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700125
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000126 As a special case, accept None and return type(None) instead. Also wrap strings
127 into ForwardRef instances. Consider several corner cases, for example plain
128 special forms like Union are not valid, while Union[int, str] is OK, etc.
129 The msg argument is a human-readable error message, e.g::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700130
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000131 "Union[arg, ...]: arg should be a type."
Guido van Rossum4cefe742016-09-27 15:20:12 -0700132
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000133 We append the repr() of the actual value (truncated to 100 chars).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700134 """
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100135 invalid_generic_forms = (Generic, Protocol)
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700136 if is_argument:
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100137 invalid_generic_forms = invalid_generic_forms + (ClassVar, Final)
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400138
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000139 if arg is None:
140 return type(None)
141 if isinstance(arg, str):
142 return ForwardRef(arg)
143 if (isinstance(arg, _GenericAlias) and
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400144 arg.__origin__ in invalid_generic_forms):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000145 raise TypeError(f"{arg} is not valid as type argument")
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300146 if arg in (Any, NoReturn):
147 return arg
148 if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000149 raise TypeError(f"Plain {arg} is not valid as type argument")
Maggie Moss1b4552c2020-09-09 13:23:24 -0700150 if isinstance(arg, (type, TypeVar, ForwardRef, types.Union)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000151 return arg
152 if not callable(arg):
153 raise TypeError(f"{msg} Got {arg!r:.100}.")
154 return arg
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700155
156
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000157def _type_repr(obj):
158 """Return the repr() of an object, special-casing types (internal helper).
159
160 If obj is a type, we return a shorter version than the default
161 type.__repr__, based on the module and qualified name, which is
162 typically enough to uniquely identify a type. For everything
163 else, we fall back on repr(obj).
164 """
165 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:
Maggie Moss1b4552c2020-09-09 13:23:24 -0700210 if isinstance(p, (_UnionGenericAlias, types.Union)):
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
wyfo653f4202020-07-22 21:47:28 +0200249def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
Graham Bleaney84ef33c2020-09-08 18:41:10 -0400250 """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().
wyfo653f4202020-07-22 21:47:28 +0200252 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):
wyfo653f4202020-07-22 21:47:28 +0200256 return t._evaluate(globalns, localns, recursive_guard)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300257 if isinstance(t, (_GenericAlias, GenericAlias)):
wyfo653f4202020-07-22 21:47:28 +0200258 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
Mikhail Golubev4f3c2502020-10-08 00:44:31 +0300464@_SpecialForm
465def TypeAlias(self, parameters):
466 """Special marker indicating that an assignment should
467 be recognized as a proper type alias definition by type
468 checkers.
469
470 For example::
471
472 Predicate: TypeAlias = Callable[..., bool]
473
474 It's invalid when used anywhere except as in the example above.
475 """
476 raise TypeError(f"{self} is not subscriptable")
477
478
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000479class ForwardRef(_Final, _root=True):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800480 """Internal wrapper to hold a forward reference."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700481
Guido van Rossum4cefe742016-09-27 15:20:12 -0700482 __slots__ = ('__forward_arg__', '__forward_code__',
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400483 '__forward_evaluated__', '__forward_value__',
484 '__forward_is_argument__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700485
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700486 def __init__(self, arg, is_argument=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700487 if not isinstance(arg, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000488 raise TypeError(f"Forward reference must be a string -- got {arg!r}")
Batuhan Taskaya044a1042020-10-06 23:03:02 +0300489
490 # Double-stringified forward references is a result of activating
491 # the 'annotations' future by default. This way, we eliminate them in
492 # the runtime.
493 if arg.startswith(("'", '\"')) and arg.endswith(("'", '"')):
494 arg = arg[1:-1]
495
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700496 try:
497 code = compile(arg, '<string>', 'eval')
498 except SyntaxError:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000499 raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700500 self.__forward_arg__ = arg
501 self.__forward_code__ = code
502 self.__forward_evaluated__ = False
503 self.__forward_value__ = None
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400504 self.__forward_is_argument__ = is_argument
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700505
wyfo653f4202020-07-22 21:47:28 +0200506 def _evaluate(self, globalns, localns, recursive_guard):
507 if self.__forward_arg__ in recursive_guard:
508 return self
Guido van Rossumdad17902016-11-10 08:29:18 -0800509 if not self.__forward_evaluated__ or localns is not globalns:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700510 if globalns is None and localns is None:
511 globalns = localns = {}
512 elif globalns is None:
513 globalns = localns
514 elif localns is None:
515 localns = globalns
wyfo653f4202020-07-22 21:47:28 +0200516 type_ =_type_check(
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700517 eval(self.__forward_code__, globalns, localns),
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400518 "Forward references must evaluate to types.",
wyfo653f4202020-07-22 21:47:28 +0200519 is_argument=self.__forward_is_argument__,
520 )
521 self.__forward_value__ = _eval_type(
522 type_, globalns, localns, recursive_guard | {self.__forward_arg__}
523 )
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700524 self.__forward_evaluated__ = True
525 return self.__forward_value__
526
Guido van Rossum4cefe742016-09-27 15:20:12 -0700527 def __eq__(self, other):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000528 if not isinstance(other, ForwardRef):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700529 return NotImplemented
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100530 if self.__forward_evaluated__ and other.__forward_evaluated__:
531 return (self.__forward_arg__ == other.__forward_arg__ and
532 self.__forward_value__ == other.__forward_value__)
533 return self.__forward_arg__ == other.__forward_arg__
Guido van Rossum4cefe742016-09-27 15:20:12 -0700534
535 def __hash__(self):
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100536 return hash(self.__forward_arg__)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700537
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700538 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000539 return f'ForwardRef({self.__forward_arg__!r})'
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700540
541
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100542class TypeVar(_Final, _Immutable, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700543 """Type variable.
544
545 Usage::
546
547 T = TypeVar('T') # Can be anything
548 A = TypeVar('A', str, bytes) # Must be str or bytes
549
550 Type variables exist primarily for the benefit of static type
551 checkers. They serve as the parameters for generic types as well
552 as for generic function definitions. See class Generic for more
553 information on generic types. Generic functions work as follows:
554
Guido van Rossumb24569a2016-11-20 18:01:29 -0800555 def repeat(x: T, n: int) -> List[T]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700556 '''Return a list containing n references to x.'''
557 return [x]*n
558
559 def longest(x: A, y: A) -> A:
560 '''Return the longest of two strings.'''
561 return x if len(x) >= len(y) else y
562
563 The latter example's signature is essentially the overloading
564 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
565 that if the arguments are instances of some subclass of str,
566 the return type is still plain str.
567
Guido van Rossumb24569a2016-11-20 18:01:29 -0800568 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700569
Guido van Rossumefa798d2016-08-23 11:01:50 -0700570 Type variables defined with covariant=True or contravariant=True
João D. Ferreira86bfed32018-07-07 16:41:20 +0100571 can be used to declare covariant or contravariant generic types.
Guido van Rossumefa798d2016-08-23 11:01:50 -0700572 See PEP 484 for more details. By default generic types are invariant
573 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700574
575 Type variables can be introspected. e.g.:
576
577 T.__name__ == 'T'
578 T.__constraints__ == ()
579 T.__covariant__ == False
580 T.__contravariant__ = False
581 A.__constraints__ == (str, bytes)
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100582
583 Note that only type variables defined in global scope can be pickled.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700584 """
585
Guido van Rossum4cefe742016-09-27 15:20:12 -0700586 __slots__ = ('__name__', '__bound__', '__constraints__',
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300587 '__covariant__', '__contravariant__', '__dict__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700588
589 def __init__(self, name, *constraints, bound=None,
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800590 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700591 self.__name__ = name
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700592 if covariant and contravariant:
Guido van Rossumefa798d2016-08-23 11:01:50 -0700593 raise ValueError("Bivariant types are not supported.")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700594 self.__covariant__ = bool(covariant)
595 self.__contravariant__ = bool(contravariant)
596 if constraints and bound is not None:
597 raise TypeError("Constraints cannot be combined with bound=...")
598 if constraints and len(constraints) == 1:
599 raise TypeError("A single constraint is not allowed")
600 msg = "TypeVar(name, constraint, ...): constraints must be types."
601 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
602 if bound:
603 self.__bound__ = _type_check(bound, "Bound must be a type.")
604 else:
605 self.__bound__ = None
HongWeipenga25a04f2020-04-21 04:01:53 +0800606 try:
607 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling
608 except (AttributeError, ValueError):
609 def_mod = None
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300610 if def_mod != 'typing':
611 self.__module__ = def_mod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700612
Maggie Moss1b4552c2020-09-09 13:23:24 -0700613 def __or__(self, right):
614 return Union[self, right]
615
616 def __ror__(self, right):
617 return Union[self, right]
618
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700619 def __repr__(self):
620 if self.__covariant__:
621 prefix = '+'
622 elif self.__contravariant__:
623 prefix = '-'
624 else:
625 prefix = '~'
626 return prefix + self.__name__
627
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100628 def __reduce__(self):
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300629 return self.__name__
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100630
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700631
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000632def _is_dunder(attr):
633 return attr.startswith('__') and attr.endswith('__')
Guido van Rossum83ec3022017-01-17 20:43:28 -0800634
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300635class _BaseGenericAlias(_Final, _root=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000636 """The central part of internal API.
637
638 This represents a generic version of type 'origin' with type arguments 'params'.
639 There are two kind of these aliases: user defined and special. The special ones
640 are wrappers around builtin collections and ABCs in collections.abc. These must
641 have 'name' always set. If 'inst' is False, then the alias can't be instantiated,
642 this is used by e.g. typing.List and typing.Dict.
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700643 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300644 def __init__(self, origin, *, inst=True, name=None):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000645 self._inst = inst
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000646 self._name = name
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700647 self.__origin__ = origin
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000648 self.__slots__ = None # This is not documented.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300649
650 def __call__(self, *args, **kwargs):
651 if not self._inst:
652 raise TypeError(f"Type {self._name} cannot be instantiated; "
653 f"use {self.__origin__.__name__}() instead")
654 result = self.__origin__(*args, **kwargs)
655 try:
656 result.__orig_class__ = self
657 except AttributeError:
658 pass
659 return result
660
661 def __mro_entries__(self, bases):
662 res = []
663 if self.__origin__ not in bases:
664 res.append(self.__origin__)
665 i = bases.index(self)
666 for b in bases[i+1:]:
667 if isinstance(b, _BaseGenericAlias) or issubclass(b, Generic):
668 break
669 else:
670 res.append(Generic)
671 return tuple(res)
672
673 def __getattr__(self, attr):
674 # We are careful for copy and pickle.
675 # Also for simplicity we just don't relay all dunder names
676 if '__origin__' in self.__dict__ and not _is_dunder(attr):
677 return getattr(self.__origin__, attr)
678 raise AttributeError(attr)
679
680 def __setattr__(self, attr, val):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300681 if _is_dunder(attr) or attr in ('_name', '_inst', '_nparams'):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300682 super().__setattr__(attr, val)
683 else:
684 setattr(self.__origin__, attr, val)
685
686 def __instancecheck__(self, obj):
687 return self.__subclasscheck__(type(obj))
688
689 def __subclasscheck__(self, cls):
690 raise TypeError("Subscripted generics cannot be used with"
691 " class and instance checks")
692
693
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300694# Special typing constructs Union, Optional, Generic, Callable and Tuple
695# use three special attributes for internal bookkeeping of generic types:
696# * __parameters__ is a tuple of unique free type parameters of a generic
697# type, for example, Dict[T, T].__parameters__ == (T,);
698# * __origin__ keeps a reference to a type that was subscripted,
699# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
700# the type.
701# * __args__ is a tuple of all arguments used in subscripting,
702# e.g., Dict[T, int].__args__ == (T, int).
703
704
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300705class _GenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300706 def __init__(self, origin, params, *, inst=True, name=None):
707 super().__init__(origin, inst=inst, name=name)
708 if not isinstance(params, tuple):
709 params = (params,)
710 self.__args__ = tuple(... if a is _TypingEllipsis else
711 () if a is _TypingEmpty else
712 a for a in params)
713 self.__parameters__ = _collect_type_vars(params)
714 if not name:
715 self.__module__ = origin.__module__
716
717 def __eq__(self, other):
718 if not isinstance(other, _GenericAlias):
719 return NotImplemented
720 return (self.__origin__ == other.__origin__
721 and self.__args__ == other.__args__)
722
723 def __hash__(self):
724 return hash((self.__origin__, self.__args__))
725
Maggie Moss1b4552c2020-09-09 13:23:24 -0700726 def __or__(self, right):
727 return Union[self, right]
728
729 def __ror__(self, right):
730 return Union[self, right]
731
Guido van Rossum4cefe742016-09-27 15:20:12 -0700732 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700733 def __getitem__(self, params):
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100734 if self.__origin__ in (Generic, Protocol):
735 # Can't subscript Generic[...] or Protocol[...].
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000736 raise TypeError(f"Cannot subscript already-subscripted {self}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700737 if not isinstance(params, tuple):
738 params = (params,)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700739 msg = "Parameters to generic types must be types."
740 params = tuple(_type_check(p, msg) for p in params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300741 _check_generic(self, params, len(self.__parameters__))
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300742
743 subst = dict(zip(self.__parameters__, params))
744 new_args = []
745 for arg in self.__args__:
746 if isinstance(arg, TypeVar):
747 arg = subst[arg]
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300748 elif isinstance(arg, (_GenericAlias, GenericAlias)):
Serhiy Storchaka0122d482020-05-10 13:39:40 +0300749 subparams = arg.__parameters__
750 if subparams:
751 subargs = tuple(subst[x] for x in subparams)
752 arg = arg[subargs]
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300753 new_args.append(arg)
754 return self.copy_with(tuple(new_args))
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100755
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000756 def copy_with(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300757 return self.__class__(self.__origin__, params, name=self._name, inst=self._inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700758
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000759 def __repr__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300760 if self._name:
761 name = 'typing.' + self._name
762 else:
763 name = _type_repr(self.__origin__)
764 args = ", ".join([_type_repr(a) for a in self.__args__])
765 return f'{name}[{args}]'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000766
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300767 def __reduce__(self):
768 if self._name:
769 origin = globals()[self._name]
770 else:
771 origin = self.__origin__
772 args = tuple(self.__args__)
773 if len(args) == 1 and not isinstance(args[0], tuple):
774 args, = args
775 return operator.getitem, (origin, args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000776
777 def __mro_entries__(self, bases):
778 if self._name: # generic version of an ABC or built-in class
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300779 return super().__mro_entries__(bases)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000780 if self.__origin__ is Generic:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100781 if Protocol in bases:
782 return ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000783 i = bases.index(self)
784 for b in bases[i+1:]:
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300785 if isinstance(b, _BaseGenericAlias) and b is not self:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000786 return ()
787 return (self.__origin__,)
788
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000789
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300790# _nparams is the number of accepted parameters, e.g. 0 for Hashable,
791# 1 for List and 2 for Dict. It may be -1 if variable number of
792# parameters are accepted (needs custom __getitem__).
793
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300794class _SpecialGenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300795 def __init__(self, origin, nparams, *, inst=True, name=None):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300796 if name is None:
797 name = origin.__name__
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300798 super().__init__(origin, inst=inst, name=name)
799 self._nparams = nparams
Serhiy Storchaka2fbc57a2020-05-10 15:14:27 +0300800 if origin.__module__ == 'builtins':
801 self.__doc__ = f'A generic version of {origin.__qualname__}.'
802 else:
803 self.__doc__ = f'A generic version of {origin.__module__}.{origin.__qualname__}.'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000804
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300805 @_tp_cache
806 def __getitem__(self, params):
807 if not isinstance(params, tuple):
808 params = (params,)
809 msg = "Parameters to generic types must be types."
810 params = tuple(_type_check(p, msg) for p in params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300811 _check_generic(self, params, self._nparams)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300812 return self.copy_with(params)
813
814 def copy_with(self, params):
815 return _GenericAlias(self.__origin__, params,
816 name=self._name, inst=self._inst)
817
818 def __repr__(self):
819 return 'typing.' + self._name
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000820
821 def __subclasscheck__(self, cls):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300822 if isinstance(cls, _SpecialGenericAlias):
823 return issubclass(cls.__origin__, self.__origin__)
824 if not isinstance(cls, _GenericAlias):
825 return issubclass(cls, self.__origin__)
826 return super().__subclasscheck__(cls)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700827
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100828 def __reduce__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300829 return self._name
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100830
Maggie Moss1b4552c2020-09-09 13:23:24 -0700831 def __or__(self, right):
832 return Union[self, right]
833
834 def __ror__(self, right):
835 return Union[self, right]
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700836
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300837class _CallableGenericAlias(_GenericAlias, _root=True):
838 def __repr__(self):
839 assert self._name == 'Callable'
840 if len(self.__args__) == 2 and self.__args__[0] is Ellipsis:
841 return super().__repr__()
842 return (f'typing.Callable'
843 f'[[{", ".join([_type_repr(a) for a in self.__args__[:-1]])}], '
844 f'{_type_repr(self.__args__[-1])}]')
845
846 def __reduce__(self):
847 args = self.__args__
848 if not (len(args) == 2 and args[0] is ...):
849 args = list(args[:-1]), args[-1]
850 return operator.getitem, (Callable, args)
851
852
853class _CallableType(_SpecialGenericAlias, _root=True):
854 def copy_with(self, params):
855 return _CallableGenericAlias(self.__origin__, params,
856 name=self._name, inst=self._inst)
857
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000858 def __getitem__(self, params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000859 if not isinstance(params, tuple) or len(params) != 2:
860 raise TypeError("Callable must be used as "
861 "Callable[[arg, ...], result].")
862 args, result = params
863 if args is Ellipsis:
864 params = (Ellipsis, result)
865 else:
866 if not isinstance(args, list):
867 raise TypeError(f"Callable[args, result]: args must be a list."
868 f" Got {args}")
869 params = (tuple(args), result)
870 return self.__getitem_inner__(params)
871
872 @_tp_cache
873 def __getitem_inner__(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300874 args, result = params
875 msg = "Callable[args, result]: result must be a type."
876 result = _type_check(result, msg)
877 if args is Ellipsis:
878 return self.copy_with((_TypingEllipsis, result))
879 msg = "Callable[[arg, ...], result]: each arg must be a type."
880 args = tuple(_type_check(arg, msg) for arg in args)
881 params = args + (result,)
882 return self.copy_with(params)
883
884
885class _TupleType(_SpecialGenericAlias, _root=True):
886 @_tp_cache
887 def __getitem__(self, params):
888 if params == ():
889 return self.copy_with((_TypingEmpty,))
890 if not isinstance(params, tuple):
891 params = (params,)
892 if len(params) == 2 and params[1] is ...:
893 msg = "Tuple[t, ...]: t must be a type."
894 p = _type_check(params[0], msg)
895 return self.copy_with((p, _TypingEllipsis))
896 msg = "Tuple[t0, t1, ...]: each t must be a type."
897 params = tuple(_type_check(p, msg) for p in params)
898 return self.copy_with(params)
899
900
901class _UnionGenericAlias(_GenericAlias, _root=True):
902 def copy_with(self, params):
903 return Union[params]
904
905 def __eq__(self, other):
906 if not isinstance(other, _UnionGenericAlias):
907 return NotImplemented
908 return set(self.__args__) == set(other.__args__)
909
910 def __hash__(self):
911 return hash(frozenset(self.__args__))
912
913 def __repr__(self):
914 args = self.__args__
915 if len(args) == 2:
916 if args[0] is type(None):
917 return f'typing.Optional[{_type_repr(args[1])}]'
918 elif args[1] is type(None):
919 return f'typing.Optional[{_type_repr(args[0])}]'
920 return super().__repr__()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000921
Maggie Moss1b4552c2020-09-09 13:23:24 -0700922 def __instancecheck__(self, obj):
923 return self.__subclasscheck__(type(obj))
924
925 def __subclasscheck__(self, cls):
926 for arg in self.__args__:
927 if issubclass(cls, arg):
928 return True
929
930
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000931
932class Generic:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700933 """Abstract base class for generic types.
934
Guido van Rossumb24569a2016-11-20 18:01:29 -0800935 A generic type is typically declared by inheriting from
936 this class parameterized with one or more type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700937 For example, a generic mapping type might be defined as::
938
939 class Mapping(Generic[KT, VT]):
940 def __getitem__(self, key: KT) -> VT:
941 ...
942 # Etc.
943
944 This class can then be used as follows::
945
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700946 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700947 try:
948 return mapping[key]
949 except KeyError:
950 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700951 """
Guido van Rossumd70fe632015-08-05 12:11:06 +0200952 __slots__ = ()
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100953 _is_protocol = False
Guido van Rossumd70fe632015-08-05 12:11:06 +0200954
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000955 @_tp_cache
956 def __class_getitem__(cls, params):
957 if not isinstance(params, tuple):
958 params = (params,)
959 if not params and cls is not Tuple:
960 raise TypeError(
961 f"Parameter list to {cls.__qualname__}[...] cannot be empty")
962 msg = "Parameters to generic types must be types."
963 params = tuple(_type_check(p, msg) for p in params)
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100964 if cls in (Generic, Protocol):
965 # Generic and Protocol can only be subscripted with unique type variables.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000966 if not all(isinstance(p, TypeVar) for p in params):
967 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100968 f"Parameters to {cls.__name__}[...] must all be type variables")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000969 if len(set(params)) != len(params):
970 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100971 f"Parameters to {cls.__name__}[...] must all be unique")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000972 else:
973 # Subscripting a regular Generic subclass.
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300974 _check_generic(cls, params, len(cls.__parameters__))
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000975 return _GenericAlias(cls, params)
976
977 def __init_subclass__(cls, *args, **kwargs):
Ivan Levkivskyiee566fe2018-04-04 17:00:15 +0100978 super().__init_subclass__(*args, **kwargs)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000979 tvars = []
980 if '__orig_bases__' in cls.__dict__:
981 error = Generic in cls.__orig_bases__
982 else:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100983 error = Generic in cls.__bases__ and cls.__name__ != 'Protocol'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000984 if error:
985 raise TypeError("Cannot inherit from plain Generic")
986 if '__orig_bases__' in cls.__dict__:
987 tvars = _collect_type_vars(cls.__orig_bases__)
988 # Look for Generic[T1, ..., Tn].
989 # If found, tvars must be a subset of it.
990 # If not found, tvars is it.
991 # Also check for and reject plain Generic,
992 # and reject multiple Generic[...].
993 gvars = None
994 for base in cls.__orig_bases__:
995 if (isinstance(base, _GenericAlias) and
996 base.__origin__ is Generic):
997 if gvars is not None:
998 raise TypeError(
999 "Cannot inherit from Generic[...] multiple types.")
1000 gvars = base.__parameters__
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001001 if gvars is not None:
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001002 tvarset = set(tvars)
1003 gvarset = set(gvars)
1004 if not tvarset <= gvarset:
1005 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
1006 s_args = ', '.join(str(g) for g in gvars)
1007 raise TypeError(f"Some type variables ({s_vars}) are"
1008 f" not listed in Generic[{s_args}]")
1009 tvars = gvars
1010 cls.__parameters__ = tuple(tvars)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001011
1012
1013class _TypingEmpty:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001014 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
1015 to allow empty list/tuple in specific places, without allowing them
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001016 to sneak in where prohibited.
1017 """
1018
1019
1020class _TypingEllipsis:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001021 """Internal placeholder for ... (ellipsis)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001022
1023
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001024_TYPING_INTERNALS = ['__parameters__', '__orig_bases__', '__orig_class__',
1025 '_is_protocol', '_is_runtime_protocol']
1026
1027_SPECIAL_NAMES = ['__abstractmethods__', '__annotations__', '__dict__', '__doc__',
1028 '__init__', '__module__', '__new__', '__slots__',
Guido van Rossum48b069a2020-04-07 09:50:06 -07001029 '__subclasshook__', '__weakref__', '__class_getitem__']
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001030
1031# These special attributes will be not collected as protocol members.
1032EXCLUDED_ATTRIBUTES = _TYPING_INTERNALS + _SPECIAL_NAMES + ['_MutableMapping__marker']
1033
1034
1035def _get_protocol_attrs(cls):
1036 """Collect protocol members from a protocol class objects.
1037
1038 This includes names actually defined in the class dictionary, as well
1039 as names that appear in annotations. Special names (above) are skipped.
1040 """
1041 attrs = set()
1042 for base in cls.__mro__[:-1]: # without object
1043 if base.__name__ in ('Protocol', 'Generic'):
1044 continue
1045 annotations = getattr(base, '__annotations__', {})
1046 for attr in list(base.__dict__.keys()) + list(annotations.keys()):
1047 if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
1048 attrs.add(attr)
1049 return attrs
1050
1051
1052def _is_callable_members_only(cls):
1053 # PEP 544 prohibits using issubclass() with protocols that have non-method members.
1054 return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls))
1055
1056
1057def _no_init(self, *args, **kwargs):
1058 if type(self)._is_protocol:
1059 raise TypeError('Protocols cannot be instantiated')
1060
1061
1062def _allow_reckless_class_cheks():
Nickolena Fishercfaf4c02020-04-26 12:49:11 -05001063 """Allow instance and class checks for special stdlib modules.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001064
1065 The abc and functools modules indiscriminately call isinstance() and
1066 issubclass() on the whole MRO of a user class, which may contain protocols.
1067 """
1068 try:
1069 return sys._getframe(3).f_globals['__name__'] in ['abc', 'functools']
1070 except (AttributeError, ValueError): # For platforms without _getframe().
1071 return True
1072
1073
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001074_PROTO_ALLOWLIST = {
Divij Rajkumar692a0dc2019-09-12 11:13:51 +01001075 'collections.abc': [
1076 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
1077 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
1078 ],
1079 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
1080}
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001081
1082
1083class _ProtocolMeta(ABCMeta):
1084 # This metaclass is really unfortunate and exists only because of
1085 # the lack of __instancehook__.
1086 def __instancecheck__(cls, instance):
1087 # We need this method for situations where attributes are
1088 # assigned in __init__.
1089 if ((not getattr(cls, '_is_protocol', False) or
1090 _is_callable_members_only(cls)) and
1091 issubclass(instance.__class__, cls)):
1092 return True
1093 if cls._is_protocol:
1094 if all(hasattr(instance, attr) and
1095 # All *methods* can be blocked by setting them to None.
1096 (not callable(getattr(cls, attr, None)) or
1097 getattr(instance, attr) is not None)
1098 for attr in _get_protocol_attrs(cls)):
1099 return True
1100 return super().__instancecheck__(instance)
1101
1102
1103class Protocol(Generic, metaclass=_ProtocolMeta):
1104 """Base class for protocol classes.
1105
1106 Protocol classes are defined as::
1107
1108 class Proto(Protocol):
1109 def meth(self) -> int:
1110 ...
1111
1112 Such classes are primarily used with static type checkers that recognize
1113 structural subtyping (static duck-typing), for example::
1114
1115 class C:
1116 def meth(self) -> int:
1117 return 0
1118
1119 def func(x: Proto) -> int:
1120 return x.meth()
1121
1122 func(C()) # Passes static type check
1123
1124 See PEP 544 for details. Protocol classes decorated with
1125 @typing.runtime_checkable act as simple-minded runtime protocols that check
1126 only the presence of given attributes, ignoring their type signatures.
1127 Protocol classes can be generic, they are defined as::
1128
1129 class GenProto(Protocol[T]):
1130 def meth(self) -> T:
1131 ...
1132 """
1133 __slots__ = ()
1134 _is_protocol = True
1135 _is_runtime_protocol = False
1136
1137 def __init_subclass__(cls, *args, **kwargs):
1138 super().__init_subclass__(*args, **kwargs)
1139
1140 # Determine if this is a protocol or a concrete subclass.
1141 if not cls.__dict__.get('_is_protocol', False):
1142 cls._is_protocol = any(b is Protocol for b in cls.__bases__)
1143
1144 # Set (or override) the protocol subclass hook.
1145 def _proto_hook(other):
1146 if not cls.__dict__.get('_is_protocol', False):
1147 return NotImplemented
1148
1149 # First, perform various sanity checks.
1150 if not getattr(cls, '_is_runtime_protocol', False):
1151 if _allow_reckless_class_cheks():
1152 return NotImplemented
1153 raise TypeError("Instance and class checks can only be used with"
1154 " @runtime_checkable protocols")
1155 if not _is_callable_members_only(cls):
1156 if _allow_reckless_class_cheks():
1157 return NotImplemented
1158 raise TypeError("Protocols with non-method members"
1159 " don't support issubclass()")
1160 if not isinstance(other, type):
1161 # Same error message as for issubclass(1, int).
1162 raise TypeError('issubclass() arg 1 must be a class')
1163
1164 # Second, perform the actual structural compatibility check.
1165 for attr in _get_protocol_attrs(cls):
1166 for base in other.__mro__:
1167 # Check if the members appears in the class dictionary...
1168 if attr in base.__dict__:
1169 if base.__dict__[attr] is None:
1170 return NotImplemented
1171 break
1172
1173 # ...or in annotations, if it is a sub-protocol.
1174 annotations = getattr(base, '__annotations__', {})
1175 if (isinstance(annotations, collections.abc.Mapping) and
1176 attr in annotations and
1177 issubclass(other, Generic) and other._is_protocol):
1178 break
1179 else:
1180 return NotImplemented
1181 return True
1182
1183 if '__subclasshook__' not in cls.__dict__:
1184 cls.__subclasshook__ = _proto_hook
1185
1186 # We have nothing more to do for non-protocols...
1187 if not cls._is_protocol:
1188 return
1189
1190 # ... otherwise check consistency of bases, and prohibit instantiation.
1191 for base in cls.__bases__:
1192 if not (base in (object, Generic) or
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001193 base.__module__ in _PROTO_ALLOWLIST and
1194 base.__name__ in _PROTO_ALLOWLIST[base.__module__] or
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001195 issubclass(base, Generic) and base._is_protocol):
1196 raise TypeError('Protocols can only inherit from other'
1197 ' protocols, got %r' % base)
1198 cls.__init__ = _no_init
1199
1200
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001201class _AnnotatedAlias(_GenericAlias, _root=True):
1202 """Runtime representation of an annotated type.
1203
1204 At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't'
1205 with extra annotations. The alias behaves like a normal typing alias,
1206 instantiating is the same as instantiating the underlying type, binding
1207 it to types is also the same.
1208 """
1209 def __init__(self, origin, metadata):
1210 if isinstance(origin, _AnnotatedAlias):
1211 metadata = origin.__metadata__ + metadata
1212 origin = origin.__origin__
1213 super().__init__(origin, origin)
1214 self.__metadata__ = metadata
1215
1216 def copy_with(self, params):
1217 assert len(params) == 1
1218 new_type = params[0]
1219 return _AnnotatedAlias(new_type, self.__metadata__)
1220
1221 def __repr__(self):
1222 return "typing.Annotated[{}, {}]".format(
1223 _type_repr(self.__origin__),
1224 ", ".join(repr(a) for a in self.__metadata__)
1225 )
1226
1227 def __reduce__(self):
1228 return operator.getitem, (
1229 Annotated, (self.__origin__,) + self.__metadata__
1230 )
1231
1232 def __eq__(self, other):
1233 if not isinstance(other, _AnnotatedAlias):
1234 return NotImplemented
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001235 return (self.__origin__ == other.__origin__
1236 and self.__metadata__ == other.__metadata__)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001237
1238 def __hash__(self):
1239 return hash((self.__origin__, self.__metadata__))
1240
1241
1242class Annotated:
1243 """Add context specific metadata to a type.
1244
1245 Example: Annotated[int, runtime_check.Unsigned] indicates to the
1246 hypothetical runtime_check module that this type is an unsigned int.
1247 Every other consumer of this type can ignore this metadata and treat
1248 this type as int.
1249
1250 The first argument to Annotated must be a valid type.
1251
1252 Details:
1253
1254 - It's an error to call `Annotated` with less than two arguments.
1255 - Nested Annotated are flattened::
1256
1257 Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
1258
1259 - Instantiating an annotated type is equivalent to instantiating the
1260 underlying type::
1261
1262 Annotated[C, Ann1](5) == C(5)
1263
1264 - Annotated can be used as a generic type alias::
1265
1266 Optimized = Annotated[T, runtime.Optimize()]
1267 Optimized[int] == Annotated[int, runtime.Optimize()]
1268
1269 OptimizedList = Annotated[List[T], runtime.Optimize()]
1270 OptimizedList[int] == Annotated[List[int], runtime.Optimize()]
1271 """
1272
1273 __slots__ = ()
1274
1275 def __new__(cls, *args, **kwargs):
1276 raise TypeError("Type Annotated cannot be instantiated.")
1277
1278 @_tp_cache
1279 def __class_getitem__(cls, params):
1280 if not isinstance(params, tuple) or len(params) < 2:
1281 raise TypeError("Annotated[...] should be used "
1282 "with at least two arguments (a type and an "
1283 "annotation).")
1284 msg = "Annotated[t, ...]: t must be a type."
1285 origin = _type_check(params[0], msg)
1286 metadata = tuple(params[1:])
1287 return _AnnotatedAlias(origin, metadata)
1288
1289 def __init_subclass__(cls, *args, **kwargs):
1290 raise TypeError(
1291 "Cannot subclass {}.Annotated".format(cls.__module__)
1292 )
1293
1294
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001295def runtime_checkable(cls):
1296 """Mark a protocol class as a runtime protocol.
1297
1298 Such protocol can be used with isinstance() and issubclass().
1299 Raise TypeError if applied to a non-protocol class.
1300 This allows a simple-minded structural check very similar to
1301 one trick ponies in collections.abc such as Iterable.
1302 For example::
1303
1304 @runtime_checkable
1305 class Closable(Protocol):
1306 def close(self): ...
1307
1308 assert isinstance(open('/some/file'), Closable)
1309
1310 Warning: this will check only the presence of the required methods,
1311 not their type signatures!
1312 """
1313 if not issubclass(cls, Generic) or not cls._is_protocol:
1314 raise TypeError('@runtime_checkable can be only applied to protocol classes,'
1315 ' got %r' % cls)
1316 cls._is_runtime_protocol = True
1317 return cls
1318
1319
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001320def cast(typ, val):
1321 """Cast a value to a type.
1322
1323 This returns the value unchanged. To the type checker this
1324 signals that the return value has the designated type, but at
1325 runtime we intentionally don't check anything (we want this
1326 to be as fast as possible).
1327 """
1328 return val
1329
1330
1331def _get_defaults(func):
1332 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -08001333 try:
1334 code = func.__code__
1335 except AttributeError:
1336 # Some built-in functions don't have __code__, __defaults__, etc.
1337 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001338 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001339 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001340 arg_names = arg_names[:pos_count]
1341 defaults = func.__defaults__ or ()
1342 kwdefaults = func.__kwdefaults__
1343 res = dict(kwdefaults) if kwdefaults else {}
1344 pos_offset = pos_count - len(defaults)
1345 for name, value in zip(arg_names[pos_offset:], defaults):
1346 assert name not in res
1347 res[name] = value
1348 return res
1349
1350
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001351_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
1352 types.MethodType, types.ModuleType,
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02001353 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001354
1355
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001356def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
Guido van Rossum991d14f2016-11-09 13:12:51 -08001357 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001358
Guido van Rossum991d14f2016-11-09 13:12:51 -08001359 This is often the same as obj.__annotations__, but it handles
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001360 forward references encoded as string literals, adds Optional[t] if a
1361 default value equal to None is set and recursively replaces all
1362 'Annotated[T, ...]' with 'T' (unless 'include_extras=True').
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001363
Guido van Rossum991d14f2016-11-09 13:12:51 -08001364 The argument may be a module, class, method, or function. The annotations
1365 are returned as a dictionary. For classes, annotations include also
1366 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001367
Guido van Rossum991d14f2016-11-09 13:12:51 -08001368 TypeError is raised if the argument is not of a type that can contain
1369 annotations, and an empty dictionary is returned if no annotations are
1370 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001371
Guido van Rossum991d14f2016-11-09 13:12:51 -08001372 BEWARE -- the behavior of globalns and localns is counterintuitive
1373 (unless you are familiar with how eval() and exec() work). The
1374 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001375
Guido van Rossum991d14f2016-11-09 13:12:51 -08001376 - If no dict arguments are passed, an attempt is made to use the
Łukasz Langaf350a262017-09-14 14:33:00 -04001377 globals from obj (or the respective module's globals for classes),
1378 and these are also used as the locals. If the object does not appear
1379 to have globals, an empty dictionary is used.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001380
Guido van Rossum991d14f2016-11-09 13:12:51 -08001381 - If one dict argument is passed, it is used for both globals and
1382 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001383
Guido van Rossum991d14f2016-11-09 13:12:51 -08001384 - If two dict arguments are passed, they specify globals and
1385 locals, respectively.
1386 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001387
Guido van Rossum991d14f2016-11-09 13:12:51 -08001388 if getattr(obj, '__no_type_check__', None):
1389 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001390 # Classes require a special treatment.
1391 if isinstance(obj, type):
1392 hints = {}
1393 for base in reversed(obj.__mro__):
Łukasz Langaf350a262017-09-14 14:33:00 -04001394 if globalns is None:
1395 base_globals = sys.modules[base.__module__].__dict__
1396 else:
1397 base_globals = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001398 ann = base.__dict__.get('__annotations__', {})
1399 for name, value in ann.items():
1400 if value is None:
1401 value = type(None)
1402 if isinstance(value, str):
Nina Zakharenko0e61dff2018-05-22 20:32:10 -07001403 value = ForwardRef(value, is_argument=False)
Łukasz Langaf350a262017-09-14 14:33:00 -04001404 value = _eval_type(value, base_globals, localns)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001405 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001406 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
Łukasz Langaf350a262017-09-14 14:33:00 -04001407
1408 if globalns is None:
1409 if isinstance(obj, types.ModuleType):
1410 globalns = obj.__dict__
1411 else:
benedwards140aca3a32019-11-21 17:24:58 +00001412 nsobj = obj
1413 # Find globalns for the unwrapped object.
1414 while hasattr(nsobj, '__wrapped__'):
1415 nsobj = nsobj.__wrapped__
1416 globalns = getattr(nsobj, '__globals__', {})
Łukasz Langaf350a262017-09-14 14:33:00 -04001417 if localns is None:
1418 localns = globalns
1419 elif localns is None:
1420 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001421 hints = getattr(obj, '__annotations__', None)
1422 if hints is None:
1423 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001424 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001425 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001426 else:
1427 raise TypeError('{!r} is not a module, class, method, '
1428 'or function.'.format(obj))
1429 defaults = _get_defaults(obj)
1430 hints = dict(hints)
1431 for name, value in hints.items():
1432 if value is None:
1433 value = type(None)
1434 if isinstance(value, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001435 value = ForwardRef(value)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001436 value = _eval_type(value, globalns, localns)
1437 if name in defaults and defaults[name] is None:
1438 value = Optional[value]
1439 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001440 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
1441
1442
1443def _strip_annotations(t):
1444 """Strips the annotations from a given type.
1445 """
1446 if isinstance(t, _AnnotatedAlias):
1447 return _strip_annotations(t.__origin__)
1448 if isinstance(t, _GenericAlias):
1449 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1450 if stripped_args == t.__args__:
1451 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001452 return t.copy_with(stripped_args)
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001453 if isinstance(t, GenericAlias):
1454 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1455 if stripped_args == t.__args__:
1456 return t
1457 return GenericAlias(t.__origin__, stripped_args)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001458 return t
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001459
1460
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001461def get_origin(tp):
1462 """Get the unsubscripted version of a type.
1463
Jakub Stasiak38aaaaa2020-02-07 02:15:12 +01001464 This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
1465 and Annotated. Return None for unsupported types. Examples::
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001466
1467 get_origin(Literal[42]) is Literal
1468 get_origin(int) is None
1469 get_origin(ClassVar[int]) is ClassVar
1470 get_origin(Generic) is Generic
1471 get_origin(Generic[T]) is Generic
1472 get_origin(Union[T, int]) is Union
1473 get_origin(List[Tuple[T, T]][int]) == list
1474 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001475 if isinstance(tp, _AnnotatedAlias):
1476 return Annotated
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001477 if isinstance(tp, (_BaseGenericAlias, GenericAlias)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001478 return tp.__origin__
1479 if tp is Generic:
1480 return Generic
1481 return None
1482
1483
1484def get_args(tp):
1485 """Get type arguments with all substitutions performed.
1486
1487 For unions, basic simplifications used by Union constructor are performed.
1488 Examples::
1489 get_args(Dict[str, int]) == (str, int)
1490 get_args(int) == ()
1491 get_args(Union[int, Union[T, int], str][int]) == (int, str)
1492 get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
1493 get_args(Callable[[], T][int]) == ([], int)
1494 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001495 if isinstance(tp, _AnnotatedAlias):
1496 return (tp.__origin__,) + tp.__metadata__
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001497 if isinstance(tp, _GenericAlias):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001498 res = tp.__args__
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001499 if tp.__origin__ is collections.abc.Callable and res[0] is not Ellipsis:
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001500 res = (list(res[:-1]), res[-1])
1501 return res
Serhiy Storchaka6292be72020-04-27 10:27:21 +03001502 if isinstance(tp, GenericAlias):
1503 return tp.__args__
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001504 return ()
1505
1506
Patrick Reader0705ec82020-09-16 05:58:32 +01001507def is_typeddict(tp):
1508 """Check if an annotation is a TypedDict class
1509
1510 For example::
1511 class Film(TypedDict):
1512 title: str
1513 year: int
1514
1515 is_typeddict(Film) # => True
1516 is_typeddict(Union[list, str]) # => False
1517 """
1518 return isinstance(tp, _TypedDictMeta)
1519
1520
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001521def no_type_check(arg):
1522 """Decorator to indicate that annotations are not type hints.
1523
1524 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001525 applies recursively to all methods and classes defined in that class
1526 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001527
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001528 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001529 """
1530 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001531 arg_attrs = arg.__dict__.copy()
1532 for attr, val in arg.__dict__.items():
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001533 if val in arg.__bases__ + (arg,):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001534 arg_attrs.pop(attr)
1535 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001536 if isinstance(obj, types.FunctionType):
1537 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001538 if isinstance(obj, type):
1539 no_type_check(obj)
1540 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001541 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001542 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001543 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001544 return arg
1545
1546
1547def no_type_check_decorator(decorator):
1548 """Decorator to give another decorator the @no_type_check effect.
1549
1550 This wraps the decorator with something that wraps the decorated
1551 function in @no_type_check.
1552 """
1553
1554 @functools.wraps(decorator)
1555 def wrapped_decorator(*args, **kwds):
1556 func = decorator(*args, **kwds)
1557 func = no_type_check(func)
1558 return func
1559
1560 return wrapped_decorator
1561
1562
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001563def _overload_dummy(*args, **kwds):
1564 """Helper for @overload to raise when called."""
1565 raise NotImplementedError(
1566 "You should not call an overloaded function. "
1567 "A series of @overload-decorated functions "
1568 "outside a stub module should always be followed "
1569 "by an implementation that is not @overload-ed.")
1570
1571
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001572def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001573 """Decorator for overloaded functions/methods.
1574
1575 In a stub file, place two or more stub definitions for the same
1576 function in a row, each decorated with @overload. For example:
1577
1578 @overload
1579 def utf8(value: None) -> None: ...
1580 @overload
1581 def utf8(value: bytes) -> bytes: ...
1582 @overload
1583 def utf8(value: str) -> bytes: ...
1584
1585 In a non-stub file (i.e. a regular .py file), do the same but
1586 follow it with an implementation. The implementation should *not*
1587 be decorated with @overload. For example:
1588
1589 @overload
1590 def utf8(value: None) -> None: ...
1591 @overload
1592 def utf8(value: bytes) -> bytes: ...
1593 @overload
1594 def utf8(value: str) -> bytes: ...
1595 def utf8(value):
1596 # implementation goes here
1597 """
1598 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001599
1600
Ivan Levkivskyif3672422019-05-26 09:37:07 +01001601def final(f):
1602 """A decorator to indicate final methods and final classes.
1603
1604 Use this decorator to indicate to type checkers that the decorated
1605 method cannot be overridden, and decorated class cannot be subclassed.
1606 For example:
1607
1608 class Base:
1609 @final
1610 def done(self) -> None:
1611 ...
1612 class Sub(Base):
1613 def done(self) -> None: # Error reported by type checker
1614 ...
1615
1616 @final
1617 class Leaf:
1618 ...
1619 class Other(Leaf): # Error reported by type checker
1620 ...
1621
1622 There is no runtime checking of these properties.
1623 """
1624 return f
1625
1626
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001627# Some unconstrained type variables. These are used by the container types.
1628# (These are not for export.)
1629T = TypeVar('T') # Any type.
1630KT = TypeVar('KT') # Key type.
1631VT = TypeVar('VT') # Value type.
1632T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
1633V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
1634VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
1635T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
1636# Internal type variable used for Type[].
1637CT_co = TypeVar('CT_co', covariant=True, bound=type)
1638
1639# A useful type variable with constraints. This represents string types.
1640# (This one *is* for export!)
1641AnyStr = TypeVar('AnyStr', bytes, str)
1642
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001643
1644# Various ABCs mimicking those in collections.abc.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001645_alias = _SpecialGenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001646
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001647Hashable = _alias(collections.abc.Hashable, 0) # Not generic.
1648Awaitable = _alias(collections.abc.Awaitable, 1)
1649Coroutine = _alias(collections.abc.Coroutine, 3)
1650AsyncIterable = _alias(collections.abc.AsyncIterable, 1)
1651AsyncIterator = _alias(collections.abc.AsyncIterator, 1)
1652Iterable = _alias(collections.abc.Iterable, 1)
1653Iterator = _alias(collections.abc.Iterator, 1)
1654Reversible = _alias(collections.abc.Reversible, 1)
1655Sized = _alias(collections.abc.Sized, 0) # Not generic.
1656Container = _alias(collections.abc.Container, 1)
1657Collection = _alias(collections.abc.Collection, 1)
1658Callable = _CallableType(collections.abc.Callable, 2)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001659Callable.__doc__ = \
1660 """Callable type; Callable[[int], str] is a function of (int) -> str.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001661
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001662 The subscription syntax must always be used with exactly two
1663 values: the argument list and the return type. The argument list
1664 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001665
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001666 There is no syntax to indicate optional or keyword arguments,
1667 such function types are rarely used as callback types.
1668 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001669AbstractSet = _alias(collections.abc.Set, 1, name='AbstractSet')
1670MutableSet = _alias(collections.abc.MutableSet, 1)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001671# NOTE: Mapping is only covariant in the value type.
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001672Mapping = _alias(collections.abc.Mapping, 2)
1673MutableMapping = _alias(collections.abc.MutableMapping, 2)
1674Sequence = _alias(collections.abc.Sequence, 1)
1675MutableSequence = _alias(collections.abc.MutableSequence, 1)
1676ByteString = _alias(collections.abc.ByteString, 0) # Not generic
1677# Tuple accepts variable number of parameters.
1678Tuple = _TupleType(tuple, -1, inst=False, name='Tuple')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001679Tuple.__doc__ = \
1680 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001681
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001682 Example: Tuple[T1, T2] is a tuple of two elements corresponding
1683 to type variables T1 and T2. Tuple[int, float, str] is a tuple
1684 of an int, a float and a string.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001685
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001686 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
1687 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001688List = _alias(list, 1, inst=False, name='List')
1689Deque = _alias(collections.deque, 1, name='Deque')
1690Set = _alias(set, 1, inst=False, name='Set')
1691FrozenSet = _alias(frozenset, 1, inst=False, name='FrozenSet')
1692MappingView = _alias(collections.abc.MappingView, 1)
1693KeysView = _alias(collections.abc.KeysView, 1)
1694ItemsView = _alias(collections.abc.ItemsView, 2)
1695ValuesView = _alias(collections.abc.ValuesView, 1)
1696ContextManager = _alias(contextlib.AbstractContextManager, 1, name='ContextManager')
1697AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, 1, name='AsyncContextManager')
1698Dict = _alias(dict, 2, inst=False, name='Dict')
1699DefaultDict = _alias(collections.defaultdict, 2, name='DefaultDict')
1700OrderedDict = _alias(collections.OrderedDict, 2)
1701Counter = _alias(collections.Counter, 1)
1702ChainMap = _alias(collections.ChainMap, 2)
1703Generator = _alias(collections.abc.Generator, 3)
1704AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2)
1705Type = _alias(type, 1, inst=False, name='Type')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001706Type.__doc__ = \
1707 """A special construct usable to annotate class objects.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001708
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001709 For example, suppose we have the following classes::
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001710
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001711 class User: ... # Abstract base for User classes
1712 class BasicUser(User): ...
1713 class ProUser(User): ...
1714 class TeamUser(User): ...
Guido van Rossumf17c2002015-12-03 17:31:24 -08001715
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001716 And a function that takes a class argument that's a subclass of
1717 User and returns an instance of the corresponding class::
Guido van Rossumf17c2002015-12-03 17:31:24 -08001718
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001719 U = TypeVar('U', bound=User)
1720 def new_user(user_class: Type[U]) -> U:
1721 user = user_class()
1722 # (Here we could write the user object to a database)
1723 return user
Guido van Rossumf17c2002015-12-03 17:31:24 -08001724
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001725 joe = new_user(BasicUser)
Guido van Rossumf17c2002015-12-03 17:31:24 -08001726
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001727 At this point the type checker knows that joe has type BasicUser.
1728 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001729
1730
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001731@runtime_checkable
1732class SupportsInt(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001733 """An ABC with one abstract method __int__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001734 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001735
1736 @abstractmethod
1737 def __int__(self) -> int:
1738 pass
1739
1740
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001741@runtime_checkable
1742class SupportsFloat(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001743 """An ABC with one abstract method __float__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001744 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001745
1746 @abstractmethod
1747 def __float__(self) -> float:
1748 pass
1749
1750
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001751@runtime_checkable
1752class SupportsComplex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001753 """An ABC with one abstract method __complex__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001754 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001755
1756 @abstractmethod
1757 def __complex__(self) -> complex:
1758 pass
1759
1760
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001761@runtime_checkable
1762class SupportsBytes(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001763 """An ABC with one abstract method __bytes__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001764 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001765
1766 @abstractmethod
1767 def __bytes__(self) -> bytes:
1768 pass
1769
1770
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001771@runtime_checkable
1772class SupportsIndex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001773 """An ABC with one abstract method __index__."""
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -07001774 __slots__ = ()
1775
1776 @abstractmethod
1777 def __index__(self) -> int:
1778 pass
1779
1780
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001781@runtime_checkable
1782class SupportsAbs(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001783 """An ABC with one abstract method __abs__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001784 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001785
1786 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001787 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001788 pass
1789
1790
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001791@runtime_checkable
1792class SupportsRound(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001793 """An ABC with one abstract method __round__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001794 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001795
1796 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001797 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001798 pass
1799
1800
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001801def _make_nmtuple(name, types, module, defaults = ()):
1802 fields = [n for n, t in types]
1803 types = {n: _type_check(t, f"field {n} annotation must be a type")
1804 for n, t in types}
1805 nm_tpl = collections.namedtuple(name, fields,
1806 defaults=defaults, module=module)
1807 nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001808 return nm_tpl
1809
1810
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001811# attributes prohibited to set in NamedTuple class syntax
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001812_prohibited = frozenset({'__new__', '__init__', '__slots__', '__getnewargs__',
1813 '_fields', '_field_defaults',
1814 '_make', '_replace', '_asdict', '_source'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001815
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001816_special = frozenset({'__module__', '__name__', '__annotations__'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001817
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001818
Guido van Rossum2f841442016-11-15 09:48:06 -08001819class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001820
Guido van Rossum2f841442016-11-15 09:48:06 -08001821 def __new__(cls, typename, bases, ns):
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001822 assert bases[0] is _NamedTuple
Guido van Rossum2f841442016-11-15 09:48:06 -08001823 types = ns.get('__annotations__', {})
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001824 default_names = []
Guido van Rossum3c268be2017-01-18 08:03:50 -08001825 for field_name in types:
1826 if field_name in ns:
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001827 default_names.append(field_name)
1828 elif default_names:
1829 raise TypeError(f"Non-default namedtuple field {field_name} "
1830 f"cannot follow default field"
1831 f"{'s' if len(default_names) > 1 else ''} "
1832 f"{', '.join(default_names)}")
1833 nm_tpl = _make_nmtuple(typename, types.items(),
1834 defaults=[ns[n] for n in default_names],
1835 module=ns['__module__'])
Guido van Rossum95919c02017-01-22 17:47:20 -08001836 # update from user namespace without overriding special namedtuple attributes
1837 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001838 if key in _prohibited:
1839 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
1840 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08001841 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08001842 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001843
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001844
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001845def NamedTuple(typename, fields=None, /, **kwargs):
Guido van Rossum2f841442016-11-15 09:48:06 -08001846 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001847
Guido van Rossum2f841442016-11-15 09:48:06 -08001848 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001849
Guido van Rossum2f841442016-11-15 09:48:06 -08001850 class Employee(NamedTuple):
1851 name: str
1852 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001853
Guido van Rossum2f841442016-11-15 09:48:06 -08001854 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001855
Guido van Rossum2f841442016-11-15 09:48:06 -08001856 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001857
Raymond Hettingerf7b57df2019-03-18 09:53:56 -07001858 The resulting class has an extra __annotations__ attribute, giving a
1859 dict that maps field names to types. (The field names are also in
1860 the _fields attribute, which is part of the namedtuple API.)
1861 Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001862
Guido van Rossum2f841442016-11-15 09:48:06 -08001863 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001864
Guido van Rossum2f841442016-11-15 09:48:06 -08001865 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001866
Guido van Rossum2f841442016-11-15 09:48:06 -08001867 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
1868 """
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001869 if fields is None:
1870 fields = kwargs.items()
1871 elif kwargs:
1872 raise TypeError("Either list of fields or keywords"
1873 " can be provided to NamedTuple, not both")
1874 try:
1875 module = sys._getframe(1).f_globals.get('__name__', '__main__')
1876 except (AttributeError, ValueError):
1877 module = None
1878 return _make_nmtuple(typename, fields, module=module)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001879
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001880_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})
1881
1882def _namedtuple_mro_entries(bases):
1883 if len(bases) > 1:
1884 raise TypeError("Multiple inheritance with NamedTuple is not supported")
1885 assert bases[0] is NamedTuple
1886 return (_NamedTuple,)
1887
1888NamedTuple.__mro_entries__ = _namedtuple_mro_entries
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001889
1890
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001891class _TypedDictMeta(type):
1892 def __new__(cls, name, bases, ns, total=True):
1893 """Create new typed dict class object.
1894
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001895 This method is called when TypedDict is subclassed,
1896 or when TypedDict is instantiated. This way
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001897 TypedDict supports all three syntax forms described in its docstring.
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001898 Subclasses and instances of TypedDict return actual dictionaries.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001899 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001900 for base in bases:
1901 if type(base) is not _TypedDictMeta:
1902 raise TypeError('cannot inherit from both a TypedDict type '
1903 'and a non-TypedDict base class')
1904 tp_dict = type.__new__(_TypedDictMeta, name, (dict,), ns)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001905
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001906 annotations = {}
1907 own_annotations = ns.get('__annotations__', {})
1908 own_annotation_keys = set(own_annotations.keys())
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001909 msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001910 own_annotations = {
1911 n: _type_check(tp, msg) for n, tp in own_annotations.items()
1912 }
1913 required_keys = set()
1914 optional_keys = set()
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11001915
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001916 for base in bases:
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001917 annotations.update(base.__dict__.get('__annotations__', {}))
1918 required_keys.update(base.__dict__.get('__required_keys__', ()))
1919 optional_keys.update(base.__dict__.get('__optional_keys__', ()))
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11001920
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001921 annotations.update(own_annotations)
1922 if total:
1923 required_keys.update(own_annotation_keys)
1924 else:
1925 optional_keys.update(own_annotation_keys)
1926
1927 tp_dict.__annotations__ = annotations
1928 tp_dict.__required_keys__ = frozenset(required_keys)
1929 tp_dict.__optional_keys__ = frozenset(optional_keys)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001930 if not hasattr(tp_dict, '__total__'):
1931 tp_dict.__total__ = total
1932 return tp_dict
1933
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001934 __call__ = dict # static method
1935
1936 def __subclasscheck__(cls, other):
1937 # Typed dicts are only for static structural subtyping.
1938 raise TypeError('TypedDict does not support instance and class checks')
1939
1940 __instancecheck__ = __subclasscheck__
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001941
1942
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001943def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001944 """A simple typed namespace. At runtime it is equivalent to a plain dict.
1945
1946 TypedDict creates a dictionary type that expects all of its
1947 instances to have a certain set of keys, where each key is
1948 associated with a value of a consistent type. This expectation
1949 is not checked at runtime but is only enforced by type checkers.
1950 Usage::
1951
1952 class Point2D(TypedDict):
1953 x: int
1954 y: int
1955 label: str
1956
1957 a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
1958 b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
1959
1960 assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
1961
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11001962 The type info can be accessed via the Point2D.__annotations__ dict, and
1963 the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
1964 TypedDict supports two additional equivalent forms::
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001965
1966 Point2D = TypedDict('Point2D', x=int, y=int, label=str)
1967 Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
1968
ananthan-123ab6423f2020-02-19 10:03:05 +05301969 By default, all keys must be present in a TypedDict. It is possible
1970 to override this by specifying totality.
1971 Usage::
1972
1973 class point2D(TypedDict, total=False):
1974 x: int
1975 y: int
1976
1977 This means that a point2D TypedDict can have any of the keys omitted.A type
1978 checker is only expected to support a literal False or True as the value of
1979 the total argument. True is the default, and makes all items defined in the
1980 class body be required.
1981
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001982 The class syntax is only supported in Python 3.6+, while two other
1983 syntax forms work for Python 2.7 and 3.2+
1984 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001985 if fields is None:
1986 fields = kwargs
1987 elif kwargs:
1988 raise TypeError("TypedDict takes either a dict or keyword arguments,"
1989 " but not both")
1990
1991 ns = {'__annotations__': dict(fields), '__total__': total}
1992 try:
1993 # Setting correct module is necessary to make typed dict classes pickleable.
1994 ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
1995 except (AttributeError, ValueError):
1996 pass
1997
1998 return _TypedDictMeta(typename, (), ns)
1999
2000_TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {})
2001TypedDict.__mro_entries__ = lambda bases: (_TypedDict,)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002002
2003
Guido van Rossum91185fe2016-06-08 11:19:11 -07002004def NewType(name, tp):
2005 """NewType creates simple unique types with almost zero
2006 runtime overhead. NewType(name, tp) is considered a subtype of tp
2007 by static type checkers. At runtime, NewType(name, tp) returns
2008 a dummy function that simply returns its argument. Usage::
2009
2010 UserId = NewType('UserId', int)
2011
2012 def name_by_id(user_id: UserId) -> str:
2013 ...
2014
2015 UserId('user') # Fails type check
2016
2017 name_by_id(42) # Fails type check
2018 name_by_id(UserId(42)) # OK
2019
2020 num = UserId(5) + 1 # type: int
2021 """
2022
2023 def new_type(x):
2024 return x
2025
2026 new_type.__name__ = name
2027 new_type.__supertype__ = tp
2028 return new_type
2029
2030
Guido van Rossum0e0563c2016-04-05 14:54:25 -07002031# Python-version-specific alias (Python 2: unicode; Python 3: str)
2032Text = str
2033
2034
Guido van Rossum91185fe2016-06-08 11:19:11 -07002035# Constant that's True when type checking, but False here.
2036TYPE_CHECKING = False
2037
2038
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002039class IO(Generic[AnyStr]):
2040 """Generic base class for TextIO and BinaryIO.
2041
2042 This is an abstract, generic version of the return of open().
2043
2044 NOTE: This does not distinguish between the different possible
2045 classes (text vs. binary, read vs. write vs. read/write,
2046 append-only, unbuffered). The TextIO and BinaryIO subclasses
2047 below capture the distinctions between text vs. binary, which is
2048 pervasive in the interface; however we currently do not offer a
2049 way to track the other distinctions in the type system.
2050 """
2051
Guido van Rossumd70fe632015-08-05 12:11:06 +02002052 __slots__ = ()
2053
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002054 @property
2055 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002056 def mode(self) -> str:
2057 pass
2058
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002059 @property
2060 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002061 def name(self) -> str:
2062 pass
2063
2064 @abstractmethod
2065 def close(self) -> None:
2066 pass
2067
Shantanu2e6569b2020-01-29 18:52:36 -08002068 @property
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002069 @abstractmethod
2070 def closed(self) -> bool:
2071 pass
2072
2073 @abstractmethod
2074 def fileno(self) -> int:
2075 pass
2076
2077 @abstractmethod
2078 def flush(self) -> None:
2079 pass
2080
2081 @abstractmethod
2082 def isatty(self) -> bool:
2083 pass
2084
2085 @abstractmethod
2086 def read(self, n: int = -1) -> AnyStr:
2087 pass
2088
2089 @abstractmethod
2090 def readable(self) -> bool:
2091 pass
2092
2093 @abstractmethod
2094 def readline(self, limit: int = -1) -> AnyStr:
2095 pass
2096
2097 @abstractmethod
2098 def readlines(self, hint: int = -1) -> List[AnyStr]:
2099 pass
2100
2101 @abstractmethod
2102 def seek(self, offset: int, whence: int = 0) -> int:
2103 pass
2104
2105 @abstractmethod
2106 def seekable(self) -> bool:
2107 pass
2108
2109 @abstractmethod
2110 def tell(self) -> int:
2111 pass
2112
2113 @abstractmethod
2114 def truncate(self, size: int = None) -> int:
2115 pass
2116
2117 @abstractmethod
2118 def writable(self) -> bool:
2119 pass
2120
2121 @abstractmethod
2122 def write(self, s: AnyStr) -> int:
2123 pass
2124
2125 @abstractmethod
2126 def writelines(self, lines: List[AnyStr]) -> None:
2127 pass
2128
2129 @abstractmethod
2130 def __enter__(self) -> 'IO[AnyStr]':
2131 pass
2132
2133 @abstractmethod
2134 def __exit__(self, type, value, traceback) -> None:
2135 pass
2136
2137
2138class BinaryIO(IO[bytes]):
2139 """Typed version of the return of open() in binary mode."""
2140
Guido van Rossumd70fe632015-08-05 12:11:06 +02002141 __slots__ = ()
2142
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002143 @abstractmethod
2144 def write(self, s: Union[bytes, bytearray]) -> int:
2145 pass
2146
2147 @abstractmethod
2148 def __enter__(self) -> 'BinaryIO':
2149 pass
2150
2151
2152class TextIO(IO[str]):
2153 """Typed version of the return of open() in text mode."""
2154
Guido van Rossumd70fe632015-08-05 12:11:06 +02002155 __slots__ = ()
2156
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002157 @property
2158 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002159 def buffer(self) -> BinaryIO:
2160 pass
2161
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002162 @property
2163 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002164 def encoding(self) -> str:
2165 pass
2166
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002167 @property
2168 @abstractmethod
Guido van Rossum991d14f2016-11-09 13:12:51 -08002169 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002170 pass
2171
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002172 @property
2173 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002174 def line_buffering(self) -> bool:
2175 pass
2176
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002177 @property
2178 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002179 def newlines(self) -> Any:
2180 pass
2181
2182 @abstractmethod
2183 def __enter__(self) -> 'TextIO':
2184 pass
2185
2186
2187class io:
2188 """Wrapper namespace for IO generic classes."""
2189
2190 __all__ = ['IO', 'TextIO', 'BinaryIO']
2191 IO = IO
2192 TextIO = TextIO
2193 BinaryIO = BinaryIO
2194
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002195
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002196io.__name__ = __name__ + '.io'
2197sys.modules[io.__name__] = io
2198
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002199Pattern = _alias(stdlib_re.Pattern, 1)
2200Match = _alias(stdlib_re.Match, 1)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002201
2202class re:
2203 """Wrapper namespace for re type aliases."""
2204
2205 __all__ = ['Pattern', 'Match']
2206 Pattern = Pattern
2207 Match = Match
2208
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002209
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002210re.__name__ = __name__ + '.re'
2211sys.modules[re.__name__] = re