blob: 9cacaa840ca352d08b3ff85a766e6a7624948393 [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")
Noah Wood5eea0ad2018-10-08 14:50:16 -0400144 if (isinstance(arg, _SpecialForm) and arg not in (Any, NoReturn) or
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100145 arg in (Generic, Protocol)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000146 raise TypeError(f"Plain {arg} is not valid as type argument")
147 if isinstance(arg, (type, TypeVar, ForwardRef)):
148 return arg
149 if not callable(arg):
150 raise TypeError(f"{msg} Got {arg!r:.100}.")
151 return arg
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700152
153
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000154def _type_repr(obj):
155 """Return the repr() of an object, special-casing types (internal helper).
156
157 If obj is a type, we return a shorter version than the default
158 type.__repr__, based on the module and qualified name, which is
159 typically enough to uniquely identify a type. For everything
160 else, we fall back on repr(obj).
161 """
162 if isinstance(obj, type):
163 if obj.__module__ == 'builtins':
164 return obj.__qualname__
165 return f'{obj.__module__}.{obj.__qualname__}'
166 if obj is ...:
167 return('...')
168 if isinstance(obj, types.FunctionType):
169 return obj.__name__
170 return repr(obj)
171
172
173def _collect_type_vars(types):
174 """Collect all type variable contained in types in order of
175 first appearance (lexicographic order). For example::
176
177 _collect_type_vars((T, List[S, T])) == (T, S)
178 """
179 tvars = []
180 for t in types:
181 if isinstance(t, TypeVar) and t not in tvars:
182 tvars.append(t)
Guido van Rossum48b069a2020-04-07 09:50:06 -0700183 if ((isinstance(t, _GenericAlias) and not t._special)
184 or isinstance(t, 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
189def _subs_tvars(tp, tvars, subs):
190 """Substitute type variables 'tvars' with substitutions 'subs'.
191 These two must have the same length.
192 """
193 if not isinstance(tp, _GenericAlias):
194 return tp
195 new_args = list(tp.__args__)
196 for a, arg in enumerate(tp.__args__):
197 if isinstance(arg, TypeVar):
198 for i, tvar in enumerate(tvars):
199 if arg == tvar:
200 new_args[a] = subs[i]
201 else:
202 new_args[a] = _subs_tvars(arg, tvars, subs)
203 if tp.__origin__ is Union:
204 return Union[tuple(new_args)]
205 return tp.copy_with(tuple(new_args))
206
207
208def _check_generic(cls, parameters):
209 """Check correct count for parameters of a generic cls (internal helper).
210 This gives a nice error message in case of count mismatch.
211 """
212 if not cls.__parameters__:
213 raise TypeError(f"{cls} is not a generic class")
214 alen = len(parameters)
215 elen = len(cls.__parameters__)
216 if alen != elen:
217 raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
218 f" actual {alen}, expected {elen}")
219
220
221def _remove_dups_flatten(parameters):
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700222 """An internal helper for Union creation and substitution: flatten Unions
223 among parameters, then remove duplicates.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000224 """
225 # Flatten out Union[Union[...], ...].
226 params = []
227 for p in parameters:
228 if isinstance(p, _GenericAlias) and p.__origin__ is Union:
229 params.extend(p.__args__)
230 elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
231 params.extend(p[1:])
232 else:
233 params.append(p)
234 # Weed out strict duplicates, preserving the first of each occurrence.
235 all_params = set(params)
236 if len(all_params) < len(params):
237 new_params = []
238 for t in params:
239 if t in all_params:
240 new_params.append(t)
241 all_params.remove(t)
242 params = new_params
243 assert not all_params, all_params
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700244 return tuple(params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000245
246
247_cleanups = []
248
249
250def _tp_cache(func):
251 """Internal wrapper caching __getitem__ of generic types with a fallback to
252 original function for non-hashable arguments.
253 """
254 cached = functools.lru_cache()(func)
255 _cleanups.append(cached.cache_clear)
256
257 @functools.wraps(func)
258 def inner(*args, **kwds):
259 try:
260 return cached(*args, **kwds)
261 except TypeError:
262 pass # All real errors (not unhashable args) are raised below.
263 return func(*args, **kwds)
264 return inner
265
266
267def _eval_type(t, globalns, localns):
268 """Evaluate all forward reverences in the given type t.
269 For use of globalns and localns see the docstring for get_type_hints().
270 """
271 if isinstance(t, ForwardRef):
272 return t._evaluate(globalns, localns)
273 if isinstance(t, _GenericAlias):
274 ev_args = tuple(_eval_type(a, globalns, localns) for a in t.__args__)
275 if ev_args == t.__args__:
276 return t
277 res = t.copy_with(ev_args)
278 res._special = t._special
279 return res
280 return t
281
282
283class _Final:
284 """Mixin to prohibit subclassing"""
Guido van Rossum4cefe742016-09-27 15:20:12 -0700285
Guido van Rossum83ec3022017-01-17 20:43:28 -0800286 __slots__ = ('__weakref__',)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700287
Serhiy Storchaka2085bd02019-06-01 11:00:15 +0300288 def __init_subclass__(self, /, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000289 if '_root' not in kwds:
290 raise TypeError("Cannot subclass special typing classes")
291
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100292class _Immutable:
293 """Mixin to indicate that object should not be copied."""
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000294
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100295 def __copy__(self):
296 return self
297
298 def __deepcopy__(self, memo):
299 return self
300
301
302class _SpecialForm(_Final, _Immutable, _root=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000303 """Internal indicator of special typing constructs.
304 See _doc instance attribute for specific docs.
305 """
306
307 __slots__ = ('_name', '_doc')
308
Guido van Rossum4cefe742016-09-27 15:20:12 -0700309 def __new__(cls, *args, **kwds):
310 """Constructor.
311
312 This only exists to give a better error message in case
313 someone tries to subclass a special typing object (not a good idea).
314 """
315 if (len(args) == 3 and
316 isinstance(args[0], str) and
317 isinstance(args[1], tuple)):
318 # Close enough.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000319 raise TypeError(f"Cannot subclass {cls!r}")
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700320 return super().__new__(cls)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700321
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000322 def __init__(self, name, doc):
323 self._name = name
324 self._doc = doc
Guido van Rossum4cefe742016-09-27 15:20:12 -0700325
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000326 def __eq__(self, other):
327 if not isinstance(other, _SpecialForm):
328 return NotImplemented
329 return self._name == other._name
330
331 def __hash__(self):
332 return hash((self._name,))
Guido van Rossum4cefe742016-09-27 15:20:12 -0700333
334 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000335 return 'typing.' + self._name
336
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100337 def __reduce__(self):
338 return self._name
Guido van Rossum4cefe742016-09-27 15:20:12 -0700339
340 def __call__(self, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000341 raise TypeError(f"Cannot instantiate {self!r}")
342
343 def __instancecheck__(self, obj):
344 raise TypeError(f"{self} cannot be used with isinstance()")
345
346 def __subclasscheck__(self, cls):
347 raise TypeError(f"{self} cannot be used with issubclass()")
348
349 @_tp_cache
350 def __getitem__(self, parameters):
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100351 if self._name in ('ClassVar', 'Final'):
352 item = _type_check(parameters, f'{self._name} accepts only single type.')
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000353 return _GenericAlias(self, (item,))
354 if self._name == 'Union':
355 if parameters == ():
356 raise TypeError("Cannot take a Union of no types.")
357 if not isinstance(parameters, tuple):
358 parameters = (parameters,)
359 msg = "Union[arg, ...]: each arg must be a type."
360 parameters = tuple(_type_check(p, msg) for p in parameters)
361 parameters = _remove_dups_flatten(parameters)
362 if len(parameters) == 1:
363 return parameters[0]
364 return _GenericAlias(self, parameters)
365 if self._name == 'Optional':
366 arg = _type_check(parameters, "Optional[t] requires a single type.")
367 return Union[arg, type(None)]
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100368 if self._name == 'Literal':
369 # There is no '_type_check' call because arguments to Literal[...] are
370 # values, not types.
371 return _GenericAlias(self, parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000372 raise TypeError(f"{self} is not subscriptable")
Guido van Rossum4cefe742016-09-27 15:20:12 -0700373
374
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000375Any = _SpecialForm('Any', doc=
376 """Special type indicating an unconstrained type.
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700377
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000378 - Any is compatible with every type.
379 - Any assumed to have all methods.
380 - All values assumed to be instances of Any.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700381
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000382 Note that all the above statements are true from the point of view of
383 static type checkers. At runtime, Any should not be used with instance
384 or class checks.
385 """)
Guido van Rossumd70fe632015-08-05 12:11:06 +0200386
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000387NoReturn = _SpecialForm('NoReturn', doc=
388 """Special type indicating functions that never return.
389 Example::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700390
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000391 from typing import NoReturn
392
393 def stop() -> NoReturn:
394 raise Exception('no way')
395
396 This type is invalid in other positions, e.g., ``List[NoReturn]``
397 will fail in static type checkers.
398 """)
399
400ClassVar = _SpecialForm('ClassVar', doc=
401 """Special type construct to mark class variables.
402
403 An annotation wrapped in ClassVar indicates that a given
404 attribute is intended to be used as a class variable and
405 should not be set on instances of that class. Usage::
406
407 class Starship:
408 stats: ClassVar[Dict[str, int]] = {} # class variable
409 damage: int = 10 # instance variable
410
411 ClassVar accepts only types and cannot be further subscribed.
412
413 Note that ClassVar is not a class itself, and should not
414 be used with isinstance() or issubclass().
415 """)
416
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100417Final = _SpecialForm('Final', doc=
418 """Special typing construct to indicate final names to type checkers.
419
420 A final name cannot be re-assigned or overridden in a subclass.
421 For example:
422
423 MAX_SIZE: Final = 9000
424 MAX_SIZE += 1 # Error reported by type checker
425
426 class Connection:
427 TIMEOUT: Final[int] = 10
428
429 class FastConnector(Connection):
430 TIMEOUT = 1 # Error reported by type checker
431
432 There is no runtime checking of these properties.
433 """)
434
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000435Union = _SpecialForm('Union', doc=
436 """Union type; Union[X, Y] means either X or Y.
437
438 To define a union, use e.g. Union[int, str]. Details:
439 - The arguments must be types and there must be at least one.
440 - None as an argument is a special case and is replaced by
441 type(None).
442 - Unions of unions are flattened, e.g.::
443
444 Union[Union[int, str], float] == Union[int, str, float]
445
446 - Unions of a single argument vanish, e.g.::
447
448 Union[int] == int # The constructor actually returns int
449
450 - Redundant arguments are skipped, e.g.::
451
452 Union[int, str, int] == Union[int, str]
453
454 - When comparing unions, the argument order is ignored, e.g.::
455
456 Union[int, str] == Union[str, int]
457
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000458 - You cannot subclass or instantiate a union.
459 - You can use Optional[X] as a shorthand for Union[X, None].
460 """)
461
462Optional = _SpecialForm('Optional', doc=
463 """Optional type.
464
465 Optional[X] is equivalent to Union[X, None].
466 """)
Guido van Rossumb7dedc82016-10-29 12:44:29 -0700467
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100468Literal = _SpecialForm('Literal', doc=
469 """Special typing form to define literal types (a.k.a. value types).
470
471 This form can be used to indicate to type checkers that the corresponding
472 variable or function parameter has a value equivalent to the provided
473 literal (or one of several literals):
474
475 def validate_simple(data: Any) -> Literal[True]: # always returns True
476 ...
477
478 MODE = Literal['r', 'rb', 'w', 'wb']
479 def open_helper(file: str, mode: MODE) -> str:
480 ...
481
482 open_helper('/some/path', 'r') # Passes type check
483 open_helper('/other/path', 'typo') # Error in type checker
484
485 Literal[...] cannot be subclassed. At runtime, an arbitrary value
486 is allowed as type argument to Literal[...], but type checkers may
487 impose restrictions.
488 """)
489
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700490
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000491class ForwardRef(_Final, _root=True):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800492 """Internal wrapper to hold a forward reference."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700493
Guido van Rossum4cefe742016-09-27 15:20:12 -0700494 __slots__ = ('__forward_arg__', '__forward_code__',
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400495 '__forward_evaluated__', '__forward_value__',
496 '__forward_is_argument__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700497
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700498 def __init__(self, arg, is_argument=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700499 if not isinstance(arg, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000500 raise TypeError(f"Forward reference must be a string -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700501 try:
502 code = compile(arg, '<string>', 'eval')
503 except SyntaxError:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000504 raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700505 self.__forward_arg__ = arg
506 self.__forward_code__ = code
507 self.__forward_evaluated__ = False
508 self.__forward_value__ = None
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400509 self.__forward_is_argument__ = is_argument
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700510
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000511 def _evaluate(self, globalns, localns):
Guido van Rossumdad17902016-11-10 08:29:18 -0800512 if not self.__forward_evaluated__ or localns is not globalns:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700513 if globalns is None and localns is None:
514 globalns = localns = {}
515 elif globalns is None:
516 globalns = localns
517 elif localns is None:
518 localns = globalns
519 self.__forward_value__ = _type_check(
520 eval(self.__forward_code__, globalns, localns),
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400521 "Forward references must evaluate to types.",
522 is_argument=self.__forward_is_argument__)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700523 self.__forward_evaluated__ = True
524 return self.__forward_value__
525
Guido van Rossum4cefe742016-09-27 15:20:12 -0700526 def __eq__(self, other):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000527 if not isinstance(other, ForwardRef):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700528 return NotImplemented
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100529 if self.__forward_evaluated__ and other.__forward_evaluated__:
530 return (self.__forward_arg__ == other.__forward_arg__ and
531 self.__forward_value__ == other.__forward_value__)
532 return self.__forward_arg__ == other.__forward_arg__
Guido van Rossum4cefe742016-09-27 15:20:12 -0700533
534 def __hash__(self):
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100535 return hash(self.__forward_arg__)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700536
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700537 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000538 return f'ForwardRef({self.__forward_arg__!r})'
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700539
540
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100541class TypeVar(_Final, _Immutable, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700542 """Type variable.
543
544 Usage::
545
546 T = TypeVar('T') # Can be anything
547 A = TypeVar('A', str, bytes) # Must be str or bytes
548
549 Type variables exist primarily for the benefit of static type
550 checkers. They serve as the parameters for generic types as well
551 as for generic function definitions. See class Generic for more
552 information on generic types. Generic functions work as follows:
553
Guido van Rossumb24569a2016-11-20 18:01:29 -0800554 def repeat(x: T, n: int) -> List[T]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700555 '''Return a list containing n references to x.'''
556 return [x]*n
557
558 def longest(x: A, y: A) -> A:
559 '''Return the longest of two strings.'''
560 return x if len(x) >= len(y) else y
561
562 The latter example's signature is essentially the overloading
563 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
564 that if the arguments are instances of some subclass of str,
565 the return type is still plain str.
566
Guido van Rossumb24569a2016-11-20 18:01:29 -0800567 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700568
Guido van Rossumefa798d2016-08-23 11:01:50 -0700569 Type variables defined with covariant=True or contravariant=True
João D. Ferreira86bfed32018-07-07 16:41:20 +0100570 can be used to declare covariant or contravariant generic types.
Guido van Rossumefa798d2016-08-23 11:01:50 -0700571 See PEP 484 for more details. By default generic types are invariant
572 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700573
574 Type variables can be introspected. e.g.:
575
576 T.__name__ == 'T'
577 T.__constraints__ == ()
578 T.__covariant__ == False
579 T.__contravariant__ = False
580 A.__constraints__ == (str, bytes)
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100581
582 Note that only type variables defined in global scope can be pickled.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700583 """
584
Guido van Rossum4cefe742016-09-27 15:20:12 -0700585 __slots__ = ('__name__', '__bound__', '__constraints__',
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300586 '__covariant__', '__contravariant__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700587
588 def __init__(self, name, *constraints, bound=None,
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800589 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700590 self.__name__ = name
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700591 if covariant and contravariant:
Guido van Rossumefa798d2016-08-23 11:01:50 -0700592 raise ValueError("Bivariant types are not supported.")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700593 self.__covariant__ = bool(covariant)
594 self.__contravariant__ = bool(contravariant)
595 if constraints and bound is not None:
596 raise TypeError("Constraints cannot be combined with bound=...")
597 if constraints and len(constraints) == 1:
598 raise TypeError("A single constraint is not allowed")
599 msg = "TypeVar(name, constraint, ...): constraints must be types."
600 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
601 if bound:
602 self.__bound__ = _type_check(bound, "Bound must be a type.")
603 else:
604 self.__bound__ = None
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300605 def_mod = sys._getframe(1).f_globals['__name__'] # for pickling
606 if def_mod != 'typing':
607 self.__module__ = def_mod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700608
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700609 def __repr__(self):
610 if self.__covariant__:
611 prefix = '+'
612 elif self.__contravariant__:
613 prefix = '-'
614 else:
615 prefix = '~'
616 return prefix + self.__name__
617
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100618 def __reduce__(self):
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300619 return self.__name__
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100620
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700621
Guido van Rossum83ec3022017-01-17 20:43:28 -0800622# Special typing constructs Union, Optional, Generic, Callable and Tuple
623# use three special attributes for internal bookkeeping of generic types:
624# * __parameters__ is a tuple of unique free type parameters of a generic
625# type, for example, Dict[T, T].__parameters__ == (T,);
626# * __origin__ keeps a reference to a type that was subscripted,
Ivan Levkivskyi43d12a62018-05-09 02:23:46 +0100627# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
628# the type.
Guido van Rossum83ec3022017-01-17 20:43:28 -0800629# * __args__ is a tuple of all arguments used in subscripting,
630# e.g., Dict[T, int].__args__ == (T, int).
631
Ivan Levkivskyi2a363d22018-04-05 01:25:15 +0100632
633# Mapping from non-generic type names that have a generic alias in typing
634# but with a different name.
635_normalize_alias = {'list': 'List',
636 'tuple': 'Tuple',
637 'dict': 'Dict',
638 'set': 'Set',
639 'frozenset': 'FrozenSet',
640 'deque': 'Deque',
641 'defaultdict': 'DefaultDict',
642 'type': 'Type',
643 'Set': 'AbstractSet'}
644
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000645def _is_dunder(attr):
646 return attr.startswith('__') and attr.endswith('__')
Guido van Rossum83ec3022017-01-17 20:43:28 -0800647
Guido van Rossumb24569a2016-11-20 18:01:29 -0800648
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000649class _GenericAlias(_Final, _root=True):
650 """The central part of internal API.
651
652 This represents a generic version of type 'origin' with type arguments 'params'.
653 There are two kind of these aliases: user defined and special. The special ones
654 are wrappers around builtin collections and ABCs in collections.abc. These must
655 have 'name' always set. If 'inst' is False, then the alias can't be instantiated,
656 this is used by e.g. typing.List and typing.Dict.
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700657 """
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000658 def __init__(self, origin, params, *, inst=True, special=False, name=None):
659 self._inst = inst
660 self._special = special
661 if special and name is None:
662 orig_name = origin.__name__
Ivan Levkivskyi2a363d22018-04-05 01:25:15 +0100663 name = _normalize_alias.get(orig_name, orig_name)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000664 self._name = name
665 if not isinstance(params, tuple):
666 params = (params,)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700667 self.__origin__ = origin
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700668 self.__args__ = tuple(... if a is _TypingEllipsis else
669 () if a is _TypingEmpty else
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000670 a for a in params)
671 self.__parameters__ = _collect_type_vars(params)
672 self.__slots__ = None # This is not documented.
673 if not name:
674 self.__module__ = origin.__module__
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700675
Guido van Rossum4cefe742016-09-27 15:20:12 -0700676 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700677 def __getitem__(self, params):
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100678 if self.__origin__ in (Generic, Protocol):
679 # Can't subscript Generic[...] or Protocol[...].
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000680 raise TypeError(f"Cannot subscript already-subscripted {self}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700681 if not isinstance(params, tuple):
682 params = (params,)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700683 msg = "Parameters to generic types must be types."
684 params = tuple(_type_check(p, msg) for p in params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000685 _check_generic(self, params)
686 return _subs_tvars(self, self.__parameters__, params)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100687
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000688 def copy_with(self, params):
689 # We don't copy self._special.
690 return _GenericAlias(self.__origin__, params, name=self._name, inst=self._inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700691
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000692 def __repr__(self):
693 if (self._name != 'Callable' or
694 len(self.__args__) == 2 and self.__args__[0] is Ellipsis):
695 if self._name:
696 name = 'typing.' + self._name
697 else:
698 name = _type_repr(self.__origin__)
699 if not self._special:
700 args = f'[{", ".join([_type_repr(a) for a in self.__args__])}]'
701 else:
702 args = ''
703 return (f'{name}{args}')
704 if self._special:
705 return 'typing.Callable'
706 return (f'typing.Callable'
707 f'[[{", ".join([_type_repr(a) for a in self.__args__[:-1]])}], '
708 f'{_type_repr(self.__args__[-1])}]')
709
710 def __eq__(self, other):
711 if not isinstance(other, _GenericAlias):
712 return NotImplemented
713 if self.__origin__ != other.__origin__:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100714 return False
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000715 if self.__origin__ is Union and other.__origin__ is Union:
716 return frozenset(self.__args__) == frozenset(other.__args__)
717 return self.__args__ == other.__args__
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100718
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000719 def __hash__(self):
720 if self.__origin__ is Union:
721 return hash((Union, frozenset(self.__args__)))
722 return hash((self.__origin__, self.__args__))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700723
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000724 def __call__(self, *args, **kwargs):
725 if not self._inst:
726 raise TypeError(f"Type {self._name} cannot be instantiated; "
727 f"use {self._name.lower()}() instead")
728 result = self.__origin__(*args, **kwargs)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700729 try:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000730 result.__orig_class__ = self
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700731 except AttributeError:
732 pass
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000733 return result
734
735 def __mro_entries__(self, bases):
736 if self._name: # generic version of an ABC or built-in class
737 res = []
738 if self.__origin__ not in bases:
739 res.append(self.__origin__)
740 i = bases.index(self)
741 if not any(isinstance(b, _GenericAlias) or issubclass(b, Generic)
742 for b in bases[i+1:]):
743 res.append(Generic)
744 return tuple(res)
745 if self.__origin__ is Generic:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100746 if Protocol in bases:
747 return ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000748 i = bases.index(self)
749 for b in bases[i+1:]:
750 if isinstance(b, _GenericAlias) and b is not self:
751 return ()
752 return (self.__origin__,)
753
754 def __getattr__(self, attr):
Ville Skyttä61f82e02018-04-20 23:08:45 +0300755 # We are careful for copy and pickle.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000756 # Also for simplicity we just don't relay all dunder names
757 if '__origin__' in self.__dict__ and not _is_dunder(attr):
758 return getattr(self.__origin__, attr)
759 raise AttributeError(attr)
760
761 def __setattr__(self, attr, val):
762 if _is_dunder(attr) or attr in ('_name', '_inst', '_special'):
763 super().__setattr__(attr, val)
764 else:
765 setattr(self.__origin__, attr, val)
766
767 def __instancecheck__(self, obj):
768 return self.__subclasscheck__(type(obj))
769
770 def __subclasscheck__(self, cls):
771 if self._special:
772 if not isinstance(cls, _GenericAlias):
773 return issubclass(cls, self.__origin__)
774 if cls._special:
775 return issubclass(cls.__origin__, self.__origin__)
776 raise TypeError("Subscripted generics cannot be used with"
777 " class and instance checks")
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700778
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100779 def __reduce__(self):
780 if self._special:
781 return self._name
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300782
783 if self._name:
784 origin = globals()[self._name]
785 else:
786 origin = self.__origin__
787 if (origin is Callable and
788 not (len(self.__args__) == 2 and self.__args__[0] is Ellipsis)):
789 args = list(self.__args__[:-1]), self.__args__[-1]
790 else:
791 args = tuple(self.__args__)
792 if len(args) == 1 and not isinstance(args[0], tuple):
793 args, = args
794 return operator.getitem, (origin, args)
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100795
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700796
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000797class _VariadicGenericAlias(_GenericAlias, _root=True):
798 """Same as _GenericAlias above but for variadic aliases. Currently,
799 this is used only by special internal aliases: Tuple and Callable.
800 """
801 def __getitem__(self, params):
802 if self._name != 'Callable' or not self._special:
803 return self.__getitem_inner__(params)
804 if not isinstance(params, tuple) or len(params) != 2:
805 raise TypeError("Callable must be used as "
806 "Callable[[arg, ...], result].")
807 args, result = params
808 if args is Ellipsis:
809 params = (Ellipsis, result)
810 else:
811 if not isinstance(args, list):
812 raise TypeError(f"Callable[args, result]: args must be a list."
813 f" Got {args}")
814 params = (tuple(args), result)
815 return self.__getitem_inner__(params)
816
817 @_tp_cache
818 def __getitem_inner__(self, params):
819 if self.__origin__ is tuple and self._special:
820 if params == ():
821 return self.copy_with((_TypingEmpty,))
822 if not isinstance(params, tuple):
823 params = (params,)
824 if len(params) == 2 and params[1] is ...:
825 msg = "Tuple[t, ...]: t must be a type."
826 p = _type_check(params[0], msg)
827 return self.copy_with((p, _TypingEllipsis))
828 msg = "Tuple[t0, t1, ...]: each t must be a type."
829 params = tuple(_type_check(p, msg) for p in params)
830 return self.copy_with(params)
831 if self.__origin__ is collections.abc.Callable and self._special:
832 args, result = params
833 msg = "Callable[args, result]: result must be a type."
834 result = _type_check(result, msg)
835 if args is Ellipsis:
836 return self.copy_with((_TypingEllipsis, result))
837 msg = "Callable[[arg, ...], result]: each arg must be a type."
838 args = tuple(_type_check(arg, msg) for arg in args)
839 params = args + (result,)
840 return self.copy_with(params)
841 return super().__getitem__(params)
842
843
844class Generic:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700845 """Abstract base class for generic types.
846
Guido van Rossumb24569a2016-11-20 18:01:29 -0800847 A generic type is typically declared by inheriting from
848 this class parameterized with one or more type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700849 For example, a generic mapping type might be defined as::
850
851 class Mapping(Generic[KT, VT]):
852 def __getitem__(self, key: KT) -> VT:
853 ...
854 # Etc.
855
856 This class can then be used as follows::
857
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700858 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700859 try:
860 return mapping[key]
861 except KeyError:
862 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700863 """
Guido van Rossumd70fe632015-08-05 12:11:06 +0200864 __slots__ = ()
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100865 _is_protocol = False
Guido van Rossumd70fe632015-08-05 12:11:06 +0200866
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700867 def __new__(cls, *args, **kwds):
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100868 if cls in (Generic, Protocol):
869 raise TypeError(f"Type {cls.__name__} cannot be instantiated; "
Guido van Rossum62fe1bb2016-10-29 16:05:26 -0700870 "it can be used only as a base class")
Ivan Levkivskyib551e9f2018-05-10 23:10:10 -0400871 if super().__new__ is object.__new__ and cls.__init__ is not object.__init__:
Ivan Levkivskyi43d12a62018-05-09 02:23:46 +0100872 obj = super().__new__(cls)
873 else:
874 obj = super().__new__(cls, *args, **kwds)
875 return obj
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000876
877 @_tp_cache
878 def __class_getitem__(cls, params):
879 if not isinstance(params, tuple):
880 params = (params,)
881 if not params and cls is not Tuple:
882 raise TypeError(
883 f"Parameter list to {cls.__qualname__}[...] cannot be empty")
884 msg = "Parameters to generic types must be types."
885 params = tuple(_type_check(p, msg) for p in params)
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100886 if cls in (Generic, Protocol):
887 # Generic and Protocol can only be subscripted with unique type variables.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000888 if not all(isinstance(p, TypeVar) for p in params):
889 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100890 f"Parameters to {cls.__name__}[...] must all be type variables")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000891 if len(set(params)) != len(params):
892 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100893 f"Parameters to {cls.__name__}[...] must all be unique")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000894 else:
895 # Subscripting a regular Generic subclass.
896 _check_generic(cls, params)
897 return _GenericAlias(cls, params)
898
899 def __init_subclass__(cls, *args, **kwargs):
Ivan Levkivskyiee566fe2018-04-04 17:00:15 +0100900 super().__init_subclass__(*args, **kwargs)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000901 tvars = []
902 if '__orig_bases__' in cls.__dict__:
903 error = Generic in cls.__orig_bases__
904 else:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100905 error = Generic in cls.__bases__ and cls.__name__ != 'Protocol'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000906 if error:
907 raise TypeError("Cannot inherit from plain Generic")
908 if '__orig_bases__' in cls.__dict__:
909 tvars = _collect_type_vars(cls.__orig_bases__)
910 # Look for Generic[T1, ..., Tn].
911 # If found, tvars must be a subset of it.
912 # If not found, tvars is it.
913 # Also check for and reject plain Generic,
914 # and reject multiple Generic[...].
915 gvars = None
916 for base in cls.__orig_bases__:
917 if (isinstance(base, _GenericAlias) and
918 base.__origin__ is Generic):
919 if gvars is not None:
920 raise TypeError(
921 "Cannot inherit from Generic[...] multiple types.")
922 gvars = base.__parameters__
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100923 if gvars is not None:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000924 tvarset = set(tvars)
925 gvarset = set(gvars)
926 if not tvarset <= gvarset:
927 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
928 s_args = ', '.join(str(g) for g in gvars)
929 raise TypeError(f"Some type variables ({s_vars}) are"
930 f" not listed in Generic[{s_args}]")
931 tvars = gvars
932 cls.__parameters__ = tuple(tvars)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700933
934
935class _TypingEmpty:
Guido van Rossumb24569a2016-11-20 18:01:29 -0800936 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
937 to allow empty list/tuple in specific places, without allowing them
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700938 to sneak in where prohibited.
939 """
940
941
942class _TypingEllipsis:
Guido van Rossumb24569a2016-11-20 18:01:29 -0800943 """Internal placeholder for ... (ellipsis)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700944
945
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100946_TYPING_INTERNALS = ['__parameters__', '__orig_bases__', '__orig_class__',
947 '_is_protocol', '_is_runtime_protocol']
948
949_SPECIAL_NAMES = ['__abstractmethods__', '__annotations__', '__dict__', '__doc__',
950 '__init__', '__module__', '__new__', '__slots__',
Guido van Rossum48b069a2020-04-07 09:50:06 -0700951 '__subclasshook__', '__weakref__', '__class_getitem__']
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100952
953# These special attributes will be not collected as protocol members.
954EXCLUDED_ATTRIBUTES = _TYPING_INTERNALS + _SPECIAL_NAMES + ['_MutableMapping__marker']
955
956
957def _get_protocol_attrs(cls):
958 """Collect protocol members from a protocol class objects.
959
960 This includes names actually defined in the class dictionary, as well
961 as names that appear in annotations. Special names (above) are skipped.
962 """
963 attrs = set()
964 for base in cls.__mro__[:-1]: # without object
965 if base.__name__ in ('Protocol', 'Generic'):
966 continue
967 annotations = getattr(base, '__annotations__', {})
968 for attr in list(base.__dict__.keys()) + list(annotations.keys()):
969 if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
970 attrs.add(attr)
971 return attrs
972
973
974def _is_callable_members_only(cls):
975 # PEP 544 prohibits using issubclass() with protocols that have non-method members.
976 return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls))
977
978
979def _no_init(self, *args, **kwargs):
980 if type(self)._is_protocol:
981 raise TypeError('Protocols cannot be instantiated')
982
983
984def _allow_reckless_class_cheks():
985 """Allow instnance and class checks for special stdlib modules.
986
987 The abc and functools modules indiscriminately call isinstance() and
988 issubclass() on the whole MRO of a user class, which may contain protocols.
989 """
990 try:
991 return sys._getframe(3).f_globals['__name__'] in ['abc', 'functools']
992 except (AttributeError, ValueError): # For platforms without _getframe().
993 return True
994
995
Divij Rajkumar692a0dc2019-09-12 11:13:51 +0100996_PROTO_WHITELIST = {
997 'collections.abc': [
998 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
999 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
1000 ],
1001 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
1002}
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001003
1004
1005class _ProtocolMeta(ABCMeta):
1006 # This metaclass is really unfortunate and exists only because of
1007 # the lack of __instancehook__.
1008 def __instancecheck__(cls, instance):
1009 # We need this method for situations where attributes are
1010 # assigned in __init__.
1011 if ((not getattr(cls, '_is_protocol', False) or
1012 _is_callable_members_only(cls)) and
1013 issubclass(instance.__class__, cls)):
1014 return True
1015 if cls._is_protocol:
1016 if all(hasattr(instance, attr) and
1017 # All *methods* can be blocked by setting them to None.
1018 (not callable(getattr(cls, attr, None)) or
1019 getattr(instance, attr) is not None)
1020 for attr in _get_protocol_attrs(cls)):
1021 return True
1022 return super().__instancecheck__(instance)
1023
1024
1025class Protocol(Generic, metaclass=_ProtocolMeta):
1026 """Base class for protocol classes.
1027
1028 Protocol classes are defined as::
1029
1030 class Proto(Protocol):
1031 def meth(self) -> int:
1032 ...
1033
1034 Such classes are primarily used with static type checkers that recognize
1035 structural subtyping (static duck-typing), for example::
1036
1037 class C:
1038 def meth(self) -> int:
1039 return 0
1040
1041 def func(x: Proto) -> int:
1042 return x.meth()
1043
1044 func(C()) # Passes static type check
1045
1046 See PEP 544 for details. Protocol classes decorated with
1047 @typing.runtime_checkable act as simple-minded runtime protocols that check
1048 only the presence of given attributes, ignoring their type signatures.
1049 Protocol classes can be generic, they are defined as::
1050
1051 class GenProto(Protocol[T]):
1052 def meth(self) -> T:
1053 ...
1054 """
1055 __slots__ = ()
1056 _is_protocol = True
1057 _is_runtime_protocol = False
1058
1059 def __init_subclass__(cls, *args, **kwargs):
1060 super().__init_subclass__(*args, **kwargs)
1061
1062 # Determine if this is a protocol or a concrete subclass.
1063 if not cls.__dict__.get('_is_protocol', False):
1064 cls._is_protocol = any(b is Protocol for b in cls.__bases__)
1065
1066 # Set (or override) the protocol subclass hook.
1067 def _proto_hook(other):
1068 if not cls.__dict__.get('_is_protocol', False):
1069 return NotImplemented
1070
1071 # First, perform various sanity checks.
1072 if not getattr(cls, '_is_runtime_protocol', False):
1073 if _allow_reckless_class_cheks():
1074 return NotImplemented
1075 raise TypeError("Instance and class checks can only be used with"
1076 " @runtime_checkable protocols")
1077 if not _is_callable_members_only(cls):
1078 if _allow_reckless_class_cheks():
1079 return NotImplemented
1080 raise TypeError("Protocols with non-method members"
1081 " don't support issubclass()")
1082 if not isinstance(other, type):
1083 # Same error message as for issubclass(1, int).
1084 raise TypeError('issubclass() arg 1 must be a class')
1085
1086 # Second, perform the actual structural compatibility check.
1087 for attr in _get_protocol_attrs(cls):
1088 for base in other.__mro__:
1089 # Check if the members appears in the class dictionary...
1090 if attr in base.__dict__:
1091 if base.__dict__[attr] is None:
1092 return NotImplemented
1093 break
1094
1095 # ...or in annotations, if it is a sub-protocol.
1096 annotations = getattr(base, '__annotations__', {})
1097 if (isinstance(annotations, collections.abc.Mapping) and
1098 attr in annotations and
1099 issubclass(other, Generic) and other._is_protocol):
1100 break
1101 else:
1102 return NotImplemented
1103 return True
1104
1105 if '__subclasshook__' not in cls.__dict__:
1106 cls.__subclasshook__ = _proto_hook
1107
1108 # We have nothing more to do for non-protocols...
1109 if not cls._is_protocol:
1110 return
1111
1112 # ... otherwise check consistency of bases, and prohibit instantiation.
1113 for base in cls.__bases__:
1114 if not (base in (object, Generic) or
Divij Rajkumar692a0dc2019-09-12 11:13:51 +01001115 base.__module__ in _PROTO_WHITELIST and
1116 base.__name__ in _PROTO_WHITELIST[base.__module__] or
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001117 issubclass(base, Generic) and base._is_protocol):
1118 raise TypeError('Protocols can only inherit from other'
1119 ' protocols, got %r' % base)
1120 cls.__init__ = _no_init
1121
1122
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001123class _AnnotatedAlias(_GenericAlias, _root=True):
1124 """Runtime representation of an annotated type.
1125
1126 At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't'
1127 with extra annotations. The alias behaves like a normal typing alias,
1128 instantiating is the same as instantiating the underlying type, binding
1129 it to types is also the same.
1130 """
1131 def __init__(self, origin, metadata):
1132 if isinstance(origin, _AnnotatedAlias):
1133 metadata = origin.__metadata__ + metadata
1134 origin = origin.__origin__
1135 super().__init__(origin, origin)
1136 self.__metadata__ = metadata
1137
1138 def copy_with(self, params):
1139 assert len(params) == 1
1140 new_type = params[0]
1141 return _AnnotatedAlias(new_type, self.__metadata__)
1142
1143 def __repr__(self):
1144 return "typing.Annotated[{}, {}]".format(
1145 _type_repr(self.__origin__),
1146 ", ".join(repr(a) for a in self.__metadata__)
1147 )
1148
1149 def __reduce__(self):
1150 return operator.getitem, (
1151 Annotated, (self.__origin__,) + self.__metadata__
1152 )
1153
1154 def __eq__(self, other):
1155 if not isinstance(other, _AnnotatedAlias):
1156 return NotImplemented
1157 if self.__origin__ != other.__origin__:
1158 return False
1159 return self.__metadata__ == other.__metadata__
1160
1161 def __hash__(self):
1162 return hash((self.__origin__, self.__metadata__))
1163
1164
1165class Annotated:
1166 """Add context specific metadata to a type.
1167
1168 Example: Annotated[int, runtime_check.Unsigned] indicates to the
1169 hypothetical runtime_check module that this type is an unsigned int.
1170 Every other consumer of this type can ignore this metadata and treat
1171 this type as int.
1172
1173 The first argument to Annotated must be a valid type.
1174
1175 Details:
1176
1177 - It's an error to call `Annotated` with less than two arguments.
1178 - Nested Annotated are flattened::
1179
1180 Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
1181
1182 - Instantiating an annotated type is equivalent to instantiating the
1183 underlying type::
1184
1185 Annotated[C, Ann1](5) == C(5)
1186
1187 - Annotated can be used as a generic type alias::
1188
1189 Optimized = Annotated[T, runtime.Optimize()]
1190 Optimized[int] == Annotated[int, runtime.Optimize()]
1191
1192 OptimizedList = Annotated[List[T], runtime.Optimize()]
1193 OptimizedList[int] == Annotated[List[int], runtime.Optimize()]
1194 """
1195
1196 __slots__ = ()
1197
1198 def __new__(cls, *args, **kwargs):
1199 raise TypeError("Type Annotated cannot be instantiated.")
1200
1201 @_tp_cache
1202 def __class_getitem__(cls, params):
1203 if not isinstance(params, tuple) or len(params) < 2:
1204 raise TypeError("Annotated[...] should be used "
1205 "with at least two arguments (a type and an "
1206 "annotation).")
1207 msg = "Annotated[t, ...]: t must be a type."
1208 origin = _type_check(params[0], msg)
1209 metadata = tuple(params[1:])
1210 return _AnnotatedAlias(origin, metadata)
1211
1212 def __init_subclass__(cls, *args, **kwargs):
1213 raise TypeError(
1214 "Cannot subclass {}.Annotated".format(cls.__module__)
1215 )
1216
1217
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001218def runtime_checkable(cls):
1219 """Mark a protocol class as a runtime protocol.
1220
1221 Such protocol can be used with isinstance() and issubclass().
1222 Raise TypeError if applied to a non-protocol class.
1223 This allows a simple-minded structural check very similar to
1224 one trick ponies in collections.abc such as Iterable.
1225 For example::
1226
1227 @runtime_checkable
1228 class Closable(Protocol):
1229 def close(self): ...
1230
1231 assert isinstance(open('/some/file'), Closable)
1232
1233 Warning: this will check only the presence of the required methods,
1234 not their type signatures!
1235 """
1236 if not issubclass(cls, Generic) or not cls._is_protocol:
1237 raise TypeError('@runtime_checkable can be only applied to protocol classes,'
1238 ' got %r' % cls)
1239 cls._is_runtime_protocol = True
1240 return cls
1241
1242
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001243def cast(typ, val):
1244 """Cast a value to a type.
1245
1246 This returns the value unchanged. To the type checker this
1247 signals that the return value has the designated type, but at
1248 runtime we intentionally don't check anything (we want this
1249 to be as fast as possible).
1250 """
1251 return val
1252
1253
1254def _get_defaults(func):
1255 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -08001256 try:
1257 code = func.__code__
1258 except AttributeError:
1259 # Some built-in functions don't have __code__, __defaults__, etc.
1260 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001261 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001262 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001263 arg_names = arg_names[:pos_count]
1264 defaults = func.__defaults__ or ()
1265 kwdefaults = func.__kwdefaults__
1266 res = dict(kwdefaults) if kwdefaults else {}
1267 pos_offset = pos_count - len(defaults)
1268 for name, value in zip(arg_names[pos_offset:], defaults):
1269 assert name not in res
1270 res[name] = value
1271 return res
1272
1273
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001274_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
1275 types.MethodType, types.ModuleType,
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02001276 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001277
1278
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001279def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
Guido van Rossum991d14f2016-11-09 13:12:51 -08001280 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001281
Guido van Rossum991d14f2016-11-09 13:12:51 -08001282 This is often the same as obj.__annotations__, but it handles
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001283 forward references encoded as string literals, adds Optional[t] if a
1284 default value equal to None is set and recursively replaces all
1285 'Annotated[T, ...]' with 'T' (unless 'include_extras=True').
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001286
Guido van Rossum991d14f2016-11-09 13:12:51 -08001287 The argument may be a module, class, method, or function. The annotations
1288 are returned as a dictionary. For classes, annotations include also
1289 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001290
Guido van Rossum991d14f2016-11-09 13:12:51 -08001291 TypeError is raised if the argument is not of a type that can contain
1292 annotations, and an empty dictionary is returned if no annotations are
1293 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001294
Guido van Rossum991d14f2016-11-09 13:12:51 -08001295 BEWARE -- the behavior of globalns and localns is counterintuitive
1296 (unless you are familiar with how eval() and exec() work). The
1297 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001298
Guido van Rossum991d14f2016-11-09 13:12:51 -08001299 - If no dict arguments are passed, an attempt is made to use the
Łukasz Langaf350a262017-09-14 14:33:00 -04001300 globals from obj (or the respective module's globals for classes),
1301 and these are also used as the locals. If the object does not appear
1302 to have globals, an empty dictionary is used.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001303
Guido van Rossum991d14f2016-11-09 13:12:51 -08001304 - If one dict argument is passed, it is used for both globals and
1305 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001306
Guido van Rossum991d14f2016-11-09 13:12:51 -08001307 - If two dict arguments are passed, they specify globals and
1308 locals, respectively.
1309 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001310
Guido van Rossum991d14f2016-11-09 13:12:51 -08001311 if getattr(obj, '__no_type_check__', None):
1312 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001313 # Classes require a special treatment.
1314 if isinstance(obj, type):
1315 hints = {}
1316 for base in reversed(obj.__mro__):
Łukasz Langaf350a262017-09-14 14:33:00 -04001317 if globalns is None:
1318 base_globals = sys.modules[base.__module__].__dict__
1319 else:
1320 base_globals = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001321 ann = base.__dict__.get('__annotations__', {})
1322 for name, value in ann.items():
1323 if value is None:
1324 value = type(None)
1325 if isinstance(value, str):
Nina Zakharenko0e61dff2018-05-22 20:32:10 -07001326 value = ForwardRef(value, is_argument=False)
Łukasz Langaf350a262017-09-14 14:33:00 -04001327 value = _eval_type(value, base_globals, localns)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001328 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001329 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
Łukasz Langaf350a262017-09-14 14:33:00 -04001330
1331 if globalns is None:
1332 if isinstance(obj, types.ModuleType):
1333 globalns = obj.__dict__
1334 else:
benedwards140aca3a32019-11-21 17:24:58 +00001335 nsobj = obj
1336 # Find globalns for the unwrapped object.
1337 while hasattr(nsobj, '__wrapped__'):
1338 nsobj = nsobj.__wrapped__
1339 globalns = getattr(nsobj, '__globals__', {})
Łukasz Langaf350a262017-09-14 14:33:00 -04001340 if localns is None:
1341 localns = globalns
1342 elif localns is None:
1343 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001344 hints = getattr(obj, '__annotations__', None)
1345 if hints is None:
1346 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001347 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001348 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001349 else:
1350 raise TypeError('{!r} is not a module, class, method, '
1351 'or function.'.format(obj))
1352 defaults = _get_defaults(obj)
1353 hints = dict(hints)
1354 for name, value in hints.items():
1355 if value is None:
1356 value = type(None)
1357 if isinstance(value, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001358 value = ForwardRef(value)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001359 value = _eval_type(value, globalns, localns)
1360 if name in defaults and defaults[name] is None:
1361 value = Optional[value]
1362 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001363 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
1364
1365
1366def _strip_annotations(t):
1367 """Strips the annotations from a given type.
1368 """
1369 if isinstance(t, _AnnotatedAlias):
1370 return _strip_annotations(t.__origin__)
1371 if isinstance(t, _GenericAlias):
1372 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1373 if stripped_args == t.__args__:
1374 return t
1375 res = t.copy_with(stripped_args)
1376 res._special = t._special
1377 return res
1378 return t
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001379
1380
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001381def get_origin(tp):
1382 """Get the unsubscripted version of a type.
1383
Jakub Stasiak38aaaaa2020-02-07 02:15:12 +01001384 This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
1385 and Annotated. Return None for unsupported types. Examples::
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001386
1387 get_origin(Literal[42]) is Literal
1388 get_origin(int) is None
1389 get_origin(ClassVar[int]) is ClassVar
1390 get_origin(Generic) is Generic
1391 get_origin(Generic[T]) is Generic
1392 get_origin(Union[T, int]) is Union
1393 get_origin(List[Tuple[T, T]][int]) == list
1394 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001395 if isinstance(tp, _AnnotatedAlias):
1396 return Annotated
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001397 if isinstance(tp, _GenericAlias):
1398 return tp.__origin__
1399 if tp is Generic:
1400 return Generic
1401 return None
1402
1403
1404def get_args(tp):
1405 """Get type arguments with all substitutions performed.
1406
1407 For unions, basic simplifications used by Union constructor are performed.
1408 Examples::
1409 get_args(Dict[str, int]) == (str, int)
1410 get_args(int) == ()
1411 get_args(Union[int, Union[T, int], str][int]) == (int, str)
1412 get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
1413 get_args(Callable[[], T][int]) == ([], int)
1414 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001415 if isinstance(tp, _AnnotatedAlias):
1416 return (tp.__origin__,) + tp.__metadata__
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001417 if isinstance(tp, _GenericAlias):
1418 res = tp.__args__
1419 if get_origin(tp) is collections.abc.Callable and res[0] is not Ellipsis:
1420 res = (list(res[:-1]), res[-1])
1421 return res
1422 return ()
1423
1424
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001425def no_type_check(arg):
1426 """Decorator to indicate that annotations are not type hints.
1427
1428 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001429 applies recursively to all methods and classes defined in that class
1430 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001431
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001432 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001433 """
1434 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001435 arg_attrs = arg.__dict__.copy()
1436 for attr, val in arg.__dict__.items():
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001437 if val in arg.__bases__ + (arg,):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001438 arg_attrs.pop(attr)
1439 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001440 if isinstance(obj, types.FunctionType):
1441 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001442 if isinstance(obj, type):
1443 no_type_check(obj)
1444 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001445 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001446 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001447 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001448 return arg
1449
1450
1451def no_type_check_decorator(decorator):
1452 """Decorator to give another decorator the @no_type_check effect.
1453
1454 This wraps the decorator with something that wraps the decorated
1455 function in @no_type_check.
1456 """
1457
1458 @functools.wraps(decorator)
1459 def wrapped_decorator(*args, **kwds):
1460 func = decorator(*args, **kwds)
1461 func = no_type_check(func)
1462 return func
1463
1464 return wrapped_decorator
1465
1466
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001467def _overload_dummy(*args, **kwds):
1468 """Helper for @overload to raise when called."""
1469 raise NotImplementedError(
1470 "You should not call an overloaded function. "
1471 "A series of @overload-decorated functions "
1472 "outside a stub module should always be followed "
1473 "by an implementation that is not @overload-ed.")
1474
1475
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001476def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001477 """Decorator for overloaded functions/methods.
1478
1479 In a stub file, place two or more stub definitions for the same
1480 function in a row, each decorated with @overload. For example:
1481
1482 @overload
1483 def utf8(value: None) -> None: ...
1484 @overload
1485 def utf8(value: bytes) -> bytes: ...
1486 @overload
1487 def utf8(value: str) -> bytes: ...
1488
1489 In a non-stub file (i.e. a regular .py file), do the same but
1490 follow it with an implementation. The implementation should *not*
1491 be decorated with @overload. For example:
1492
1493 @overload
1494 def utf8(value: None) -> None: ...
1495 @overload
1496 def utf8(value: bytes) -> bytes: ...
1497 @overload
1498 def utf8(value: str) -> bytes: ...
1499 def utf8(value):
1500 # implementation goes here
1501 """
1502 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001503
1504
Ivan Levkivskyif3672422019-05-26 09:37:07 +01001505def final(f):
1506 """A decorator to indicate final methods and final classes.
1507
1508 Use this decorator to indicate to type checkers that the decorated
1509 method cannot be overridden, and decorated class cannot be subclassed.
1510 For example:
1511
1512 class Base:
1513 @final
1514 def done(self) -> None:
1515 ...
1516 class Sub(Base):
1517 def done(self) -> None: # Error reported by type checker
1518 ...
1519
1520 @final
1521 class Leaf:
1522 ...
1523 class Other(Leaf): # Error reported by type checker
1524 ...
1525
1526 There is no runtime checking of these properties.
1527 """
1528 return f
1529
1530
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001531# Some unconstrained type variables. These are used by the container types.
1532# (These are not for export.)
1533T = TypeVar('T') # Any type.
1534KT = TypeVar('KT') # Key type.
1535VT = TypeVar('VT') # Value type.
1536T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
1537V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
1538VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
1539T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
1540# Internal type variable used for Type[].
1541CT_co = TypeVar('CT_co', covariant=True, bound=type)
1542
1543# A useful type variable with constraints. This represents string types.
1544# (This one *is* for export!)
1545AnyStr = TypeVar('AnyStr', bytes, str)
1546
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001547
1548# Various ABCs mimicking those in collections.abc.
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001549def _alias(origin, params, inst=True):
1550 return _GenericAlias(origin, params, special=True, inst=inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001551
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001552Hashable = _alias(collections.abc.Hashable, ()) # Not generic.
1553Awaitable = _alias(collections.abc.Awaitable, T_co)
1554Coroutine = _alias(collections.abc.Coroutine, (T_co, T_contra, V_co))
1555AsyncIterable = _alias(collections.abc.AsyncIterable, T_co)
1556AsyncIterator = _alias(collections.abc.AsyncIterator, T_co)
1557Iterable = _alias(collections.abc.Iterable, T_co)
1558Iterator = _alias(collections.abc.Iterator, T_co)
1559Reversible = _alias(collections.abc.Reversible, T_co)
1560Sized = _alias(collections.abc.Sized, ()) # Not generic.
1561Container = _alias(collections.abc.Container, T_co)
1562Collection = _alias(collections.abc.Collection, T_co)
1563Callable = _VariadicGenericAlias(collections.abc.Callable, (), special=True)
1564Callable.__doc__ = \
1565 """Callable type; Callable[[int], str] is a function of (int) -> str.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001566
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001567 The subscription syntax must always be used with exactly two
1568 values: the argument list and the return type. The argument list
1569 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001570
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001571 There is no syntax to indicate optional or keyword arguments,
1572 such function types are rarely used as callback types.
1573 """
1574AbstractSet = _alias(collections.abc.Set, T_co)
1575MutableSet = _alias(collections.abc.MutableSet, T)
1576# NOTE: Mapping is only covariant in the value type.
1577Mapping = _alias(collections.abc.Mapping, (KT, VT_co))
1578MutableMapping = _alias(collections.abc.MutableMapping, (KT, VT))
1579Sequence = _alias(collections.abc.Sequence, T_co)
1580MutableSequence = _alias(collections.abc.MutableSequence, T)
1581ByteString = _alias(collections.abc.ByteString, ()) # Not generic
1582Tuple = _VariadicGenericAlias(tuple, (), inst=False, special=True)
1583Tuple.__doc__ = \
1584 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001585
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001586 Example: Tuple[T1, T2] is a tuple of two elements corresponding
1587 to type variables T1 and T2. Tuple[int, float, str] is a tuple
1588 of an int, a float and a string.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001589
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001590 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
1591 """
1592List = _alias(list, T, inst=False)
1593Deque = _alias(collections.deque, T)
1594Set = _alias(set, T, inst=False)
1595FrozenSet = _alias(frozenset, T_co, inst=False)
1596MappingView = _alias(collections.abc.MappingView, T_co)
1597KeysView = _alias(collections.abc.KeysView, KT)
1598ItemsView = _alias(collections.abc.ItemsView, (KT, VT_co))
1599ValuesView = _alias(collections.abc.ValuesView, VT_co)
1600ContextManager = _alias(contextlib.AbstractContextManager, T_co)
1601AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, T_co)
1602Dict = _alias(dict, (KT, VT), inst=False)
1603DefaultDict = _alias(collections.defaultdict, (KT, VT))
Ismo Toijala68b56d02018-12-02 17:53:14 +02001604OrderedDict = _alias(collections.OrderedDict, (KT, VT))
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001605Counter = _alias(collections.Counter, T)
1606ChainMap = _alias(collections.ChainMap, (KT, VT))
1607Generator = _alias(collections.abc.Generator, (T_co, T_contra, V_co))
1608AsyncGenerator = _alias(collections.abc.AsyncGenerator, (T_co, T_contra))
1609Type = _alias(type, CT_co, inst=False)
1610Type.__doc__ = \
1611 """A special construct usable to annotate class objects.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001612
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001613 For example, suppose we have the following classes::
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001614
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001615 class User: ... # Abstract base for User classes
1616 class BasicUser(User): ...
1617 class ProUser(User): ...
1618 class TeamUser(User): ...
Guido van Rossumf17c2002015-12-03 17:31:24 -08001619
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001620 And a function that takes a class argument that's a subclass of
1621 User and returns an instance of the corresponding class::
Guido van Rossumf17c2002015-12-03 17:31:24 -08001622
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001623 U = TypeVar('U', bound=User)
1624 def new_user(user_class: Type[U]) -> U:
1625 user = user_class()
1626 # (Here we could write the user object to a database)
1627 return user
Guido van Rossumf17c2002015-12-03 17:31:24 -08001628
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001629 joe = new_user(BasicUser)
Guido van Rossumf17c2002015-12-03 17:31:24 -08001630
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001631 At this point the type checker knows that joe has type BasicUser.
1632 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001633
1634
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001635@runtime_checkable
1636class SupportsInt(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001637 """An ABC with one abstract method __int__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001638 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001639
1640 @abstractmethod
1641 def __int__(self) -> int:
1642 pass
1643
1644
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001645@runtime_checkable
1646class SupportsFloat(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001647 """An ABC with one abstract method __float__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001648 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001649
1650 @abstractmethod
1651 def __float__(self) -> float:
1652 pass
1653
1654
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001655@runtime_checkable
1656class SupportsComplex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001657 """An ABC with one abstract method __complex__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001658 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001659
1660 @abstractmethod
1661 def __complex__(self) -> complex:
1662 pass
1663
1664
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001665@runtime_checkable
1666class SupportsBytes(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001667 """An ABC with one abstract method __bytes__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001668 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001669
1670 @abstractmethod
1671 def __bytes__(self) -> bytes:
1672 pass
1673
1674
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001675@runtime_checkable
1676class SupportsIndex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001677 """An ABC with one abstract method __index__."""
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -07001678 __slots__ = ()
1679
1680 @abstractmethod
1681 def __index__(self) -> int:
1682 pass
1683
1684
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001685@runtime_checkable
1686class SupportsAbs(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001687 """An ABC with one abstract method __abs__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001688 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001689
1690 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001691 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001692 pass
1693
1694
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001695@runtime_checkable
1696class SupportsRound(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001697 """An ABC with one abstract method __round__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001698 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001699
1700 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001701 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001702 pass
1703
1704
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001705def _make_nmtuple(name, types, module, defaults = ()):
1706 fields = [n for n, t in types]
1707 types = {n: _type_check(t, f"field {n} annotation must be a type")
1708 for n, t in types}
1709 nm_tpl = collections.namedtuple(name, fields,
1710 defaults=defaults, module=module)
1711 nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001712 return nm_tpl
1713
1714
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001715# attributes prohibited to set in NamedTuple class syntax
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001716_prohibited = frozenset({'__new__', '__init__', '__slots__', '__getnewargs__',
1717 '_fields', '_field_defaults',
1718 '_make', '_replace', '_asdict', '_source'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001719
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001720_special = frozenset({'__module__', '__name__', '__annotations__'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001721
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001722
Guido van Rossum2f841442016-11-15 09:48:06 -08001723class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001724
Guido van Rossum2f841442016-11-15 09:48:06 -08001725 def __new__(cls, typename, bases, ns):
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001726 assert bases[0] is _NamedTuple
Guido van Rossum2f841442016-11-15 09:48:06 -08001727 types = ns.get('__annotations__', {})
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001728 default_names = []
Guido van Rossum3c268be2017-01-18 08:03:50 -08001729 for field_name in types:
1730 if field_name in ns:
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001731 default_names.append(field_name)
1732 elif default_names:
1733 raise TypeError(f"Non-default namedtuple field {field_name} "
1734 f"cannot follow default field"
1735 f"{'s' if len(default_names) > 1 else ''} "
1736 f"{', '.join(default_names)}")
1737 nm_tpl = _make_nmtuple(typename, types.items(),
1738 defaults=[ns[n] for n in default_names],
1739 module=ns['__module__'])
Guido van Rossum95919c02017-01-22 17:47:20 -08001740 # update from user namespace without overriding special namedtuple attributes
1741 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001742 if key in _prohibited:
1743 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
1744 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08001745 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08001746 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001747
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001748
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001749def NamedTuple(typename, fields=None, /, **kwargs):
Guido van Rossum2f841442016-11-15 09:48:06 -08001750 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001751
Guido van Rossum2f841442016-11-15 09:48:06 -08001752 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001753
Guido van Rossum2f841442016-11-15 09:48:06 -08001754 class Employee(NamedTuple):
1755 name: str
1756 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001757
Guido van Rossum2f841442016-11-15 09:48:06 -08001758 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001759
Guido van Rossum2f841442016-11-15 09:48:06 -08001760 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001761
Raymond Hettingerf7b57df2019-03-18 09:53:56 -07001762 The resulting class has an extra __annotations__ attribute, giving a
1763 dict that maps field names to types. (The field names are also in
1764 the _fields attribute, which is part of the namedtuple API.)
1765 Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001766
Guido van Rossum2f841442016-11-15 09:48:06 -08001767 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001768
Guido van Rossum2f841442016-11-15 09:48:06 -08001769 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001770
Guido van Rossum2f841442016-11-15 09:48:06 -08001771 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
1772 """
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001773 if fields is None:
1774 fields = kwargs.items()
1775 elif kwargs:
1776 raise TypeError("Either list of fields or keywords"
1777 " can be provided to NamedTuple, not both")
1778 try:
1779 module = sys._getframe(1).f_globals.get('__name__', '__main__')
1780 except (AttributeError, ValueError):
1781 module = None
1782 return _make_nmtuple(typename, fields, module=module)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001783
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001784_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})
1785
1786def _namedtuple_mro_entries(bases):
1787 if len(bases) > 1:
1788 raise TypeError("Multiple inheritance with NamedTuple is not supported")
1789 assert bases[0] is NamedTuple
1790 return (_NamedTuple,)
1791
1792NamedTuple.__mro_entries__ = _namedtuple_mro_entries
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001793
1794
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001795class _TypedDictMeta(type):
1796 def __new__(cls, name, bases, ns, total=True):
1797 """Create new typed dict class object.
1798
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001799 This method is called when TypedDict is subclassed,
1800 or when TypedDict is instantiated. This way
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001801 TypedDict supports all three syntax forms described in its docstring.
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001802 Subclasses and instances of TypedDict return actual dictionaries.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001803 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001804 for base in bases:
1805 if type(base) is not _TypedDictMeta:
1806 raise TypeError('cannot inherit from both a TypedDict type '
1807 'and a non-TypedDict base class')
1808 tp_dict = type.__new__(_TypedDictMeta, name, (dict,), ns)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001809
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001810 annotations = {}
1811 own_annotations = ns.get('__annotations__', {})
1812 own_annotation_keys = set(own_annotations.keys())
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001813 msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001814 own_annotations = {
1815 n: _type_check(tp, msg) for n, tp in own_annotations.items()
1816 }
1817 required_keys = set()
1818 optional_keys = set()
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11001819
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001820 for base in bases:
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001821 annotations.update(base.__dict__.get('__annotations__', {}))
1822 required_keys.update(base.__dict__.get('__required_keys__', ()))
1823 optional_keys.update(base.__dict__.get('__optional_keys__', ()))
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11001824
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001825 annotations.update(own_annotations)
1826 if total:
1827 required_keys.update(own_annotation_keys)
1828 else:
1829 optional_keys.update(own_annotation_keys)
1830
1831 tp_dict.__annotations__ = annotations
1832 tp_dict.__required_keys__ = frozenset(required_keys)
1833 tp_dict.__optional_keys__ = frozenset(optional_keys)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001834 if not hasattr(tp_dict, '__total__'):
1835 tp_dict.__total__ = total
1836 return tp_dict
1837
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001838 __call__ = dict # static method
1839
1840 def __subclasscheck__(cls, other):
1841 # Typed dicts are only for static structural subtyping.
1842 raise TypeError('TypedDict does not support instance and class checks')
1843
1844 __instancecheck__ = __subclasscheck__
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001845
1846
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001847def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001848 """A simple typed namespace. At runtime it is equivalent to a plain dict.
1849
1850 TypedDict creates a dictionary type that expects all of its
1851 instances to have a certain set of keys, where each key is
1852 associated with a value of a consistent type. This expectation
1853 is not checked at runtime but is only enforced by type checkers.
1854 Usage::
1855
1856 class Point2D(TypedDict):
1857 x: int
1858 y: int
1859 label: str
1860
1861 a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
1862 b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
1863
1864 assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
1865
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11001866 The type info can be accessed via the Point2D.__annotations__ dict, and
1867 the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
1868 TypedDict supports two additional equivalent forms::
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001869
1870 Point2D = TypedDict('Point2D', x=int, y=int, label=str)
1871 Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
1872
ananthan-123ab6423f2020-02-19 10:03:05 +05301873 By default, all keys must be present in a TypedDict. It is possible
1874 to override this by specifying totality.
1875 Usage::
1876
1877 class point2D(TypedDict, total=False):
1878 x: int
1879 y: int
1880
1881 This means that a point2D TypedDict can have any of the keys omitted.A type
1882 checker is only expected to support a literal False or True as the value of
1883 the total argument. True is the default, and makes all items defined in the
1884 class body be required.
1885
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001886 The class syntax is only supported in Python 3.6+, while two other
1887 syntax forms work for Python 2.7 and 3.2+
1888 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001889 if fields is None:
1890 fields = kwargs
1891 elif kwargs:
1892 raise TypeError("TypedDict takes either a dict or keyword arguments,"
1893 " but not both")
1894
1895 ns = {'__annotations__': dict(fields), '__total__': total}
1896 try:
1897 # Setting correct module is necessary to make typed dict classes pickleable.
1898 ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
1899 except (AttributeError, ValueError):
1900 pass
1901
1902 return _TypedDictMeta(typename, (), ns)
1903
1904_TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {})
1905TypedDict.__mro_entries__ = lambda bases: (_TypedDict,)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001906
1907
Guido van Rossum91185fe2016-06-08 11:19:11 -07001908def NewType(name, tp):
1909 """NewType creates simple unique types with almost zero
1910 runtime overhead. NewType(name, tp) is considered a subtype of tp
1911 by static type checkers. At runtime, NewType(name, tp) returns
1912 a dummy function that simply returns its argument. Usage::
1913
1914 UserId = NewType('UserId', int)
1915
1916 def name_by_id(user_id: UserId) -> str:
1917 ...
1918
1919 UserId('user') # Fails type check
1920
1921 name_by_id(42) # Fails type check
1922 name_by_id(UserId(42)) # OK
1923
1924 num = UserId(5) + 1 # type: int
1925 """
1926
1927 def new_type(x):
1928 return x
1929
1930 new_type.__name__ = name
1931 new_type.__supertype__ = tp
1932 return new_type
1933
1934
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001935# Python-version-specific alias (Python 2: unicode; Python 3: str)
1936Text = str
1937
1938
Guido van Rossum91185fe2016-06-08 11:19:11 -07001939# Constant that's True when type checking, but False here.
1940TYPE_CHECKING = False
1941
1942
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001943class IO(Generic[AnyStr]):
1944 """Generic base class for TextIO and BinaryIO.
1945
1946 This is an abstract, generic version of the return of open().
1947
1948 NOTE: This does not distinguish between the different possible
1949 classes (text vs. binary, read vs. write vs. read/write,
1950 append-only, unbuffered). The TextIO and BinaryIO subclasses
1951 below capture the distinctions between text vs. binary, which is
1952 pervasive in the interface; however we currently do not offer a
1953 way to track the other distinctions in the type system.
1954 """
1955
Guido van Rossumd70fe632015-08-05 12:11:06 +02001956 __slots__ = ()
1957
HongWeipeng6ce03ec2019-09-27 15:54:26 +08001958 @property
1959 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001960 def mode(self) -> str:
1961 pass
1962
HongWeipeng6ce03ec2019-09-27 15:54:26 +08001963 @property
1964 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001965 def name(self) -> str:
1966 pass
1967
1968 @abstractmethod
1969 def close(self) -> None:
1970 pass
1971
Shantanu2e6569b2020-01-29 18:52:36 -08001972 @property
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001973 @abstractmethod
1974 def closed(self) -> bool:
1975 pass
1976
1977 @abstractmethod
1978 def fileno(self) -> int:
1979 pass
1980
1981 @abstractmethod
1982 def flush(self) -> None:
1983 pass
1984
1985 @abstractmethod
1986 def isatty(self) -> bool:
1987 pass
1988
1989 @abstractmethod
1990 def read(self, n: int = -1) -> AnyStr:
1991 pass
1992
1993 @abstractmethod
1994 def readable(self) -> bool:
1995 pass
1996
1997 @abstractmethod
1998 def readline(self, limit: int = -1) -> AnyStr:
1999 pass
2000
2001 @abstractmethod
2002 def readlines(self, hint: int = -1) -> List[AnyStr]:
2003 pass
2004
2005 @abstractmethod
2006 def seek(self, offset: int, whence: int = 0) -> int:
2007 pass
2008
2009 @abstractmethod
2010 def seekable(self) -> bool:
2011 pass
2012
2013 @abstractmethod
2014 def tell(self) -> int:
2015 pass
2016
2017 @abstractmethod
2018 def truncate(self, size: int = None) -> int:
2019 pass
2020
2021 @abstractmethod
2022 def writable(self) -> bool:
2023 pass
2024
2025 @abstractmethod
2026 def write(self, s: AnyStr) -> int:
2027 pass
2028
2029 @abstractmethod
2030 def writelines(self, lines: List[AnyStr]) -> None:
2031 pass
2032
2033 @abstractmethod
2034 def __enter__(self) -> 'IO[AnyStr]':
2035 pass
2036
2037 @abstractmethod
2038 def __exit__(self, type, value, traceback) -> None:
2039 pass
2040
2041
2042class BinaryIO(IO[bytes]):
2043 """Typed version of the return of open() in binary mode."""
2044
Guido van Rossumd70fe632015-08-05 12:11:06 +02002045 __slots__ = ()
2046
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002047 @abstractmethod
2048 def write(self, s: Union[bytes, bytearray]) -> int:
2049 pass
2050
2051 @abstractmethod
2052 def __enter__(self) -> 'BinaryIO':
2053 pass
2054
2055
2056class TextIO(IO[str]):
2057 """Typed version of the return of open() in text mode."""
2058
Guido van Rossumd70fe632015-08-05 12:11:06 +02002059 __slots__ = ()
2060
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002061 @property
2062 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002063 def buffer(self) -> BinaryIO:
2064 pass
2065
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002066 @property
2067 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002068 def encoding(self) -> str:
2069 pass
2070
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002071 @property
2072 @abstractmethod
Guido van Rossum991d14f2016-11-09 13:12:51 -08002073 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002074 pass
2075
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002076 @property
2077 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002078 def line_buffering(self) -> bool:
2079 pass
2080
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002081 @property
2082 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002083 def newlines(self) -> Any:
2084 pass
2085
2086 @abstractmethod
2087 def __enter__(self) -> 'TextIO':
2088 pass
2089
2090
2091class io:
2092 """Wrapper namespace for IO generic classes."""
2093
2094 __all__ = ['IO', 'TextIO', 'BinaryIO']
2095 IO = IO
2096 TextIO = TextIO
2097 BinaryIO = BinaryIO
2098
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002099
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002100io.__name__ = __name__ + '.io'
2101sys.modules[io.__name__] = io
2102
Ivan Levkivskyid911e402018-01-20 11:23:59 +00002103Pattern = _alias(stdlib_re.Pattern, AnyStr)
2104Match = _alias(stdlib_re.Match, AnyStr)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002105
2106class re:
2107 """Wrapper namespace for re type aliases."""
2108
2109 __all__ = ['Pattern', 'Match']
2110 Pattern = Pattern
2111 Match = Match
2112
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002113
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002114re.__name__ = __name__ + '.re'
2115sys.modules[re.__name__] = re