blob: 3fa97a4a15f954aa65c05eea68695a04849e210b [file] [log] [blame]
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001"""
2The typing module: Support for gradual typing as defined by PEP 484.
3
4At large scale, the structure of the module is following:
Tim McNamara5265b3a2018-09-01 20:56:58 +12005* Imports and exports, all public names should be explicitly added to __all__.
Ivan Levkivskyid911e402018-01-20 11:23:59 +00006* Internal helper functions: these should never be used in code outside this module.
7* _SpecialForm and its instances (special forms): Any, NoReturn, ClassVar, Union, Optional
8* Two classes whose instances can be type arguments in addition to types: ForwardRef and TypeVar
9* The core of internal generics API: _GenericAlias and _VariadicGenericAlias, the latter is
10 currently only used by Tuple and Callable. All subscripted types like X[int], Union[int, str],
11 etc., are instances of either of these classes.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +010012* The public counterpart of the generics API consists of two classes: Generic and Protocol.
Ivan Levkivskyid911e402018-01-20 11:23:59 +000013* Public helper functions: get_type_hints, overload, cast, no_type_check,
14 no_type_check_decorator.
15* Generic aliases for collections.abc ABCs and few additional protocols.
ananthan-123ab6423f2020-02-19 10:03:05 +053016* Special types: NewType, NamedTuple, TypedDict.
Ivan Levkivskyid911e402018-01-20 11:23:59 +000017* Wrapper submodules for re and io related types.
18"""
19
HongWeipeng6ce03ec2019-09-27 15:54:26 +080020from abc import abstractmethod, ABCMeta
Batuhan Taskaya044a1042020-10-06 23:03:02 +030021import ast
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070022import collections
Ivan Levkivskyid911e402018-01-20 11:23:59 +000023import collections.abc
Brett Cannonf3ad0422016-04-15 10:51:30 -070024import contextlib
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070025import functools
Serhiy Storchaka09f32212018-05-26 21:19:26 +030026import operator
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070027import re as stdlib_re # Avoid confusion with the re we export.
28import sys
29import types
Guido van Rossum48b069a2020-04-07 09:50:06 -070030from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType, GenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070031
32# Please keep __all__ alphabetized within each category.
33__all__ = [
34 # Super-special typing primitives.
Jakub Stasiakcf5b1092020-02-05 02:10:19 +010035 'Annotated',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070036 'Any',
37 'Callable',
Guido van Rossum0a6976d2016-09-11 15:34:56 -070038 'ClassVar',
Ivan Levkivskyif3672422019-05-26 09:37:07 +010039 'Final',
Anthony Sottiled30da5d2019-05-29 11:19:38 -070040 'ForwardRef',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070041 'Generic',
Ivan Levkivskyib891c462019-05-26 09:37:48 +010042 'Literal',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070043 'Optional',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +010044 'Protocol',
Guido van Rossumeb9aca32016-05-24 16:38:22 -070045 'Tuple',
46 'Type',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070047 'TypeVar',
48 'Union',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070049
50 # ABCs (from collections.abc).
51 'AbstractSet', # collections.abc.Set.
52 'ByteString',
53 'Container',
Ivan Levkivskyi29fda8d2017-06-10 21:57:56 +020054 'ContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070055 'Hashable',
56 'ItemsView',
57 'Iterable',
58 'Iterator',
59 'KeysView',
60 'Mapping',
61 'MappingView',
62 'MutableMapping',
63 'MutableSequence',
64 'MutableSet',
65 'Sequence',
66 'Sized',
67 'ValuesView',
Ivan Levkivskyid911e402018-01-20 11:23:59 +000068 'Awaitable',
69 'AsyncIterator',
70 'AsyncIterable',
71 'Coroutine',
72 'Collection',
73 'AsyncGenerator',
74 'AsyncContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070075
76 # Structural checks, a.k.a. protocols.
77 'Reversible',
78 'SupportsAbs',
Ivan Levkivskyif06e0212017-05-02 19:14:07 +020079 'SupportsBytes',
80 'SupportsComplex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070081 'SupportsFloat',
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -070082 'SupportsIndex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070083 'SupportsInt',
84 'SupportsRound',
85
86 # Concrete collection types.
Anthony Sottiled30da5d2019-05-29 11:19:38 -070087 'ChainMap',
Ivan Levkivskyib692dc82017-02-13 22:50:14 +010088 'Counter',
Raymond Hettinger80490522017-01-16 22:42:37 -080089 'Deque',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070090 'Dict',
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070091 'DefaultDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070092 'List',
Anthony Sottiled30da5d2019-05-29 11:19:38 -070093 'OrderedDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070094 'Set',
Guido van Rossumefa798d2016-08-23 11:01:50 -070095 'FrozenSet',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070096 'NamedTuple', # Not really a type.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +010097 'TypedDict', # Not really a type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070098 'Generator',
99
100 # One-off things.
101 'AnyStr',
102 'cast',
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100103 'final',
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +0100104 'get_args',
105 'get_origin',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700106 'get_type_hints',
Patrick Reader0705ec82020-09-16 05:58:32 +0100107 'is_typeddict',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700108 'NewType',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700109 'no_type_check',
110 'no_type_check_decorator',
aetracht45738202018-03-19 14:41:32 -0400111 'NoReturn',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700112 'overload',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100113 'runtime_checkable',
Guido van Rossum0e0563c2016-04-05 14:54:25 -0700114 'Text',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700115 'TYPE_CHECKING',
Mikhail Golubev4f3c2502020-10-08 00:44:31 +0300116 'TypeAlias',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700117]
118
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700119# The pseudo-submodules 're' and 'io' are part of the public
120# namespace, but excluded from __all__ because they might stomp on
121# legitimate imports of those modules.
122
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700123def _type_check(arg, msg, is_argument=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000124 """Check that the argument is a type, and return it (internal helper).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700125
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000126 As a special case, accept None and return type(None) instead. Also wrap strings
127 into ForwardRef instances. Consider several corner cases, for example plain
128 special forms like Union are not valid, while Union[int, str] is OK, etc.
129 The msg argument is a human-readable error message, e.g::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700130
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000131 "Union[arg, ...]: arg should be a type."
Guido van Rossum4cefe742016-09-27 15:20:12 -0700132
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000133 We append the repr() of the actual value (truncated to 100 chars).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700134 """
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100135 invalid_generic_forms = (Generic, Protocol)
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700136 if is_argument:
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100137 invalid_generic_forms = invalid_generic_forms + (ClassVar, Final)
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400138
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000139 if arg is None:
140 return type(None)
141 if isinstance(arg, str):
142 return ForwardRef(arg)
143 if (isinstance(arg, _GenericAlias) and
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400144 arg.__origin__ in invalid_generic_forms):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000145 raise TypeError(f"{arg} is not valid as type argument")
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300146 if arg in (Any, NoReturn):
147 return arg
148 if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000149 raise TypeError(f"Plain {arg} is not valid as type argument")
Maggie Moss1b4552c2020-09-09 13:23:24 -0700150 if isinstance(arg, (type, TypeVar, ForwardRef, types.Union)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000151 return arg
152 if not callable(arg):
153 raise TypeError(f"{msg} Got {arg!r:.100}.")
154 return arg
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700155
156
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000157def _type_repr(obj):
158 """Return the repr() of an object, special-casing types (internal helper).
159
160 If obj is a type, we return a shorter version than the default
161 type.__repr__, based on the module and qualified name, which is
162 typically enough to uniquely identify a type. For everything
163 else, we fall back on repr(obj).
164 """
kj1f7dfb22020-11-02 02:13:38 +0800165 if isinstance(obj, types.GenericAlias):
166 return repr(obj)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000167 if isinstance(obj, type):
168 if obj.__module__ == 'builtins':
169 return obj.__qualname__
170 return f'{obj.__module__}.{obj.__qualname__}'
171 if obj is ...:
172 return('...')
173 if isinstance(obj, types.FunctionType):
174 return obj.__name__
175 return repr(obj)
176
177
178def _collect_type_vars(types):
179 """Collect all type variable contained in types in order of
180 first appearance (lexicographic order). For example::
181
182 _collect_type_vars((T, List[S, T])) == (T, S)
183 """
184 tvars = []
185 for t in types:
186 if isinstance(t, TypeVar) and t not in tvars:
187 tvars.append(t)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300188 if isinstance(t, (_GenericAlias, GenericAlias)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000189 tvars.extend([t for t in t.__parameters__ if t not in tvars])
190 return tuple(tvars)
191
192
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300193def _check_generic(cls, parameters, elen):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000194 """Check correct count for parameters of a generic cls (internal helper).
195 This gives a nice error message in case of count mismatch.
196 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300197 if not elen:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000198 raise TypeError(f"{cls} is not a generic class")
199 alen = len(parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000200 if alen != elen:
201 raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
202 f" actual {alen}, expected {elen}")
203
204
205def _remove_dups_flatten(parameters):
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700206 """An internal helper for Union creation and substitution: flatten Unions
207 among parameters, then remove duplicates.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000208 """
209 # Flatten out Union[Union[...], ...].
210 params = []
211 for p in parameters:
Maggie Moss1b4552c2020-09-09 13:23:24 -0700212 if isinstance(p, (_UnionGenericAlias, types.Union)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000213 params.extend(p.__args__)
214 elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
215 params.extend(p[1:])
216 else:
217 params.append(p)
218 # Weed out strict duplicates, preserving the first of each occurrence.
219 all_params = set(params)
220 if len(all_params) < len(params):
221 new_params = []
222 for t in params:
223 if t in all_params:
224 new_params.append(t)
225 all_params.remove(t)
226 params = new_params
227 assert not all_params, all_params
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700228 return tuple(params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000229
230
231_cleanups = []
232
233
234def _tp_cache(func):
235 """Internal wrapper caching __getitem__ of generic types with a fallback to
236 original function for non-hashable arguments.
237 """
238 cached = functools.lru_cache()(func)
239 _cleanups.append(cached.cache_clear)
240
241 @functools.wraps(func)
242 def inner(*args, **kwds):
243 try:
244 return cached(*args, **kwds)
245 except TypeError:
246 pass # All real errors (not unhashable args) are raised below.
247 return func(*args, **kwds)
248 return inner
249
250
wyfo653f4202020-07-22 21:47:28 +0200251def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
Graham Bleaney84ef33c2020-09-08 18:41:10 -0400252 """Evaluate all forward references in the given type t.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000253 For use of globalns and localns see the docstring for get_type_hints().
wyfo653f4202020-07-22 21:47:28 +0200254 recursive_guard is used to prevent prevent infinite recursion
255 with recursive ForwardRef.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000256 """
257 if isinstance(t, ForwardRef):
wyfo653f4202020-07-22 21:47:28 +0200258 return t._evaluate(globalns, localns, recursive_guard)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300259 if isinstance(t, (_GenericAlias, GenericAlias)):
wyfo653f4202020-07-22 21:47:28 +0200260 ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000261 if ev_args == t.__args__:
262 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300263 if isinstance(t, GenericAlias):
264 return GenericAlias(t.__origin__, ev_args)
265 else:
266 return t.copy_with(ev_args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000267 return t
268
269
270class _Final:
271 """Mixin to prohibit subclassing"""
Guido van Rossum4cefe742016-09-27 15:20:12 -0700272
Guido van Rossum83ec3022017-01-17 20:43:28 -0800273 __slots__ = ('__weakref__',)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700274
Serhiy Storchaka2085bd02019-06-01 11:00:15 +0300275 def __init_subclass__(self, /, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000276 if '_root' not in kwds:
277 raise TypeError("Cannot subclass special typing classes")
278
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100279class _Immutable:
280 """Mixin to indicate that object should not be copied."""
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300281 __slots__ = ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000282
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100283 def __copy__(self):
284 return self
285
286 def __deepcopy__(self, memo):
287 return self
288
289
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300290# Internal indicator of special typing constructs.
291# See __doc__ instance attribute for specific docs.
292class _SpecialForm(_Final, _root=True):
293 __slots__ = ('_name', '__doc__', '_getitem')
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000294
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300295 def __init__(self, getitem):
296 self._getitem = getitem
297 self._name = getitem.__name__
298 self.__doc__ = getitem.__doc__
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000299
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300300 def __mro_entries__(self, bases):
301 raise TypeError(f"Cannot subclass {self!r}")
Guido van Rossum4cefe742016-09-27 15:20:12 -0700302
303 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000304 return 'typing.' + self._name
305
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100306 def __reduce__(self):
307 return self._name
Guido van Rossum4cefe742016-09-27 15:20:12 -0700308
309 def __call__(self, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000310 raise TypeError(f"Cannot instantiate {self!r}")
311
312 def __instancecheck__(self, obj):
313 raise TypeError(f"{self} cannot be used with isinstance()")
314
315 def __subclasscheck__(self, cls):
316 raise TypeError(f"{self} cannot be used with issubclass()")
317
318 @_tp_cache
319 def __getitem__(self, parameters):
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300320 return self._getitem(self, parameters)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700321
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300322@_SpecialForm
323def Any(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000324 """Special type indicating an unconstrained type.
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700325
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000326 - Any is compatible with every type.
327 - Any assumed to have all methods.
328 - All values assumed to be instances of Any.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700329
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000330 Note that all the above statements are true from the point of view of
331 static type checkers. At runtime, Any should not be used with instance
332 or class checks.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300333 """
334 raise TypeError(f"{self} is not subscriptable")
Guido van Rossumd70fe632015-08-05 12:11:06 +0200335
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300336@_SpecialForm
337def NoReturn(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000338 """Special type indicating functions that never return.
339 Example::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700340
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000341 from typing import NoReturn
342
343 def stop() -> NoReturn:
344 raise Exception('no way')
345
346 This type is invalid in other positions, e.g., ``List[NoReturn]``
347 will fail in static type checkers.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300348 """
349 raise TypeError(f"{self} is not subscriptable")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000350
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300351@_SpecialForm
352def ClassVar(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000353 """Special type construct to mark class variables.
354
355 An annotation wrapped in ClassVar indicates that a given
356 attribute is intended to be used as a class variable and
357 should not be set on instances of that class. Usage::
358
359 class Starship:
360 stats: ClassVar[Dict[str, int]] = {} # class variable
361 damage: int = 10 # instance variable
362
363 ClassVar accepts only types and cannot be further subscribed.
364
365 Note that ClassVar is not a class itself, and should not
366 be used with isinstance() or issubclass().
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300367 """
368 item = _type_check(parameters, f'{self} accepts only single type.')
369 return _GenericAlias(self, (item,))
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000370
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300371@_SpecialForm
372def Final(self, parameters):
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100373 """Special typing construct to indicate final names to type checkers.
374
375 A final name cannot be re-assigned or overridden in a subclass.
376 For example:
377
378 MAX_SIZE: Final = 9000
379 MAX_SIZE += 1 # Error reported by type checker
380
381 class Connection:
382 TIMEOUT: Final[int] = 10
383
384 class FastConnector(Connection):
385 TIMEOUT = 1 # Error reported by type checker
386
387 There is no runtime checking of these properties.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300388 """
389 item = _type_check(parameters, f'{self} accepts only single type.')
390 return _GenericAlias(self, (item,))
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100391
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300392@_SpecialForm
393def Union(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000394 """Union type; Union[X, Y] means either X or Y.
395
396 To define a union, use e.g. Union[int, str]. Details:
397 - The arguments must be types and there must be at least one.
398 - None as an argument is a special case and is replaced by
399 type(None).
400 - Unions of unions are flattened, e.g.::
401
402 Union[Union[int, str], float] == Union[int, str, float]
403
404 - Unions of a single argument vanish, e.g.::
405
406 Union[int] == int # The constructor actually returns int
407
408 - Redundant arguments are skipped, e.g.::
409
410 Union[int, str, int] == Union[int, str]
411
412 - When comparing unions, the argument order is ignored, e.g.::
413
414 Union[int, str] == Union[str, int]
415
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000416 - You cannot subclass or instantiate a union.
417 - You can use Optional[X] as a shorthand for Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300418 """
419 if parameters == ():
420 raise TypeError("Cannot take a Union of no types.")
421 if not isinstance(parameters, tuple):
422 parameters = (parameters,)
423 msg = "Union[arg, ...]: each arg must be a type."
424 parameters = tuple(_type_check(p, msg) for p in parameters)
425 parameters = _remove_dups_flatten(parameters)
426 if len(parameters) == 1:
427 return parameters[0]
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300428 return _UnionGenericAlias(self, parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000429
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300430@_SpecialForm
431def Optional(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000432 """Optional type.
433
434 Optional[X] is equivalent to Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300435 """
436 arg = _type_check(parameters, f"{self} requires a single type.")
437 return Union[arg, type(None)]
Guido van Rossumb7dedc82016-10-29 12:44:29 -0700438
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300439@_SpecialForm
440def Literal(self, parameters):
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100441 """Special typing form to define literal types (a.k.a. value types).
442
443 This form can be used to indicate to type checkers that the corresponding
444 variable or function parameter has a value equivalent to the provided
445 literal (or one of several literals):
446
447 def validate_simple(data: Any) -> Literal[True]: # always returns True
448 ...
449
450 MODE = Literal['r', 'rb', 'w', 'wb']
451 def open_helper(file: str, mode: MODE) -> str:
452 ...
453
454 open_helper('/some/path', 'r') # Passes type check
455 open_helper('/other/path', 'typo') # Error in type checker
456
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300457 Literal[...] cannot be subclassed. At runtime, an arbitrary value
458 is allowed as type argument to Literal[...], but type checkers may
459 impose restrictions.
460 """
461 # There is no '_type_check' call because arguments to Literal[...] are
462 # values, not types.
463 return _GenericAlias(self, parameters)
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100464
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700465
Mikhail Golubev4f3c2502020-10-08 00:44:31 +0300466@_SpecialForm
467def TypeAlias(self, parameters):
468 """Special marker indicating that an assignment should
469 be recognized as a proper type alias definition by type
470 checkers.
471
472 For example::
473
474 Predicate: TypeAlias = Callable[..., bool]
475
476 It's invalid when used anywhere except as in the example above.
477 """
478 raise TypeError(f"{self} is not subscriptable")
479
480
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000481class ForwardRef(_Final, _root=True):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800482 """Internal wrapper to hold a forward reference."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700483
Guido van Rossum4cefe742016-09-27 15:20:12 -0700484 __slots__ = ('__forward_arg__', '__forward_code__',
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400485 '__forward_evaluated__', '__forward_value__',
486 '__forward_is_argument__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700487
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700488 def __init__(self, arg, is_argument=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700489 if not isinstance(arg, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000490 raise TypeError(f"Forward reference must be a string -- got {arg!r}")
Batuhan Taskaya044a1042020-10-06 23:03:02 +0300491
492 # Double-stringified forward references is a result of activating
493 # the 'annotations' future by default. This way, we eliminate them in
494 # the runtime.
495 if arg.startswith(("'", '\"')) and arg.endswith(("'", '"')):
496 arg = arg[1:-1]
497
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700498 try:
499 code = compile(arg, '<string>', 'eval')
500 except SyntaxError:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000501 raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700502 self.__forward_arg__ = arg
503 self.__forward_code__ = code
504 self.__forward_evaluated__ = False
505 self.__forward_value__ = None
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400506 self.__forward_is_argument__ = is_argument
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700507
wyfo653f4202020-07-22 21:47:28 +0200508 def _evaluate(self, globalns, localns, recursive_guard):
509 if self.__forward_arg__ in recursive_guard:
510 return self
Guido van Rossumdad17902016-11-10 08:29:18 -0800511 if not self.__forward_evaluated__ or localns is not globalns:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700512 if globalns is None and localns is None:
513 globalns = localns = {}
514 elif globalns is None:
515 globalns = localns
516 elif localns is None:
517 localns = globalns
wyfo653f4202020-07-22 21:47:28 +0200518 type_ =_type_check(
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700519 eval(self.__forward_code__, globalns, localns),
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400520 "Forward references must evaluate to types.",
wyfo653f4202020-07-22 21:47:28 +0200521 is_argument=self.__forward_is_argument__,
522 )
523 self.__forward_value__ = _eval_type(
524 type_, globalns, localns, recursive_guard | {self.__forward_arg__}
525 )
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700526 self.__forward_evaluated__ = True
527 return self.__forward_value__
528
Guido van Rossum4cefe742016-09-27 15:20:12 -0700529 def __eq__(self, other):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000530 if not isinstance(other, ForwardRef):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700531 return NotImplemented
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100532 if self.__forward_evaluated__ and other.__forward_evaluated__:
533 return (self.__forward_arg__ == other.__forward_arg__ and
534 self.__forward_value__ == other.__forward_value__)
535 return self.__forward_arg__ == other.__forward_arg__
Guido van Rossum4cefe742016-09-27 15:20:12 -0700536
537 def __hash__(self):
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100538 return hash(self.__forward_arg__)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700539
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700540 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000541 return f'ForwardRef({self.__forward_arg__!r})'
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700542
543
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100544class TypeVar(_Final, _Immutable, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700545 """Type variable.
546
547 Usage::
548
549 T = TypeVar('T') # Can be anything
550 A = TypeVar('A', str, bytes) # Must be str or bytes
551
552 Type variables exist primarily for the benefit of static type
553 checkers. They serve as the parameters for generic types as well
554 as for generic function definitions. See class Generic for more
555 information on generic types. Generic functions work as follows:
556
Guido van Rossumb24569a2016-11-20 18:01:29 -0800557 def repeat(x: T, n: int) -> List[T]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700558 '''Return a list containing n references to x.'''
559 return [x]*n
560
561 def longest(x: A, y: A) -> A:
562 '''Return the longest of two strings.'''
563 return x if len(x) >= len(y) else y
564
565 The latter example's signature is essentially the overloading
566 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
567 that if the arguments are instances of some subclass of str,
568 the return type is still plain str.
569
Guido van Rossumb24569a2016-11-20 18:01:29 -0800570 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700571
Guido van Rossumefa798d2016-08-23 11:01:50 -0700572 Type variables defined with covariant=True or contravariant=True
João D. Ferreira86bfed32018-07-07 16:41:20 +0100573 can be used to declare covariant or contravariant generic types.
Guido van Rossumefa798d2016-08-23 11:01:50 -0700574 See PEP 484 for more details. By default generic types are invariant
575 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700576
577 Type variables can be introspected. e.g.:
578
579 T.__name__ == 'T'
580 T.__constraints__ == ()
581 T.__covariant__ == False
582 T.__contravariant__ = False
583 A.__constraints__ == (str, bytes)
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100584
585 Note that only type variables defined in global scope can be pickled.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700586 """
587
Guido van Rossum4cefe742016-09-27 15:20:12 -0700588 __slots__ = ('__name__', '__bound__', '__constraints__',
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300589 '__covariant__', '__contravariant__', '__dict__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700590
591 def __init__(self, name, *constraints, bound=None,
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800592 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700593 self.__name__ = name
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700594 if covariant and contravariant:
Guido van Rossumefa798d2016-08-23 11:01:50 -0700595 raise ValueError("Bivariant types are not supported.")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700596 self.__covariant__ = bool(covariant)
597 self.__contravariant__ = bool(contravariant)
598 if constraints and bound is not None:
599 raise TypeError("Constraints cannot be combined with bound=...")
600 if constraints and len(constraints) == 1:
601 raise TypeError("A single constraint is not allowed")
602 msg = "TypeVar(name, constraint, ...): constraints must be types."
603 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
604 if bound:
605 self.__bound__ = _type_check(bound, "Bound must be a type.")
606 else:
607 self.__bound__ = None
HongWeipenga25a04f2020-04-21 04:01:53 +0800608 try:
609 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling
610 except (AttributeError, ValueError):
611 def_mod = None
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300612 if def_mod != 'typing':
613 self.__module__ = def_mod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700614
Maggie Moss1b4552c2020-09-09 13:23:24 -0700615 def __or__(self, right):
616 return Union[self, right]
617
618 def __ror__(self, right):
619 return Union[self, right]
620
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700621 def __repr__(self):
622 if self.__covariant__:
623 prefix = '+'
624 elif self.__contravariant__:
625 prefix = '-'
626 else:
627 prefix = '~'
628 return prefix + self.__name__
629
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100630 def __reduce__(self):
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300631 return self.__name__
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100632
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700633
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000634def _is_dunder(attr):
635 return attr.startswith('__') and attr.endswith('__')
Guido van Rossum83ec3022017-01-17 20:43:28 -0800636
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300637class _BaseGenericAlias(_Final, _root=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000638 """The central part of internal API.
639
640 This represents a generic version of type 'origin' with type arguments 'params'.
641 There are two kind of these aliases: user defined and special. The special ones
642 are wrappers around builtin collections and ABCs in collections.abc. These must
643 have 'name' always set. If 'inst' is False, then the alias can't be instantiated,
644 this is used by e.g. typing.List and typing.Dict.
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700645 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300646 def __init__(self, origin, *, inst=True, name=None):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000647 self._inst = inst
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000648 self._name = name
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700649 self.__origin__ = origin
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000650 self.__slots__ = None # This is not documented.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300651
652 def __call__(self, *args, **kwargs):
653 if not self._inst:
654 raise TypeError(f"Type {self._name} cannot be instantiated; "
655 f"use {self.__origin__.__name__}() instead")
656 result = self.__origin__(*args, **kwargs)
657 try:
658 result.__orig_class__ = self
659 except AttributeError:
660 pass
661 return result
662
663 def __mro_entries__(self, bases):
664 res = []
665 if self.__origin__ not in bases:
666 res.append(self.__origin__)
667 i = bases.index(self)
668 for b in bases[i+1:]:
669 if isinstance(b, _BaseGenericAlias) or issubclass(b, Generic):
670 break
671 else:
672 res.append(Generic)
673 return tuple(res)
674
675 def __getattr__(self, attr):
676 # We are careful for copy and pickle.
677 # Also for simplicity we just don't relay all dunder names
678 if '__origin__' in self.__dict__ and not _is_dunder(attr):
679 return getattr(self.__origin__, attr)
680 raise AttributeError(attr)
681
682 def __setattr__(self, attr, val):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300683 if _is_dunder(attr) or attr in ('_name', '_inst', '_nparams'):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300684 super().__setattr__(attr, val)
685 else:
686 setattr(self.__origin__, attr, val)
687
688 def __instancecheck__(self, obj):
689 return self.__subclasscheck__(type(obj))
690
691 def __subclasscheck__(self, cls):
692 raise TypeError("Subscripted generics cannot be used with"
693 " class and instance checks")
694
695
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300696# Special typing constructs Union, Optional, Generic, Callable and Tuple
697# use three special attributes for internal bookkeeping of generic types:
698# * __parameters__ is a tuple of unique free type parameters of a generic
699# type, for example, Dict[T, T].__parameters__ == (T,);
700# * __origin__ keeps a reference to a type that was subscripted,
701# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
702# the type.
703# * __args__ is a tuple of all arguments used in subscripting,
704# e.g., Dict[T, int].__args__ == (T, int).
705
706
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300707class _GenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300708 def __init__(self, origin, params, *, inst=True, name=None):
709 super().__init__(origin, inst=inst, name=name)
710 if not isinstance(params, tuple):
711 params = (params,)
712 self.__args__ = tuple(... if a is _TypingEllipsis else
713 () if a is _TypingEmpty else
714 a for a in params)
715 self.__parameters__ = _collect_type_vars(params)
716 if not name:
717 self.__module__ = origin.__module__
718
719 def __eq__(self, other):
720 if not isinstance(other, _GenericAlias):
721 return NotImplemented
722 return (self.__origin__ == other.__origin__
723 and self.__args__ == other.__args__)
724
725 def __hash__(self):
726 return hash((self.__origin__, self.__args__))
727
Maggie Moss1b4552c2020-09-09 13:23:24 -0700728 def __or__(self, right):
729 return Union[self, right]
730
731 def __ror__(self, right):
732 return Union[self, right]
733
Guido van Rossum4cefe742016-09-27 15:20:12 -0700734 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700735 def __getitem__(self, params):
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100736 if self.__origin__ in (Generic, Protocol):
737 # Can't subscript Generic[...] or Protocol[...].
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000738 raise TypeError(f"Cannot subscript already-subscripted {self}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700739 if not isinstance(params, tuple):
740 params = (params,)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700741 msg = "Parameters to generic types must be types."
742 params = tuple(_type_check(p, msg) for p in params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300743 _check_generic(self, params, len(self.__parameters__))
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300744
745 subst = dict(zip(self.__parameters__, params))
746 new_args = []
747 for arg in self.__args__:
748 if isinstance(arg, TypeVar):
749 arg = subst[arg]
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300750 elif isinstance(arg, (_GenericAlias, GenericAlias)):
Serhiy Storchaka0122d482020-05-10 13:39:40 +0300751 subparams = arg.__parameters__
752 if subparams:
753 subargs = tuple(subst[x] for x in subparams)
754 arg = arg[subargs]
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300755 new_args.append(arg)
756 return self.copy_with(tuple(new_args))
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100757
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000758 def copy_with(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300759 return self.__class__(self.__origin__, params, name=self._name, inst=self._inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700760
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000761 def __repr__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300762 if self._name:
763 name = 'typing.' + self._name
764 else:
765 name = _type_repr(self.__origin__)
766 args = ", ".join([_type_repr(a) for a in self.__args__])
767 return f'{name}[{args}]'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000768
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300769 def __reduce__(self):
770 if self._name:
771 origin = globals()[self._name]
772 else:
773 origin = self.__origin__
774 args = tuple(self.__args__)
775 if len(args) == 1 and not isinstance(args[0], tuple):
776 args, = args
777 return operator.getitem, (origin, args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000778
779 def __mro_entries__(self, bases):
780 if self._name: # generic version of an ABC or built-in class
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300781 return super().__mro_entries__(bases)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000782 if self.__origin__ is Generic:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100783 if Protocol in bases:
784 return ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000785 i = bases.index(self)
786 for b in bases[i+1:]:
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300787 if isinstance(b, _BaseGenericAlias) and b is not self:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000788 return ()
789 return (self.__origin__,)
790
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000791
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300792# _nparams is the number of accepted parameters, e.g. 0 for Hashable,
793# 1 for List and 2 for Dict. It may be -1 if variable number of
794# parameters are accepted (needs custom __getitem__).
795
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300796class _SpecialGenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300797 def __init__(self, origin, nparams, *, inst=True, name=None):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300798 if name is None:
799 name = origin.__name__
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300800 super().__init__(origin, inst=inst, name=name)
801 self._nparams = nparams
Serhiy Storchaka2fbc57a2020-05-10 15:14:27 +0300802 if origin.__module__ == 'builtins':
803 self.__doc__ = f'A generic version of {origin.__qualname__}.'
804 else:
805 self.__doc__ = f'A generic version of {origin.__module__}.{origin.__qualname__}.'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000806
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300807 @_tp_cache
808 def __getitem__(self, params):
809 if not isinstance(params, tuple):
810 params = (params,)
811 msg = "Parameters to generic types must be types."
812 params = tuple(_type_check(p, msg) for p in params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300813 _check_generic(self, params, self._nparams)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300814 return self.copy_with(params)
815
816 def copy_with(self, params):
817 return _GenericAlias(self.__origin__, params,
818 name=self._name, inst=self._inst)
819
820 def __repr__(self):
821 return 'typing.' + self._name
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000822
823 def __subclasscheck__(self, cls):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300824 if isinstance(cls, _SpecialGenericAlias):
825 return issubclass(cls.__origin__, self.__origin__)
826 if not isinstance(cls, _GenericAlias):
827 return issubclass(cls, self.__origin__)
828 return super().__subclasscheck__(cls)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700829
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100830 def __reduce__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300831 return self._name
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100832
Maggie Moss1b4552c2020-09-09 13:23:24 -0700833 def __or__(self, right):
834 return Union[self, right]
835
836 def __ror__(self, right):
837 return Union[self, right]
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700838
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300839class _CallableGenericAlias(_GenericAlias, _root=True):
840 def __repr__(self):
841 assert self._name == 'Callable'
842 if len(self.__args__) == 2 and self.__args__[0] is Ellipsis:
843 return super().__repr__()
844 return (f'typing.Callable'
845 f'[[{", ".join([_type_repr(a) for a in self.__args__[:-1]])}], '
846 f'{_type_repr(self.__args__[-1])}]')
847
848 def __reduce__(self):
849 args = self.__args__
850 if not (len(args) == 2 and args[0] is ...):
851 args = list(args[:-1]), args[-1]
852 return operator.getitem, (Callable, args)
853
854
855class _CallableType(_SpecialGenericAlias, _root=True):
856 def copy_with(self, params):
857 return _CallableGenericAlias(self.__origin__, params,
858 name=self._name, inst=self._inst)
859
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000860 def __getitem__(self, params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000861 if not isinstance(params, tuple) or len(params) != 2:
862 raise TypeError("Callable must be used as "
863 "Callable[[arg, ...], result].")
864 args, result = params
865 if args is Ellipsis:
866 params = (Ellipsis, result)
867 else:
868 if not isinstance(args, list):
869 raise TypeError(f"Callable[args, result]: args must be a list."
870 f" Got {args}")
871 params = (tuple(args), result)
872 return self.__getitem_inner__(params)
873
874 @_tp_cache
875 def __getitem_inner__(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300876 args, result = params
877 msg = "Callable[args, result]: result must be a type."
878 result = _type_check(result, msg)
879 if args is Ellipsis:
880 return self.copy_with((_TypingEllipsis, result))
881 msg = "Callable[[arg, ...], result]: each arg must be a type."
882 args = tuple(_type_check(arg, msg) for arg in args)
883 params = args + (result,)
884 return self.copy_with(params)
885
886
887class _TupleType(_SpecialGenericAlias, _root=True):
888 @_tp_cache
889 def __getitem__(self, params):
890 if params == ():
891 return self.copy_with((_TypingEmpty,))
892 if not isinstance(params, tuple):
893 params = (params,)
894 if len(params) == 2 and params[1] is ...:
895 msg = "Tuple[t, ...]: t must be a type."
896 p = _type_check(params[0], msg)
897 return self.copy_with((p, _TypingEllipsis))
898 msg = "Tuple[t0, t1, ...]: each t must be a type."
899 params = tuple(_type_check(p, msg) for p in params)
900 return self.copy_with(params)
901
902
903class _UnionGenericAlias(_GenericAlias, _root=True):
904 def copy_with(self, params):
905 return Union[params]
906
907 def __eq__(self, other):
908 if not isinstance(other, _UnionGenericAlias):
909 return NotImplemented
910 return set(self.__args__) == set(other.__args__)
911
912 def __hash__(self):
913 return hash(frozenset(self.__args__))
914
915 def __repr__(self):
916 args = self.__args__
917 if len(args) == 2:
918 if args[0] is type(None):
919 return f'typing.Optional[{_type_repr(args[1])}]'
920 elif args[1] is type(None):
921 return f'typing.Optional[{_type_repr(args[0])}]'
922 return super().__repr__()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000923
Maggie Moss1b4552c2020-09-09 13:23:24 -0700924 def __instancecheck__(self, obj):
925 return self.__subclasscheck__(type(obj))
926
927 def __subclasscheck__(self, cls):
928 for arg in self.__args__:
929 if issubclass(cls, arg):
930 return True
931
932
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000933
934class Generic:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700935 """Abstract base class for generic types.
936
Guido van Rossumb24569a2016-11-20 18:01:29 -0800937 A generic type is typically declared by inheriting from
938 this class parameterized with one or more type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700939 For example, a generic mapping type might be defined as::
940
941 class Mapping(Generic[KT, VT]):
942 def __getitem__(self, key: KT) -> VT:
943 ...
944 # Etc.
945
946 This class can then be used as follows::
947
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700948 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700949 try:
950 return mapping[key]
951 except KeyError:
952 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700953 """
Guido van Rossumd70fe632015-08-05 12:11:06 +0200954 __slots__ = ()
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100955 _is_protocol = False
Guido van Rossumd70fe632015-08-05 12:11:06 +0200956
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000957 @_tp_cache
958 def __class_getitem__(cls, params):
959 if not isinstance(params, tuple):
960 params = (params,)
961 if not params and cls is not Tuple:
962 raise TypeError(
963 f"Parameter list to {cls.__qualname__}[...] cannot be empty")
964 msg = "Parameters to generic types must be types."
965 params = tuple(_type_check(p, msg) for p in params)
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100966 if cls in (Generic, Protocol):
967 # Generic and Protocol can only be subscripted with unique type variables.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000968 if not all(isinstance(p, TypeVar) for p in params):
969 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100970 f"Parameters to {cls.__name__}[...] must all be type variables")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000971 if len(set(params)) != len(params):
972 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100973 f"Parameters to {cls.__name__}[...] must all be unique")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000974 else:
975 # Subscripting a regular Generic subclass.
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300976 _check_generic(cls, params, len(cls.__parameters__))
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000977 return _GenericAlias(cls, params)
978
979 def __init_subclass__(cls, *args, **kwargs):
Ivan Levkivskyiee566fe2018-04-04 17:00:15 +0100980 super().__init_subclass__(*args, **kwargs)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000981 tvars = []
982 if '__orig_bases__' in cls.__dict__:
983 error = Generic in cls.__orig_bases__
984 else:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100985 error = Generic in cls.__bases__ and cls.__name__ != 'Protocol'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000986 if error:
987 raise TypeError("Cannot inherit from plain Generic")
988 if '__orig_bases__' in cls.__dict__:
989 tvars = _collect_type_vars(cls.__orig_bases__)
990 # Look for Generic[T1, ..., Tn].
991 # If found, tvars must be a subset of it.
992 # If not found, tvars is it.
993 # Also check for and reject plain Generic,
994 # and reject multiple Generic[...].
995 gvars = None
996 for base in cls.__orig_bases__:
997 if (isinstance(base, _GenericAlias) and
998 base.__origin__ is Generic):
999 if gvars is not None:
1000 raise TypeError(
1001 "Cannot inherit from Generic[...] multiple types.")
1002 gvars = base.__parameters__
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001003 if gvars is not None:
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001004 tvarset = set(tvars)
1005 gvarset = set(gvars)
1006 if not tvarset <= gvarset:
1007 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
1008 s_args = ', '.join(str(g) for g in gvars)
1009 raise TypeError(f"Some type variables ({s_vars}) are"
1010 f" not listed in Generic[{s_args}]")
1011 tvars = gvars
1012 cls.__parameters__ = tuple(tvars)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001013
1014
1015class _TypingEmpty:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001016 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
1017 to allow empty list/tuple in specific places, without allowing them
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001018 to sneak in where prohibited.
1019 """
1020
1021
1022class _TypingEllipsis:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001023 """Internal placeholder for ... (ellipsis)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001024
1025
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001026_TYPING_INTERNALS = ['__parameters__', '__orig_bases__', '__orig_class__',
1027 '_is_protocol', '_is_runtime_protocol']
1028
1029_SPECIAL_NAMES = ['__abstractmethods__', '__annotations__', '__dict__', '__doc__',
1030 '__init__', '__module__', '__new__', '__slots__',
Guido van Rossum48b069a2020-04-07 09:50:06 -07001031 '__subclasshook__', '__weakref__', '__class_getitem__']
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001032
1033# These special attributes will be not collected as protocol members.
1034EXCLUDED_ATTRIBUTES = _TYPING_INTERNALS + _SPECIAL_NAMES + ['_MutableMapping__marker']
1035
1036
1037def _get_protocol_attrs(cls):
1038 """Collect protocol members from a protocol class objects.
1039
1040 This includes names actually defined in the class dictionary, as well
1041 as names that appear in annotations. Special names (above) are skipped.
1042 """
1043 attrs = set()
1044 for base in cls.__mro__[:-1]: # without object
1045 if base.__name__ in ('Protocol', 'Generic'):
1046 continue
1047 annotations = getattr(base, '__annotations__', {})
1048 for attr in list(base.__dict__.keys()) + list(annotations.keys()):
1049 if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
1050 attrs.add(attr)
1051 return attrs
1052
1053
1054def _is_callable_members_only(cls):
1055 # PEP 544 prohibits using issubclass() with protocols that have non-method members.
1056 return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls))
1057
1058
1059def _no_init(self, *args, **kwargs):
1060 if type(self)._is_protocol:
1061 raise TypeError('Protocols cannot be instantiated')
1062
1063
1064def _allow_reckless_class_cheks():
Nickolena Fishercfaf4c02020-04-26 12:49:11 -05001065 """Allow instance and class checks for special stdlib modules.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001066
1067 The abc and functools modules indiscriminately call isinstance() and
1068 issubclass() on the whole MRO of a user class, which may contain protocols.
1069 """
1070 try:
1071 return sys._getframe(3).f_globals['__name__'] in ['abc', 'functools']
1072 except (AttributeError, ValueError): # For platforms without _getframe().
1073 return True
1074
1075
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001076_PROTO_ALLOWLIST = {
Divij Rajkumar692a0dc2019-09-12 11:13:51 +01001077 'collections.abc': [
1078 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
1079 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
1080 ],
1081 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
1082}
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001083
1084
1085class _ProtocolMeta(ABCMeta):
1086 # This metaclass is really unfortunate and exists only because of
1087 # the lack of __instancehook__.
1088 def __instancecheck__(cls, instance):
1089 # We need this method for situations where attributes are
1090 # assigned in __init__.
1091 if ((not getattr(cls, '_is_protocol', False) or
1092 _is_callable_members_only(cls)) and
1093 issubclass(instance.__class__, cls)):
1094 return True
1095 if cls._is_protocol:
1096 if all(hasattr(instance, attr) and
1097 # All *methods* can be blocked by setting them to None.
1098 (not callable(getattr(cls, attr, None)) or
1099 getattr(instance, attr) is not None)
1100 for attr in _get_protocol_attrs(cls)):
1101 return True
1102 return super().__instancecheck__(instance)
1103
1104
1105class Protocol(Generic, metaclass=_ProtocolMeta):
1106 """Base class for protocol classes.
1107
1108 Protocol classes are defined as::
1109
1110 class Proto(Protocol):
1111 def meth(self) -> int:
1112 ...
1113
1114 Such classes are primarily used with static type checkers that recognize
1115 structural subtyping (static duck-typing), for example::
1116
1117 class C:
1118 def meth(self) -> int:
1119 return 0
1120
1121 def func(x: Proto) -> int:
1122 return x.meth()
1123
1124 func(C()) # Passes static type check
1125
1126 See PEP 544 for details. Protocol classes decorated with
1127 @typing.runtime_checkable act as simple-minded runtime protocols that check
1128 only the presence of given attributes, ignoring their type signatures.
1129 Protocol classes can be generic, they are defined as::
1130
1131 class GenProto(Protocol[T]):
1132 def meth(self) -> T:
1133 ...
1134 """
1135 __slots__ = ()
1136 _is_protocol = True
1137 _is_runtime_protocol = False
1138
1139 def __init_subclass__(cls, *args, **kwargs):
1140 super().__init_subclass__(*args, **kwargs)
1141
1142 # Determine if this is a protocol or a concrete subclass.
1143 if not cls.__dict__.get('_is_protocol', False):
1144 cls._is_protocol = any(b is Protocol for b in cls.__bases__)
1145
1146 # Set (or override) the protocol subclass hook.
1147 def _proto_hook(other):
1148 if not cls.__dict__.get('_is_protocol', False):
1149 return NotImplemented
1150
1151 # First, perform various sanity checks.
1152 if not getattr(cls, '_is_runtime_protocol', False):
1153 if _allow_reckless_class_cheks():
1154 return NotImplemented
1155 raise TypeError("Instance and class checks can only be used with"
1156 " @runtime_checkable protocols")
1157 if not _is_callable_members_only(cls):
1158 if _allow_reckless_class_cheks():
1159 return NotImplemented
1160 raise TypeError("Protocols with non-method members"
1161 " don't support issubclass()")
1162 if not isinstance(other, type):
1163 # Same error message as for issubclass(1, int).
1164 raise TypeError('issubclass() arg 1 must be a class')
1165
1166 # Second, perform the actual structural compatibility check.
1167 for attr in _get_protocol_attrs(cls):
1168 for base in other.__mro__:
1169 # Check if the members appears in the class dictionary...
1170 if attr in base.__dict__:
1171 if base.__dict__[attr] is None:
1172 return NotImplemented
1173 break
1174
1175 # ...or in annotations, if it is a sub-protocol.
1176 annotations = getattr(base, '__annotations__', {})
1177 if (isinstance(annotations, collections.abc.Mapping) and
1178 attr in annotations and
1179 issubclass(other, Generic) and other._is_protocol):
1180 break
1181 else:
1182 return NotImplemented
1183 return True
1184
1185 if '__subclasshook__' not in cls.__dict__:
1186 cls.__subclasshook__ = _proto_hook
1187
1188 # We have nothing more to do for non-protocols...
1189 if not cls._is_protocol:
1190 return
1191
1192 # ... otherwise check consistency of bases, and prohibit instantiation.
1193 for base in cls.__bases__:
1194 if not (base in (object, Generic) or
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001195 base.__module__ in _PROTO_ALLOWLIST and
1196 base.__name__ in _PROTO_ALLOWLIST[base.__module__] or
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001197 issubclass(base, Generic) and base._is_protocol):
1198 raise TypeError('Protocols can only inherit from other'
1199 ' protocols, got %r' % base)
1200 cls.__init__ = _no_init
1201
1202
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001203class _AnnotatedAlias(_GenericAlias, _root=True):
1204 """Runtime representation of an annotated type.
1205
1206 At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't'
1207 with extra annotations. The alias behaves like a normal typing alias,
1208 instantiating is the same as instantiating the underlying type, binding
1209 it to types is also the same.
1210 """
1211 def __init__(self, origin, metadata):
1212 if isinstance(origin, _AnnotatedAlias):
1213 metadata = origin.__metadata__ + metadata
1214 origin = origin.__origin__
1215 super().__init__(origin, origin)
1216 self.__metadata__ = metadata
1217
1218 def copy_with(self, params):
1219 assert len(params) == 1
1220 new_type = params[0]
1221 return _AnnotatedAlias(new_type, self.__metadata__)
1222
1223 def __repr__(self):
1224 return "typing.Annotated[{}, {}]".format(
1225 _type_repr(self.__origin__),
1226 ", ".join(repr(a) for a in self.__metadata__)
1227 )
1228
1229 def __reduce__(self):
1230 return operator.getitem, (
1231 Annotated, (self.__origin__,) + self.__metadata__
1232 )
1233
1234 def __eq__(self, other):
1235 if not isinstance(other, _AnnotatedAlias):
1236 return NotImplemented
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001237 return (self.__origin__ == other.__origin__
1238 and self.__metadata__ == other.__metadata__)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001239
1240 def __hash__(self):
1241 return hash((self.__origin__, self.__metadata__))
1242
1243
1244class Annotated:
1245 """Add context specific metadata to a type.
1246
1247 Example: Annotated[int, runtime_check.Unsigned] indicates to the
1248 hypothetical runtime_check module that this type is an unsigned int.
1249 Every other consumer of this type can ignore this metadata and treat
1250 this type as int.
1251
1252 The first argument to Annotated must be a valid type.
1253
1254 Details:
1255
1256 - It's an error to call `Annotated` with less than two arguments.
1257 - Nested Annotated are flattened::
1258
1259 Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
1260
1261 - Instantiating an annotated type is equivalent to instantiating the
1262 underlying type::
1263
1264 Annotated[C, Ann1](5) == C(5)
1265
1266 - Annotated can be used as a generic type alias::
1267
1268 Optimized = Annotated[T, runtime.Optimize()]
1269 Optimized[int] == Annotated[int, runtime.Optimize()]
1270
1271 OptimizedList = Annotated[List[T], runtime.Optimize()]
1272 OptimizedList[int] == Annotated[List[int], runtime.Optimize()]
1273 """
1274
1275 __slots__ = ()
1276
1277 def __new__(cls, *args, **kwargs):
1278 raise TypeError("Type Annotated cannot be instantiated.")
1279
1280 @_tp_cache
1281 def __class_getitem__(cls, params):
1282 if not isinstance(params, tuple) or len(params) < 2:
1283 raise TypeError("Annotated[...] should be used "
1284 "with at least two arguments (a type and an "
1285 "annotation).")
1286 msg = "Annotated[t, ...]: t must be a type."
1287 origin = _type_check(params[0], msg)
1288 metadata = tuple(params[1:])
1289 return _AnnotatedAlias(origin, metadata)
1290
1291 def __init_subclass__(cls, *args, **kwargs):
1292 raise TypeError(
1293 "Cannot subclass {}.Annotated".format(cls.__module__)
1294 )
1295
1296
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001297def runtime_checkable(cls):
1298 """Mark a protocol class as a runtime protocol.
1299
1300 Such protocol can be used with isinstance() and issubclass().
1301 Raise TypeError if applied to a non-protocol class.
1302 This allows a simple-minded structural check very similar to
1303 one trick ponies in collections.abc such as Iterable.
1304 For example::
1305
1306 @runtime_checkable
1307 class Closable(Protocol):
1308 def close(self): ...
1309
1310 assert isinstance(open('/some/file'), Closable)
1311
1312 Warning: this will check only the presence of the required methods,
1313 not their type signatures!
1314 """
1315 if not issubclass(cls, Generic) or not cls._is_protocol:
1316 raise TypeError('@runtime_checkable can be only applied to protocol classes,'
1317 ' got %r' % cls)
1318 cls._is_runtime_protocol = True
1319 return cls
1320
1321
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001322def cast(typ, val):
1323 """Cast a value to a type.
1324
1325 This returns the value unchanged. To the type checker this
1326 signals that the return value has the designated type, but at
1327 runtime we intentionally don't check anything (we want this
1328 to be as fast as possible).
1329 """
1330 return val
1331
1332
1333def _get_defaults(func):
1334 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -08001335 try:
1336 code = func.__code__
1337 except AttributeError:
1338 # Some built-in functions don't have __code__, __defaults__, etc.
1339 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001340 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001341 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001342 arg_names = arg_names[:pos_count]
1343 defaults = func.__defaults__ or ()
1344 kwdefaults = func.__kwdefaults__
1345 res = dict(kwdefaults) if kwdefaults else {}
1346 pos_offset = pos_count - len(defaults)
1347 for name, value in zip(arg_names[pos_offset:], defaults):
1348 assert name not in res
1349 res[name] = value
1350 return res
1351
1352
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001353_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
1354 types.MethodType, types.ModuleType,
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02001355 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001356
1357
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001358def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
Guido van Rossum991d14f2016-11-09 13:12:51 -08001359 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001360
Guido van Rossum991d14f2016-11-09 13:12:51 -08001361 This is often the same as obj.__annotations__, but it handles
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001362 forward references encoded as string literals, adds Optional[t] if a
1363 default value equal to None is set and recursively replaces all
1364 'Annotated[T, ...]' with 'T' (unless 'include_extras=True').
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001365
Guido van Rossum991d14f2016-11-09 13:12:51 -08001366 The argument may be a module, class, method, or function. The annotations
1367 are returned as a dictionary. For classes, annotations include also
1368 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001369
Guido van Rossum991d14f2016-11-09 13:12:51 -08001370 TypeError is raised if the argument is not of a type that can contain
1371 annotations, and an empty dictionary is returned if no annotations are
1372 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001373
Guido van Rossum991d14f2016-11-09 13:12:51 -08001374 BEWARE -- the behavior of globalns and localns is counterintuitive
1375 (unless you are familiar with how eval() and exec() work). The
1376 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001377
Guido van Rossum991d14f2016-11-09 13:12:51 -08001378 - If no dict arguments are passed, an attempt is made to use the
Łukasz Langaf350a262017-09-14 14:33:00 -04001379 globals from obj (or the respective module's globals for classes),
1380 and these are also used as the locals. If the object does not appear
1381 to have globals, an empty dictionary is used.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001382
Guido van Rossum991d14f2016-11-09 13:12:51 -08001383 - If one dict argument is passed, it is used for both globals and
1384 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001385
Guido van Rossum991d14f2016-11-09 13:12:51 -08001386 - If two dict arguments are passed, they specify globals and
1387 locals, respectively.
1388 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001389
Guido van Rossum991d14f2016-11-09 13:12:51 -08001390 if getattr(obj, '__no_type_check__', None):
1391 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001392 # Classes require a special treatment.
1393 if isinstance(obj, type):
1394 hints = {}
1395 for base in reversed(obj.__mro__):
Łukasz Langaf350a262017-09-14 14:33:00 -04001396 if globalns is None:
1397 base_globals = sys.modules[base.__module__].__dict__
1398 else:
1399 base_globals = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001400 ann = base.__dict__.get('__annotations__', {})
1401 for name, value in ann.items():
1402 if value is None:
1403 value = type(None)
1404 if isinstance(value, str):
Nina Zakharenko0e61dff2018-05-22 20:32:10 -07001405 value = ForwardRef(value, is_argument=False)
Łukasz Langaf350a262017-09-14 14:33:00 -04001406 value = _eval_type(value, base_globals, localns)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001407 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001408 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
Łukasz Langaf350a262017-09-14 14:33:00 -04001409
1410 if globalns is None:
1411 if isinstance(obj, types.ModuleType):
1412 globalns = obj.__dict__
1413 else:
benedwards140aca3a32019-11-21 17:24:58 +00001414 nsobj = obj
1415 # Find globalns for the unwrapped object.
1416 while hasattr(nsobj, '__wrapped__'):
1417 nsobj = nsobj.__wrapped__
1418 globalns = getattr(nsobj, '__globals__', {})
Łukasz Langaf350a262017-09-14 14:33:00 -04001419 if localns is None:
1420 localns = globalns
1421 elif localns is None:
1422 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001423 hints = getattr(obj, '__annotations__', None)
1424 if hints is None:
1425 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001426 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001427 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001428 else:
1429 raise TypeError('{!r} is not a module, class, method, '
1430 'or function.'.format(obj))
1431 defaults = _get_defaults(obj)
1432 hints = dict(hints)
1433 for name, value in hints.items():
1434 if value is None:
1435 value = type(None)
1436 if isinstance(value, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001437 value = ForwardRef(value)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001438 value = _eval_type(value, globalns, localns)
1439 if name in defaults and defaults[name] is None:
1440 value = Optional[value]
1441 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001442 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
1443
1444
1445def _strip_annotations(t):
1446 """Strips the annotations from a given type.
1447 """
1448 if isinstance(t, _AnnotatedAlias):
1449 return _strip_annotations(t.__origin__)
1450 if isinstance(t, _GenericAlias):
1451 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1452 if stripped_args == t.__args__:
1453 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001454 return t.copy_with(stripped_args)
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001455 if isinstance(t, GenericAlias):
1456 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1457 if stripped_args == t.__args__:
1458 return t
1459 return GenericAlias(t.__origin__, stripped_args)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001460 return t
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001461
1462
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001463def get_origin(tp):
1464 """Get the unsubscripted version of a type.
1465
Jakub Stasiak38aaaaa2020-02-07 02:15:12 +01001466 This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
1467 and Annotated. Return None for unsupported types. Examples::
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001468
1469 get_origin(Literal[42]) is Literal
1470 get_origin(int) is None
1471 get_origin(ClassVar[int]) is ClassVar
1472 get_origin(Generic) is Generic
1473 get_origin(Generic[T]) is Generic
1474 get_origin(Union[T, int]) is Union
1475 get_origin(List[Tuple[T, T]][int]) == list
1476 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001477 if isinstance(tp, _AnnotatedAlias):
1478 return Annotated
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001479 if isinstance(tp, (_BaseGenericAlias, GenericAlias)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001480 return tp.__origin__
1481 if tp is Generic:
1482 return Generic
1483 return None
1484
1485
1486def get_args(tp):
1487 """Get type arguments with all substitutions performed.
1488
1489 For unions, basic simplifications used by Union constructor are performed.
1490 Examples::
1491 get_args(Dict[str, int]) == (str, int)
1492 get_args(int) == ()
1493 get_args(Union[int, Union[T, int], str][int]) == (int, str)
1494 get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
1495 get_args(Callable[[], T][int]) == ([], int)
1496 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001497 if isinstance(tp, _AnnotatedAlias):
1498 return (tp.__origin__,) + tp.__metadata__
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001499 if isinstance(tp, _GenericAlias):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001500 res = tp.__args__
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001501 if tp.__origin__ is collections.abc.Callable and res[0] is not Ellipsis:
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001502 res = (list(res[:-1]), res[-1])
1503 return res
Serhiy Storchaka6292be72020-04-27 10:27:21 +03001504 if isinstance(tp, GenericAlias):
1505 return tp.__args__
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001506 return ()
1507
1508
Patrick Reader0705ec82020-09-16 05:58:32 +01001509def is_typeddict(tp):
1510 """Check if an annotation is a TypedDict class
1511
1512 For example::
1513 class Film(TypedDict):
1514 title: str
1515 year: int
1516
1517 is_typeddict(Film) # => True
1518 is_typeddict(Union[list, str]) # => False
1519 """
1520 return isinstance(tp, _TypedDictMeta)
1521
1522
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001523def no_type_check(arg):
1524 """Decorator to indicate that annotations are not type hints.
1525
1526 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001527 applies recursively to all methods and classes defined in that class
1528 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001529
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001530 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001531 """
1532 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001533 arg_attrs = arg.__dict__.copy()
1534 for attr, val in arg.__dict__.items():
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001535 if val in arg.__bases__ + (arg,):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001536 arg_attrs.pop(attr)
1537 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001538 if isinstance(obj, types.FunctionType):
1539 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001540 if isinstance(obj, type):
1541 no_type_check(obj)
1542 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001543 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001544 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001545 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001546 return arg
1547
1548
1549def no_type_check_decorator(decorator):
1550 """Decorator to give another decorator the @no_type_check effect.
1551
1552 This wraps the decorator with something that wraps the decorated
1553 function in @no_type_check.
1554 """
1555
1556 @functools.wraps(decorator)
1557 def wrapped_decorator(*args, **kwds):
1558 func = decorator(*args, **kwds)
1559 func = no_type_check(func)
1560 return func
1561
1562 return wrapped_decorator
1563
1564
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001565def _overload_dummy(*args, **kwds):
1566 """Helper for @overload to raise when called."""
1567 raise NotImplementedError(
1568 "You should not call an overloaded function. "
1569 "A series of @overload-decorated functions "
1570 "outside a stub module should always be followed "
1571 "by an implementation that is not @overload-ed.")
1572
1573
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001574def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001575 """Decorator for overloaded functions/methods.
1576
1577 In a stub file, place two or more stub definitions for the same
1578 function in a row, each decorated with @overload. For example:
1579
1580 @overload
1581 def utf8(value: None) -> None: ...
1582 @overload
1583 def utf8(value: bytes) -> bytes: ...
1584 @overload
1585 def utf8(value: str) -> bytes: ...
1586
1587 In a non-stub file (i.e. a regular .py file), do the same but
1588 follow it with an implementation. The implementation should *not*
1589 be decorated with @overload. For example:
1590
1591 @overload
1592 def utf8(value: None) -> None: ...
1593 @overload
1594 def utf8(value: bytes) -> bytes: ...
1595 @overload
1596 def utf8(value: str) -> bytes: ...
1597 def utf8(value):
1598 # implementation goes here
1599 """
1600 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001601
1602
Ivan Levkivskyif3672422019-05-26 09:37:07 +01001603def final(f):
1604 """A decorator to indicate final methods and final classes.
1605
1606 Use this decorator to indicate to type checkers that the decorated
1607 method cannot be overridden, and decorated class cannot be subclassed.
1608 For example:
1609
1610 class Base:
1611 @final
1612 def done(self) -> None:
1613 ...
1614 class Sub(Base):
1615 def done(self) -> None: # Error reported by type checker
1616 ...
1617
1618 @final
1619 class Leaf:
1620 ...
1621 class Other(Leaf): # Error reported by type checker
1622 ...
1623
1624 There is no runtime checking of these properties.
1625 """
1626 return f
1627
1628
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001629# Some unconstrained type variables. These are used by the container types.
1630# (These are not for export.)
1631T = TypeVar('T') # Any type.
1632KT = TypeVar('KT') # Key type.
1633VT = TypeVar('VT') # Value type.
1634T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
1635V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
1636VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
1637T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
1638# Internal type variable used for Type[].
1639CT_co = TypeVar('CT_co', covariant=True, bound=type)
1640
1641# A useful type variable with constraints. This represents string types.
1642# (This one *is* for export!)
1643AnyStr = TypeVar('AnyStr', bytes, str)
1644
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001645
1646# Various ABCs mimicking those in collections.abc.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001647_alias = _SpecialGenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001648
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001649Hashable = _alias(collections.abc.Hashable, 0) # Not generic.
1650Awaitable = _alias(collections.abc.Awaitable, 1)
1651Coroutine = _alias(collections.abc.Coroutine, 3)
1652AsyncIterable = _alias(collections.abc.AsyncIterable, 1)
1653AsyncIterator = _alias(collections.abc.AsyncIterator, 1)
1654Iterable = _alias(collections.abc.Iterable, 1)
1655Iterator = _alias(collections.abc.Iterator, 1)
1656Reversible = _alias(collections.abc.Reversible, 1)
1657Sized = _alias(collections.abc.Sized, 0) # Not generic.
1658Container = _alias(collections.abc.Container, 1)
1659Collection = _alias(collections.abc.Collection, 1)
1660Callable = _CallableType(collections.abc.Callable, 2)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001661Callable.__doc__ = \
1662 """Callable type; Callable[[int], str] is a function of (int) -> str.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001663
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001664 The subscription syntax must always be used with exactly two
1665 values: the argument list and the return type. The argument list
1666 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001667
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001668 There is no syntax to indicate optional or keyword arguments,
1669 such function types are rarely used as callback types.
1670 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001671AbstractSet = _alias(collections.abc.Set, 1, name='AbstractSet')
1672MutableSet = _alias(collections.abc.MutableSet, 1)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001673# NOTE: Mapping is only covariant in the value type.
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001674Mapping = _alias(collections.abc.Mapping, 2)
1675MutableMapping = _alias(collections.abc.MutableMapping, 2)
1676Sequence = _alias(collections.abc.Sequence, 1)
1677MutableSequence = _alias(collections.abc.MutableSequence, 1)
1678ByteString = _alias(collections.abc.ByteString, 0) # Not generic
1679# Tuple accepts variable number of parameters.
1680Tuple = _TupleType(tuple, -1, inst=False, name='Tuple')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001681Tuple.__doc__ = \
1682 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001683
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001684 Example: Tuple[T1, T2] is a tuple of two elements corresponding
1685 to type variables T1 and T2. Tuple[int, float, str] is a tuple
1686 of an int, a float and a string.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001687
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001688 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
1689 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001690List = _alias(list, 1, inst=False, name='List')
1691Deque = _alias(collections.deque, 1, name='Deque')
1692Set = _alias(set, 1, inst=False, name='Set')
1693FrozenSet = _alias(frozenset, 1, inst=False, name='FrozenSet')
1694MappingView = _alias(collections.abc.MappingView, 1)
1695KeysView = _alias(collections.abc.KeysView, 1)
1696ItemsView = _alias(collections.abc.ItemsView, 2)
1697ValuesView = _alias(collections.abc.ValuesView, 1)
1698ContextManager = _alias(contextlib.AbstractContextManager, 1, name='ContextManager')
1699AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, 1, name='AsyncContextManager')
1700Dict = _alias(dict, 2, inst=False, name='Dict')
1701DefaultDict = _alias(collections.defaultdict, 2, name='DefaultDict')
1702OrderedDict = _alias(collections.OrderedDict, 2)
1703Counter = _alias(collections.Counter, 1)
1704ChainMap = _alias(collections.ChainMap, 2)
1705Generator = _alias(collections.abc.Generator, 3)
1706AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2)
1707Type = _alias(type, 1, inst=False, name='Type')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001708Type.__doc__ = \
1709 """A special construct usable to annotate class objects.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001710
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001711 For example, suppose we have the following classes::
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001712
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001713 class User: ... # Abstract base for User classes
1714 class BasicUser(User): ...
1715 class ProUser(User): ...
1716 class TeamUser(User): ...
Guido van Rossumf17c2002015-12-03 17:31:24 -08001717
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001718 And a function that takes a class argument that's a subclass of
1719 User and returns an instance of the corresponding class::
Guido van Rossumf17c2002015-12-03 17:31:24 -08001720
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001721 U = TypeVar('U', bound=User)
1722 def new_user(user_class: Type[U]) -> U:
1723 user = user_class()
1724 # (Here we could write the user object to a database)
1725 return user
Guido van Rossumf17c2002015-12-03 17:31:24 -08001726
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001727 joe = new_user(BasicUser)
Guido van Rossumf17c2002015-12-03 17:31:24 -08001728
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001729 At this point the type checker knows that joe has type BasicUser.
1730 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001731
1732
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001733@runtime_checkable
1734class SupportsInt(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001735 """An ABC with one abstract method __int__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001736 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001737
1738 @abstractmethod
1739 def __int__(self) -> int:
1740 pass
1741
1742
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001743@runtime_checkable
1744class SupportsFloat(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001745 """An ABC with one abstract method __float__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001746 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001747
1748 @abstractmethod
1749 def __float__(self) -> float:
1750 pass
1751
1752
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001753@runtime_checkable
1754class SupportsComplex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001755 """An ABC with one abstract method __complex__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001756 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001757
1758 @abstractmethod
1759 def __complex__(self) -> complex:
1760 pass
1761
1762
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001763@runtime_checkable
1764class SupportsBytes(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001765 """An ABC with one abstract method __bytes__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001766 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001767
1768 @abstractmethod
1769 def __bytes__(self) -> bytes:
1770 pass
1771
1772
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001773@runtime_checkable
1774class SupportsIndex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001775 """An ABC with one abstract method __index__."""
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -07001776 __slots__ = ()
1777
1778 @abstractmethod
1779 def __index__(self) -> int:
1780 pass
1781
1782
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001783@runtime_checkable
1784class SupportsAbs(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001785 """An ABC with one abstract method __abs__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001786 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001787
1788 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001789 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001790 pass
1791
1792
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001793@runtime_checkable
1794class SupportsRound(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001795 """An ABC with one abstract method __round__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001796 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001797
1798 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001799 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001800 pass
1801
1802
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001803def _make_nmtuple(name, types, module, defaults = ()):
1804 fields = [n for n, t in types]
1805 types = {n: _type_check(t, f"field {n} annotation must be a type")
1806 for n, t in types}
1807 nm_tpl = collections.namedtuple(name, fields,
1808 defaults=defaults, module=module)
1809 nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001810 return nm_tpl
1811
1812
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001813# attributes prohibited to set in NamedTuple class syntax
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001814_prohibited = frozenset({'__new__', '__init__', '__slots__', '__getnewargs__',
1815 '_fields', '_field_defaults',
1816 '_make', '_replace', '_asdict', '_source'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001817
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001818_special = frozenset({'__module__', '__name__', '__annotations__'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001819
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001820
Guido van Rossum2f841442016-11-15 09:48:06 -08001821class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001822
Guido van Rossum2f841442016-11-15 09:48:06 -08001823 def __new__(cls, typename, bases, ns):
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001824 assert bases[0] is _NamedTuple
Guido van Rossum2f841442016-11-15 09:48:06 -08001825 types = ns.get('__annotations__', {})
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001826 default_names = []
Guido van Rossum3c268be2017-01-18 08:03:50 -08001827 for field_name in types:
1828 if field_name in ns:
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001829 default_names.append(field_name)
1830 elif default_names:
1831 raise TypeError(f"Non-default namedtuple field {field_name} "
1832 f"cannot follow default field"
1833 f"{'s' if len(default_names) > 1 else ''} "
1834 f"{', '.join(default_names)}")
1835 nm_tpl = _make_nmtuple(typename, types.items(),
1836 defaults=[ns[n] for n in default_names],
1837 module=ns['__module__'])
Guido van Rossum95919c02017-01-22 17:47:20 -08001838 # update from user namespace without overriding special namedtuple attributes
1839 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001840 if key in _prohibited:
1841 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
1842 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08001843 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08001844 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001845
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001846
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001847def NamedTuple(typename, fields=None, /, **kwargs):
Guido van Rossum2f841442016-11-15 09:48:06 -08001848 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001849
Guido van Rossum2f841442016-11-15 09:48:06 -08001850 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001851
Guido van Rossum2f841442016-11-15 09:48:06 -08001852 class Employee(NamedTuple):
1853 name: str
1854 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001855
Guido van Rossum2f841442016-11-15 09:48:06 -08001856 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001857
Guido van Rossum2f841442016-11-15 09:48:06 -08001858 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001859
Raymond Hettingerf7b57df2019-03-18 09:53:56 -07001860 The resulting class has an extra __annotations__ attribute, giving a
1861 dict that maps field names to types. (The field names are also in
1862 the _fields attribute, which is part of the namedtuple API.)
1863 Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001864
Guido van Rossum2f841442016-11-15 09:48:06 -08001865 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001866
Guido van Rossum2f841442016-11-15 09:48:06 -08001867 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001868
Guido van Rossum2f841442016-11-15 09:48:06 -08001869 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
1870 """
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001871 if fields is None:
1872 fields = kwargs.items()
1873 elif kwargs:
1874 raise TypeError("Either list of fields or keywords"
1875 " can be provided to NamedTuple, not both")
1876 try:
1877 module = sys._getframe(1).f_globals.get('__name__', '__main__')
1878 except (AttributeError, ValueError):
1879 module = None
1880 return _make_nmtuple(typename, fields, module=module)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001881
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001882_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})
1883
1884def _namedtuple_mro_entries(bases):
1885 if len(bases) > 1:
1886 raise TypeError("Multiple inheritance with NamedTuple is not supported")
1887 assert bases[0] is NamedTuple
1888 return (_NamedTuple,)
1889
1890NamedTuple.__mro_entries__ = _namedtuple_mro_entries
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001891
1892
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001893class _TypedDictMeta(type):
1894 def __new__(cls, name, bases, ns, total=True):
1895 """Create new typed dict class object.
1896
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001897 This method is called when TypedDict is subclassed,
1898 or when TypedDict is instantiated. This way
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001899 TypedDict supports all three syntax forms described in its docstring.
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001900 Subclasses and instances of TypedDict return actual dictionaries.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001901 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001902 for base in bases:
1903 if type(base) is not _TypedDictMeta:
1904 raise TypeError('cannot inherit from both a TypedDict type '
1905 'and a non-TypedDict base class')
1906 tp_dict = type.__new__(_TypedDictMeta, name, (dict,), ns)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001907
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001908 annotations = {}
1909 own_annotations = ns.get('__annotations__', {})
1910 own_annotation_keys = set(own_annotations.keys())
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001911 msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001912 own_annotations = {
1913 n: _type_check(tp, msg) for n, tp in own_annotations.items()
1914 }
1915 required_keys = set()
1916 optional_keys = set()
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11001917
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001918 for base in bases:
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001919 annotations.update(base.__dict__.get('__annotations__', {}))
1920 required_keys.update(base.__dict__.get('__required_keys__', ()))
1921 optional_keys.update(base.__dict__.get('__optional_keys__', ()))
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11001922
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001923 annotations.update(own_annotations)
1924 if total:
1925 required_keys.update(own_annotation_keys)
1926 else:
1927 optional_keys.update(own_annotation_keys)
1928
1929 tp_dict.__annotations__ = annotations
1930 tp_dict.__required_keys__ = frozenset(required_keys)
1931 tp_dict.__optional_keys__ = frozenset(optional_keys)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001932 if not hasattr(tp_dict, '__total__'):
1933 tp_dict.__total__ = total
1934 return tp_dict
1935
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001936 __call__ = dict # static method
1937
1938 def __subclasscheck__(cls, other):
1939 # Typed dicts are only for static structural subtyping.
1940 raise TypeError('TypedDict does not support instance and class checks')
1941
1942 __instancecheck__ = __subclasscheck__
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001943
1944
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001945def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001946 """A simple typed namespace. At runtime it is equivalent to a plain dict.
1947
1948 TypedDict creates a dictionary type that expects all of its
1949 instances to have a certain set of keys, where each key is
1950 associated with a value of a consistent type. This expectation
1951 is not checked at runtime but is only enforced by type checkers.
1952 Usage::
1953
1954 class Point2D(TypedDict):
1955 x: int
1956 y: int
1957 label: str
1958
1959 a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
1960 b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
1961
1962 assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
1963
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11001964 The type info can be accessed via the Point2D.__annotations__ dict, and
1965 the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
1966 TypedDict supports two additional equivalent forms::
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001967
1968 Point2D = TypedDict('Point2D', x=int, y=int, label=str)
1969 Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
1970
ananthan-123ab6423f2020-02-19 10:03:05 +05301971 By default, all keys must be present in a TypedDict. It is possible
1972 to override this by specifying totality.
1973 Usage::
1974
1975 class point2D(TypedDict, total=False):
1976 x: int
1977 y: int
1978
1979 This means that a point2D TypedDict can have any of the keys omitted.A type
1980 checker is only expected to support a literal False or True as the value of
1981 the total argument. True is the default, and makes all items defined in the
1982 class body be required.
1983
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001984 The class syntax is only supported in Python 3.6+, while two other
1985 syntax forms work for Python 2.7 and 3.2+
1986 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001987 if fields is None:
1988 fields = kwargs
1989 elif kwargs:
1990 raise TypeError("TypedDict takes either a dict or keyword arguments,"
1991 " but not both")
1992
1993 ns = {'__annotations__': dict(fields), '__total__': total}
1994 try:
1995 # Setting correct module is necessary to make typed dict classes pickleable.
1996 ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
1997 except (AttributeError, ValueError):
1998 pass
1999
2000 return _TypedDictMeta(typename, (), ns)
2001
2002_TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {})
2003TypedDict.__mro_entries__ = lambda bases: (_TypedDict,)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002004
2005
Guido van Rossum91185fe2016-06-08 11:19:11 -07002006def NewType(name, tp):
2007 """NewType creates simple unique types with almost zero
2008 runtime overhead. NewType(name, tp) is considered a subtype of tp
2009 by static type checkers. At runtime, NewType(name, tp) returns
2010 a dummy function that simply returns its argument. Usage::
2011
2012 UserId = NewType('UserId', int)
2013
2014 def name_by_id(user_id: UserId) -> str:
2015 ...
2016
2017 UserId('user') # Fails type check
2018
2019 name_by_id(42) # Fails type check
2020 name_by_id(UserId(42)) # OK
2021
2022 num = UserId(5) + 1 # type: int
2023 """
2024
2025 def new_type(x):
2026 return x
2027
2028 new_type.__name__ = name
2029 new_type.__supertype__ = tp
2030 return new_type
2031
2032
Guido van Rossum0e0563c2016-04-05 14:54:25 -07002033# Python-version-specific alias (Python 2: unicode; Python 3: str)
2034Text = str
2035
2036
Guido van Rossum91185fe2016-06-08 11:19:11 -07002037# Constant that's True when type checking, but False here.
2038TYPE_CHECKING = False
2039
2040
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002041class IO(Generic[AnyStr]):
2042 """Generic base class for TextIO and BinaryIO.
2043
2044 This is an abstract, generic version of the return of open().
2045
2046 NOTE: This does not distinguish between the different possible
2047 classes (text vs. binary, read vs. write vs. read/write,
2048 append-only, unbuffered). The TextIO and BinaryIO subclasses
2049 below capture the distinctions between text vs. binary, which is
2050 pervasive in the interface; however we currently do not offer a
2051 way to track the other distinctions in the type system.
2052 """
2053
Guido van Rossumd70fe632015-08-05 12:11:06 +02002054 __slots__ = ()
2055
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002056 @property
2057 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002058 def mode(self) -> str:
2059 pass
2060
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002061 @property
2062 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002063 def name(self) -> str:
2064 pass
2065
2066 @abstractmethod
2067 def close(self) -> None:
2068 pass
2069
Shantanu2e6569b2020-01-29 18:52:36 -08002070 @property
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002071 @abstractmethod
2072 def closed(self) -> bool:
2073 pass
2074
2075 @abstractmethod
2076 def fileno(self) -> int:
2077 pass
2078
2079 @abstractmethod
2080 def flush(self) -> None:
2081 pass
2082
2083 @abstractmethod
2084 def isatty(self) -> bool:
2085 pass
2086
2087 @abstractmethod
2088 def read(self, n: int = -1) -> AnyStr:
2089 pass
2090
2091 @abstractmethod
2092 def readable(self) -> bool:
2093 pass
2094
2095 @abstractmethod
2096 def readline(self, limit: int = -1) -> AnyStr:
2097 pass
2098
2099 @abstractmethod
2100 def readlines(self, hint: int = -1) -> List[AnyStr]:
2101 pass
2102
2103 @abstractmethod
2104 def seek(self, offset: int, whence: int = 0) -> int:
2105 pass
2106
2107 @abstractmethod
2108 def seekable(self) -> bool:
2109 pass
2110
2111 @abstractmethod
2112 def tell(self) -> int:
2113 pass
2114
2115 @abstractmethod
2116 def truncate(self, size: int = None) -> int:
2117 pass
2118
2119 @abstractmethod
2120 def writable(self) -> bool:
2121 pass
2122
2123 @abstractmethod
2124 def write(self, s: AnyStr) -> int:
2125 pass
2126
2127 @abstractmethod
2128 def writelines(self, lines: List[AnyStr]) -> None:
2129 pass
2130
2131 @abstractmethod
2132 def __enter__(self) -> 'IO[AnyStr]':
2133 pass
2134
2135 @abstractmethod
2136 def __exit__(self, type, value, traceback) -> None:
2137 pass
2138
2139
2140class BinaryIO(IO[bytes]):
2141 """Typed version of the return of open() in binary mode."""
2142
Guido van Rossumd70fe632015-08-05 12:11:06 +02002143 __slots__ = ()
2144
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002145 @abstractmethod
2146 def write(self, s: Union[bytes, bytearray]) -> int:
2147 pass
2148
2149 @abstractmethod
2150 def __enter__(self) -> 'BinaryIO':
2151 pass
2152
2153
2154class TextIO(IO[str]):
2155 """Typed version of the return of open() in text mode."""
2156
Guido van Rossumd70fe632015-08-05 12:11:06 +02002157 __slots__ = ()
2158
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002159 @property
2160 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002161 def buffer(self) -> BinaryIO:
2162 pass
2163
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002164 @property
2165 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002166 def encoding(self) -> str:
2167 pass
2168
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002169 @property
2170 @abstractmethod
Guido van Rossum991d14f2016-11-09 13:12:51 -08002171 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002172 pass
2173
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002174 @property
2175 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002176 def line_buffering(self) -> bool:
2177 pass
2178
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002179 @property
2180 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002181 def newlines(self) -> Any:
2182 pass
2183
2184 @abstractmethod
2185 def __enter__(self) -> 'TextIO':
2186 pass
2187
2188
2189class io:
2190 """Wrapper namespace for IO generic classes."""
2191
2192 __all__ = ['IO', 'TextIO', 'BinaryIO']
2193 IO = IO
2194 TextIO = TextIO
2195 BinaryIO = BinaryIO
2196
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002197
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002198io.__name__ = __name__ + '.io'
2199sys.modules[io.__name__] = io
2200
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002201Pattern = _alias(stdlib_re.Pattern, 1)
2202Match = _alias(stdlib_re.Match, 1)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002203
2204class re:
2205 """Wrapper namespace for re type aliases."""
2206
2207 __all__ = ['Pattern', 'Match']
2208 Pattern = Pattern
2209 Match = Match
2210
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002211
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002212re.__name__ = __name__ + '.re'
2213sys.modules[re.__name__] = re