blob: fd657caafee870f59ad112f97ff6e80d084e2319 [file] [log] [blame]
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001"""
2The typing module: Support for gradual typing as defined by PEP 484.
3
4At large scale, the structure of the module is following:
Tim McNamara5265b3a2018-09-01 20:56:58 +12005* Imports and exports, all public names should be explicitly added to __all__.
Ivan Levkivskyid911e402018-01-20 11:23:59 +00006* Internal helper functions: these should never be used in code outside this module.
7* _SpecialForm and its instances (special forms): Any, NoReturn, ClassVar, Union, Optional
8* Two classes whose instances can be type arguments in addition to types: ForwardRef and TypeVar
9* The core of internal generics API: _GenericAlias and _VariadicGenericAlias, the latter is
10 currently only used by Tuple and Callable. All subscripted types like X[int], Union[int, str],
11 etc., are instances of either of these classes.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +010012* The public counterpart of the generics API consists of two classes: Generic and Protocol.
Ivan Levkivskyid911e402018-01-20 11:23:59 +000013* Public helper functions: get_type_hints, overload, cast, no_type_check,
14 no_type_check_decorator.
15* Generic aliases for collections.abc ABCs and few additional protocols.
ananthan-123ab6423f2020-02-19 10:03:05 +053016* Special types: NewType, NamedTuple, TypedDict.
Ivan Levkivskyid911e402018-01-20 11:23:59 +000017* Wrapper submodules for re and io related types.
18"""
19
HongWeipeng6ce03ec2019-09-27 15:54:26 +080020from abc import abstractmethod, ABCMeta
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070021import collections
Ivan Levkivskyid911e402018-01-20 11:23:59 +000022import collections.abc
Brett Cannonf3ad0422016-04-15 10:51:30 -070023import contextlib
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070024import functools
Serhiy Storchaka09f32212018-05-26 21:19:26 +030025import operator
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070026import re as stdlib_re # Avoid confusion with the re we export.
27import sys
28import types
Guido van Rossum48b069a2020-04-07 09:50:06 -070029from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType, GenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070030
31# Please keep __all__ alphabetized within each category.
32__all__ = [
33 # Super-special typing primitives.
Jakub Stasiakcf5b1092020-02-05 02:10:19 +010034 'Annotated',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070035 'Any',
36 'Callable',
Guido van Rossum0a6976d2016-09-11 15:34:56 -070037 'ClassVar',
Ivan Levkivskyif3672422019-05-26 09:37:07 +010038 'Final',
Anthony Sottiled30da5d2019-05-29 11:19:38 -070039 'ForwardRef',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070040 'Generic',
Ivan Levkivskyib891c462019-05-26 09:37:48 +010041 'Literal',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070042 'Optional',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +010043 'Protocol',
Guido van Rossumeb9aca32016-05-24 16:38:22 -070044 'Tuple',
45 'Type',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070046 'TypeVar',
47 'Union',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070048
49 # ABCs (from collections.abc).
50 'AbstractSet', # collections.abc.Set.
51 'ByteString',
52 'Container',
Ivan Levkivskyi29fda8d2017-06-10 21:57:56 +020053 'ContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070054 'Hashable',
55 'ItemsView',
56 'Iterable',
57 'Iterator',
58 'KeysView',
59 'Mapping',
60 'MappingView',
61 'MutableMapping',
62 'MutableSequence',
63 'MutableSet',
64 'Sequence',
65 'Sized',
66 'ValuesView',
Ivan Levkivskyid911e402018-01-20 11:23:59 +000067 'Awaitable',
68 'AsyncIterator',
69 'AsyncIterable',
70 'Coroutine',
71 'Collection',
72 'AsyncGenerator',
73 'AsyncContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070074
75 # Structural checks, a.k.a. protocols.
76 'Reversible',
77 'SupportsAbs',
Ivan Levkivskyif06e0212017-05-02 19:14:07 +020078 'SupportsBytes',
79 'SupportsComplex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070080 'SupportsFloat',
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -070081 'SupportsIndex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070082 'SupportsInt',
83 'SupportsRound',
84
85 # Concrete collection types.
Anthony Sottiled30da5d2019-05-29 11:19:38 -070086 'ChainMap',
Ivan Levkivskyib692dc82017-02-13 22:50:14 +010087 'Counter',
Raymond Hettinger80490522017-01-16 22:42:37 -080088 'Deque',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070089 'Dict',
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070090 'DefaultDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070091 'List',
Anthony Sottiled30da5d2019-05-29 11:19:38 -070092 'OrderedDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070093 'Set',
Guido van Rossumefa798d2016-08-23 11:01:50 -070094 'FrozenSet',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070095 'NamedTuple', # Not really a type.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +010096 'TypedDict', # Not really a type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070097 'Generator',
98
99 # One-off things.
100 'AnyStr',
101 'cast',
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100102 'final',
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +0100103 'get_args',
104 'get_origin',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700105 'get_type_hints',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700106 'NewType',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700107 'no_type_check',
108 'no_type_check_decorator',
aetracht45738202018-03-19 14:41:32 -0400109 'NoReturn',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700110 'overload',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100111 'runtime_checkable',
Guido van Rossum0e0563c2016-04-05 14:54:25 -0700112 'Text',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700113 'TYPE_CHECKING',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700114]
115
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700116# The pseudo-submodules 're' and 'io' are part of the public
117# namespace, but excluded from __all__ because they might stomp on
118# legitimate imports of those modules.
119
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700120
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700121def _type_check(arg, msg, is_argument=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000122 """Check that the argument is a type, and return it (internal helper).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700123
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000124 As a special case, accept None and return type(None) instead. Also wrap strings
125 into ForwardRef instances. Consider several corner cases, for example plain
126 special forms like Union are not valid, while Union[int, str] is OK, etc.
127 The msg argument is a human-readable error message, e.g::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700128
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000129 "Union[arg, ...]: arg should be a type."
Guido van Rossum4cefe742016-09-27 15:20:12 -0700130
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000131 We append the repr() of the actual value (truncated to 100 chars).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700132 """
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100133 invalid_generic_forms = (Generic, Protocol)
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700134 if is_argument:
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100135 invalid_generic_forms = invalid_generic_forms + (ClassVar, Final)
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400136
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000137 if arg is None:
138 return type(None)
139 if isinstance(arg, str):
140 return ForwardRef(arg)
141 if (isinstance(arg, _GenericAlias) and
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400142 arg.__origin__ in invalid_generic_forms):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000143 raise TypeError(f"{arg} is not valid as type argument")
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300144 if arg in (Any, NoReturn):
145 return arg
146 if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000147 raise TypeError(f"Plain {arg} is not valid as type argument")
148 if isinstance(arg, (type, TypeVar, ForwardRef)):
149 return arg
150 if not callable(arg):
151 raise TypeError(f"{msg} Got {arg!r:.100}.")
152 return arg
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700153
154
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000155def _type_repr(obj):
156 """Return the repr() of an object, special-casing types (internal helper).
157
158 If obj is a type, we return a shorter version than the default
159 type.__repr__, based on the module and qualified name, which is
160 typically enough to uniquely identify a type. For everything
161 else, we fall back on repr(obj).
162 """
163 if isinstance(obj, type):
164 if obj.__module__ == 'builtins':
165 return obj.__qualname__
166 return f'{obj.__module__}.{obj.__qualname__}'
167 if obj is ...:
168 return('...')
169 if isinstance(obj, types.FunctionType):
170 return obj.__name__
171 return repr(obj)
172
173
174def _collect_type_vars(types):
175 """Collect all type variable contained in types in order of
176 first appearance (lexicographic order). For example::
177
178 _collect_type_vars((T, List[S, T])) == (T, S)
179 """
180 tvars = []
181 for t in types:
182 if isinstance(t, TypeVar) and t not in tvars:
183 tvars.append(t)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300184 if isinstance(t, (_GenericAlias, GenericAlias)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000185 tvars.extend([t for t in t.__parameters__ if t not in tvars])
186 return tuple(tvars)
187
188
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300189def _check_generic(cls, parameters, elen):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000190 """Check correct count for parameters of a generic cls (internal helper).
191 This gives a nice error message in case of count mismatch.
192 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300193 if not elen:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000194 raise TypeError(f"{cls} is not a generic class")
195 alen = len(parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000196 if alen != elen:
197 raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
198 f" actual {alen}, expected {elen}")
199
200
201def _remove_dups_flatten(parameters):
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700202 """An internal helper for Union creation and substitution: flatten Unions
203 among parameters, then remove duplicates.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000204 """
205 # Flatten out Union[Union[...], ...].
206 params = []
207 for p in parameters:
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300208 if isinstance(p, _UnionGenericAlias):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000209 params.extend(p.__args__)
210 elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
211 params.extend(p[1:])
212 else:
213 params.append(p)
214 # Weed out strict duplicates, preserving the first of each occurrence.
215 all_params = set(params)
216 if len(all_params) < len(params):
217 new_params = []
218 for t in params:
219 if t in all_params:
220 new_params.append(t)
221 all_params.remove(t)
222 params = new_params
223 assert not all_params, all_params
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700224 return tuple(params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000225
226
227_cleanups = []
228
229
230def _tp_cache(func):
231 """Internal wrapper caching __getitem__ of generic types with a fallback to
232 original function for non-hashable arguments.
233 """
234 cached = functools.lru_cache()(func)
235 _cleanups.append(cached.cache_clear)
236
237 @functools.wraps(func)
238 def inner(*args, **kwds):
239 try:
240 return cached(*args, **kwds)
241 except TypeError:
242 pass # All real errors (not unhashable args) are raised below.
243 return func(*args, **kwds)
244 return inner
245
246
247def _eval_type(t, globalns, localns):
248 """Evaluate all forward reverences in the given type t.
249 For use of globalns and localns see the docstring for get_type_hints().
250 """
251 if isinstance(t, ForwardRef):
252 return t._evaluate(globalns, localns)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300253 if isinstance(t, (_GenericAlias, GenericAlias)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000254 ev_args = tuple(_eval_type(a, globalns, localns) for a in t.__args__)
255 if ev_args == t.__args__:
256 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300257 if isinstance(t, GenericAlias):
258 return GenericAlias(t.__origin__, ev_args)
259 else:
260 return t.copy_with(ev_args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000261 return t
262
263
264class _Final:
265 """Mixin to prohibit subclassing"""
Guido van Rossum4cefe742016-09-27 15:20:12 -0700266
Guido van Rossum83ec3022017-01-17 20:43:28 -0800267 __slots__ = ('__weakref__',)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700268
Serhiy Storchaka2085bd02019-06-01 11:00:15 +0300269 def __init_subclass__(self, /, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000270 if '_root' not in kwds:
271 raise TypeError("Cannot subclass special typing classes")
272
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100273class _Immutable:
274 """Mixin to indicate that object should not be copied."""
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300275 __slots__ = ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000276
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100277 def __copy__(self):
278 return self
279
280 def __deepcopy__(self, memo):
281 return self
282
283
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300284# Internal indicator of special typing constructs.
285# See __doc__ instance attribute for specific docs.
286class _SpecialForm(_Final, _root=True):
287 __slots__ = ('_name', '__doc__', '_getitem')
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000288
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300289 def __init__(self, getitem):
290 self._getitem = getitem
291 self._name = getitem.__name__
292 self.__doc__ = getitem.__doc__
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000293
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300294 def __mro_entries__(self, bases):
295 raise TypeError(f"Cannot subclass {self!r}")
Guido van Rossum4cefe742016-09-27 15:20:12 -0700296
297 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000298 return 'typing.' + self._name
299
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100300 def __reduce__(self):
301 return self._name
Guido van Rossum4cefe742016-09-27 15:20:12 -0700302
303 def __call__(self, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000304 raise TypeError(f"Cannot instantiate {self!r}")
305
306 def __instancecheck__(self, obj):
307 raise TypeError(f"{self} cannot be used with isinstance()")
308
309 def __subclasscheck__(self, cls):
310 raise TypeError(f"{self} cannot be used with issubclass()")
311
312 @_tp_cache
313 def __getitem__(self, parameters):
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300314 return self._getitem(self, parameters)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700315
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300316@_SpecialForm
317def Any(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000318 """Special type indicating an unconstrained type.
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700319
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000320 - Any is compatible with every type.
321 - Any assumed to have all methods.
322 - All values assumed to be instances of Any.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700323
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000324 Note that all the above statements are true from the point of view of
325 static type checkers. At runtime, Any should not be used with instance
326 or class checks.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300327 """
328 raise TypeError(f"{self} is not subscriptable")
Guido van Rossumd70fe632015-08-05 12:11:06 +0200329
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300330@_SpecialForm
331def NoReturn(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000332 """Special type indicating functions that never return.
333 Example::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700334
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000335 from typing import NoReturn
336
337 def stop() -> NoReturn:
338 raise Exception('no way')
339
340 This type is invalid in other positions, e.g., ``List[NoReturn]``
341 will fail in static type checkers.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300342 """
343 raise TypeError(f"{self} is not subscriptable")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000344
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300345@_SpecialForm
346def ClassVar(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000347 """Special type construct to mark class variables.
348
349 An annotation wrapped in ClassVar indicates that a given
350 attribute is intended to be used as a class variable and
351 should not be set on instances of that class. Usage::
352
353 class Starship:
354 stats: ClassVar[Dict[str, int]] = {} # class variable
355 damage: int = 10 # instance variable
356
357 ClassVar accepts only types and cannot be further subscribed.
358
359 Note that ClassVar is not a class itself, and should not
360 be used with isinstance() or issubclass().
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300361 """
362 item = _type_check(parameters, f'{self} accepts only single type.')
363 return _GenericAlias(self, (item,))
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000364
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300365@_SpecialForm
366def Final(self, parameters):
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100367 """Special typing construct to indicate final names to type checkers.
368
369 A final name cannot be re-assigned or overridden in a subclass.
370 For example:
371
372 MAX_SIZE: Final = 9000
373 MAX_SIZE += 1 # Error reported by type checker
374
375 class Connection:
376 TIMEOUT: Final[int] = 10
377
378 class FastConnector(Connection):
379 TIMEOUT = 1 # Error reported by type checker
380
381 There is no runtime checking of these properties.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300382 """
383 item = _type_check(parameters, f'{self} accepts only single type.')
384 return _GenericAlias(self, (item,))
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100385
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300386@_SpecialForm
387def Union(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000388 """Union type; Union[X, Y] means either X or Y.
389
390 To define a union, use e.g. Union[int, str]. Details:
391 - The arguments must be types and there must be at least one.
392 - None as an argument is a special case and is replaced by
393 type(None).
394 - Unions of unions are flattened, e.g.::
395
396 Union[Union[int, str], float] == Union[int, str, float]
397
398 - Unions of a single argument vanish, e.g.::
399
400 Union[int] == int # The constructor actually returns int
401
402 - Redundant arguments are skipped, e.g.::
403
404 Union[int, str, int] == Union[int, str]
405
406 - When comparing unions, the argument order is ignored, e.g.::
407
408 Union[int, str] == Union[str, int]
409
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000410 - You cannot subclass or instantiate a union.
411 - You can use Optional[X] as a shorthand for Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300412 """
413 if parameters == ():
414 raise TypeError("Cannot take a Union of no types.")
415 if not isinstance(parameters, tuple):
416 parameters = (parameters,)
417 msg = "Union[arg, ...]: each arg must be a type."
418 parameters = tuple(_type_check(p, msg) for p in parameters)
419 parameters = _remove_dups_flatten(parameters)
420 if len(parameters) == 1:
421 return parameters[0]
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300422 return _UnionGenericAlias(self, parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000423
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300424@_SpecialForm
425def Optional(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000426 """Optional type.
427
428 Optional[X] is equivalent to Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300429 """
430 arg = _type_check(parameters, f"{self} requires a single type.")
431 return Union[arg, type(None)]
Guido van Rossumb7dedc82016-10-29 12:44:29 -0700432
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300433@_SpecialForm
434def Literal(self, parameters):
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100435 """Special typing form to define literal types (a.k.a. value types).
436
437 This form can be used to indicate to type checkers that the corresponding
438 variable or function parameter has a value equivalent to the provided
439 literal (or one of several literals):
440
441 def validate_simple(data: Any) -> Literal[True]: # always returns True
442 ...
443
444 MODE = Literal['r', 'rb', 'w', 'wb']
445 def open_helper(file: str, mode: MODE) -> str:
446 ...
447
448 open_helper('/some/path', 'r') # Passes type check
449 open_helper('/other/path', 'typo') # Error in type checker
450
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300451 Literal[...] cannot be subclassed. At runtime, an arbitrary value
452 is allowed as type argument to Literal[...], but type checkers may
453 impose restrictions.
454 """
455 # There is no '_type_check' call because arguments to Literal[...] are
456 # values, not types.
457 return _GenericAlias(self, parameters)
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100458
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700459
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000460class ForwardRef(_Final, _root=True):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800461 """Internal wrapper to hold a forward reference."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700462
Guido van Rossum4cefe742016-09-27 15:20:12 -0700463 __slots__ = ('__forward_arg__', '__forward_code__',
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400464 '__forward_evaluated__', '__forward_value__',
465 '__forward_is_argument__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700466
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700467 def __init__(self, arg, is_argument=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700468 if not isinstance(arg, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000469 raise TypeError(f"Forward reference must be a string -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700470 try:
471 code = compile(arg, '<string>', 'eval')
472 except SyntaxError:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000473 raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700474 self.__forward_arg__ = arg
475 self.__forward_code__ = code
476 self.__forward_evaluated__ = False
477 self.__forward_value__ = None
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400478 self.__forward_is_argument__ = is_argument
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700479
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000480 def _evaluate(self, globalns, localns):
Guido van Rossumdad17902016-11-10 08:29:18 -0800481 if not self.__forward_evaluated__ or localns is not globalns:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700482 if globalns is None and localns is None:
483 globalns = localns = {}
484 elif globalns is None:
485 globalns = localns
486 elif localns is None:
487 localns = globalns
488 self.__forward_value__ = _type_check(
489 eval(self.__forward_code__, globalns, localns),
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400490 "Forward references must evaluate to types.",
491 is_argument=self.__forward_is_argument__)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700492 self.__forward_evaluated__ = True
493 return self.__forward_value__
494
Guido van Rossum4cefe742016-09-27 15:20:12 -0700495 def __eq__(self, other):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000496 if not isinstance(other, ForwardRef):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700497 return NotImplemented
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100498 if self.__forward_evaluated__ and other.__forward_evaluated__:
499 return (self.__forward_arg__ == other.__forward_arg__ and
500 self.__forward_value__ == other.__forward_value__)
501 return self.__forward_arg__ == other.__forward_arg__
Guido van Rossum4cefe742016-09-27 15:20:12 -0700502
503 def __hash__(self):
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100504 return hash(self.__forward_arg__)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700505
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700506 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000507 return f'ForwardRef({self.__forward_arg__!r})'
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700508
509
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100510class TypeVar(_Final, _Immutable, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700511 """Type variable.
512
513 Usage::
514
515 T = TypeVar('T') # Can be anything
516 A = TypeVar('A', str, bytes) # Must be str or bytes
517
518 Type variables exist primarily for the benefit of static type
519 checkers. They serve as the parameters for generic types as well
520 as for generic function definitions. See class Generic for more
521 information on generic types. Generic functions work as follows:
522
Guido van Rossumb24569a2016-11-20 18:01:29 -0800523 def repeat(x: T, n: int) -> List[T]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700524 '''Return a list containing n references to x.'''
525 return [x]*n
526
527 def longest(x: A, y: A) -> A:
528 '''Return the longest of two strings.'''
529 return x if len(x) >= len(y) else y
530
531 The latter example's signature is essentially the overloading
532 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
533 that if the arguments are instances of some subclass of str,
534 the return type is still plain str.
535
Guido van Rossumb24569a2016-11-20 18:01:29 -0800536 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700537
Guido van Rossumefa798d2016-08-23 11:01:50 -0700538 Type variables defined with covariant=True or contravariant=True
João D. Ferreira86bfed32018-07-07 16:41:20 +0100539 can be used to declare covariant or contravariant generic types.
Guido van Rossumefa798d2016-08-23 11:01:50 -0700540 See PEP 484 for more details. By default generic types are invariant
541 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700542
543 Type variables can be introspected. e.g.:
544
545 T.__name__ == 'T'
546 T.__constraints__ == ()
547 T.__covariant__ == False
548 T.__contravariant__ = False
549 A.__constraints__ == (str, bytes)
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100550
551 Note that only type variables defined in global scope can be pickled.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700552 """
553
Guido van Rossum4cefe742016-09-27 15:20:12 -0700554 __slots__ = ('__name__', '__bound__', '__constraints__',
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300555 '__covariant__', '__contravariant__', '__dict__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700556
557 def __init__(self, name, *constraints, bound=None,
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800558 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700559 self.__name__ = name
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700560 if covariant and contravariant:
Guido van Rossumefa798d2016-08-23 11:01:50 -0700561 raise ValueError("Bivariant types are not supported.")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700562 self.__covariant__ = bool(covariant)
563 self.__contravariant__ = bool(contravariant)
564 if constraints and bound is not None:
565 raise TypeError("Constraints cannot be combined with bound=...")
566 if constraints and len(constraints) == 1:
567 raise TypeError("A single constraint is not allowed")
568 msg = "TypeVar(name, constraint, ...): constraints must be types."
569 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
570 if bound:
571 self.__bound__ = _type_check(bound, "Bound must be a type.")
572 else:
573 self.__bound__ = None
HongWeipenga25a04f2020-04-21 04:01:53 +0800574 try:
575 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling
576 except (AttributeError, ValueError):
577 def_mod = None
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300578 if def_mod != 'typing':
579 self.__module__ = def_mod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700580
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700581 def __repr__(self):
582 if self.__covariant__:
583 prefix = '+'
584 elif self.__contravariant__:
585 prefix = '-'
586 else:
587 prefix = '~'
588 return prefix + self.__name__
589
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100590 def __reduce__(self):
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300591 return self.__name__
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100592
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700593
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000594def _is_dunder(attr):
595 return attr.startswith('__') and attr.endswith('__')
Guido van Rossum83ec3022017-01-17 20:43:28 -0800596
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300597class _BaseGenericAlias(_Final, _root=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000598 """The central part of internal API.
599
600 This represents a generic version of type 'origin' with type arguments 'params'.
601 There are two kind of these aliases: user defined and special. The special ones
602 are wrappers around builtin collections and ABCs in collections.abc. These must
603 have 'name' always set. If 'inst' is False, then the alias can't be instantiated,
604 this is used by e.g. typing.List and typing.Dict.
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700605 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300606 def __init__(self, origin, *, inst=True, name=None):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000607 self._inst = inst
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000608 self._name = name
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700609 self.__origin__ = origin
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000610 self.__slots__ = None # This is not documented.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300611
612 def __call__(self, *args, **kwargs):
613 if not self._inst:
614 raise TypeError(f"Type {self._name} cannot be instantiated; "
615 f"use {self.__origin__.__name__}() instead")
616 result = self.__origin__(*args, **kwargs)
617 try:
618 result.__orig_class__ = self
619 except AttributeError:
620 pass
621 return result
622
623 def __mro_entries__(self, bases):
624 res = []
625 if self.__origin__ not in bases:
626 res.append(self.__origin__)
627 i = bases.index(self)
628 for b in bases[i+1:]:
629 if isinstance(b, _BaseGenericAlias) or issubclass(b, Generic):
630 break
631 else:
632 res.append(Generic)
633 return tuple(res)
634
635 def __getattr__(self, attr):
636 # We are careful for copy and pickle.
637 # Also for simplicity we just don't relay all dunder names
638 if '__origin__' in self.__dict__ and not _is_dunder(attr):
639 return getattr(self.__origin__, attr)
640 raise AttributeError(attr)
641
642 def __setattr__(self, attr, val):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300643 if _is_dunder(attr) or attr in ('_name', '_inst', '_nparams'):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300644 super().__setattr__(attr, val)
645 else:
646 setattr(self.__origin__, attr, val)
647
648 def __instancecheck__(self, obj):
649 return self.__subclasscheck__(type(obj))
650
651 def __subclasscheck__(self, cls):
652 raise TypeError("Subscripted generics cannot be used with"
653 " class and instance checks")
654
655
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300656# Special typing constructs Union, Optional, Generic, Callable and Tuple
657# use three special attributes for internal bookkeeping of generic types:
658# * __parameters__ is a tuple of unique free type parameters of a generic
659# type, for example, Dict[T, T].__parameters__ == (T,);
660# * __origin__ keeps a reference to a type that was subscripted,
661# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
662# the type.
663# * __args__ is a tuple of all arguments used in subscripting,
664# e.g., Dict[T, int].__args__ == (T, int).
665
666
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300667class _GenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300668 def __init__(self, origin, params, *, inst=True, name=None):
669 super().__init__(origin, inst=inst, name=name)
670 if not isinstance(params, tuple):
671 params = (params,)
672 self.__args__ = tuple(... if a is _TypingEllipsis else
673 () if a is _TypingEmpty else
674 a for a in params)
675 self.__parameters__ = _collect_type_vars(params)
676 if not name:
677 self.__module__ = origin.__module__
678
679 def __eq__(self, other):
680 if not isinstance(other, _GenericAlias):
681 return NotImplemented
682 return (self.__origin__ == other.__origin__
683 and self.__args__ == other.__args__)
684
685 def __hash__(self):
686 return hash((self.__origin__, self.__args__))
687
Guido van Rossum4cefe742016-09-27 15:20:12 -0700688 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700689 def __getitem__(self, params):
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100690 if self.__origin__ in (Generic, Protocol):
691 # Can't subscript Generic[...] or Protocol[...].
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000692 raise TypeError(f"Cannot subscript already-subscripted {self}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700693 if not isinstance(params, tuple):
694 params = (params,)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700695 msg = "Parameters to generic types must be types."
696 params = tuple(_type_check(p, msg) for p in params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300697 _check_generic(self, params, len(self.__parameters__))
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300698
699 subst = dict(zip(self.__parameters__, params))
700 new_args = []
701 for arg in self.__args__:
702 if isinstance(arg, TypeVar):
703 arg = subst[arg]
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300704 elif isinstance(arg, (_GenericAlias, GenericAlias)):
Serhiy Storchaka0122d482020-05-10 13:39:40 +0300705 subparams = arg.__parameters__
706 if subparams:
707 subargs = tuple(subst[x] for x in subparams)
708 arg = arg[subargs]
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300709 new_args.append(arg)
710 return self.copy_with(tuple(new_args))
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100711
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000712 def copy_with(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300713 return self.__class__(self.__origin__, params, name=self._name, inst=self._inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700714
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000715 def __repr__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300716 if self._name:
717 name = 'typing.' + self._name
718 else:
719 name = _type_repr(self.__origin__)
720 args = ", ".join([_type_repr(a) for a in self.__args__])
721 return f'{name}[{args}]'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000722
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300723 def __reduce__(self):
724 if self._name:
725 origin = globals()[self._name]
726 else:
727 origin = self.__origin__
728 args = tuple(self.__args__)
729 if len(args) == 1 and not isinstance(args[0], tuple):
730 args, = args
731 return operator.getitem, (origin, args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000732
733 def __mro_entries__(self, bases):
734 if self._name: # generic version of an ABC or built-in class
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300735 return super().__mro_entries__(bases)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000736 if self.__origin__ is Generic:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100737 if Protocol in bases:
738 return ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000739 i = bases.index(self)
740 for b in bases[i+1:]:
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300741 if isinstance(b, _BaseGenericAlias) and b is not self:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000742 return ()
743 return (self.__origin__,)
744
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000745
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300746# _nparams is the number of accepted parameters, e.g. 0 for Hashable,
747# 1 for List and 2 for Dict. It may be -1 if variable number of
748# parameters are accepted (needs custom __getitem__).
749
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300750class _SpecialGenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300751 def __init__(self, origin, nparams, *, inst=True, name=None):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300752 if name is None:
753 name = origin.__name__
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300754 super().__init__(origin, inst=inst, name=name)
755 self._nparams = nparams
Serhiy Storchaka2fbc57a2020-05-10 15:14:27 +0300756 if origin.__module__ == 'builtins':
757 self.__doc__ = f'A generic version of {origin.__qualname__}.'
758 else:
759 self.__doc__ = f'A generic version of {origin.__module__}.{origin.__qualname__}.'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000760
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300761 @_tp_cache
762 def __getitem__(self, params):
763 if not isinstance(params, tuple):
764 params = (params,)
765 msg = "Parameters to generic types must be types."
766 params = tuple(_type_check(p, msg) for p in params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300767 _check_generic(self, params, self._nparams)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300768 return self.copy_with(params)
769
770 def copy_with(self, params):
771 return _GenericAlias(self.__origin__, params,
772 name=self._name, inst=self._inst)
773
774 def __repr__(self):
775 return 'typing.' + self._name
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000776
777 def __subclasscheck__(self, cls):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300778 if isinstance(cls, _SpecialGenericAlias):
779 return issubclass(cls.__origin__, self.__origin__)
780 if not isinstance(cls, _GenericAlias):
781 return issubclass(cls, self.__origin__)
782 return super().__subclasscheck__(cls)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700783
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100784 def __reduce__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300785 return self._name
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100786
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700787
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300788class _CallableGenericAlias(_GenericAlias, _root=True):
789 def __repr__(self):
790 assert self._name == 'Callable'
791 if len(self.__args__) == 2 and self.__args__[0] is Ellipsis:
792 return super().__repr__()
793 return (f'typing.Callable'
794 f'[[{", ".join([_type_repr(a) for a in self.__args__[:-1]])}], '
795 f'{_type_repr(self.__args__[-1])}]')
796
797 def __reduce__(self):
798 args = self.__args__
799 if not (len(args) == 2 and args[0] is ...):
800 args = list(args[:-1]), args[-1]
801 return operator.getitem, (Callable, args)
802
803
804class _CallableType(_SpecialGenericAlias, _root=True):
805 def copy_with(self, params):
806 return _CallableGenericAlias(self.__origin__, params,
807 name=self._name, inst=self._inst)
808
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000809 def __getitem__(self, params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000810 if not isinstance(params, tuple) or len(params) != 2:
811 raise TypeError("Callable must be used as "
812 "Callable[[arg, ...], result].")
813 args, result = params
814 if args is Ellipsis:
815 params = (Ellipsis, result)
816 else:
817 if not isinstance(args, list):
818 raise TypeError(f"Callable[args, result]: args must be a list."
819 f" Got {args}")
820 params = (tuple(args), result)
821 return self.__getitem_inner__(params)
822
823 @_tp_cache
824 def __getitem_inner__(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300825 args, result = params
826 msg = "Callable[args, result]: result must be a type."
827 result = _type_check(result, msg)
828 if args is Ellipsis:
829 return self.copy_with((_TypingEllipsis, result))
830 msg = "Callable[[arg, ...], result]: each arg must be a type."
831 args = tuple(_type_check(arg, msg) for arg in args)
832 params = args + (result,)
833 return self.copy_with(params)
834
835
836class _TupleType(_SpecialGenericAlias, _root=True):
837 @_tp_cache
838 def __getitem__(self, params):
839 if params == ():
840 return self.copy_with((_TypingEmpty,))
841 if not isinstance(params, tuple):
842 params = (params,)
843 if len(params) == 2 and params[1] is ...:
844 msg = "Tuple[t, ...]: t must be a type."
845 p = _type_check(params[0], msg)
846 return self.copy_with((p, _TypingEllipsis))
847 msg = "Tuple[t0, t1, ...]: each t must be a type."
848 params = tuple(_type_check(p, msg) for p in params)
849 return self.copy_with(params)
850
851
852class _UnionGenericAlias(_GenericAlias, _root=True):
853 def copy_with(self, params):
854 return Union[params]
855
856 def __eq__(self, other):
857 if not isinstance(other, _UnionGenericAlias):
858 return NotImplemented
859 return set(self.__args__) == set(other.__args__)
860
861 def __hash__(self):
862 return hash(frozenset(self.__args__))
863
864 def __repr__(self):
865 args = self.__args__
866 if len(args) == 2:
867 if args[0] is type(None):
868 return f'typing.Optional[{_type_repr(args[1])}]'
869 elif args[1] is type(None):
870 return f'typing.Optional[{_type_repr(args[0])}]'
871 return super().__repr__()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000872
873
874class Generic:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700875 """Abstract base class for generic types.
876
Guido van Rossumb24569a2016-11-20 18:01:29 -0800877 A generic type is typically declared by inheriting from
878 this class parameterized with one or more type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700879 For example, a generic mapping type might be defined as::
880
881 class Mapping(Generic[KT, VT]):
882 def __getitem__(self, key: KT) -> VT:
883 ...
884 # Etc.
885
886 This class can then be used as follows::
887
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700888 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700889 try:
890 return mapping[key]
891 except KeyError:
892 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700893 """
Guido van Rossumd70fe632015-08-05 12:11:06 +0200894 __slots__ = ()
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100895 _is_protocol = False
Guido van Rossumd70fe632015-08-05 12:11:06 +0200896
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000897 @_tp_cache
898 def __class_getitem__(cls, params):
899 if not isinstance(params, tuple):
900 params = (params,)
901 if not params and cls is not Tuple:
902 raise TypeError(
903 f"Parameter list to {cls.__qualname__}[...] cannot be empty")
904 msg = "Parameters to generic types must be types."
905 params = tuple(_type_check(p, msg) for p in params)
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100906 if cls in (Generic, Protocol):
907 # Generic and Protocol can only be subscripted with unique type variables.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000908 if not all(isinstance(p, TypeVar) for p in params):
909 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100910 f"Parameters to {cls.__name__}[...] must all be type variables")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000911 if len(set(params)) != len(params):
912 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100913 f"Parameters to {cls.__name__}[...] must all be unique")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000914 else:
915 # Subscripting a regular Generic subclass.
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300916 _check_generic(cls, params, len(cls.__parameters__))
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000917 return _GenericAlias(cls, params)
918
919 def __init_subclass__(cls, *args, **kwargs):
Ivan Levkivskyiee566fe2018-04-04 17:00:15 +0100920 super().__init_subclass__(*args, **kwargs)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000921 tvars = []
922 if '__orig_bases__' in cls.__dict__:
923 error = Generic in cls.__orig_bases__
924 else:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100925 error = Generic in cls.__bases__ and cls.__name__ != 'Protocol'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000926 if error:
927 raise TypeError("Cannot inherit from plain Generic")
928 if '__orig_bases__' in cls.__dict__:
929 tvars = _collect_type_vars(cls.__orig_bases__)
930 # Look for Generic[T1, ..., Tn].
931 # If found, tvars must be a subset of it.
932 # If not found, tvars is it.
933 # Also check for and reject plain Generic,
934 # and reject multiple Generic[...].
935 gvars = None
936 for base in cls.__orig_bases__:
937 if (isinstance(base, _GenericAlias) and
938 base.__origin__ is Generic):
939 if gvars is not None:
940 raise TypeError(
941 "Cannot inherit from Generic[...] multiple types.")
942 gvars = base.__parameters__
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100943 if gvars is not None:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000944 tvarset = set(tvars)
945 gvarset = set(gvars)
946 if not tvarset <= gvarset:
947 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
948 s_args = ', '.join(str(g) for g in gvars)
949 raise TypeError(f"Some type variables ({s_vars}) are"
950 f" not listed in Generic[{s_args}]")
951 tvars = gvars
952 cls.__parameters__ = tuple(tvars)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700953
954
955class _TypingEmpty:
Guido van Rossumb24569a2016-11-20 18:01:29 -0800956 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
957 to allow empty list/tuple in specific places, without allowing them
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700958 to sneak in where prohibited.
959 """
960
961
962class _TypingEllipsis:
Guido van Rossumb24569a2016-11-20 18:01:29 -0800963 """Internal placeholder for ... (ellipsis)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700964
965
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100966_TYPING_INTERNALS = ['__parameters__', '__orig_bases__', '__orig_class__',
967 '_is_protocol', '_is_runtime_protocol']
968
969_SPECIAL_NAMES = ['__abstractmethods__', '__annotations__', '__dict__', '__doc__',
970 '__init__', '__module__', '__new__', '__slots__',
Guido van Rossum48b069a2020-04-07 09:50:06 -0700971 '__subclasshook__', '__weakref__', '__class_getitem__']
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100972
973# These special attributes will be not collected as protocol members.
974EXCLUDED_ATTRIBUTES = _TYPING_INTERNALS + _SPECIAL_NAMES + ['_MutableMapping__marker']
975
976
977def _get_protocol_attrs(cls):
978 """Collect protocol members from a protocol class objects.
979
980 This includes names actually defined in the class dictionary, as well
981 as names that appear in annotations. Special names (above) are skipped.
982 """
983 attrs = set()
984 for base in cls.__mro__[:-1]: # without object
985 if base.__name__ in ('Protocol', 'Generic'):
986 continue
987 annotations = getattr(base, '__annotations__', {})
988 for attr in list(base.__dict__.keys()) + list(annotations.keys()):
989 if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
990 attrs.add(attr)
991 return attrs
992
993
994def _is_callable_members_only(cls):
995 # PEP 544 prohibits using issubclass() with protocols that have non-method members.
996 return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls))
997
998
999def _no_init(self, *args, **kwargs):
1000 if type(self)._is_protocol:
1001 raise TypeError('Protocols cannot be instantiated')
1002
1003
1004def _allow_reckless_class_cheks():
Nickolena Fishercfaf4c02020-04-26 12:49:11 -05001005 """Allow instance and class checks for special stdlib modules.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001006
1007 The abc and functools modules indiscriminately call isinstance() and
1008 issubclass() on the whole MRO of a user class, which may contain protocols.
1009 """
1010 try:
1011 return sys._getframe(3).f_globals['__name__'] in ['abc', 'functools']
1012 except (AttributeError, ValueError): # For platforms without _getframe().
1013 return True
1014
1015
Divij Rajkumar692a0dc2019-09-12 11:13:51 +01001016_PROTO_WHITELIST = {
1017 'collections.abc': [
1018 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
1019 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
1020 ],
1021 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
1022}
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001023
1024
1025class _ProtocolMeta(ABCMeta):
1026 # This metaclass is really unfortunate and exists only because of
1027 # the lack of __instancehook__.
1028 def __instancecheck__(cls, instance):
1029 # We need this method for situations where attributes are
1030 # assigned in __init__.
1031 if ((not getattr(cls, '_is_protocol', False) or
1032 _is_callable_members_only(cls)) and
1033 issubclass(instance.__class__, cls)):
1034 return True
1035 if cls._is_protocol:
1036 if all(hasattr(instance, attr) and
1037 # All *methods* can be blocked by setting them to None.
1038 (not callable(getattr(cls, attr, None)) or
1039 getattr(instance, attr) is not None)
1040 for attr in _get_protocol_attrs(cls)):
1041 return True
1042 return super().__instancecheck__(instance)
1043
1044
1045class Protocol(Generic, metaclass=_ProtocolMeta):
1046 """Base class for protocol classes.
1047
1048 Protocol classes are defined as::
1049
1050 class Proto(Protocol):
1051 def meth(self) -> int:
1052 ...
1053
1054 Such classes are primarily used with static type checkers that recognize
1055 structural subtyping (static duck-typing), for example::
1056
1057 class C:
1058 def meth(self) -> int:
1059 return 0
1060
1061 def func(x: Proto) -> int:
1062 return x.meth()
1063
1064 func(C()) # Passes static type check
1065
1066 See PEP 544 for details. Protocol classes decorated with
1067 @typing.runtime_checkable act as simple-minded runtime protocols that check
1068 only the presence of given attributes, ignoring their type signatures.
1069 Protocol classes can be generic, they are defined as::
1070
1071 class GenProto(Protocol[T]):
1072 def meth(self) -> T:
1073 ...
1074 """
1075 __slots__ = ()
1076 _is_protocol = True
1077 _is_runtime_protocol = False
1078
1079 def __init_subclass__(cls, *args, **kwargs):
1080 super().__init_subclass__(*args, **kwargs)
1081
1082 # Determine if this is a protocol or a concrete subclass.
1083 if not cls.__dict__.get('_is_protocol', False):
1084 cls._is_protocol = any(b is Protocol for b in cls.__bases__)
1085
1086 # Set (or override) the protocol subclass hook.
1087 def _proto_hook(other):
1088 if not cls.__dict__.get('_is_protocol', False):
1089 return NotImplemented
1090
1091 # First, perform various sanity checks.
1092 if not getattr(cls, '_is_runtime_protocol', False):
1093 if _allow_reckless_class_cheks():
1094 return NotImplemented
1095 raise TypeError("Instance and class checks can only be used with"
1096 " @runtime_checkable protocols")
1097 if not _is_callable_members_only(cls):
1098 if _allow_reckless_class_cheks():
1099 return NotImplemented
1100 raise TypeError("Protocols with non-method members"
1101 " don't support issubclass()")
1102 if not isinstance(other, type):
1103 # Same error message as for issubclass(1, int).
1104 raise TypeError('issubclass() arg 1 must be a class')
1105
1106 # Second, perform the actual structural compatibility check.
1107 for attr in _get_protocol_attrs(cls):
1108 for base in other.__mro__:
1109 # Check if the members appears in the class dictionary...
1110 if attr in base.__dict__:
1111 if base.__dict__[attr] is None:
1112 return NotImplemented
1113 break
1114
1115 # ...or in annotations, if it is a sub-protocol.
1116 annotations = getattr(base, '__annotations__', {})
1117 if (isinstance(annotations, collections.abc.Mapping) and
1118 attr in annotations and
1119 issubclass(other, Generic) and other._is_protocol):
1120 break
1121 else:
1122 return NotImplemented
1123 return True
1124
1125 if '__subclasshook__' not in cls.__dict__:
1126 cls.__subclasshook__ = _proto_hook
1127
1128 # We have nothing more to do for non-protocols...
1129 if not cls._is_protocol:
1130 return
1131
1132 # ... otherwise check consistency of bases, and prohibit instantiation.
1133 for base in cls.__bases__:
1134 if not (base in (object, Generic) or
Divij Rajkumar692a0dc2019-09-12 11:13:51 +01001135 base.__module__ in _PROTO_WHITELIST and
1136 base.__name__ in _PROTO_WHITELIST[base.__module__] or
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001137 issubclass(base, Generic) and base._is_protocol):
1138 raise TypeError('Protocols can only inherit from other'
1139 ' protocols, got %r' % base)
1140 cls.__init__ = _no_init
1141
1142
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001143class _AnnotatedAlias(_GenericAlias, _root=True):
1144 """Runtime representation of an annotated type.
1145
1146 At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't'
1147 with extra annotations. The alias behaves like a normal typing alias,
1148 instantiating is the same as instantiating the underlying type, binding
1149 it to types is also the same.
1150 """
1151 def __init__(self, origin, metadata):
1152 if isinstance(origin, _AnnotatedAlias):
1153 metadata = origin.__metadata__ + metadata
1154 origin = origin.__origin__
1155 super().__init__(origin, origin)
1156 self.__metadata__ = metadata
1157
1158 def copy_with(self, params):
1159 assert len(params) == 1
1160 new_type = params[0]
1161 return _AnnotatedAlias(new_type, self.__metadata__)
1162
1163 def __repr__(self):
1164 return "typing.Annotated[{}, {}]".format(
1165 _type_repr(self.__origin__),
1166 ", ".join(repr(a) for a in self.__metadata__)
1167 )
1168
1169 def __reduce__(self):
1170 return operator.getitem, (
1171 Annotated, (self.__origin__,) + self.__metadata__
1172 )
1173
1174 def __eq__(self, other):
1175 if not isinstance(other, _AnnotatedAlias):
1176 return NotImplemented
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001177 return (self.__origin__ == other.__origin__
1178 and self.__metadata__ == other.__metadata__)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001179
1180 def __hash__(self):
1181 return hash((self.__origin__, self.__metadata__))
1182
1183
1184class Annotated:
1185 """Add context specific metadata to a type.
1186
1187 Example: Annotated[int, runtime_check.Unsigned] indicates to the
1188 hypothetical runtime_check module that this type is an unsigned int.
1189 Every other consumer of this type can ignore this metadata and treat
1190 this type as int.
1191
1192 The first argument to Annotated must be a valid type.
1193
1194 Details:
1195
1196 - It's an error to call `Annotated` with less than two arguments.
1197 - Nested Annotated are flattened::
1198
1199 Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
1200
1201 - Instantiating an annotated type is equivalent to instantiating the
1202 underlying type::
1203
1204 Annotated[C, Ann1](5) == C(5)
1205
1206 - Annotated can be used as a generic type alias::
1207
1208 Optimized = Annotated[T, runtime.Optimize()]
1209 Optimized[int] == Annotated[int, runtime.Optimize()]
1210
1211 OptimizedList = Annotated[List[T], runtime.Optimize()]
1212 OptimizedList[int] == Annotated[List[int], runtime.Optimize()]
1213 """
1214
1215 __slots__ = ()
1216
1217 def __new__(cls, *args, **kwargs):
1218 raise TypeError("Type Annotated cannot be instantiated.")
1219
1220 @_tp_cache
1221 def __class_getitem__(cls, params):
1222 if not isinstance(params, tuple) or len(params) < 2:
1223 raise TypeError("Annotated[...] should be used "
1224 "with at least two arguments (a type and an "
1225 "annotation).")
1226 msg = "Annotated[t, ...]: t must be a type."
1227 origin = _type_check(params[0], msg)
1228 metadata = tuple(params[1:])
1229 return _AnnotatedAlias(origin, metadata)
1230
1231 def __init_subclass__(cls, *args, **kwargs):
1232 raise TypeError(
1233 "Cannot subclass {}.Annotated".format(cls.__module__)
1234 )
1235
1236
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001237def runtime_checkable(cls):
1238 """Mark a protocol class as a runtime protocol.
1239
1240 Such protocol can be used with isinstance() and issubclass().
1241 Raise TypeError if applied to a non-protocol class.
1242 This allows a simple-minded structural check very similar to
1243 one trick ponies in collections.abc such as Iterable.
1244 For example::
1245
1246 @runtime_checkable
1247 class Closable(Protocol):
1248 def close(self): ...
1249
1250 assert isinstance(open('/some/file'), Closable)
1251
1252 Warning: this will check only the presence of the required methods,
1253 not their type signatures!
1254 """
1255 if not issubclass(cls, Generic) or not cls._is_protocol:
1256 raise TypeError('@runtime_checkable can be only applied to protocol classes,'
1257 ' got %r' % cls)
1258 cls._is_runtime_protocol = True
1259 return cls
1260
1261
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001262def cast(typ, val):
1263 """Cast a value to a type.
1264
1265 This returns the value unchanged. To the type checker this
1266 signals that the return value has the designated type, but at
1267 runtime we intentionally don't check anything (we want this
1268 to be as fast as possible).
1269 """
1270 return val
1271
1272
1273def _get_defaults(func):
1274 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -08001275 try:
1276 code = func.__code__
1277 except AttributeError:
1278 # Some built-in functions don't have __code__, __defaults__, etc.
1279 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001280 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001281 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001282 arg_names = arg_names[:pos_count]
1283 defaults = func.__defaults__ or ()
1284 kwdefaults = func.__kwdefaults__
1285 res = dict(kwdefaults) if kwdefaults else {}
1286 pos_offset = pos_count - len(defaults)
1287 for name, value in zip(arg_names[pos_offset:], defaults):
1288 assert name not in res
1289 res[name] = value
1290 return res
1291
1292
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001293_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
1294 types.MethodType, types.ModuleType,
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02001295 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001296
1297
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001298def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
Guido van Rossum991d14f2016-11-09 13:12:51 -08001299 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001300
Guido van Rossum991d14f2016-11-09 13:12:51 -08001301 This is often the same as obj.__annotations__, but it handles
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001302 forward references encoded as string literals, adds Optional[t] if a
1303 default value equal to None is set and recursively replaces all
1304 'Annotated[T, ...]' with 'T' (unless 'include_extras=True').
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001305
Guido van Rossum991d14f2016-11-09 13:12:51 -08001306 The argument may be a module, class, method, or function. The annotations
1307 are returned as a dictionary. For classes, annotations include also
1308 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001309
Guido van Rossum991d14f2016-11-09 13:12:51 -08001310 TypeError is raised if the argument is not of a type that can contain
1311 annotations, and an empty dictionary is returned if no annotations are
1312 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001313
Guido van Rossum991d14f2016-11-09 13:12:51 -08001314 BEWARE -- the behavior of globalns and localns is counterintuitive
1315 (unless you are familiar with how eval() and exec() work). The
1316 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001317
Guido van Rossum991d14f2016-11-09 13:12:51 -08001318 - If no dict arguments are passed, an attempt is made to use the
Łukasz Langaf350a262017-09-14 14:33:00 -04001319 globals from obj (or the respective module's globals for classes),
1320 and these are also used as the locals. If the object does not appear
1321 to have globals, an empty dictionary is used.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001322
Guido van Rossum991d14f2016-11-09 13:12:51 -08001323 - If one dict argument is passed, it is used for both globals and
1324 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001325
Guido van Rossum991d14f2016-11-09 13:12:51 -08001326 - If two dict arguments are passed, they specify globals and
1327 locals, respectively.
1328 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001329
Guido van Rossum991d14f2016-11-09 13:12:51 -08001330 if getattr(obj, '__no_type_check__', None):
1331 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001332 # Classes require a special treatment.
1333 if isinstance(obj, type):
1334 hints = {}
1335 for base in reversed(obj.__mro__):
Łukasz Langaf350a262017-09-14 14:33:00 -04001336 if globalns is None:
1337 base_globals = sys.modules[base.__module__].__dict__
1338 else:
1339 base_globals = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001340 ann = base.__dict__.get('__annotations__', {})
1341 for name, value in ann.items():
1342 if value is None:
1343 value = type(None)
1344 if isinstance(value, str):
Nina Zakharenko0e61dff2018-05-22 20:32:10 -07001345 value = ForwardRef(value, is_argument=False)
Łukasz Langaf350a262017-09-14 14:33:00 -04001346 value = _eval_type(value, base_globals, localns)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001347 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001348 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
Łukasz Langaf350a262017-09-14 14:33:00 -04001349
1350 if globalns is None:
1351 if isinstance(obj, types.ModuleType):
1352 globalns = obj.__dict__
1353 else:
benedwards140aca3a32019-11-21 17:24:58 +00001354 nsobj = obj
1355 # Find globalns for the unwrapped object.
1356 while hasattr(nsobj, '__wrapped__'):
1357 nsobj = nsobj.__wrapped__
1358 globalns = getattr(nsobj, '__globals__', {})
Łukasz Langaf350a262017-09-14 14:33:00 -04001359 if localns is None:
1360 localns = globalns
1361 elif localns is None:
1362 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001363 hints = getattr(obj, '__annotations__', None)
1364 if hints is None:
1365 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001366 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001367 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001368 else:
1369 raise TypeError('{!r} is not a module, class, method, '
1370 'or function.'.format(obj))
1371 defaults = _get_defaults(obj)
1372 hints = dict(hints)
1373 for name, value in hints.items():
1374 if value is None:
1375 value = type(None)
1376 if isinstance(value, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001377 value = ForwardRef(value)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001378 value = _eval_type(value, globalns, localns)
1379 if name in defaults and defaults[name] is None:
1380 value = Optional[value]
1381 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001382 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
1383
1384
1385def _strip_annotations(t):
1386 """Strips the annotations from a given type.
1387 """
1388 if isinstance(t, _AnnotatedAlias):
1389 return _strip_annotations(t.__origin__)
1390 if isinstance(t, _GenericAlias):
1391 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1392 if stripped_args == t.__args__:
1393 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001394 return t.copy_with(stripped_args)
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001395 if isinstance(t, GenericAlias):
1396 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1397 if stripped_args == t.__args__:
1398 return t
1399 return GenericAlias(t.__origin__, stripped_args)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001400 return t
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001401
1402
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001403def get_origin(tp):
1404 """Get the unsubscripted version of a type.
1405
Jakub Stasiak38aaaaa2020-02-07 02:15:12 +01001406 This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
1407 and Annotated. Return None for unsupported types. Examples::
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001408
1409 get_origin(Literal[42]) is Literal
1410 get_origin(int) is None
1411 get_origin(ClassVar[int]) is ClassVar
1412 get_origin(Generic) is Generic
1413 get_origin(Generic[T]) is Generic
1414 get_origin(Union[T, int]) is Union
1415 get_origin(List[Tuple[T, T]][int]) == list
1416 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001417 if isinstance(tp, _AnnotatedAlias):
1418 return Annotated
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001419 if isinstance(tp, (_BaseGenericAlias, GenericAlias)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001420 return tp.__origin__
1421 if tp is Generic:
1422 return Generic
1423 return None
1424
1425
1426def get_args(tp):
1427 """Get type arguments with all substitutions performed.
1428
1429 For unions, basic simplifications used by Union constructor are performed.
1430 Examples::
1431 get_args(Dict[str, int]) == (str, int)
1432 get_args(int) == ()
1433 get_args(Union[int, Union[T, int], str][int]) == (int, str)
1434 get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
1435 get_args(Callable[[], T][int]) == ([], int)
1436 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001437 if isinstance(tp, _AnnotatedAlias):
1438 return (tp.__origin__,) + tp.__metadata__
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001439 if isinstance(tp, _GenericAlias):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001440 res = tp.__args__
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001441 if tp.__origin__ is collections.abc.Callable and res[0] is not Ellipsis:
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001442 res = (list(res[:-1]), res[-1])
1443 return res
Serhiy Storchaka6292be72020-04-27 10:27:21 +03001444 if isinstance(tp, GenericAlias):
1445 return tp.__args__
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001446 return ()
1447
1448
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001449def no_type_check(arg):
1450 """Decorator to indicate that annotations are not type hints.
1451
1452 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001453 applies recursively to all methods and classes defined in that class
1454 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001455
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001456 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001457 """
1458 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001459 arg_attrs = arg.__dict__.copy()
1460 for attr, val in arg.__dict__.items():
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001461 if val in arg.__bases__ + (arg,):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001462 arg_attrs.pop(attr)
1463 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001464 if isinstance(obj, types.FunctionType):
1465 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001466 if isinstance(obj, type):
1467 no_type_check(obj)
1468 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001469 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001470 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001471 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001472 return arg
1473
1474
1475def no_type_check_decorator(decorator):
1476 """Decorator to give another decorator the @no_type_check effect.
1477
1478 This wraps the decorator with something that wraps the decorated
1479 function in @no_type_check.
1480 """
1481
1482 @functools.wraps(decorator)
1483 def wrapped_decorator(*args, **kwds):
1484 func = decorator(*args, **kwds)
1485 func = no_type_check(func)
1486 return func
1487
1488 return wrapped_decorator
1489
1490
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001491def _overload_dummy(*args, **kwds):
1492 """Helper for @overload to raise when called."""
1493 raise NotImplementedError(
1494 "You should not call an overloaded function. "
1495 "A series of @overload-decorated functions "
1496 "outside a stub module should always be followed "
1497 "by an implementation that is not @overload-ed.")
1498
1499
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001500def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001501 """Decorator for overloaded functions/methods.
1502
1503 In a stub file, place two or more stub definitions for the same
1504 function in a row, each decorated with @overload. For example:
1505
1506 @overload
1507 def utf8(value: None) -> None: ...
1508 @overload
1509 def utf8(value: bytes) -> bytes: ...
1510 @overload
1511 def utf8(value: str) -> bytes: ...
1512
1513 In a non-stub file (i.e. a regular .py file), do the same but
1514 follow it with an implementation. The implementation should *not*
1515 be decorated with @overload. For example:
1516
1517 @overload
1518 def utf8(value: None) -> None: ...
1519 @overload
1520 def utf8(value: bytes) -> bytes: ...
1521 @overload
1522 def utf8(value: str) -> bytes: ...
1523 def utf8(value):
1524 # implementation goes here
1525 """
1526 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001527
1528
Ivan Levkivskyif3672422019-05-26 09:37:07 +01001529def final(f):
1530 """A decorator to indicate final methods and final classes.
1531
1532 Use this decorator to indicate to type checkers that the decorated
1533 method cannot be overridden, and decorated class cannot be subclassed.
1534 For example:
1535
1536 class Base:
1537 @final
1538 def done(self) -> None:
1539 ...
1540 class Sub(Base):
1541 def done(self) -> None: # Error reported by type checker
1542 ...
1543
1544 @final
1545 class Leaf:
1546 ...
1547 class Other(Leaf): # Error reported by type checker
1548 ...
1549
1550 There is no runtime checking of these properties.
1551 """
1552 return f
1553
1554
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001555# Some unconstrained type variables. These are used by the container types.
1556# (These are not for export.)
1557T = TypeVar('T') # Any type.
1558KT = TypeVar('KT') # Key type.
1559VT = TypeVar('VT') # Value type.
1560T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
1561V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
1562VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
1563T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
1564# Internal type variable used for Type[].
1565CT_co = TypeVar('CT_co', covariant=True, bound=type)
1566
1567# A useful type variable with constraints. This represents string types.
1568# (This one *is* for export!)
1569AnyStr = TypeVar('AnyStr', bytes, str)
1570
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001571
1572# Various ABCs mimicking those in collections.abc.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001573_alias = _SpecialGenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001574
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001575Hashable = _alias(collections.abc.Hashable, 0) # Not generic.
1576Awaitable = _alias(collections.abc.Awaitable, 1)
1577Coroutine = _alias(collections.abc.Coroutine, 3)
1578AsyncIterable = _alias(collections.abc.AsyncIterable, 1)
1579AsyncIterator = _alias(collections.abc.AsyncIterator, 1)
1580Iterable = _alias(collections.abc.Iterable, 1)
1581Iterator = _alias(collections.abc.Iterator, 1)
1582Reversible = _alias(collections.abc.Reversible, 1)
1583Sized = _alias(collections.abc.Sized, 0) # Not generic.
1584Container = _alias(collections.abc.Container, 1)
1585Collection = _alias(collections.abc.Collection, 1)
1586Callable = _CallableType(collections.abc.Callable, 2)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001587Callable.__doc__ = \
1588 """Callable type; Callable[[int], str] is a function of (int) -> str.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001589
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001590 The subscription syntax must always be used with exactly two
1591 values: the argument list and the return type. The argument list
1592 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001593
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001594 There is no syntax to indicate optional or keyword arguments,
1595 such function types are rarely used as callback types.
1596 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001597AbstractSet = _alias(collections.abc.Set, 1, name='AbstractSet')
1598MutableSet = _alias(collections.abc.MutableSet, 1)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001599# NOTE: Mapping is only covariant in the value type.
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001600Mapping = _alias(collections.abc.Mapping, 2)
1601MutableMapping = _alias(collections.abc.MutableMapping, 2)
1602Sequence = _alias(collections.abc.Sequence, 1)
1603MutableSequence = _alias(collections.abc.MutableSequence, 1)
1604ByteString = _alias(collections.abc.ByteString, 0) # Not generic
1605# Tuple accepts variable number of parameters.
1606Tuple = _TupleType(tuple, -1, inst=False, name='Tuple')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001607Tuple.__doc__ = \
1608 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001609
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001610 Example: Tuple[T1, T2] is a tuple of two elements corresponding
1611 to type variables T1 and T2. Tuple[int, float, str] is a tuple
1612 of an int, a float and a string.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001613
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001614 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
1615 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001616List = _alias(list, 1, inst=False, name='List')
1617Deque = _alias(collections.deque, 1, name='Deque')
1618Set = _alias(set, 1, inst=False, name='Set')
1619FrozenSet = _alias(frozenset, 1, inst=False, name='FrozenSet')
1620MappingView = _alias(collections.abc.MappingView, 1)
1621KeysView = _alias(collections.abc.KeysView, 1)
1622ItemsView = _alias(collections.abc.ItemsView, 2)
1623ValuesView = _alias(collections.abc.ValuesView, 1)
1624ContextManager = _alias(contextlib.AbstractContextManager, 1, name='ContextManager')
1625AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, 1, name='AsyncContextManager')
1626Dict = _alias(dict, 2, inst=False, name='Dict')
1627DefaultDict = _alias(collections.defaultdict, 2, name='DefaultDict')
1628OrderedDict = _alias(collections.OrderedDict, 2)
1629Counter = _alias(collections.Counter, 1)
1630ChainMap = _alias(collections.ChainMap, 2)
1631Generator = _alias(collections.abc.Generator, 3)
1632AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2)
1633Type = _alias(type, 1, inst=False, name='Type')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001634Type.__doc__ = \
1635 """A special construct usable to annotate class objects.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001636
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001637 For example, suppose we have the following classes::
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001638
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001639 class User: ... # Abstract base for User classes
1640 class BasicUser(User): ...
1641 class ProUser(User): ...
1642 class TeamUser(User): ...
Guido van Rossumf17c2002015-12-03 17:31:24 -08001643
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001644 And a function that takes a class argument that's a subclass of
1645 User and returns an instance of the corresponding class::
Guido van Rossumf17c2002015-12-03 17:31:24 -08001646
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001647 U = TypeVar('U', bound=User)
1648 def new_user(user_class: Type[U]) -> U:
1649 user = user_class()
1650 # (Here we could write the user object to a database)
1651 return user
Guido van Rossumf17c2002015-12-03 17:31:24 -08001652
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001653 joe = new_user(BasicUser)
Guido van Rossumf17c2002015-12-03 17:31:24 -08001654
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001655 At this point the type checker knows that joe has type BasicUser.
1656 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001657
1658
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001659@runtime_checkable
1660class SupportsInt(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001661 """An ABC with one abstract method __int__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001662 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001663
1664 @abstractmethod
1665 def __int__(self) -> int:
1666 pass
1667
1668
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001669@runtime_checkable
1670class SupportsFloat(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001671 """An ABC with one abstract method __float__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001672 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001673
1674 @abstractmethod
1675 def __float__(self) -> float:
1676 pass
1677
1678
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001679@runtime_checkable
1680class SupportsComplex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001681 """An ABC with one abstract method __complex__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001682 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001683
1684 @abstractmethod
1685 def __complex__(self) -> complex:
1686 pass
1687
1688
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001689@runtime_checkable
1690class SupportsBytes(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001691 """An ABC with one abstract method __bytes__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001692 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001693
1694 @abstractmethod
1695 def __bytes__(self) -> bytes:
1696 pass
1697
1698
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001699@runtime_checkable
1700class SupportsIndex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001701 """An ABC with one abstract method __index__."""
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -07001702 __slots__ = ()
1703
1704 @abstractmethod
1705 def __index__(self) -> int:
1706 pass
1707
1708
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001709@runtime_checkable
1710class SupportsAbs(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001711 """An ABC with one abstract method __abs__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001712 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001713
1714 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001715 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001716 pass
1717
1718
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001719@runtime_checkable
1720class SupportsRound(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001721 """An ABC with one abstract method __round__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001722 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001723
1724 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001725 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001726 pass
1727
1728
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001729def _make_nmtuple(name, types, module, defaults = ()):
1730 fields = [n for n, t in types]
1731 types = {n: _type_check(t, f"field {n} annotation must be a type")
1732 for n, t in types}
1733 nm_tpl = collections.namedtuple(name, fields,
1734 defaults=defaults, module=module)
1735 nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001736 return nm_tpl
1737
1738
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001739# attributes prohibited to set in NamedTuple class syntax
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001740_prohibited = frozenset({'__new__', '__init__', '__slots__', '__getnewargs__',
1741 '_fields', '_field_defaults',
1742 '_make', '_replace', '_asdict', '_source'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001743
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001744_special = frozenset({'__module__', '__name__', '__annotations__'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001745
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001746
Guido van Rossum2f841442016-11-15 09:48:06 -08001747class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001748
Guido van Rossum2f841442016-11-15 09:48:06 -08001749 def __new__(cls, typename, bases, ns):
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001750 assert bases[0] is _NamedTuple
Guido van Rossum2f841442016-11-15 09:48:06 -08001751 types = ns.get('__annotations__', {})
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001752 default_names = []
Guido van Rossum3c268be2017-01-18 08:03:50 -08001753 for field_name in types:
1754 if field_name in ns:
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001755 default_names.append(field_name)
1756 elif default_names:
1757 raise TypeError(f"Non-default namedtuple field {field_name} "
1758 f"cannot follow default field"
1759 f"{'s' if len(default_names) > 1 else ''} "
1760 f"{', '.join(default_names)}")
1761 nm_tpl = _make_nmtuple(typename, types.items(),
1762 defaults=[ns[n] for n in default_names],
1763 module=ns['__module__'])
Guido van Rossum95919c02017-01-22 17:47:20 -08001764 # update from user namespace without overriding special namedtuple attributes
1765 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001766 if key in _prohibited:
1767 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
1768 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08001769 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08001770 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001771
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001772
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001773def NamedTuple(typename, fields=None, /, **kwargs):
Guido van Rossum2f841442016-11-15 09:48:06 -08001774 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001775
Guido van Rossum2f841442016-11-15 09:48:06 -08001776 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001777
Guido van Rossum2f841442016-11-15 09:48:06 -08001778 class Employee(NamedTuple):
1779 name: str
1780 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001781
Guido van Rossum2f841442016-11-15 09:48:06 -08001782 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001783
Guido van Rossum2f841442016-11-15 09:48:06 -08001784 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001785
Raymond Hettingerf7b57df2019-03-18 09:53:56 -07001786 The resulting class has an extra __annotations__ attribute, giving a
1787 dict that maps field names to types. (The field names are also in
1788 the _fields attribute, which is part of the namedtuple API.)
1789 Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001790
Guido van Rossum2f841442016-11-15 09:48:06 -08001791 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001792
Guido van Rossum2f841442016-11-15 09:48:06 -08001793 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001794
Guido van Rossum2f841442016-11-15 09:48:06 -08001795 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
1796 """
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001797 if fields is None:
1798 fields = kwargs.items()
1799 elif kwargs:
1800 raise TypeError("Either list of fields or keywords"
1801 " can be provided to NamedTuple, not both")
1802 try:
1803 module = sys._getframe(1).f_globals.get('__name__', '__main__')
1804 except (AttributeError, ValueError):
1805 module = None
1806 return _make_nmtuple(typename, fields, module=module)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001807
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001808_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})
1809
1810def _namedtuple_mro_entries(bases):
1811 if len(bases) > 1:
1812 raise TypeError("Multiple inheritance with NamedTuple is not supported")
1813 assert bases[0] is NamedTuple
1814 return (_NamedTuple,)
1815
1816NamedTuple.__mro_entries__ = _namedtuple_mro_entries
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001817
1818
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001819class _TypedDictMeta(type):
1820 def __new__(cls, name, bases, ns, total=True):
1821 """Create new typed dict class object.
1822
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001823 This method is called when TypedDict is subclassed,
1824 or when TypedDict is instantiated. This way
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001825 TypedDict supports all three syntax forms described in its docstring.
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001826 Subclasses and instances of TypedDict return actual dictionaries.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001827 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001828 for base in bases:
1829 if type(base) is not _TypedDictMeta:
1830 raise TypeError('cannot inherit from both a TypedDict type '
1831 'and a non-TypedDict base class')
1832 tp_dict = type.__new__(_TypedDictMeta, name, (dict,), ns)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001833
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001834 annotations = {}
1835 own_annotations = ns.get('__annotations__', {})
1836 own_annotation_keys = set(own_annotations.keys())
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001837 msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001838 own_annotations = {
1839 n: _type_check(tp, msg) for n, tp in own_annotations.items()
1840 }
1841 required_keys = set()
1842 optional_keys = set()
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11001843
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001844 for base in bases:
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001845 annotations.update(base.__dict__.get('__annotations__', {}))
1846 required_keys.update(base.__dict__.get('__required_keys__', ()))
1847 optional_keys.update(base.__dict__.get('__optional_keys__', ()))
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11001848
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001849 annotations.update(own_annotations)
1850 if total:
1851 required_keys.update(own_annotation_keys)
1852 else:
1853 optional_keys.update(own_annotation_keys)
1854
1855 tp_dict.__annotations__ = annotations
1856 tp_dict.__required_keys__ = frozenset(required_keys)
1857 tp_dict.__optional_keys__ = frozenset(optional_keys)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001858 if not hasattr(tp_dict, '__total__'):
1859 tp_dict.__total__ = total
1860 return tp_dict
1861
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001862 __call__ = dict # static method
1863
1864 def __subclasscheck__(cls, other):
1865 # Typed dicts are only for static structural subtyping.
1866 raise TypeError('TypedDict does not support instance and class checks')
1867
1868 __instancecheck__ = __subclasscheck__
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001869
1870
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001871def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001872 """A simple typed namespace. At runtime it is equivalent to a plain dict.
1873
1874 TypedDict creates a dictionary type that expects all of its
1875 instances to have a certain set of keys, where each key is
1876 associated with a value of a consistent type. This expectation
1877 is not checked at runtime but is only enforced by type checkers.
1878 Usage::
1879
1880 class Point2D(TypedDict):
1881 x: int
1882 y: int
1883 label: str
1884
1885 a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
1886 b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
1887
1888 assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
1889
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11001890 The type info can be accessed via the Point2D.__annotations__ dict, and
1891 the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
1892 TypedDict supports two additional equivalent forms::
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001893
1894 Point2D = TypedDict('Point2D', x=int, y=int, label=str)
1895 Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
1896
ananthan-123ab6423f2020-02-19 10:03:05 +05301897 By default, all keys must be present in a TypedDict. It is possible
1898 to override this by specifying totality.
1899 Usage::
1900
1901 class point2D(TypedDict, total=False):
1902 x: int
1903 y: int
1904
1905 This means that a point2D TypedDict can have any of the keys omitted.A type
1906 checker is only expected to support a literal False or True as the value of
1907 the total argument. True is the default, and makes all items defined in the
1908 class body be required.
1909
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001910 The class syntax is only supported in Python 3.6+, while two other
1911 syntax forms work for Python 2.7 and 3.2+
1912 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001913 if fields is None:
1914 fields = kwargs
1915 elif kwargs:
1916 raise TypeError("TypedDict takes either a dict or keyword arguments,"
1917 " but not both")
1918
1919 ns = {'__annotations__': dict(fields), '__total__': total}
1920 try:
1921 # Setting correct module is necessary to make typed dict classes pickleable.
1922 ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
1923 except (AttributeError, ValueError):
1924 pass
1925
1926 return _TypedDictMeta(typename, (), ns)
1927
1928_TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {})
1929TypedDict.__mro_entries__ = lambda bases: (_TypedDict,)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001930
1931
Guido van Rossum91185fe2016-06-08 11:19:11 -07001932def NewType(name, tp):
1933 """NewType creates simple unique types with almost zero
1934 runtime overhead. NewType(name, tp) is considered a subtype of tp
1935 by static type checkers. At runtime, NewType(name, tp) returns
1936 a dummy function that simply returns its argument. Usage::
1937
1938 UserId = NewType('UserId', int)
1939
1940 def name_by_id(user_id: UserId) -> str:
1941 ...
1942
1943 UserId('user') # Fails type check
1944
1945 name_by_id(42) # Fails type check
1946 name_by_id(UserId(42)) # OK
1947
1948 num = UserId(5) + 1 # type: int
1949 """
1950
1951 def new_type(x):
1952 return x
1953
1954 new_type.__name__ = name
1955 new_type.__supertype__ = tp
1956 return new_type
1957
1958
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001959# Python-version-specific alias (Python 2: unicode; Python 3: str)
1960Text = str
1961
1962
Guido van Rossum91185fe2016-06-08 11:19:11 -07001963# Constant that's True when type checking, but False here.
1964TYPE_CHECKING = False
1965
1966
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001967class IO(Generic[AnyStr]):
1968 """Generic base class for TextIO and BinaryIO.
1969
1970 This is an abstract, generic version of the return of open().
1971
1972 NOTE: This does not distinguish between the different possible
1973 classes (text vs. binary, read vs. write vs. read/write,
1974 append-only, unbuffered). The TextIO and BinaryIO subclasses
1975 below capture the distinctions between text vs. binary, which is
1976 pervasive in the interface; however we currently do not offer a
1977 way to track the other distinctions in the type system.
1978 """
1979
Guido van Rossumd70fe632015-08-05 12:11:06 +02001980 __slots__ = ()
1981
HongWeipeng6ce03ec2019-09-27 15:54:26 +08001982 @property
1983 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001984 def mode(self) -> str:
1985 pass
1986
HongWeipeng6ce03ec2019-09-27 15:54:26 +08001987 @property
1988 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001989 def name(self) -> str:
1990 pass
1991
1992 @abstractmethod
1993 def close(self) -> None:
1994 pass
1995
Shantanu2e6569b2020-01-29 18:52:36 -08001996 @property
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001997 @abstractmethod
1998 def closed(self) -> bool:
1999 pass
2000
2001 @abstractmethod
2002 def fileno(self) -> int:
2003 pass
2004
2005 @abstractmethod
2006 def flush(self) -> None:
2007 pass
2008
2009 @abstractmethod
2010 def isatty(self) -> bool:
2011 pass
2012
2013 @abstractmethod
2014 def read(self, n: int = -1) -> AnyStr:
2015 pass
2016
2017 @abstractmethod
2018 def readable(self) -> bool:
2019 pass
2020
2021 @abstractmethod
2022 def readline(self, limit: int = -1) -> AnyStr:
2023 pass
2024
2025 @abstractmethod
2026 def readlines(self, hint: int = -1) -> List[AnyStr]:
2027 pass
2028
2029 @abstractmethod
2030 def seek(self, offset: int, whence: int = 0) -> int:
2031 pass
2032
2033 @abstractmethod
2034 def seekable(self) -> bool:
2035 pass
2036
2037 @abstractmethod
2038 def tell(self) -> int:
2039 pass
2040
2041 @abstractmethod
2042 def truncate(self, size: int = None) -> int:
2043 pass
2044
2045 @abstractmethod
2046 def writable(self) -> bool:
2047 pass
2048
2049 @abstractmethod
2050 def write(self, s: AnyStr) -> int:
2051 pass
2052
2053 @abstractmethod
2054 def writelines(self, lines: List[AnyStr]) -> None:
2055 pass
2056
2057 @abstractmethod
2058 def __enter__(self) -> 'IO[AnyStr]':
2059 pass
2060
2061 @abstractmethod
2062 def __exit__(self, type, value, traceback) -> None:
2063 pass
2064
2065
2066class BinaryIO(IO[bytes]):
2067 """Typed version of the return of open() in binary mode."""
2068
Guido van Rossumd70fe632015-08-05 12:11:06 +02002069 __slots__ = ()
2070
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002071 @abstractmethod
2072 def write(self, s: Union[bytes, bytearray]) -> int:
2073 pass
2074
2075 @abstractmethod
2076 def __enter__(self) -> 'BinaryIO':
2077 pass
2078
2079
2080class TextIO(IO[str]):
2081 """Typed version of the return of open() in text mode."""
2082
Guido van Rossumd70fe632015-08-05 12:11:06 +02002083 __slots__ = ()
2084
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002085 @property
2086 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002087 def buffer(self) -> BinaryIO:
2088 pass
2089
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002090 @property
2091 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002092 def encoding(self) -> str:
2093 pass
2094
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002095 @property
2096 @abstractmethod
Guido van Rossum991d14f2016-11-09 13:12:51 -08002097 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002098 pass
2099
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002100 @property
2101 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002102 def line_buffering(self) -> bool:
2103 pass
2104
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002105 @property
2106 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002107 def newlines(self) -> Any:
2108 pass
2109
2110 @abstractmethod
2111 def __enter__(self) -> 'TextIO':
2112 pass
2113
2114
2115class io:
2116 """Wrapper namespace for IO generic classes."""
2117
2118 __all__ = ['IO', 'TextIO', 'BinaryIO']
2119 IO = IO
2120 TextIO = TextIO
2121 BinaryIO = BinaryIO
2122
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002123
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002124io.__name__ = __name__ + '.io'
2125sys.modules[io.__name__] = io
2126
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002127Pattern = _alias(stdlib_re.Pattern, 1)
2128Match = _alias(stdlib_re.Match, 1)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002129
2130class re:
2131 """Wrapper namespace for re type aliases."""
2132
2133 __all__ = ['Pattern', 'Match']
2134 Pattern = Pattern
2135 Match = Match
2136
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002137
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002138re.__name__ = __name__ + '.re'
2139sys.modules[re.__name__] = re