blob: 3b4e9df0482edaada6ddb0cee9779e35b23a2dce [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.
16* Special types: NewType, NamedTuple, TypedDict (may be added soon).
17* Wrapper submodules for re and io related types.
18"""
19
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +010020from abc import abstractmethod, abstractproperty, 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
Ivan Levkivskyid911e402018-01-20 11:23:59 +000029from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070030
31# Please keep __all__ alphabetized within each category.
32__all__ = [
33 # Super-special typing primitives.
34 'Any',
35 'Callable',
Guido van Rossum0a6976d2016-09-11 15:34:56 -070036 'ClassVar',
Ivan Levkivskyif3672422019-05-26 09:37:07 +010037 'Final',
Anthony Sottiled30da5d2019-05-29 11:19:38 -070038 'ForwardRef',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070039 'Generic',
Ivan Levkivskyib891c462019-05-26 09:37:48 +010040 'Literal',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070041 'Optional',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +010042 'Protocol',
Guido van Rossumeb9aca32016-05-24 16:38:22 -070043 'Tuple',
44 'Type',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070045 'TypeVar',
46 'Union',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070047
48 # ABCs (from collections.abc).
49 'AbstractSet', # collections.abc.Set.
50 'ByteString',
51 'Container',
Ivan Levkivskyi29fda8d2017-06-10 21:57:56 +020052 'ContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070053 'Hashable',
54 'ItemsView',
55 'Iterable',
56 'Iterator',
57 'KeysView',
58 'Mapping',
59 'MappingView',
60 'MutableMapping',
61 'MutableSequence',
62 'MutableSet',
63 'Sequence',
64 'Sized',
65 'ValuesView',
Ivan Levkivskyid911e402018-01-20 11:23:59 +000066 'Awaitable',
67 'AsyncIterator',
68 'AsyncIterable',
69 'Coroutine',
70 'Collection',
71 'AsyncGenerator',
72 'AsyncContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070073
74 # Structural checks, a.k.a. protocols.
75 'Reversible',
76 'SupportsAbs',
Ivan Levkivskyif06e0212017-05-02 19:14:07 +020077 'SupportsBytes',
78 'SupportsComplex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070079 'SupportsFloat',
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -070080 'SupportsIndex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070081 'SupportsInt',
82 'SupportsRound',
83
84 # Concrete collection types.
Anthony Sottiled30da5d2019-05-29 11:19:38 -070085 'ChainMap',
Ivan Levkivskyib692dc82017-02-13 22:50:14 +010086 'Counter',
Raymond Hettinger80490522017-01-16 22:42:37 -080087 'Deque',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070088 'Dict',
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070089 'DefaultDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070090 'List',
Anthony Sottiled30da5d2019-05-29 11:19:38 -070091 'OrderedDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070092 'Set',
Guido van Rossumefa798d2016-08-23 11:01:50 -070093 'FrozenSet',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070094 'NamedTuple', # Not really a type.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +010095 'TypedDict', # Not really a type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070096 'Generator',
97
98 # One-off things.
99 'AnyStr',
100 'cast',
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100101 'final',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700102 'get_type_hints',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700103 'NewType',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700104 'no_type_check',
105 'no_type_check_decorator',
aetracht45738202018-03-19 14:41:32 -0400106 'NoReturn',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700107 'overload',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100108 'runtime_checkable',
Guido van Rossum0e0563c2016-04-05 14:54:25 -0700109 'Text',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700110 'TYPE_CHECKING',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700111]
112
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700113# The pseudo-submodules 're' and 'io' are part of the public
114# namespace, but excluded from __all__ because they might stomp on
115# legitimate imports of those modules.
116
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700117
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700118def _type_check(arg, msg, is_argument=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000119 """Check that the argument is a type, and return it (internal helper).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700120
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000121 As a special case, accept None and return type(None) instead. Also wrap strings
122 into ForwardRef instances. Consider several corner cases, for example plain
123 special forms like Union are not valid, while Union[int, str] is OK, etc.
124 The msg argument is a human-readable error message, e.g::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700125
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000126 "Union[arg, ...]: arg should be a type."
Guido van Rossum4cefe742016-09-27 15:20:12 -0700127
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000128 We append the repr() of the actual value (truncated to 100 chars).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700129 """
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100130 invalid_generic_forms = (Generic, Protocol)
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700131 if is_argument:
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100132 invalid_generic_forms = invalid_generic_forms + (ClassVar, Final)
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400133
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000134 if arg is None:
135 return type(None)
136 if isinstance(arg, str):
137 return ForwardRef(arg)
138 if (isinstance(arg, _GenericAlias) and
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400139 arg.__origin__ in invalid_generic_forms):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000140 raise TypeError(f"{arg} is not valid as type argument")
Noah Wood5eea0ad2018-10-08 14:50:16 -0400141 if (isinstance(arg, _SpecialForm) and arg not in (Any, NoReturn) or
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100142 arg in (Generic, Protocol)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000143 raise TypeError(f"Plain {arg} is not valid as type argument")
144 if isinstance(arg, (type, TypeVar, ForwardRef)):
145 return arg
146 if not callable(arg):
147 raise TypeError(f"{msg} Got {arg!r:.100}.")
148 return arg
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700149
150
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000151def _type_repr(obj):
152 """Return the repr() of an object, special-casing types (internal helper).
153
154 If obj is a type, we return a shorter version than the default
155 type.__repr__, based on the module and qualified name, which is
156 typically enough to uniquely identify a type. For everything
157 else, we fall back on repr(obj).
158 """
159 if isinstance(obj, type):
160 if obj.__module__ == 'builtins':
161 return obj.__qualname__
162 return f'{obj.__module__}.{obj.__qualname__}'
163 if obj is ...:
164 return('...')
165 if isinstance(obj, types.FunctionType):
166 return obj.__name__
167 return repr(obj)
168
169
170def _collect_type_vars(types):
171 """Collect all type variable contained in types in order of
172 first appearance (lexicographic order). For example::
173
174 _collect_type_vars((T, List[S, T])) == (T, S)
175 """
176 tvars = []
177 for t in types:
178 if isinstance(t, TypeVar) and t not in tvars:
179 tvars.append(t)
180 if isinstance(t, _GenericAlias) and not t._special:
181 tvars.extend([t for t in t.__parameters__ if t not in tvars])
182 return tuple(tvars)
183
184
185def _subs_tvars(tp, tvars, subs):
186 """Substitute type variables 'tvars' with substitutions 'subs'.
187 These two must have the same length.
188 """
189 if not isinstance(tp, _GenericAlias):
190 return tp
191 new_args = list(tp.__args__)
192 for a, arg in enumerate(tp.__args__):
193 if isinstance(arg, TypeVar):
194 for i, tvar in enumerate(tvars):
195 if arg == tvar:
196 new_args[a] = subs[i]
197 else:
198 new_args[a] = _subs_tvars(arg, tvars, subs)
199 if tp.__origin__ is Union:
200 return Union[tuple(new_args)]
201 return tp.copy_with(tuple(new_args))
202
203
204def _check_generic(cls, parameters):
205 """Check correct count for parameters of a generic cls (internal helper).
206 This gives a nice error message in case of count mismatch.
207 """
208 if not cls.__parameters__:
209 raise TypeError(f"{cls} is not a generic class")
210 alen = len(parameters)
211 elen = len(cls.__parameters__)
212 if alen != elen:
213 raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
214 f" actual {alen}, expected {elen}")
215
216
217def _remove_dups_flatten(parameters):
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700218 """An internal helper for Union creation and substitution: flatten Unions
219 among parameters, then remove duplicates.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000220 """
221 # Flatten out Union[Union[...], ...].
222 params = []
223 for p in parameters:
224 if isinstance(p, _GenericAlias) and p.__origin__ is Union:
225 params.extend(p.__args__)
226 elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
227 params.extend(p[1:])
228 else:
229 params.append(p)
230 # Weed out strict duplicates, preserving the first of each occurrence.
231 all_params = set(params)
232 if len(all_params) < len(params):
233 new_params = []
234 for t in params:
235 if t in all_params:
236 new_params.append(t)
237 all_params.remove(t)
238 params = new_params
239 assert not all_params, all_params
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700240 return tuple(params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000241
242
243_cleanups = []
244
245
246def _tp_cache(func):
247 """Internal wrapper caching __getitem__ of generic types with a fallback to
248 original function for non-hashable arguments.
249 """
250 cached = functools.lru_cache()(func)
251 _cleanups.append(cached.cache_clear)
252
253 @functools.wraps(func)
254 def inner(*args, **kwds):
255 try:
256 return cached(*args, **kwds)
257 except TypeError:
258 pass # All real errors (not unhashable args) are raised below.
259 return func(*args, **kwds)
260 return inner
261
262
263def _eval_type(t, globalns, localns):
264 """Evaluate all forward reverences in the given type t.
265 For use of globalns and localns see the docstring for get_type_hints().
266 """
267 if isinstance(t, ForwardRef):
268 return t._evaluate(globalns, localns)
269 if isinstance(t, _GenericAlias):
270 ev_args = tuple(_eval_type(a, globalns, localns) for a in t.__args__)
271 if ev_args == t.__args__:
272 return t
273 res = t.copy_with(ev_args)
274 res._special = t._special
275 return res
276 return t
277
278
279class _Final:
280 """Mixin to prohibit subclassing"""
Guido van Rossum4cefe742016-09-27 15:20:12 -0700281
Guido van Rossum83ec3022017-01-17 20:43:28 -0800282 __slots__ = ('__weakref__',)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700283
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000284 def __init_subclass__(self, *args, **kwds):
285 if '_root' not in kwds:
286 raise TypeError("Cannot subclass special typing classes")
287
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100288class _Immutable:
289 """Mixin to indicate that object should not be copied."""
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000290
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100291 def __copy__(self):
292 return self
293
294 def __deepcopy__(self, memo):
295 return self
296
297
298class _SpecialForm(_Final, _Immutable, _root=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000299 """Internal indicator of special typing constructs.
300 See _doc instance attribute for specific docs.
301 """
302
303 __slots__ = ('_name', '_doc')
304
Guido van Rossum4cefe742016-09-27 15:20:12 -0700305 def __new__(cls, *args, **kwds):
306 """Constructor.
307
308 This only exists to give a better error message in case
309 someone tries to subclass a special typing object (not a good idea).
310 """
311 if (len(args) == 3 and
312 isinstance(args[0], str) and
313 isinstance(args[1], tuple)):
314 # Close enough.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000315 raise TypeError(f"Cannot subclass {cls!r}")
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700316 return super().__new__(cls)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700317
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000318 def __init__(self, name, doc):
319 self._name = name
320 self._doc = doc
Guido van Rossum4cefe742016-09-27 15:20:12 -0700321
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000322 def __eq__(self, other):
323 if not isinstance(other, _SpecialForm):
324 return NotImplemented
325 return self._name == other._name
326
327 def __hash__(self):
328 return hash((self._name,))
Guido van Rossum4cefe742016-09-27 15:20:12 -0700329
330 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000331 return 'typing.' + self._name
332
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100333 def __reduce__(self):
334 return self._name
Guido van Rossum4cefe742016-09-27 15:20:12 -0700335
336 def __call__(self, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000337 raise TypeError(f"Cannot instantiate {self!r}")
338
339 def __instancecheck__(self, obj):
340 raise TypeError(f"{self} cannot be used with isinstance()")
341
342 def __subclasscheck__(self, cls):
343 raise TypeError(f"{self} cannot be used with issubclass()")
344
345 @_tp_cache
346 def __getitem__(self, parameters):
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100347 if self._name in ('ClassVar', 'Final'):
348 item = _type_check(parameters, f'{self._name} accepts only single type.')
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000349 return _GenericAlias(self, (item,))
350 if self._name == 'Union':
351 if parameters == ():
352 raise TypeError("Cannot take a Union of no types.")
353 if not isinstance(parameters, tuple):
354 parameters = (parameters,)
355 msg = "Union[arg, ...]: each arg must be a type."
356 parameters = tuple(_type_check(p, msg) for p in parameters)
357 parameters = _remove_dups_flatten(parameters)
358 if len(parameters) == 1:
359 return parameters[0]
360 return _GenericAlias(self, parameters)
361 if self._name == 'Optional':
362 arg = _type_check(parameters, "Optional[t] requires a single type.")
363 return Union[arg, type(None)]
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100364 if self._name == 'Literal':
365 # There is no '_type_check' call because arguments to Literal[...] are
366 # values, not types.
367 return _GenericAlias(self, parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000368 raise TypeError(f"{self} is not subscriptable")
Guido van Rossum4cefe742016-09-27 15:20:12 -0700369
370
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000371Any = _SpecialForm('Any', doc=
372 """Special type indicating an unconstrained type.
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700373
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000374 - Any is compatible with every type.
375 - Any assumed to have all methods.
376 - All values assumed to be instances of Any.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700377
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000378 Note that all the above statements are true from the point of view of
379 static type checkers. At runtime, Any should not be used with instance
380 or class checks.
381 """)
Guido van Rossumd70fe632015-08-05 12:11:06 +0200382
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000383NoReturn = _SpecialForm('NoReturn', doc=
384 """Special type indicating functions that never return.
385 Example::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700386
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000387 from typing import NoReturn
388
389 def stop() -> NoReturn:
390 raise Exception('no way')
391
392 This type is invalid in other positions, e.g., ``List[NoReturn]``
393 will fail in static type checkers.
394 """)
395
396ClassVar = _SpecialForm('ClassVar', doc=
397 """Special type construct to mark class variables.
398
399 An annotation wrapped in ClassVar indicates that a given
400 attribute is intended to be used as a class variable and
401 should not be set on instances of that class. Usage::
402
403 class Starship:
404 stats: ClassVar[Dict[str, int]] = {} # class variable
405 damage: int = 10 # instance variable
406
407 ClassVar accepts only types and cannot be further subscribed.
408
409 Note that ClassVar is not a class itself, and should not
410 be used with isinstance() or issubclass().
411 """)
412
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100413Final = _SpecialForm('Final', doc=
414 """Special typing construct to indicate final names to type checkers.
415
416 A final name cannot be re-assigned or overridden in a subclass.
417 For example:
418
419 MAX_SIZE: Final = 9000
420 MAX_SIZE += 1 # Error reported by type checker
421
422 class Connection:
423 TIMEOUT: Final[int] = 10
424
425 class FastConnector(Connection):
426 TIMEOUT = 1 # Error reported by type checker
427
428 There is no runtime checking of these properties.
429 """)
430
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000431Union = _SpecialForm('Union', doc=
432 """Union type; Union[X, Y] means either X or Y.
433
434 To define a union, use e.g. Union[int, str]. Details:
435 - The arguments must be types and there must be at least one.
436 - None as an argument is a special case and is replaced by
437 type(None).
438 - Unions of unions are flattened, e.g.::
439
440 Union[Union[int, str], float] == Union[int, str, float]
441
442 - Unions of a single argument vanish, e.g.::
443
444 Union[int] == int # The constructor actually returns int
445
446 - Redundant arguments are skipped, e.g.::
447
448 Union[int, str, int] == Union[int, str]
449
450 - When comparing unions, the argument order is ignored, e.g.::
451
452 Union[int, str] == Union[str, int]
453
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000454 - You cannot subclass or instantiate a union.
455 - You can use Optional[X] as a shorthand for Union[X, None].
456 """)
457
458Optional = _SpecialForm('Optional', doc=
459 """Optional type.
460
461 Optional[X] is equivalent to Union[X, None].
462 """)
Guido van Rossumb7dedc82016-10-29 12:44:29 -0700463
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100464Literal = _SpecialForm('Literal', doc=
465 """Special typing form to define literal types (a.k.a. value types).
466
467 This form can be used to indicate to type checkers that the corresponding
468 variable or function parameter has a value equivalent to the provided
469 literal (or one of several literals):
470
471 def validate_simple(data: Any) -> Literal[True]: # always returns True
472 ...
473
474 MODE = Literal['r', 'rb', 'w', 'wb']
475 def open_helper(file: str, mode: MODE) -> str:
476 ...
477
478 open_helper('/some/path', 'r') # Passes type check
479 open_helper('/other/path', 'typo') # Error in type checker
480
481 Literal[...] cannot be subclassed. At runtime, an arbitrary value
482 is allowed as type argument to Literal[...], but type checkers may
483 impose restrictions.
484 """)
485
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700486
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000487class ForwardRef(_Final, _root=True):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800488 """Internal wrapper to hold a forward reference."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700489
Guido van Rossum4cefe742016-09-27 15:20:12 -0700490 __slots__ = ('__forward_arg__', '__forward_code__',
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400491 '__forward_evaluated__', '__forward_value__',
492 '__forward_is_argument__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700493
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700494 def __init__(self, arg, is_argument=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700495 if not isinstance(arg, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000496 raise TypeError(f"Forward reference must be a string -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700497 try:
498 code = compile(arg, '<string>', 'eval')
499 except SyntaxError:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000500 raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700501 self.__forward_arg__ = arg
502 self.__forward_code__ = code
503 self.__forward_evaluated__ = False
504 self.__forward_value__ = None
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400505 self.__forward_is_argument__ = is_argument
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700506
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000507 def _evaluate(self, globalns, localns):
Guido van Rossumdad17902016-11-10 08:29:18 -0800508 if not self.__forward_evaluated__ or localns is not globalns:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700509 if globalns is None and localns is None:
510 globalns = localns = {}
511 elif globalns is None:
512 globalns = localns
513 elif localns is None:
514 localns = globalns
515 self.__forward_value__ = _type_check(
516 eval(self.__forward_code__, globalns, localns),
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400517 "Forward references must evaluate to types.",
518 is_argument=self.__forward_is_argument__)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700519 self.__forward_evaluated__ = True
520 return self.__forward_value__
521
Guido van Rossum4cefe742016-09-27 15:20:12 -0700522 def __eq__(self, other):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000523 if not isinstance(other, ForwardRef):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700524 return NotImplemented
525 return (self.__forward_arg__ == other.__forward_arg__ and
Guido van Rossumc7b92952016-11-10 08:24:06 -0800526 self.__forward_value__ == other.__forward_value__)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700527
528 def __hash__(self):
Guido van Rossumc7b92952016-11-10 08:24:06 -0800529 return hash((self.__forward_arg__, self.__forward_value__))
Guido van Rossum4cefe742016-09-27 15:20:12 -0700530
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700531 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000532 return f'ForwardRef({self.__forward_arg__!r})'
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700533
534
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100535class TypeVar(_Final, _Immutable, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700536 """Type variable.
537
538 Usage::
539
540 T = TypeVar('T') # Can be anything
541 A = TypeVar('A', str, bytes) # Must be str or bytes
542
543 Type variables exist primarily for the benefit of static type
544 checkers. They serve as the parameters for generic types as well
545 as for generic function definitions. See class Generic for more
546 information on generic types. Generic functions work as follows:
547
Guido van Rossumb24569a2016-11-20 18:01:29 -0800548 def repeat(x: T, n: int) -> List[T]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700549 '''Return a list containing n references to x.'''
550 return [x]*n
551
552 def longest(x: A, y: A) -> A:
553 '''Return the longest of two strings.'''
554 return x if len(x) >= len(y) else y
555
556 The latter example's signature is essentially the overloading
557 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
558 that if the arguments are instances of some subclass of str,
559 the return type is still plain str.
560
Guido van Rossumb24569a2016-11-20 18:01:29 -0800561 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700562
Guido van Rossumefa798d2016-08-23 11:01:50 -0700563 Type variables defined with covariant=True or contravariant=True
João D. Ferreira86bfed32018-07-07 16:41:20 +0100564 can be used to declare covariant or contravariant generic types.
Guido van Rossumefa798d2016-08-23 11:01:50 -0700565 See PEP 484 for more details. By default generic types are invariant
566 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700567
568 Type variables can be introspected. e.g.:
569
570 T.__name__ == 'T'
571 T.__constraints__ == ()
572 T.__covariant__ == False
573 T.__contravariant__ = False
574 A.__constraints__ == (str, bytes)
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100575
576 Note that only type variables defined in global scope can be pickled.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700577 """
578
Guido van Rossum4cefe742016-09-27 15:20:12 -0700579 __slots__ = ('__name__', '__bound__', '__constraints__',
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300580 '__covariant__', '__contravariant__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700581
582 def __init__(self, name, *constraints, bound=None,
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800583 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700584 self.__name__ = name
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700585 if covariant and contravariant:
Guido van Rossumefa798d2016-08-23 11:01:50 -0700586 raise ValueError("Bivariant types are not supported.")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700587 self.__covariant__ = bool(covariant)
588 self.__contravariant__ = bool(contravariant)
589 if constraints and bound is not None:
590 raise TypeError("Constraints cannot be combined with bound=...")
591 if constraints and len(constraints) == 1:
592 raise TypeError("A single constraint is not allowed")
593 msg = "TypeVar(name, constraint, ...): constraints must be types."
594 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
595 if bound:
596 self.__bound__ = _type_check(bound, "Bound must be a type.")
597 else:
598 self.__bound__ = None
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300599 def_mod = sys._getframe(1).f_globals['__name__'] # for pickling
600 if def_mod != 'typing':
601 self.__module__ = def_mod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700602
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700603 def __repr__(self):
604 if self.__covariant__:
605 prefix = '+'
606 elif self.__contravariant__:
607 prefix = '-'
608 else:
609 prefix = '~'
610 return prefix + self.__name__
611
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100612 def __reduce__(self):
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300613 return self.__name__
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100614
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700615
Guido van Rossum83ec3022017-01-17 20:43:28 -0800616# Special typing constructs Union, Optional, Generic, Callable and Tuple
617# use three special attributes for internal bookkeeping of generic types:
618# * __parameters__ is a tuple of unique free type parameters of a generic
619# type, for example, Dict[T, T].__parameters__ == (T,);
620# * __origin__ keeps a reference to a type that was subscripted,
Ivan Levkivskyi43d12a62018-05-09 02:23:46 +0100621# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
622# the type.
Guido van Rossum83ec3022017-01-17 20:43:28 -0800623# * __args__ is a tuple of all arguments used in subscripting,
624# e.g., Dict[T, int].__args__ == (T, int).
625
Ivan Levkivskyi2a363d22018-04-05 01:25:15 +0100626
627# Mapping from non-generic type names that have a generic alias in typing
628# but with a different name.
629_normalize_alias = {'list': 'List',
630 'tuple': 'Tuple',
631 'dict': 'Dict',
632 'set': 'Set',
633 'frozenset': 'FrozenSet',
634 'deque': 'Deque',
635 'defaultdict': 'DefaultDict',
636 'type': 'Type',
637 'Set': 'AbstractSet'}
638
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000639def _is_dunder(attr):
640 return attr.startswith('__') and attr.endswith('__')
Guido van Rossum83ec3022017-01-17 20:43:28 -0800641
Guido van Rossumb24569a2016-11-20 18:01:29 -0800642
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000643class _GenericAlias(_Final, _root=True):
644 """The central part of internal API.
645
646 This represents a generic version of type 'origin' with type arguments 'params'.
647 There are two kind of these aliases: user defined and special. The special ones
648 are wrappers around builtin collections and ABCs in collections.abc. These must
649 have 'name' always set. If 'inst' is False, then the alias can't be instantiated,
650 this is used by e.g. typing.List and typing.Dict.
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700651 """
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000652 def __init__(self, origin, params, *, inst=True, special=False, name=None):
653 self._inst = inst
654 self._special = special
655 if special and name is None:
656 orig_name = origin.__name__
Ivan Levkivskyi2a363d22018-04-05 01:25:15 +0100657 name = _normalize_alias.get(orig_name, orig_name)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000658 self._name = name
659 if not isinstance(params, tuple):
660 params = (params,)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700661 self.__origin__ = origin
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700662 self.__args__ = tuple(... if a is _TypingEllipsis else
663 () if a is _TypingEmpty else
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000664 a for a in params)
665 self.__parameters__ = _collect_type_vars(params)
666 self.__slots__ = None # This is not documented.
667 if not name:
668 self.__module__ = origin.__module__
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700669
Guido van Rossum4cefe742016-09-27 15:20:12 -0700670 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700671 def __getitem__(self, params):
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100672 if self.__origin__ in (Generic, Protocol):
673 # Can't subscript Generic[...] or Protocol[...].
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000674 raise TypeError(f"Cannot subscript already-subscripted {self}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700675 if not isinstance(params, tuple):
676 params = (params,)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700677 msg = "Parameters to generic types must be types."
678 params = tuple(_type_check(p, msg) for p in params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000679 _check_generic(self, params)
680 return _subs_tvars(self, self.__parameters__, params)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100681
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000682 def copy_with(self, params):
683 # We don't copy self._special.
684 return _GenericAlias(self.__origin__, params, name=self._name, inst=self._inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700685
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000686 def __repr__(self):
687 if (self._name != 'Callable' or
688 len(self.__args__) == 2 and self.__args__[0] is Ellipsis):
689 if self._name:
690 name = 'typing.' + self._name
691 else:
692 name = _type_repr(self.__origin__)
693 if not self._special:
694 args = f'[{", ".join([_type_repr(a) for a in self.__args__])}]'
695 else:
696 args = ''
697 return (f'{name}{args}')
698 if self._special:
699 return 'typing.Callable'
700 return (f'typing.Callable'
701 f'[[{", ".join([_type_repr(a) for a in self.__args__[:-1]])}], '
702 f'{_type_repr(self.__args__[-1])}]')
703
704 def __eq__(self, other):
705 if not isinstance(other, _GenericAlias):
706 return NotImplemented
707 if self.__origin__ != other.__origin__:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100708 return False
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000709 if self.__origin__ is Union and other.__origin__ is Union:
710 return frozenset(self.__args__) == frozenset(other.__args__)
711 return self.__args__ == other.__args__
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100712
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000713 def __hash__(self):
714 if self.__origin__ is Union:
715 return hash((Union, frozenset(self.__args__)))
716 return hash((self.__origin__, self.__args__))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700717
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000718 def __call__(self, *args, **kwargs):
719 if not self._inst:
720 raise TypeError(f"Type {self._name} cannot be instantiated; "
721 f"use {self._name.lower()}() instead")
722 result = self.__origin__(*args, **kwargs)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700723 try:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000724 result.__orig_class__ = self
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700725 except AttributeError:
726 pass
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000727 return result
728
729 def __mro_entries__(self, bases):
730 if self._name: # generic version of an ABC or built-in class
731 res = []
732 if self.__origin__ not in bases:
733 res.append(self.__origin__)
734 i = bases.index(self)
735 if not any(isinstance(b, _GenericAlias) or issubclass(b, Generic)
736 for b in bases[i+1:]):
737 res.append(Generic)
738 return tuple(res)
739 if self.__origin__ is Generic:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100740 if Protocol in bases:
741 return ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000742 i = bases.index(self)
743 for b in bases[i+1:]:
744 if isinstance(b, _GenericAlias) and b is not self:
745 return ()
746 return (self.__origin__,)
747
748 def __getattr__(self, attr):
Ville Skyttä61f82e02018-04-20 23:08:45 +0300749 # We are careful for copy and pickle.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000750 # Also for simplicity we just don't relay all dunder names
751 if '__origin__' in self.__dict__ and not _is_dunder(attr):
752 return getattr(self.__origin__, attr)
753 raise AttributeError(attr)
754
755 def __setattr__(self, attr, val):
756 if _is_dunder(attr) or attr in ('_name', '_inst', '_special'):
757 super().__setattr__(attr, val)
758 else:
759 setattr(self.__origin__, attr, val)
760
761 def __instancecheck__(self, obj):
762 return self.__subclasscheck__(type(obj))
763
764 def __subclasscheck__(self, cls):
765 if self._special:
766 if not isinstance(cls, _GenericAlias):
767 return issubclass(cls, self.__origin__)
768 if cls._special:
769 return issubclass(cls.__origin__, self.__origin__)
770 raise TypeError("Subscripted generics cannot be used with"
771 " class and instance checks")
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700772
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100773 def __reduce__(self):
774 if self._special:
775 return self._name
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300776
777 if self._name:
778 origin = globals()[self._name]
779 else:
780 origin = self.__origin__
781 if (origin is Callable and
782 not (len(self.__args__) == 2 and self.__args__[0] is Ellipsis)):
783 args = list(self.__args__[:-1]), self.__args__[-1]
784 else:
785 args = tuple(self.__args__)
786 if len(args) == 1 and not isinstance(args[0], tuple):
787 args, = args
788 return operator.getitem, (origin, args)
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100789
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700790
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000791class _VariadicGenericAlias(_GenericAlias, _root=True):
792 """Same as _GenericAlias above but for variadic aliases. Currently,
793 this is used only by special internal aliases: Tuple and Callable.
794 """
795 def __getitem__(self, params):
796 if self._name != 'Callable' or not self._special:
797 return self.__getitem_inner__(params)
798 if not isinstance(params, tuple) or len(params) != 2:
799 raise TypeError("Callable must be used as "
800 "Callable[[arg, ...], result].")
801 args, result = params
802 if args is Ellipsis:
803 params = (Ellipsis, result)
804 else:
805 if not isinstance(args, list):
806 raise TypeError(f"Callable[args, result]: args must be a list."
807 f" Got {args}")
808 params = (tuple(args), result)
809 return self.__getitem_inner__(params)
810
811 @_tp_cache
812 def __getitem_inner__(self, params):
813 if self.__origin__ is tuple and self._special:
814 if params == ():
815 return self.copy_with((_TypingEmpty,))
816 if not isinstance(params, tuple):
817 params = (params,)
818 if len(params) == 2 and params[1] is ...:
819 msg = "Tuple[t, ...]: t must be a type."
820 p = _type_check(params[0], msg)
821 return self.copy_with((p, _TypingEllipsis))
822 msg = "Tuple[t0, t1, ...]: each t must be a type."
823 params = tuple(_type_check(p, msg) for p in params)
824 return self.copy_with(params)
825 if self.__origin__ is collections.abc.Callable and self._special:
826 args, result = params
827 msg = "Callable[args, result]: result must be a type."
828 result = _type_check(result, msg)
829 if args is Ellipsis:
830 return self.copy_with((_TypingEllipsis, result))
831 msg = "Callable[[arg, ...], result]: each arg must be a type."
832 args = tuple(_type_check(arg, msg) for arg in args)
833 params = args + (result,)
834 return self.copy_with(params)
835 return super().__getitem__(params)
836
837
838class Generic:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700839 """Abstract base class for generic types.
840
Guido van Rossumb24569a2016-11-20 18:01:29 -0800841 A generic type is typically declared by inheriting from
842 this class parameterized with one or more type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700843 For example, a generic mapping type might be defined as::
844
845 class Mapping(Generic[KT, VT]):
846 def __getitem__(self, key: KT) -> VT:
847 ...
848 # Etc.
849
850 This class can then be used as follows::
851
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700852 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700853 try:
854 return mapping[key]
855 except KeyError:
856 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700857 """
Guido van Rossumd70fe632015-08-05 12:11:06 +0200858 __slots__ = ()
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100859 _is_protocol = False
Guido van Rossumd70fe632015-08-05 12:11:06 +0200860
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700861 def __new__(cls, *args, **kwds):
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100862 if cls in (Generic, Protocol):
863 raise TypeError(f"Type {cls.__name__} cannot be instantiated; "
Guido van Rossum62fe1bb2016-10-29 16:05:26 -0700864 "it can be used only as a base class")
Ivan Levkivskyib551e9f2018-05-10 23:10:10 -0400865 if super().__new__ is object.__new__ and cls.__init__ is not object.__init__:
Ivan Levkivskyi43d12a62018-05-09 02:23:46 +0100866 obj = super().__new__(cls)
867 else:
868 obj = super().__new__(cls, *args, **kwds)
869 return obj
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000870
871 @_tp_cache
872 def __class_getitem__(cls, params):
873 if not isinstance(params, tuple):
874 params = (params,)
875 if not params and cls is not Tuple:
876 raise TypeError(
877 f"Parameter list to {cls.__qualname__}[...] cannot be empty")
878 msg = "Parameters to generic types must be types."
879 params = tuple(_type_check(p, msg) for p in params)
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100880 if cls in (Generic, Protocol):
881 # Generic and Protocol can only be subscripted with unique type variables.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000882 if not all(isinstance(p, TypeVar) for p in params):
883 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100884 f"Parameters to {cls.__name__}[...] must all be type variables")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000885 if len(set(params)) != len(params):
886 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100887 f"Parameters to {cls.__name__}[...] must all be unique")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000888 else:
889 # Subscripting a regular Generic subclass.
890 _check_generic(cls, params)
891 return _GenericAlias(cls, params)
892
893 def __init_subclass__(cls, *args, **kwargs):
Ivan Levkivskyiee566fe2018-04-04 17:00:15 +0100894 super().__init_subclass__(*args, **kwargs)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000895 tvars = []
896 if '__orig_bases__' in cls.__dict__:
897 error = Generic in cls.__orig_bases__
898 else:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100899 error = Generic in cls.__bases__ and cls.__name__ != 'Protocol'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000900 if error:
901 raise TypeError("Cannot inherit from plain Generic")
902 if '__orig_bases__' in cls.__dict__:
903 tvars = _collect_type_vars(cls.__orig_bases__)
904 # Look for Generic[T1, ..., Tn].
905 # If found, tvars must be a subset of it.
906 # If not found, tvars is it.
907 # Also check for and reject plain Generic,
908 # and reject multiple Generic[...].
909 gvars = None
910 for base in cls.__orig_bases__:
911 if (isinstance(base, _GenericAlias) and
912 base.__origin__ is Generic):
913 if gvars is not None:
914 raise TypeError(
915 "Cannot inherit from Generic[...] multiple types.")
916 gvars = base.__parameters__
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100917 if gvars is not None:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000918 tvarset = set(tvars)
919 gvarset = set(gvars)
920 if not tvarset <= gvarset:
921 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
922 s_args = ', '.join(str(g) for g in gvars)
923 raise TypeError(f"Some type variables ({s_vars}) are"
924 f" not listed in Generic[{s_args}]")
925 tvars = gvars
926 cls.__parameters__ = tuple(tvars)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700927
928
929class _TypingEmpty:
Guido van Rossumb24569a2016-11-20 18:01:29 -0800930 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
931 to allow empty list/tuple in specific places, without allowing them
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700932 to sneak in where prohibited.
933 """
934
935
936class _TypingEllipsis:
Guido van Rossumb24569a2016-11-20 18:01:29 -0800937 """Internal placeholder for ... (ellipsis)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700938
939
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100940_TYPING_INTERNALS = ['__parameters__', '__orig_bases__', '__orig_class__',
941 '_is_protocol', '_is_runtime_protocol']
942
943_SPECIAL_NAMES = ['__abstractmethods__', '__annotations__', '__dict__', '__doc__',
944 '__init__', '__module__', '__new__', '__slots__',
945 '__subclasshook__', '__weakref__']
946
947# These special attributes will be not collected as protocol members.
948EXCLUDED_ATTRIBUTES = _TYPING_INTERNALS + _SPECIAL_NAMES + ['_MutableMapping__marker']
949
950
951def _get_protocol_attrs(cls):
952 """Collect protocol members from a protocol class objects.
953
954 This includes names actually defined in the class dictionary, as well
955 as names that appear in annotations. Special names (above) are skipped.
956 """
957 attrs = set()
958 for base in cls.__mro__[:-1]: # without object
959 if base.__name__ in ('Protocol', 'Generic'):
960 continue
961 annotations = getattr(base, '__annotations__', {})
962 for attr in list(base.__dict__.keys()) + list(annotations.keys()):
963 if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
964 attrs.add(attr)
965 return attrs
966
967
968def _is_callable_members_only(cls):
969 # PEP 544 prohibits using issubclass() with protocols that have non-method members.
970 return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls))
971
972
973def _no_init(self, *args, **kwargs):
974 if type(self)._is_protocol:
975 raise TypeError('Protocols cannot be instantiated')
976
977
978def _allow_reckless_class_cheks():
979 """Allow instnance and class checks for special stdlib modules.
980
981 The abc and functools modules indiscriminately call isinstance() and
982 issubclass() on the whole MRO of a user class, which may contain protocols.
983 """
984 try:
985 return sys._getframe(3).f_globals['__name__'] in ['abc', 'functools']
986 except (AttributeError, ValueError): # For platforms without _getframe().
987 return True
988
989
990_PROTO_WHITELIST = ['Callable', 'Awaitable',
991 'Iterable', 'Iterator', 'AsyncIterable', 'AsyncIterator',
992 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
993 'ContextManager', 'AsyncContextManager']
994
995
996class _ProtocolMeta(ABCMeta):
997 # This metaclass is really unfortunate and exists only because of
998 # the lack of __instancehook__.
999 def __instancecheck__(cls, instance):
1000 # We need this method for situations where attributes are
1001 # assigned in __init__.
1002 if ((not getattr(cls, '_is_protocol', False) or
1003 _is_callable_members_only(cls)) and
1004 issubclass(instance.__class__, cls)):
1005 return True
1006 if cls._is_protocol:
1007 if all(hasattr(instance, attr) and
1008 # All *methods* can be blocked by setting them to None.
1009 (not callable(getattr(cls, attr, None)) or
1010 getattr(instance, attr) is not None)
1011 for attr in _get_protocol_attrs(cls)):
1012 return True
1013 return super().__instancecheck__(instance)
1014
1015
1016class Protocol(Generic, metaclass=_ProtocolMeta):
1017 """Base class for protocol classes.
1018
1019 Protocol classes are defined as::
1020
1021 class Proto(Protocol):
1022 def meth(self) -> int:
1023 ...
1024
1025 Such classes are primarily used with static type checkers that recognize
1026 structural subtyping (static duck-typing), for example::
1027
1028 class C:
1029 def meth(self) -> int:
1030 return 0
1031
1032 def func(x: Proto) -> int:
1033 return x.meth()
1034
1035 func(C()) # Passes static type check
1036
1037 See PEP 544 for details. Protocol classes decorated with
1038 @typing.runtime_checkable act as simple-minded runtime protocols that check
1039 only the presence of given attributes, ignoring their type signatures.
1040 Protocol classes can be generic, they are defined as::
1041
1042 class GenProto(Protocol[T]):
1043 def meth(self) -> T:
1044 ...
1045 """
1046 __slots__ = ()
1047 _is_protocol = True
1048 _is_runtime_protocol = False
1049
1050 def __init_subclass__(cls, *args, **kwargs):
1051 super().__init_subclass__(*args, **kwargs)
1052
1053 # Determine if this is a protocol or a concrete subclass.
1054 if not cls.__dict__.get('_is_protocol', False):
1055 cls._is_protocol = any(b is Protocol for b in cls.__bases__)
1056
1057 # Set (or override) the protocol subclass hook.
1058 def _proto_hook(other):
1059 if not cls.__dict__.get('_is_protocol', False):
1060 return NotImplemented
1061
1062 # First, perform various sanity checks.
1063 if not getattr(cls, '_is_runtime_protocol', False):
1064 if _allow_reckless_class_cheks():
1065 return NotImplemented
1066 raise TypeError("Instance and class checks can only be used with"
1067 " @runtime_checkable protocols")
1068 if not _is_callable_members_only(cls):
1069 if _allow_reckless_class_cheks():
1070 return NotImplemented
1071 raise TypeError("Protocols with non-method members"
1072 " don't support issubclass()")
1073 if not isinstance(other, type):
1074 # Same error message as for issubclass(1, int).
1075 raise TypeError('issubclass() arg 1 must be a class')
1076
1077 # Second, perform the actual structural compatibility check.
1078 for attr in _get_protocol_attrs(cls):
1079 for base in other.__mro__:
1080 # Check if the members appears in the class dictionary...
1081 if attr in base.__dict__:
1082 if base.__dict__[attr] is None:
1083 return NotImplemented
1084 break
1085
1086 # ...or in annotations, if it is a sub-protocol.
1087 annotations = getattr(base, '__annotations__', {})
1088 if (isinstance(annotations, collections.abc.Mapping) and
1089 attr in annotations and
1090 issubclass(other, Generic) and other._is_protocol):
1091 break
1092 else:
1093 return NotImplemented
1094 return True
1095
1096 if '__subclasshook__' not in cls.__dict__:
1097 cls.__subclasshook__ = _proto_hook
1098
1099 # We have nothing more to do for non-protocols...
1100 if not cls._is_protocol:
1101 return
1102
1103 # ... otherwise check consistency of bases, and prohibit instantiation.
1104 for base in cls.__bases__:
1105 if not (base in (object, Generic) or
1106 base.__module__ == 'collections.abc' and base.__name__ in _PROTO_WHITELIST or
1107 issubclass(base, Generic) and base._is_protocol):
1108 raise TypeError('Protocols can only inherit from other'
1109 ' protocols, got %r' % base)
1110 cls.__init__ = _no_init
1111
1112
1113def runtime_checkable(cls):
1114 """Mark a protocol class as a runtime protocol.
1115
1116 Such protocol can be used with isinstance() and issubclass().
1117 Raise TypeError if applied to a non-protocol class.
1118 This allows a simple-minded structural check very similar to
1119 one trick ponies in collections.abc such as Iterable.
1120 For example::
1121
1122 @runtime_checkable
1123 class Closable(Protocol):
1124 def close(self): ...
1125
1126 assert isinstance(open('/some/file'), Closable)
1127
1128 Warning: this will check only the presence of the required methods,
1129 not their type signatures!
1130 """
1131 if not issubclass(cls, Generic) or not cls._is_protocol:
1132 raise TypeError('@runtime_checkable can be only applied to protocol classes,'
1133 ' got %r' % cls)
1134 cls._is_runtime_protocol = True
1135 return cls
1136
1137
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001138def cast(typ, val):
1139 """Cast a value to a type.
1140
1141 This returns the value unchanged. To the type checker this
1142 signals that the return value has the designated type, but at
1143 runtime we intentionally don't check anything (we want this
1144 to be as fast as possible).
1145 """
1146 return val
1147
1148
1149def _get_defaults(func):
1150 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -08001151 try:
1152 code = func.__code__
1153 except AttributeError:
1154 # Some built-in functions don't have __code__, __defaults__, etc.
1155 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001156 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001157 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001158 arg_names = arg_names[:pos_count]
1159 defaults = func.__defaults__ or ()
1160 kwdefaults = func.__kwdefaults__
1161 res = dict(kwdefaults) if kwdefaults else {}
1162 pos_offset = pos_count - len(defaults)
1163 for name, value in zip(arg_names[pos_offset:], defaults):
1164 assert name not in res
1165 res[name] = value
1166 return res
1167
1168
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001169_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
1170 types.MethodType, types.ModuleType,
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02001171 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001172
1173
Guido van Rossum991d14f2016-11-09 13:12:51 -08001174def get_type_hints(obj, globalns=None, localns=None):
1175 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001176
Guido van Rossum991d14f2016-11-09 13:12:51 -08001177 This is often the same as obj.__annotations__, but it handles
1178 forward references encoded as string literals, and if necessary
1179 adds Optional[t] if a default value equal to None is set.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001180
Guido van Rossum991d14f2016-11-09 13:12:51 -08001181 The argument may be a module, class, method, or function. The annotations
1182 are returned as a dictionary. For classes, annotations include also
1183 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001184
Guido van Rossum991d14f2016-11-09 13:12:51 -08001185 TypeError is raised if the argument is not of a type that can contain
1186 annotations, and an empty dictionary is returned if no annotations are
1187 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001188
Guido van Rossum991d14f2016-11-09 13:12:51 -08001189 BEWARE -- the behavior of globalns and localns is counterintuitive
1190 (unless you are familiar with how eval() and exec() work). The
1191 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001192
Guido van Rossum991d14f2016-11-09 13:12:51 -08001193 - If no dict arguments are passed, an attempt is made to use the
Łukasz Langaf350a262017-09-14 14:33:00 -04001194 globals from obj (or the respective module's globals for classes),
1195 and these are also used as the locals. If the object does not appear
1196 to have globals, an empty dictionary is used.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001197
Guido van Rossum991d14f2016-11-09 13:12:51 -08001198 - If one dict argument is passed, it is used for both globals and
1199 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001200
Guido van Rossum991d14f2016-11-09 13:12:51 -08001201 - If two dict arguments are passed, they specify globals and
1202 locals, respectively.
1203 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001204
Guido van Rossum991d14f2016-11-09 13:12:51 -08001205 if getattr(obj, '__no_type_check__', None):
1206 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001207 # Classes require a special treatment.
1208 if isinstance(obj, type):
1209 hints = {}
1210 for base in reversed(obj.__mro__):
Łukasz Langaf350a262017-09-14 14:33:00 -04001211 if globalns is None:
1212 base_globals = sys.modules[base.__module__].__dict__
1213 else:
1214 base_globals = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001215 ann = base.__dict__.get('__annotations__', {})
1216 for name, value in ann.items():
1217 if value is None:
1218 value = type(None)
1219 if isinstance(value, str):
Nina Zakharenko0e61dff2018-05-22 20:32:10 -07001220 value = ForwardRef(value, is_argument=False)
Łukasz Langaf350a262017-09-14 14:33:00 -04001221 value = _eval_type(value, base_globals, localns)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001222 hints[name] = value
1223 return hints
Łukasz Langaf350a262017-09-14 14:33:00 -04001224
1225 if globalns is None:
1226 if isinstance(obj, types.ModuleType):
1227 globalns = obj.__dict__
1228 else:
1229 globalns = getattr(obj, '__globals__', {})
1230 if localns is None:
1231 localns = globalns
1232 elif localns is None:
1233 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001234 hints = getattr(obj, '__annotations__', None)
1235 if hints is None:
1236 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001237 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001238 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001239 else:
1240 raise TypeError('{!r} is not a module, class, method, '
1241 'or function.'.format(obj))
1242 defaults = _get_defaults(obj)
1243 hints = dict(hints)
1244 for name, value in hints.items():
1245 if value is None:
1246 value = type(None)
1247 if isinstance(value, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001248 value = ForwardRef(value)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001249 value = _eval_type(value, globalns, localns)
1250 if name in defaults and defaults[name] is None:
1251 value = Optional[value]
1252 hints[name] = value
1253 return hints
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001254
1255
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001256def no_type_check(arg):
1257 """Decorator to indicate that annotations are not type hints.
1258
1259 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001260 applies recursively to all methods and classes defined in that class
1261 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001262
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001263 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001264 """
1265 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001266 arg_attrs = arg.__dict__.copy()
1267 for attr, val in arg.__dict__.items():
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001268 if val in arg.__bases__ + (arg,):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001269 arg_attrs.pop(attr)
1270 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001271 if isinstance(obj, types.FunctionType):
1272 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001273 if isinstance(obj, type):
1274 no_type_check(obj)
1275 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001276 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001277 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001278 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001279 return arg
1280
1281
1282def no_type_check_decorator(decorator):
1283 """Decorator to give another decorator the @no_type_check effect.
1284
1285 This wraps the decorator with something that wraps the decorated
1286 function in @no_type_check.
1287 """
1288
1289 @functools.wraps(decorator)
1290 def wrapped_decorator(*args, **kwds):
1291 func = decorator(*args, **kwds)
1292 func = no_type_check(func)
1293 return func
1294
1295 return wrapped_decorator
1296
1297
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001298def _overload_dummy(*args, **kwds):
1299 """Helper for @overload to raise when called."""
1300 raise NotImplementedError(
1301 "You should not call an overloaded function. "
1302 "A series of @overload-decorated functions "
1303 "outside a stub module should always be followed "
1304 "by an implementation that is not @overload-ed.")
1305
1306
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001307def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001308 """Decorator for overloaded functions/methods.
1309
1310 In a stub file, place two or more stub definitions for the same
1311 function in a row, each decorated with @overload. For example:
1312
1313 @overload
1314 def utf8(value: None) -> None: ...
1315 @overload
1316 def utf8(value: bytes) -> bytes: ...
1317 @overload
1318 def utf8(value: str) -> bytes: ...
1319
1320 In a non-stub file (i.e. a regular .py file), do the same but
1321 follow it with an implementation. The implementation should *not*
1322 be decorated with @overload. For example:
1323
1324 @overload
1325 def utf8(value: None) -> None: ...
1326 @overload
1327 def utf8(value: bytes) -> bytes: ...
1328 @overload
1329 def utf8(value: str) -> bytes: ...
1330 def utf8(value):
1331 # implementation goes here
1332 """
1333 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001334
1335
Ivan Levkivskyif3672422019-05-26 09:37:07 +01001336def final(f):
1337 """A decorator to indicate final methods and final classes.
1338
1339 Use this decorator to indicate to type checkers that the decorated
1340 method cannot be overridden, and decorated class cannot be subclassed.
1341 For example:
1342
1343 class Base:
1344 @final
1345 def done(self) -> None:
1346 ...
1347 class Sub(Base):
1348 def done(self) -> None: # Error reported by type checker
1349 ...
1350
1351 @final
1352 class Leaf:
1353 ...
1354 class Other(Leaf): # Error reported by type checker
1355 ...
1356
1357 There is no runtime checking of these properties.
1358 """
1359 return f
1360
1361
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001362# Some unconstrained type variables. These are used by the container types.
1363# (These are not for export.)
1364T = TypeVar('T') # Any type.
1365KT = TypeVar('KT') # Key type.
1366VT = TypeVar('VT') # Value type.
1367T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
1368V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
1369VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
1370T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
1371# Internal type variable used for Type[].
1372CT_co = TypeVar('CT_co', covariant=True, bound=type)
1373
1374# A useful type variable with constraints. This represents string types.
1375# (This one *is* for export!)
1376AnyStr = TypeVar('AnyStr', bytes, str)
1377
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001378
1379# Various ABCs mimicking those in collections.abc.
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001380def _alias(origin, params, inst=True):
1381 return _GenericAlias(origin, params, special=True, inst=inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001382
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001383Hashable = _alias(collections.abc.Hashable, ()) # Not generic.
1384Awaitable = _alias(collections.abc.Awaitable, T_co)
1385Coroutine = _alias(collections.abc.Coroutine, (T_co, T_contra, V_co))
1386AsyncIterable = _alias(collections.abc.AsyncIterable, T_co)
1387AsyncIterator = _alias(collections.abc.AsyncIterator, T_co)
1388Iterable = _alias(collections.abc.Iterable, T_co)
1389Iterator = _alias(collections.abc.Iterator, T_co)
1390Reversible = _alias(collections.abc.Reversible, T_co)
1391Sized = _alias(collections.abc.Sized, ()) # Not generic.
1392Container = _alias(collections.abc.Container, T_co)
1393Collection = _alias(collections.abc.Collection, T_co)
1394Callable = _VariadicGenericAlias(collections.abc.Callable, (), special=True)
1395Callable.__doc__ = \
1396 """Callable type; Callable[[int], str] is a function of (int) -> str.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001397
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001398 The subscription syntax must always be used with exactly two
1399 values: the argument list and the return type. The argument list
1400 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001401
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001402 There is no syntax to indicate optional or keyword arguments,
1403 such function types are rarely used as callback types.
1404 """
1405AbstractSet = _alias(collections.abc.Set, T_co)
1406MutableSet = _alias(collections.abc.MutableSet, T)
1407# NOTE: Mapping is only covariant in the value type.
1408Mapping = _alias(collections.abc.Mapping, (KT, VT_co))
1409MutableMapping = _alias(collections.abc.MutableMapping, (KT, VT))
1410Sequence = _alias(collections.abc.Sequence, T_co)
1411MutableSequence = _alias(collections.abc.MutableSequence, T)
1412ByteString = _alias(collections.abc.ByteString, ()) # Not generic
1413Tuple = _VariadicGenericAlias(tuple, (), inst=False, special=True)
1414Tuple.__doc__ = \
1415 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001416
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001417 Example: Tuple[T1, T2] is a tuple of two elements corresponding
1418 to type variables T1 and T2. Tuple[int, float, str] is a tuple
1419 of an int, a float and a string.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001420
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001421 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
1422 """
1423List = _alias(list, T, inst=False)
1424Deque = _alias(collections.deque, T)
1425Set = _alias(set, T, inst=False)
1426FrozenSet = _alias(frozenset, T_co, inst=False)
1427MappingView = _alias(collections.abc.MappingView, T_co)
1428KeysView = _alias(collections.abc.KeysView, KT)
1429ItemsView = _alias(collections.abc.ItemsView, (KT, VT_co))
1430ValuesView = _alias(collections.abc.ValuesView, VT_co)
1431ContextManager = _alias(contextlib.AbstractContextManager, T_co)
1432AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, T_co)
1433Dict = _alias(dict, (KT, VT), inst=False)
1434DefaultDict = _alias(collections.defaultdict, (KT, VT))
Ismo Toijala68b56d02018-12-02 17:53:14 +02001435OrderedDict = _alias(collections.OrderedDict, (KT, VT))
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001436Counter = _alias(collections.Counter, T)
1437ChainMap = _alias(collections.ChainMap, (KT, VT))
1438Generator = _alias(collections.abc.Generator, (T_co, T_contra, V_co))
1439AsyncGenerator = _alias(collections.abc.AsyncGenerator, (T_co, T_contra))
1440Type = _alias(type, CT_co, inst=False)
1441Type.__doc__ = \
1442 """A special construct usable to annotate class objects.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001443
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001444 For example, suppose we have the following classes::
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001445
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001446 class User: ... # Abstract base for User classes
1447 class BasicUser(User): ...
1448 class ProUser(User): ...
1449 class TeamUser(User): ...
Guido van Rossumf17c2002015-12-03 17:31:24 -08001450
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001451 And a function that takes a class argument that's a subclass of
1452 User and returns an instance of the corresponding class::
Guido van Rossumf17c2002015-12-03 17:31:24 -08001453
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001454 U = TypeVar('U', bound=User)
1455 def new_user(user_class: Type[U]) -> U:
1456 user = user_class()
1457 # (Here we could write the user object to a database)
1458 return user
Guido van Rossumf17c2002015-12-03 17:31:24 -08001459
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001460 joe = new_user(BasicUser)
Guido van Rossumf17c2002015-12-03 17:31:24 -08001461
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001462 At this point the type checker knows that joe has type BasicUser.
1463 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001464
1465
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001466@runtime_checkable
1467class SupportsInt(Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001468 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001469
1470 @abstractmethod
1471 def __int__(self) -> int:
1472 pass
1473
1474
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001475@runtime_checkable
1476class SupportsFloat(Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001477 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001478
1479 @abstractmethod
1480 def __float__(self) -> float:
1481 pass
1482
1483
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001484@runtime_checkable
1485class SupportsComplex(Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001486 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001487
1488 @abstractmethod
1489 def __complex__(self) -> complex:
1490 pass
1491
1492
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001493@runtime_checkable
1494class SupportsBytes(Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001495 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001496
1497 @abstractmethod
1498 def __bytes__(self) -> bytes:
1499 pass
1500
1501
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001502@runtime_checkable
1503class SupportsIndex(Protocol):
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -07001504 __slots__ = ()
1505
1506 @abstractmethod
1507 def __index__(self) -> int:
1508 pass
1509
1510
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001511@runtime_checkable
1512class SupportsAbs(Protocol[T_co]):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001513 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001514
1515 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001516 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001517 pass
1518
1519
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001520@runtime_checkable
1521class SupportsRound(Protocol[T_co]):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001522 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001523
1524 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001525 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001526 pass
1527
1528
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001529def _make_nmtuple(name, types):
Guido van Rossum2f841442016-11-15 09:48:06 -08001530 msg = "NamedTuple('Name', [(f0, t0), (f1, t1), ...]); each t must be a type"
1531 types = [(n, _type_check(t, msg)) for n, t in types]
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001532 nm_tpl = collections.namedtuple(name, [n for n, t in types])
Guido van Rossum83ec3022017-01-17 20:43:28 -08001533 # Prior to PEP 526, only _field_types attribute was assigned.
Raymond Hettingerf7b57df2019-03-18 09:53:56 -07001534 # Now __annotations__ are used and _field_types is deprecated (remove in 3.9)
1535 nm_tpl.__annotations__ = nm_tpl._field_types = dict(types)
Guido van Rossum557d1eb2015-11-19 08:16:31 -08001536 try:
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001537 nm_tpl.__module__ = sys._getframe(2).f_globals.get('__name__', '__main__')
Guido van Rossum557d1eb2015-11-19 08:16:31 -08001538 except (AttributeError, ValueError):
1539 pass
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001540 return nm_tpl
1541
1542
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001543# attributes prohibited to set in NamedTuple class syntax
1544_prohibited = ('__new__', '__init__', '__slots__', '__getnewargs__',
1545 '_fields', '_field_defaults', '_field_types',
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02001546 '_make', '_replace', '_asdict', '_source')
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001547
1548_special = ('__module__', '__name__', '__qualname__', '__annotations__')
1549
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001550
Guido van Rossum2f841442016-11-15 09:48:06 -08001551class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001552
Guido van Rossum2f841442016-11-15 09:48:06 -08001553 def __new__(cls, typename, bases, ns):
1554 if ns.get('_root', False):
1555 return super().__new__(cls, typename, bases, ns)
Guido van Rossum2f841442016-11-15 09:48:06 -08001556 types = ns.get('__annotations__', {})
Guido van Rossum3c268be2017-01-18 08:03:50 -08001557 nm_tpl = _make_nmtuple(typename, types.items())
1558 defaults = []
1559 defaults_dict = {}
1560 for field_name in types:
1561 if field_name in ns:
1562 default_value = ns[field_name]
1563 defaults.append(default_value)
1564 defaults_dict[field_name] = default_value
1565 elif defaults:
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001566 raise TypeError("Non-default namedtuple field {field_name} cannot "
1567 "follow default field(s) {default_names}"
Guido van Rossum3c268be2017-01-18 08:03:50 -08001568 .format(field_name=field_name,
1569 default_names=', '.join(defaults_dict.keys())))
Raymond Hettingerf7b57df2019-03-18 09:53:56 -07001570 nm_tpl.__new__.__annotations__ = dict(types)
Guido van Rossum3c268be2017-01-18 08:03:50 -08001571 nm_tpl.__new__.__defaults__ = tuple(defaults)
1572 nm_tpl._field_defaults = defaults_dict
Guido van Rossum95919c02017-01-22 17:47:20 -08001573 # update from user namespace without overriding special namedtuple attributes
1574 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001575 if key in _prohibited:
1576 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
1577 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08001578 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08001579 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001580
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001581
Guido van Rossum2f841442016-11-15 09:48:06 -08001582class NamedTuple(metaclass=NamedTupleMeta):
1583 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001584
Guido van Rossum2f841442016-11-15 09:48:06 -08001585 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001586
Guido van Rossum2f841442016-11-15 09:48:06 -08001587 class Employee(NamedTuple):
1588 name: str
1589 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001590
Guido van Rossum2f841442016-11-15 09:48:06 -08001591 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001592
Guido van Rossum2f841442016-11-15 09:48:06 -08001593 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001594
Raymond Hettingerf7b57df2019-03-18 09:53:56 -07001595 The resulting class has an extra __annotations__ attribute, giving a
1596 dict that maps field names to types. (The field names are also in
1597 the _fields attribute, which is part of the namedtuple API.)
1598 Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001599
Guido van Rossum2f841442016-11-15 09:48:06 -08001600 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001601
Guido van Rossum2f841442016-11-15 09:48:06 -08001602 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001603
Guido van Rossum2f841442016-11-15 09:48:06 -08001604 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
1605 """
1606 _root = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001607
Guido van Rossum2f841442016-11-15 09:48:06 -08001608 def __new__(self, typename, fields=None, **kwargs):
Guido van Rossum2f841442016-11-15 09:48:06 -08001609 if fields is None:
1610 fields = kwargs.items()
1611 elif kwargs:
1612 raise TypeError("Either list of fields or keywords"
1613 " can be provided to NamedTuple, not both")
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001614 return _make_nmtuple(typename, fields)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001615
1616
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001617def _dict_new(cls, *args, **kwargs):
1618 return dict(*args, **kwargs)
1619
1620
1621def _typeddict_new(cls, _typename, _fields=None, **kwargs):
1622 total = kwargs.pop('total', True)
1623 if _fields is None:
1624 _fields = kwargs
1625 elif kwargs:
1626 raise TypeError("TypedDict takes either a dict or keyword arguments,"
1627 " but not both")
1628
1629 ns = {'__annotations__': dict(_fields), '__total__': total}
1630 try:
1631 # Setting correct module is necessary to make typed dict classes pickleable.
1632 ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
1633 except (AttributeError, ValueError):
1634 pass
1635
1636 return _TypedDictMeta(_typename, (), ns)
1637
1638
1639def _check_fails(cls, other):
1640 # Typed dicts are only for static structural subtyping.
1641 raise TypeError('TypedDict does not support instance and class checks')
1642
1643
1644class _TypedDictMeta(type):
1645 def __new__(cls, name, bases, ns, total=True):
1646 """Create new typed dict class object.
1647
1648 This method is called directly when TypedDict is subclassed,
1649 or via _typeddict_new when TypedDict is instantiated. This way
1650 TypedDict supports all three syntax forms described in its docstring.
1651 Subclasses and instances of TypedDict return actual dictionaries
1652 via _dict_new.
1653 """
1654 ns['__new__'] = _typeddict_new if name == 'TypedDict' else _dict_new
1655 tp_dict = super(_TypedDictMeta, cls).__new__(cls, name, (dict,), ns)
1656
1657 anns = ns.get('__annotations__', {})
1658 msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
1659 anns = {n: _type_check(tp, msg) for n, tp in anns.items()}
1660 for base in bases:
1661 anns.update(base.__dict__.get('__annotations__', {}))
1662 tp_dict.__annotations__ = anns
1663 if not hasattr(tp_dict, '__total__'):
1664 tp_dict.__total__ = total
1665 return tp_dict
1666
1667 __instancecheck__ = __subclasscheck__ = _check_fails
1668
1669
1670class TypedDict(dict, metaclass=_TypedDictMeta):
1671 """A simple typed namespace. At runtime it is equivalent to a plain dict.
1672
1673 TypedDict creates a dictionary type that expects all of its
1674 instances to have a certain set of keys, where each key is
1675 associated with a value of a consistent type. This expectation
1676 is not checked at runtime but is only enforced by type checkers.
1677 Usage::
1678
1679 class Point2D(TypedDict):
1680 x: int
1681 y: int
1682 label: str
1683
1684 a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
1685 b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
1686
1687 assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
1688
1689 The type info can be accessed via Point2D.__annotations__. TypedDict
1690 supports two additional equivalent forms::
1691
1692 Point2D = TypedDict('Point2D', x=int, y=int, label=str)
1693 Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
1694
1695 The class syntax is only supported in Python 3.6+, while two other
1696 syntax forms work for Python 2.7 and 3.2+
1697 """
1698
1699
Guido van Rossum91185fe2016-06-08 11:19:11 -07001700def NewType(name, tp):
1701 """NewType creates simple unique types with almost zero
1702 runtime overhead. NewType(name, tp) is considered a subtype of tp
1703 by static type checkers. At runtime, NewType(name, tp) returns
1704 a dummy function that simply returns its argument. Usage::
1705
1706 UserId = NewType('UserId', int)
1707
1708 def name_by_id(user_id: UserId) -> str:
1709 ...
1710
1711 UserId('user') # Fails type check
1712
1713 name_by_id(42) # Fails type check
1714 name_by_id(UserId(42)) # OK
1715
1716 num = UserId(5) + 1 # type: int
1717 """
1718
1719 def new_type(x):
1720 return x
1721
1722 new_type.__name__ = name
1723 new_type.__supertype__ = tp
1724 return new_type
1725
1726
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001727# Python-version-specific alias (Python 2: unicode; Python 3: str)
1728Text = str
1729
1730
Guido van Rossum91185fe2016-06-08 11:19:11 -07001731# Constant that's True when type checking, but False here.
1732TYPE_CHECKING = False
1733
1734
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001735class IO(Generic[AnyStr]):
1736 """Generic base class for TextIO and BinaryIO.
1737
1738 This is an abstract, generic version of the return of open().
1739
1740 NOTE: This does not distinguish between the different possible
1741 classes (text vs. binary, read vs. write vs. read/write,
1742 append-only, unbuffered). The TextIO and BinaryIO subclasses
1743 below capture the distinctions between text vs. binary, which is
1744 pervasive in the interface; however we currently do not offer a
1745 way to track the other distinctions in the type system.
1746 """
1747
Guido van Rossumd70fe632015-08-05 12:11:06 +02001748 __slots__ = ()
1749
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001750 @abstractproperty
1751 def mode(self) -> str:
1752 pass
1753
1754 @abstractproperty
1755 def name(self) -> str:
1756 pass
1757
1758 @abstractmethod
1759 def close(self) -> None:
1760 pass
1761
1762 @abstractmethod
1763 def closed(self) -> bool:
1764 pass
1765
1766 @abstractmethod
1767 def fileno(self) -> int:
1768 pass
1769
1770 @abstractmethod
1771 def flush(self) -> None:
1772 pass
1773
1774 @abstractmethod
1775 def isatty(self) -> bool:
1776 pass
1777
1778 @abstractmethod
1779 def read(self, n: int = -1) -> AnyStr:
1780 pass
1781
1782 @abstractmethod
1783 def readable(self) -> bool:
1784 pass
1785
1786 @abstractmethod
1787 def readline(self, limit: int = -1) -> AnyStr:
1788 pass
1789
1790 @abstractmethod
1791 def readlines(self, hint: int = -1) -> List[AnyStr]:
1792 pass
1793
1794 @abstractmethod
1795 def seek(self, offset: int, whence: int = 0) -> int:
1796 pass
1797
1798 @abstractmethod
1799 def seekable(self) -> bool:
1800 pass
1801
1802 @abstractmethod
1803 def tell(self) -> int:
1804 pass
1805
1806 @abstractmethod
1807 def truncate(self, size: int = None) -> int:
1808 pass
1809
1810 @abstractmethod
1811 def writable(self) -> bool:
1812 pass
1813
1814 @abstractmethod
1815 def write(self, s: AnyStr) -> int:
1816 pass
1817
1818 @abstractmethod
1819 def writelines(self, lines: List[AnyStr]) -> None:
1820 pass
1821
1822 @abstractmethod
1823 def __enter__(self) -> 'IO[AnyStr]':
1824 pass
1825
1826 @abstractmethod
1827 def __exit__(self, type, value, traceback) -> None:
1828 pass
1829
1830
1831class BinaryIO(IO[bytes]):
1832 """Typed version of the return of open() in binary mode."""
1833
Guido van Rossumd70fe632015-08-05 12:11:06 +02001834 __slots__ = ()
1835
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001836 @abstractmethod
1837 def write(self, s: Union[bytes, bytearray]) -> int:
1838 pass
1839
1840 @abstractmethod
1841 def __enter__(self) -> 'BinaryIO':
1842 pass
1843
1844
1845class TextIO(IO[str]):
1846 """Typed version of the return of open() in text mode."""
1847
Guido van Rossumd70fe632015-08-05 12:11:06 +02001848 __slots__ = ()
1849
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001850 @abstractproperty
1851 def buffer(self) -> BinaryIO:
1852 pass
1853
1854 @abstractproperty
1855 def encoding(self) -> str:
1856 pass
1857
1858 @abstractproperty
Guido van Rossum991d14f2016-11-09 13:12:51 -08001859 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001860 pass
1861
1862 @abstractproperty
1863 def line_buffering(self) -> bool:
1864 pass
1865
1866 @abstractproperty
1867 def newlines(self) -> Any:
1868 pass
1869
1870 @abstractmethod
1871 def __enter__(self) -> 'TextIO':
1872 pass
1873
1874
1875class io:
1876 """Wrapper namespace for IO generic classes."""
1877
1878 __all__ = ['IO', 'TextIO', 'BinaryIO']
1879 IO = IO
1880 TextIO = TextIO
1881 BinaryIO = BinaryIO
1882
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001883
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001884io.__name__ = __name__ + '.io'
1885sys.modules[io.__name__] = io
1886
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001887Pattern = _alias(stdlib_re.Pattern, AnyStr)
1888Match = _alias(stdlib_re.Match, AnyStr)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001889
1890class re:
1891 """Wrapper namespace for re type aliases."""
1892
1893 __all__ = ['Pattern', 'Match']
1894 Pattern = Pattern
1895 Match = Match
1896
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001897
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001898re.__name__ = __name__ + '.re'
1899sys.modules[re.__name__] = re