blob: 9851cb4c7ebd6f8a21a856090f9a87c482e8c531 [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:
Miss Islington (bot)f6d70b82018-09-01 05:15:40 -04005* 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.
12* The public counterpart of the generics API consists of two classes: Generic and Protocol
13 (the latter is currently private, but will be made public after PEP 544 acceptance).
14* Public helper functions: get_type_hints, overload, cast, no_type_check,
15 no_type_check_decorator.
16* Generic aliases for collections.abc ABCs and few additional protocols.
17* Special types: NewType, NamedTuple, TypedDict (may be added soon).
18* Wrapper submodules for re and io related types.
19"""
20
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070021import abc
22from abc import abstractmethod, abstractproperty
23import collections
Ivan Levkivskyid911e402018-01-20 11:23:59 +000024import collections.abc
Brett Cannonf3ad0422016-04-15 10:51:30 -070025import contextlib
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070026import functools
Miss Islington (bot)d4986252018-05-26 11:38:00 -070027import operator
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070028import re as stdlib_re # Avoid confusion with the re we export.
29import sys
30import types
Ivan Levkivskyid911e402018-01-20 11:23:59 +000031from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070032
33# Please keep __all__ alphabetized within each category.
34__all__ = [
35 # Super-special typing primitives.
36 'Any',
37 'Callable',
Guido van Rossum0a6976d2016-09-11 15:34:56 -070038 'ClassVar',
Anthony Sottile3a98bbf2019-05-29 21:05:33 -070039 'ForwardRef',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070040 'Generic',
41 'Optional',
Guido van Rossumeb9aca32016-05-24 16:38:22 -070042 'Tuple',
43 'Type',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070044 'TypeVar',
45 'Union',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070046
47 # ABCs (from collections.abc).
48 'AbstractSet', # collections.abc.Set.
49 'ByteString',
50 'Container',
Ivan Levkivskyi29fda8d2017-06-10 21:57:56 +020051 'ContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070052 'Hashable',
53 'ItemsView',
54 'Iterable',
55 'Iterator',
56 'KeysView',
57 'Mapping',
58 'MappingView',
59 'MutableMapping',
60 'MutableSequence',
61 'MutableSet',
62 'Sequence',
63 'Sized',
64 'ValuesView',
Ivan Levkivskyid911e402018-01-20 11:23:59 +000065 'Awaitable',
66 'AsyncIterator',
67 'AsyncIterable',
68 'Coroutine',
69 'Collection',
70 'AsyncGenerator',
71 'AsyncContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070072
73 # Structural checks, a.k.a. protocols.
74 'Reversible',
75 'SupportsAbs',
Ivan Levkivskyif06e0212017-05-02 19:14:07 +020076 'SupportsBytes',
77 'SupportsComplex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070078 'SupportsFloat',
79 'SupportsInt',
80 'SupportsRound',
81
82 # Concrete collection types.
Anthony Sottile3a98bbf2019-05-29 21:05:33 -070083 'ChainMap',
Ivan Levkivskyib692dc82017-02-13 22:50:14 +010084 'Counter',
Raymond Hettinger80490522017-01-16 22:42:37 -080085 'Deque',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070086 'Dict',
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070087 'DefaultDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070088 'List',
Anthony Sottile3a98bbf2019-05-29 21:05:33 -070089 'OrderedDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070090 'Set',
Guido van Rossumefa798d2016-08-23 11:01:50 -070091 'FrozenSet',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070092 'NamedTuple', # Not really a type.
93 'Generator',
94
95 # One-off things.
96 'AnyStr',
97 'cast',
98 'get_type_hints',
Guido van Rossum91185fe2016-06-08 11:19:11 -070099 'NewType',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700100 'no_type_check',
101 'no_type_check_decorator',
Ivan Levkivskyiac560272018-03-23 21:44:54 +0000102 'NoReturn',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700103 'overload',
Guido van Rossum0e0563c2016-04-05 14:54:25 -0700104 'Text',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700105 'TYPE_CHECKING',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700106]
107
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700108# The pseudo-submodules 're' and 'io' are part of the public
109# namespace, but excluded from __all__ because they might stomp on
110# legitimate imports of those modules.
111
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700112
Miss Islington (bot)0f9b68a2018-05-22 20:51:15 -0700113def _type_check(arg, msg, is_argument=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000114 """Check that the argument is a type, and return it (internal helper).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700115
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000116 As a special case, accept None and return type(None) instead. Also wrap strings
117 into ForwardRef instances. Consider several corner cases, for example plain
118 special forms like Union are not valid, while Union[int, str] is OK, etc.
119 The msg argument is a human-readable error message, e.g::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700120
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000121 "Union[arg, ...]: arg should be a type."
Guido van Rossum4cefe742016-09-27 15:20:12 -0700122
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000123 We append the repr() of the actual value (truncated to 100 chars).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700124 """
Miss Islington (bot)9c17cd32018-05-16 15:04:39 -0700125 invalid_generic_forms = (Generic, _Protocol)
Miss Islington (bot)0f9b68a2018-05-22 20:51:15 -0700126 if is_argument:
Miss Islington (bot)9c17cd32018-05-16 15:04:39 -0700127 invalid_generic_forms = invalid_generic_forms + (ClassVar, )
128
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000129 if arg is None:
130 return type(None)
131 if isinstance(arg, str):
132 return ForwardRef(arg)
133 if (isinstance(arg, _GenericAlias) and
Miss Islington (bot)9c17cd32018-05-16 15:04:39 -0700134 arg.__origin__ in invalid_generic_forms):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000135 raise TypeError(f"{arg} is not valid as type argument")
Ismo Toijalaf71a5922018-11-28 19:22:09 +0200136 if (isinstance(arg, _SpecialForm) and arg not in (Any, NoReturn) or
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000137 arg in (Generic, _Protocol)):
138 raise TypeError(f"Plain {arg} is not valid as type argument")
139 if isinstance(arg, (type, TypeVar, ForwardRef)):
140 return arg
141 if not callable(arg):
142 raise TypeError(f"{msg} Got {arg!r:.100}.")
143 return arg
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700144
145
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000146def _type_repr(obj):
147 """Return the repr() of an object, special-casing types (internal helper).
148
149 If obj is a type, we return a shorter version than the default
150 type.__repr__, based on the module and qualified name, which is
151 typically enough to uniquely identify a type. For everything
152 else, we fall back on repr(obj).
153 """
154 if isinstance(obj, type):
155 if obj.__module__ == 'builtins':
156 return obj.__qualname__
157 return f'{obj.__module__}.{obj.__qualname__}'
158 if obj is ...:
159 return('...')
160 if isinstance(obj, types.FunctionType):
161 return obj.__name__
162 return repr(obj)
163
164
165def _collect_type_vars(types):
166 """Collect all type variable contained in types in order of
167 first appearance (lexicographic order). For example::
168
169 _collect_type_vars((T, List[S, T])) == (T, S)
170 """
171 tvars = []
172 for t in types:
173 if isinstance(t, TypeVar) and t not in tvars:
174 tvars.append(t)
175 if isinstance(t, _GenericAlias) and not t._special:
176 tvars.extend([t for t in t.__parameters__ if t not in tvars])
177 return tuple(tvars)
178
179
180def _subs_tvars(tp, tvars, subs):
181 """Substitute type variables 'tvars' with substitutions 'subs'.
182 These two must have the same length.
183 """
184 if not isinstance(tp, _GenericAlias):
185 return tp
186 new_args = list(tp.__args__)
187 for a, arg in enumerate(tp.__args__):
188 if isinstance(arg, TypeVar):
189 for i, tvar in enumerate(tvars):
190 if arg == tvar:
191 new_args[a] = subs[i]
192 else:
193 new_args[a] = _subs_tvars(arg, tvars, subs)
194 if tp.__origin__ is Union:
195 return Union[tuple(new_args)]
196 return tp.copy_with(tuple(new_args))
197
198
199def _check_generic(cls, parameters):
200 """Check correct count for parameters of a generic cls (internal helper).
201 This gives a nice error message in case of count mismatch.
202 """
203 if not cls.__parameters__:
204 raise TypeError(f"{cls} is not a generic class")
205 alen = len(parameters)
206 elen = len(cls.__parameters__)
207 if alen != elen:
208 raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
209 f" actual {alen}, expected {elen}")
210
211
212def _remove_dups_flatten(parameters):
Miss Islington (bot)09ca5902018-05-18 16:27:14 -0700213 """An internal helper for Union creation and substitution: flatten Unions
214 among parameters, then remove duplicates.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000215 """
216 # Flatten out Union[Union[...], ...].
217 params = []
218 for p in parameters:
219 if isinstance(p, _GenericAlias) and p.__origin__ is Union:
220 params.extend(p.__args__)
221 elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
222 params.extend(p[1:])
223 else:
224 params.append(p)
225 # Weed out strict duplicates, preserving the first of each occurrence.
226 all_params = set(params)
227 if len(all_params) < len(params):
228 new_params = []
229 for t in params:
230 if t in all_params:
231 new_params.append(t)
232 all_params.remove(t)
233 params = new_params
234 assert not all_params, all_params
Miss Islington (bot)09ca5902018-05-18 16:27:14 -0700235 return tuple(params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000236
237
238_cleanups = []
239
240
241def _tp_cache(func):
242 """Internal wrapper caching __getitem__ of generic types with a fallback to
243 original function for non-hashable arguments.
244 """
245 cached = functools.lru_cache()(func)
246 _cleanups.append(cached.cache_clear)
247
248 @functools.wraps(func)
249 def inner(*args, **kwds):
250 try:
251 return cached(*args, **kwds)
252 except TypeError:
253 pass # All real errors (not unhashable args) are raised below.
254 return func(*args, **kwds)
255 return inner
256
257
258def _eval_type(t, globalns, localns):
259 """Evaluate all forward reverences in the given type t.
260 For use of globalns and localns see the docstring for get_type_hints().
261 """
262 if isinstance(t, ForwardRef):
263 return t._evaluate(globalns, localns)
264 if isinstance(t, _GenericAlias):
265 ev_args = tuple(_eval_type(a, globalns, localns) for a in t.__args__)
266 if ev_args == t.__args__:
267 return t
268 res = t.copy_with(ev_args)
269 res._special = t._special
270 return res
271 return t
272
273
274class _Final:
275 """Mixin to prohibit subclassing"""
Guido van Rossum4cefe742016-09-27 15:20:12 -0700276
Guido van Rossum83ec3022017-01-17 20:43:28 -0800277 __slots__ = ('__weakref__',)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700278
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000279 def __init_subclass__(self, *args, **kwds):
280 if '_root' not in kwds:
281 raise TypeError("Cannot subclass special typing classes")
282
Miss Islington (bot)d0e04c82018-03-26 15:29:06 -0700283class _Immutable:
284 """Mixin to indicate that object should not be copied."""
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000285
Miss Islington (bot)d0e04c82018-03-26 15:29:06 -0700286 def __copy__(self):
287 return self
288
289 def __deepcopy__(self, memo):
290 return self
291
292
293class _SpecialForm(_Final, _Immutable, _root=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000294 """Internal indicator of special typing constructs.
295 See _doc instance attribute for specific docs.
296 """
297
298 __slots__ = ('_name', '_doc')
299
Guido van Rossum4cefe742016-09-27 15:20:12 -0700300 def __new__(cls, *args, **kwds):
301 """Constructor.
302
303 This only exists to give a better error message in case
304 someone tries to subclass a special typing object (not a good idea).
305 """
306 if (len(args) == 3 and
307 isinstance(args[0], str) and
308 isinstance(args[1], tuple)):
309 # Close enough.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000310 raise TypeError(f"Cannot subclass {cls!r}")
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700311 return super().__new__(cls)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700312
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000313 def __init__(self, name, doc):
314 self._name = name
315 self._doc = doc
Guido van Rossum4cefe742016-09-27 15:20:12 -0700316
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000317 def __eq__(self, other):
318 if not isinstance(other, _SpecialForm):
319 return NotImplemented
320 return self._name == other._name
321
322 def __hash__(self):
323 return hash((self._name,))
Guido van Rossum4cefe742016-09-27 15:20:12 -0700324
325 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000326 return 'typing.' + self._name
327
Miss Islington (bot)d0e04c82018-03-26 15:29:06 -0700328 def __reduce__(self):
329 return self._name
Guido van Rossum4cefe742016-09-27 15:20:12 -0700330
331 def __call__(self, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000332 raise TypeError(f"Cannot instantiate {self!r}")
333
334 def __instancecheck__(self, obj):
335 raise TypeError(f"{self} cannot be used with isinstance()")
336
337 def __subclasscheck__(self, cls):
338 raise TypeError(f"{self} cannot be used with issubclass()")
339
340 @_tp_cache
341 def __getitem__(self, parameters):
342 if self._name == 'ClassVar':
343 item = _type_check(parameters, 'ClassVar accepts only single type.')
344 return _GenericAlias(self, (item,))
345 if self._name == 'Union':
346 if parameters == ():
347 raise TypeError("Cannot take a Union of no types.")
348 if not isinstance(parameters, tuple):
349 parameters = (parameters,)
350 msg = "Union[arg, ...]: each arg must be a type."
351 parameters = tuple(_type_check(p, msg) for p in parameters)
352 parameters = _remove_dups_flatten(parameters)
353 if len(parameters) == 1:
354 return parameters[0]
355 return _GenericAlias(self, parameters)
356 if self._name == 'Optional':
357 arg = _type_check(parameters, "Optional[t] requires a single type.")
358 return Union[arg, type(None)]
359 raise TypeError(f"{self} is not subscriptable")
Guido van Rossum4cefe742016-09-27 15:20:12 -0700360
361
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000362Any = _SpecialForm('Any', doc=
363 """Special type indicating an unconstrained type.
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700364
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000365 - Any is compatible with every type.
366 - Any assumed to have all methods.
367 - All values assumed to be instances of Any.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700368
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000369 Note that all the above statements are true from the point of view of
370 static type checkers. At runtime, Any should not be used with instance
371 or class checks.
372 """)
Guido van Rossumd70fe632015-08-05 12:11:06 +0200373
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000374NoReturn = _SpecialForm('NoReturn', doc=
375 """Special type indicating functions that never return.
376 Example::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700377
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000378 from typing import NoReturn
379
380 def stop() -> NoReturn:
381 raise Exception('no way')
382
383 This type is invalid in other positions, e.g., ``List[NoReturn]``
384 will fail in static type checkers.
385 """)
386
387ClassVar = _SpecialForm('ClassVar', doc=
388 """Special type construct to mark class variables.
389
390 An annotation wrapped in ClassVar indicates that a given
391 attribute is intended to be used as a class variable and
392 should not be set on instances of that class. Usage::
393
394 class Starship:
395 stats: ClassVar[Dict[str, int]] = {} # class variable
396 damage: int = 10 # instance variable
397
398 ClassVar accepts only types and cannot be further subscribed.
399
400 Note that ClassVar is not a class itself, and should not
401 be used with isinstance() or issubclass().
402 """)
403
404Union = _SpecialForm('Union', doc=
405 """Union type; Union[X, Y] means either X or Y.
406
407 To define a union, use e.g. Union[int, str]. Details:
408 - The arguments must be types and there must be at least one.
409 - None as an argument is a special case and is replaced by
410 type(None).
411 - Unions of unions are flattened, e.g.::
412
413 Union[Union[int, str], float] == Union[int, str, float]
414
415 - Unions of a single argument vanish, e.g.::
416
417 Union[int] == int # The constructor actually returns int
418
419 - Redundant arguments are skipped, e.g.::
420
421 Union[int, str, int] == Union[int, str]
422
423 - When comparing unions, the argument order is ignored, e.g.::
424
425 Union[int, str] == Union[str, int]
426
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000427 - You cannot subclass or instantiate a union.
428 - You can use Optional[X] as a shorthand for Union[X, None].
429 """)
430
431Optional = _SpecialForm('Optional', doc=
432 """Optional type.
433
434 Optional[X] is equivalent to Union[X, None].
435 """)
Guido van Rossumb7dedc82016-10-29 12:44:29 -0700436
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700437
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000438class ForwardRef(_Final, _root=True):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800439 """Internal wrapper to hold a forward reference."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700440
Guido van Rossum4cefe742016-09-27 15:20:12 -0700441 __slots__ = ('__forward_arg__', '__forward_code__',
Miss Islington (bot)9c17cd32018-05-16 15:04:39 -0700442 '__forward_evaluated__', '__forward_value__',
443 '__forward_is_argument__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700444
Miss Islington (bot)0f9b68a2018-05-22 20:51:15 -0700445 def __init__(self, arg, is_argument=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700446 if not isinstance(arg, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000447 raise TypeError(f"Forward reference must be a string -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700448 try:
449 code = compile(arg, '<string>', 'eval')
450 except SyntaxError:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000451 raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700452 self.__forward_arg__ = arg
453 self.__forward_code__ = code
454 self.__forward_evaluated__ = False
455 self.__forward_value__ = None
Miss Islington (bot)9c17cd32018-05-16 15:04:39 -0700456 self.__forward_is_argument__ = is_argument
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700457
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000458 def _evaluate(self, globalns, localns):
Guido van Rossumdad17902016-11-10 08:29:18 -0800459 if not self.__forward_evaluated__ or localns is not globalns:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700460 if globalns is None and localns is None:
461 globalns = localns = {}
462 elif globalns is None:
463 globalns = localns
464 elif localns is None:
465 localns = globalns
466 self.__forward_value__ = _type_check(
467 eval(self.__forward_code__, globalns, localns),
Miss Islington (bot)9c17cd32018-05-16 15:04:39 -0700468 "Forward references must evaluate to types.",
469 is_argument=self.__forward_is_argument__)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700470 self.__forward_evaluated__ = True
471 return self.__forward_value__
472
Guido van Rossum4cefe742016-09-27 15:20:12 -0700473 def __eq__(self, other):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000474 if not isinstance(other, ForwardRef):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700475 return NotImplemented
476 return (self.__forward_arg__ == other.__forward_arg__ and
Guido van Rossumc7b92952016-11-10 08:24:06 -0800477 self.__forward_value__ == other.__forward_value__)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700478
479 def __hash__(self):
Guido van Rossumc7b92952016-11-10 08:24:06 -0800480 return hash((self.__forward_arg__, self.__forward_value__))
Guido van Rossum4cefe742016-09-27 15:20:12 -0700481
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700482 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000483 return f'ForwardRef({self.__forward_arg__!r})'
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700484
485
Miss Islington (bot)d0e04c82018-03-26 15:29:06 -0700486class TypeVar(_Final, _Immutable, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700487 """Type variable.
488
489 Usage::
490
491 T = TypeVar('T') # Can be anything
492 A = TypeVar('A', str, bytes) # Must be str or bytes
493
494 Type variables exist primarily for the benefit of static type
495 checkers. They serve as the parameters for generic types as well
496 as for generic function definitions. See class Generic for more
497 information on generic types. Generic functions work as follows:
498
Guido van Rossumb24569a2016-11-20 18:01:29 -0800499 def repeat(x: T, n: int) -> List[T]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700500 '''Return a list containing n references to x.'''
501 return [x]*n
502
503 def longest(x: A, y: A) -> A:
504 '''Return the longest of two strings.'''
505 return x if len(x) >= len(y) else y
506
507 The latter example's signature is essentially the overloading
508 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
509 that if the arguments are instances of some subclass of str,
510 the return type is still plain str.
511
Guido van Rossumb24569a2016-11-20 18:01:29 -0800512 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700513
Guido van Rossumefa798d2016-08-23 11:01:50 -0700514 Type variables defined with covariant=True or contravariant=True
Miss Islington (bot)e019f982018-07-07 09:05:36 -0700515 can be used to declare covariant or contravariant generic types.
Guido van Rossumefa798d2016-08-23 11:01:50 -0700516 See PEP 484 for more details. By default generic types are invariant
517 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700518
519 Type variables can be introspected. e.g.:
520
521 T.__name__ == 'T'
522 T.__constraints__ == ()
523 T.__covariant__ == False
524 T.__contravariant__ = False
525 A.__constraints__ == (str, bytes)
Miss Islington (bot)d0e04c82018-03-26 15:29:06 -0700526
527 Note that only type variables defined in global scope can be pickled.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700528 """
529
Guido van Rossum4cefe742016-09-27 15:20:12 -0700530 __slots__ = ('__name__', '__bound__', '__constraints__',
Miss Islington (bot)d4986252018-05-26 11:38:00 -0700531 '__covariant__', '__contravariant__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700532
533 def __init__(self, name, *constraints, bound=None,
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800534 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700535 self.__name__ = name
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700536 if covariant and contravariant:
Guido van Rossumefa798d2016-08-23 11:01:50 -0700537 raise ValueError("Bivariant types are not supported.")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700538 self.__covariant__ = bool(covariant)
539 self.__contravariant__ = bool(contravariant)
540 if constraints and bound is not None:
541 raise TypeError("Constraints cannot be combined with bound=...")
542 if constraints and len(constraints) == 1:
543 raise TypeError("A single constraint is not allowed")
544 msg = "TypeVar(name, constraint, ...): constraints must be types."
545 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
546 if bound:
547 self.__bound__ = _type_check(bound, "Bound must be a type.")
548 else:
549 self.__bound__ = None
Miss Islington (bot)d4986252018-05-26 11:38:00 -0700550 def_mod = sys._getframe(1).f_globals['__name__'] # for pickling
551 if def_mod != 'typing':
552 self.__module__ = def_mod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700553
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700554 def __repr__(self):
555 if self.__covariant__:
556 prefix = '+'
557 elif self.__contravariant__:
558 prefix = '-'
559 else:
560 prefix = '~'
561 return prefix + self.__name__
562
Miss Islington (bot)d0e04c82018-03-26 15:29:06 -0700563 def __reduce__(self):
Miss Islington (bot)d4986252018-05-26 11:38:00 -0700564 return self.__name__
Miss Islington (bot)d0e04c82018-03-26 15:29:06 -0700565
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700566
Guido van Rossum83ec3022017-01-17 20:43:28 -0800567# Special typing constructs Union, Optional, Generic, Callable and Tuple
568# use three special attributes for internal bookkeeping of generic types:
569# * __parameters__ is a tuple of unique free type parameters of a generic
570# type, for example, Dict[T, T].__parameters__ == (T,);
571# * __origin__ keeps a reference to a type that was subscripted,
Miss Islington (bot)3c28a632018-05-08 18:44:09 -0700572# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
573# the type.
Guido van Rossum83ec3022017-01-17 20:43:28 -0800574# * __args__ is a tuple of all arguments used in subscripting,
575# e.g., Dict[T, int].__args__ == (T, int).
576
Miss Islington (bot)04eac022018-04-04 17:46:40 -0700577
578# Mapping from non-generic type names that have a generic alias in typing
579# but with a different name.
580_normalize_alias = {'list': 'List',
581 'tuple': 'Tuple',
582 'dict': 'Dict',
583 'set': 'Set',
584 'frozenset': 'FrozenSet',
585 'deque': 'Deque',
586 'defaultdict': 'DefaultDict',
587 'type': 'Type',
588 'Set': 'AbstractSet'}
589
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000590def _is_dunder(attr):
591 return attr.startswith('__') and attr.endswith('__')
Guido van Rossum83ec3022017-01-17 20:43:28 -0800592
Guido van Rossumb24569a2016-11-20 18:01:29 -0800593
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000594class _GenericAlias(_Final, _root=True):
595 """The central part of internal API.
596
597 This represents a generic version of type 'origin' with type arguments 'params'.
598 There are two kind of these aliases: user defined and special. The special ones
599 are wrappers around builtin collections and ABCs in collections.abc. These must
600 have 'name' always set. If 'inst' is False, then the alias can't be instantiated,
601 this is used by e.g. typing.List and typing.Dict.
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700602 """
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000603 def __init__(self, origin, params, *, inst=True, special=False, name=None):
604 self._inst = inst
605 self._special = special
606 if special and name is None:
607 orig_name = origin.__name__
Miss Islington (bot)04eac022018-04-04 17:46:40 -0700608 name = _normalize_alias.get(orig_name, orig_name)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000609 self._name = name
610 if not isinstance(params, tuple):
611 params = (params,)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700612 self.__origin__ = origin
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700613 self.__args__ = tuple(... if a is _TypingEllipsis else
614 () if a is _TypingEmpty else
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000615 a for a in params)
616 self.__parameters__ = _collect_type_vars(params)
617 self.__slots__ = None # This is not documented.
618 if not name:
619 self.__module__ = origin.__module__
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700620
Guido van Rossum4cefe742016-09-27 15:20:12 -0700621 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700622 def __getitem__(self, params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000623 if self.__origin__ in (Generic, _Protocol):
624 # Can't subscript Generic[...] or _Protocol[...].
625 raise TypeError(f"Cannot subscript already-subscripted {self}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700626 if not isinstance(params, tuple):
627 params = (params,)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700628 msg = "Parameters to generic types must be types."
629 params = tuple(_type_check(p, msg) for p in params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000630 _check_generic(self, params)
631 return _subs_tvars(self, self.__parameters__, params)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100632
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000633 def copy_with(self, params):
634 # We don't copy self._special.
635 return _GenericAlias(self.__origin__, params, name=self._name, inst=self._inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700636
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000637 def __repr__(self):
638 if (self._name != 'Callable' or
639 len(self.__args__) == 2 and self.__args__[0] is Ellipsis):
640 if self._name:
641 name = 'typing.' + self._name
642 else:
643 name = _type_repr(self.__origin__)
644 if not self._special:
645 args = f'[{", ".join([_type_repr(a) for a in self.__args__])}]'
646 else:
647 args = ''
648 return (f'{name}{args}')
649 if self._special:
650 return 'typing.Callable'
651 return (f'typing.Callable'
652 f'[[{", ".join([_type_repr(a) for a in self.__args__[:-1]])}], '
653 f'{_type_repr(self.__args__[-1])}]')
654
655 def __eq__(self, other):
656 if not isinstance(other, _GenericAlias):
657 return NotImplemented
658 if self.__origin__ != other.__origin__:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100659 return False
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000660 if self.__origin__ is Union and other.__origin__ is Union:
661 return frozenset(self.__args__) == frozenset(other.__args__)
662 return self.__args__ == other.__args__
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100663
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000664 def __hash__(self):
665 if self.__origin__ is Union:
666 return hash((Union, frozenset(self.__args__)))
667 return hash((self.__origin__, self.__args__))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700668
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000669 def __call__(self, *args, **kwargs):
670 if not self._inst:
671 raise TypeError(f"Type {self._name} cannot be instantiated; "
672 f"use {self._name.lower()}() instead")
673 result = self.__origin__(*args, **kwargs)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700674 try:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000675 result.__orig_class__ = self
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700676 except AttributeError:
677 pass
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000678 return result
679
680 def __mro_entries__(self, bases):
681 if self._name: # generic version of an ABC or built-in class
682 res = []
683 if self.__origin__ not in bases:
684 res.append(self.__origin__)
685 i = bases.index(self)
686 if not any(isinstance(b, _GenericAlias) or issubclass(b, Generic)
687 for b in bases[i+1:]):
688 res.append(Generic)
689 return tuple(res)
690 if self.__origin__ is Generic:
691 i = bases.index(self)
692 for b in bases[i+1:]:
693 if isinstance(b, _GenericAlias) and b is not self:
694 return ()
695 return (self.__origin__,)
696
697 def __getattr__(self, attr):
Miss Islington (bot)32955292018-04-20 14:00:41 -0700698 # We are careful for copy and pickle.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000699 # Also for simplicity we just don't relay all dunder names
700 if '__origin__' in self.__dict__ and not _is_dunder(attr):
701 return getattr(self.__origin__, attr)
702 raise AttributeError(attr)
703
704 def __setattr__(self, attr, val):
705 if _is_dunder(attr) or attr in ('_name', '_inst', '_special'):
706 super().__setattr__(attr, val)
707 else:
708 setattr(self.__origin__, attr, val)
709
710 def __instancecheck__(self, obj):
711 return self.__subclasscheck__(type(obj))
712
713 def __subclasscheck__(self, cls):
714 if self._special:
715 if not isinstance(cls, _GenericAlias):
716 return issubclass(cls, self.__origin__)
717 if cls._special:
718 return issubclass(cls.__origin__, self.__origin__)
719 raise TypeError("Subscripted generics cannot be used with"
720 " class and instance checks")
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700721
Miss Islington (bot)d0e04c82018-03-26 15:29:06 -0700722 def __reduce__(self):
723 if self._special:
724 return self._name
Miss Islington (bot)d4986252018-05-26 11:38:00 -0700725
726 if self._name:
727 origin = globals()[self._name]
728 else:
729 origin = self.__origin__
730 if (origin is Callable and
731 not (len(self.__args__) == 2 and self.__args__[0] is Ellipsis)):
732 args = list(self.__args__[:-1]), self.__args__[-1]
733 else:
734 args = tuple(self.__args__)
735 if len(args) == 1 and not isinstance(args[0], tuple):
736 args, = args
737 return operator.getitem, (origin, args)
Miss Islington (bot)d0e04c82018-03-26 15:29:06 -0700738
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700739
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000740class _VariadicGenericAlias(_GenericAlias, _root=True):
741 """Same as _GenericAlias above but for variadic aliases. Currently,
742 this is used only by special internal aliases: Tuple and Callable.
743 """
744 def __getitem__(self, params):
745 if self._name != 'Callable' or not self._special:
746 return self.__getitem_inner__(params)
747 if not isinstance(params, tuple) or len(params) != 2:
748 raise TypeError("Callable must be used as "
749 "Callable[[arg, ...], result].")
750 args, result = params
751 if args is Ellipsis:
752 params = (Ellipsis, result)
753 else:
754 if not isinstance(args, list):
755 raise TypeError(f"Callable[args, result]: args must be a list."
756 f" Got {args}")
757 params = (tuple(args), result)
758 return self.__getitem_inner__(params)
759
760 @_tp_cache
761 def __getitem_inner__(self, params):
762 if self.__origin__ is tuple and self._special:
763 if params == ():
764 return self.copy_with((_TypingEmpty,))
765 if not isinstance(params, tuple):
766 params = (params,)
767 if len(params) == 2 and params[1] is ...:
768 msg = "Tuple[t, ...]: t must be a type."
769 p = _type_check(params[0], msg)
770 return self.copy_with((p, _TypingEllipsis))
771 msg = "Tuple[t0, t1, ...]: each t must be a type."
772 params = tuple(_type_check(p, msg) for p in params)
773 return self.copy_with(params)
774 if self.__origin__ is collections.abc.Callable and self._special:
775 args, result = params
776 msg = "Callable[args, result]: result must be a type."
777 result = _type_check(result, msg)
778 if args is Ellipsis:
779 return self.copy_with((_TypingEllipsis, result))
780 msg = "Callable[[arg, ...], result]: each arg must be a type."
781 args = tuple(_type_check(arg, msg) for arg in args)
782 params = args + (result,)
783 return self.copy_with(params)
784 return super().__getitem__(params)
785
786
787class Generic:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700788 """Abstract base class for generic types.
789
Guido van Rossumb24569a2016-11-20 18:01:29 -0800790 A generic type is typically declared by inheriting from
791 this class parameterized with one or more type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700792 For example, a generic mapping type might be defined as::
793
794 class Mapping(Generic[KT, VT]):
795 def __getitem__(self, key: KT) -> VT:
796 ...
797 # Etc.
798
799 This class can then be used as follows::
800
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700801 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700802 try:
803 return mapping[key]
804 except KeyError:
805 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700806 """
Guido van Rossumd70fe632015-08-05 12:11:06 +0200807 __slots__ = ()
808
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700809 def __new__(cls, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000810 if cls is Generic:
Guido van Rossum62fe1bb2016-10-29 16:05:26 -0700811 raise TypeError("Type Generic cannot be instantiated; "
812 "it can be used only as a base class")
Miss Islington (bot)c5444b32018-05-10 20:30:47 -0700813 if super().__new__ is object.__new__ and cls.__init__ is not object.__init__:
Miss Islington (bot)3c28a632018-05-08 18:44:09 -0700814 obj = super().__new__(cls)
815 else:
816 obj = super().__new__(cls, *args, **kwds)
817 return obj
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000818
819 @_tp_cache
820 def __class_getitem__(cls, params):
821 if not isinstance(params, tuple):
822 params = (params,)
823 if not params and cls is not Tuple:
824 raise TypeError(
825 f"Parameter list to {cls.__qualname__}[...] cannot be empty")
826 msg = "Parameters to generic types must be types."
827 params = tuple(_type_check(p, msg) for p in params)
828 if cls is Generic:
829 # Generic can only be subscripted with unique type variables.
830 if not all(isinstance(p, TypeVar) for p in params):
831 raise TypeError(
832 "Parameters to Generic[...] must all be type variables")
833 if len(set(params)) != len(params):
834 raise TypeError(
835 "Parameters to Generic[...] must all be unique")
836 elif cls is _Protocol:
837 # _Protocol is internal at the moment, just skip the check
838 pass
839 else:
840 # Subscripting a regular Generic subclass.
841 _check_generic(cls, params)
842 return _GenericAlias(cls, params)
843
844 def __init_subclass__(cls, *args, **kwargs):
Miss Islington (bot)bd85c972018-04-04 09:51:34 -0700845 super().__init_subclass__(*args, **kwargs)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000846 tvars = []
847 if '__orig_bases__' in cls.__dict__:
848 error = Generic in cls.__orig_bases__
849 else:
850 error = Generic in cls.__bases__ and cls.__name__ != '_Protocol'
851 if error:
852 raise TypeError("Cannot inherit from plain Generic")
853 if '__orig_bases__' in cls.__dict__:
854 tvars = _collect_type_vars(cls.__orig_bases__)
855 # Look for Generic[T1, ..., Tn].
856 # If found, tvars must be a subset of it.
857 # If not found, tvars is it.
858 # Also check for and reject plain Generic,
859 # and reject multiple Generic[...].
860 gvars = None
861 for base in cls.__orig_bases__:
862 if (isinstance(base, _GenericAlias) and
863 base.__origin__ is Generic):
864 if gvars is not None:
865 raise TypeError(
866 "Cannot inherit from Generic[...] multiple types.")
867 gvars = base.__parameters__
868 if gvars is None:
869 gvars = tvars
870 else:
871 tvarset = set(tvars)
872 gvarset = set(gvars)
873 if not tvarset <= gvarset:
874 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
875 s_args = ', '.join(str(g) for g in gvars)
876 raise TypeError(f"Some type variables ({s_vars}) are"
877 f" not listed in Generic[{s_args}]")
878 tvars = gvars
879 cls.__parameters__ = tuple(tvars)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700880
881
882class _TypingEmpty:
Guido van Rossumb24569a2016-11-20 18:01:29 -0800883 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
884 to allow empty list/tuple in specific places, without allowing them
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700885 to sneak in where prohibited.
886 """
887
888
889class _TypingEllipsis:
Guido van Rossumb24569a2016-11-20 18:01:29 -0800890 """Internal placeholder for ... (ellipsis)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700891
892
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700893def cast(typ, val):
894 """Cast a value to a type.
895
896 This returns the value unchanged. To the type checker this
897 signals that the return value has the designated type, but at
898 runtime we intentionally don't check anything (we want this
899 to be as fast as possible).
900 """
901 return val
902
903
904def _get_defaults(func):
905 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -0800906 try:
907 code = func.__code__
908 except AttributeError:
909 # Some built-in functions don't have __code__, __defaults__, etc.
910 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700911 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700912 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700913 arg_names = arg_names[:pos_count]
914 defaults = func.__defaults__ or ()
915 kwdefaults = func.__kwdefaults__
916 res = dict(kwdefaults) if kwdefaults else {}
917 pos_offset = pos_count - len(defaults)
918 for name, value in zip(arg_names[pos_offset:], defaults):
919 assert name not in res
920 res[name] = value
921 return res
922
923
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100924_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
925 types.MethodType, types.ModuleType,
Ivan Levkivskyif06e0212017-05-02 19:14:07 +0200926 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100927
928
Guido van Rossum991d14f2016-11-09 13:12:51 -0800929def get_type_hints(obj, globalns=None, localns=None):
930 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700931
Guido van Rossum991d14f2016-11-09 13:12:51 -0800932 This is often the same as obj.__annotations__, but it handles
933 forward references encoded as string literals, and if necessary
934 adds Optional[t] if a default value equal to None is set.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700935
Guido van Rossum991d14f2016-11-09 13:12:51 -0800936 The argument may be a module, class, method, or function. The annotations
937 are returned as a dictionary. For classes, annotations include also
938 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700939
Guido van Rossum991d14f2016-11-09 13:12:51 -0800940 TypeError is raised if the argument is not of a type that can contain
941 annotations, and an empty dictionary is returned if no annotations are
942 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700943
Guido van Rossum991d14f2016-11-09 13:12:51 -0800944 BEWARE -- the behavior of globalns and localns is counterintuitive
945 (unless you are familiar with how eval() and exec() work). The
946 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700947
Guido van Rossum991d14f2016-11-09 13:12:51 -0800948 - If no dict arguments are passed, an attempt is made to use the
Łukasz Langaf350a262017-09-14 14:33:00 -0400949 globals from obj (or the respective module's globals for classes),
950 and these are also used as the locals. If the object does not appear
951 to have globals, an empty dictionary is used.
Guido van Rossum0a6976d2016-09-11 15:34:56 -0700952
Guido van Rossum991d14f2016-11-09 13:12:51 -0800953 - If one dict argument is passed, it is used for both globals and
954 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -0700955
Guido van Rossum991d14f2016-11-09 13:12:51 -0800956 - If two dict arguments are passed, they specify globals and
957 locals, respectively.
958 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -0700959
Guido van Rossum991d14f2016-11-09 13:12:51 -0800960 if getattr(obj, '__no_type_check__', None):
961 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -0800962 # Classes require a special treatment.
963 if isinstance(obj, type):
964 hints = {}
965 for base in reversed(obj.__mro__):
Łukasz Langaf350a262017-09-14 14:33:00 -0400966 if globalns is None:
967 base_globals = sys.modules[base.__module__].__dict__
968 else:
969 base_globals = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -0800970 ann = base.__dict__.get('__annotations__', {})
971 for name, value in ann.items():
972 if value is None:
973 value = type(None)
974 if isinstance(value, str):
Miss Islington (bot)0f9b68a2018-05-22 20:51:15 -0700975 value = ForwardRef(value, is_argument=False)
Łukasz Langaf350a262017-09-14 14:33:00 -0400976 value = _eval_type(value, base_globals, localns)
Guido van Rossum991d14f2016-11-09 13:12:51 -0800977 hints[name] = value
978 return hints
Łukasz Langaf350a262017-09-14 14:33:00 -0400979
980 if globalns is None:
981 if isinstance(obj, types.ModuleType):
982 globalns = obj.__dict__
983 else:
984 globalns = getattr(obj, '__globals__', {})
985 if localns is None:
986 localns = globalns
987 elif localns is None:
988 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -0800989 hints = getattr(obj, '__annotations__', None)
990 if hints is None:
991 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100992 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -0700993 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -0800994 else:
995 raise TypeError('{!r} is not a module, class, method, '
996 'or function.'.format(obj))
997 defaults = _get_defaults(obj)
998 hints = dict(hints)
999 for name, value in hints.items():
1000 if value is None:
1001 value = type(None)
1002 if isinstance(value, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001003 value = ForwardRef(value)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001004 value = _eval_type(value, globalns, localns)
1005 if name in defaults and defaults[name] is None:
1006 value = Optional[value]
1007 hints[name] = value
1008 return hints
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001009
1010
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001011def no_type_check(arg):
1012 """Decorator to indicate that annotations are not type hints.
1013
1014 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001015 applies recursively to all methods and classes defined in that class
1016 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001017
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001018 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001019 """
1020 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001021 arg_attrs = arg.__dict__.copy()
1022 for attr, val in arg.__dict__.items():
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001023 if val in arg.__bases__ + (arg,):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001024 arg_attrs.pop(attr)
1025 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001026 if isinstance(obj, types.FunctionType):
1027 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001028 if isinstance(obj, type):
1029 no_type_check(obj)
1030 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001031 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001032 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001033 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001034 return arg
1035
1036
1037def no_type_check_decorator(decorator):
1038 """Decorator to give another decorator the @no_type_check effect.
1039
1040 This wraps the decorator with something that wraps the decorated
1041 function in @no_type_check.
1042 """
1043
1044 @functools.wraps(decorator)
1045 def wrapped_decorator(*args, **kwds):
1046 func = decorator(*args, **kwds)
1047 func = no_type_check(func)
1048 return func
1049
1050 return wrapped_decorator
1051
1052
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001053def _overload_dummy(*args, **kwds):
1054 """Helper for @overload to raise when called."""
1055 raise NotImplementedError(
1056 "You should not call an overloaded function. "
1057 "A series of @overload-decorated functions "
1058 "outside a stub module should always be followed "
1059 "by an implementation that is not @overload-ed.")
1060
1061
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001062def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001063 """Decorator for overloaded functions/methods.
1064
1065 In a stub file, place two or more stub definitions for the same
1066 function in a row, each decorated with @overload. For example:
1067
1068 @overload
1069 def utf8(value: None) -> None: ...
1070 @overload
1071 def utf8(value: bytes) -> bytes: ...
1072 @overload
1073 def utf8(value: str) -> bytes: ...
1074
1075 In a non-stub file (i.e. a regular .py file), do the same but
1076 follow it with an implementation. The implementation should *not*
1077 be decorated with @overload. For example:
1078
1079 @overload
1080 def utf8(value: None) -> None: ...
1081 @overload
1082 def utf8(value: bytes) -> bytes: ...
1083 @overload
1084 def utf8(value: str) -> bytes: ...
1085 def utf8(value):
1086 # implementation goes here
1087 """
1088 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001089
1090
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001091class _ProtocolMeta(type):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001092 """Internal metaclass for _Protocol.
1093
1094 This exists so _Protocol classes can be generic without deriving
1095 from Generic.
1096 """
1097
Guido van Rossumd70fe632015-08-05 12:11:06 +02001098 def __instancecheck__(self, obj):
Guido van Rossumca4b2522016-11-19 10:32:41 -08001099 if _Protocol not in self.__bases__:
1100 return super().__instancecheck__(obj)
Guido van Rossumd70fe632015-08-05 12:11:06 +02001101 raise TypeError("Protocols cannot be used with isinstance().")
1102
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001103 def __subclasscheck__(self, cls):
1104 if not self._is_protocol:
1105 # No structural checks since this isn't a protocol.
1106 return NotImplemented
1107
1108 if self is _Protocol:
1109 # Every class is a subclass of the empty protocol.
1110 return True
1111
1112 # Find all attributes defined in the protocol.
1113 attrs = self._get_protocol_attrs()
1114
1115 for attr in attrs:
1116 if not any(attr in d.__dict__ for d in cls.__mro__):
1117 return False
1118 return True
1119
1120 def _get_protocol_attrs(self):
1121 # Get all Protocol base classes.
1122 protocol_bases = []
1123 for c in self.__mro__:
1124 if getattr(c, '_is_protocol', False) and c.__name__ != '_Protocol':
1125 protocol_bases.append(c)
1126
1127 # Get attributes included in protocol.
1128 attrs = set()
1129 for base in protocol_bases:
1130 for attr in base.__dict__.keys():
1131 # Include attributes not defined in any non-protocol bases.
1132 for c in self.__mro__:
1133 if (c is not base and attr in c.__dict__ and
1134 not getattr(c, '_is_protocol', False)):
1135 break
1136 else:
1137 if (not attr.startswith('_abc_') and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001138 attr != '__abstractmethods__' and
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001139 attr != '__annotations__' and
1140 attr != '__weakref__' and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001141 attr != '_is_protocol' and
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001142 attr != '_gorg' and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001143 attr != '__dict__' and
1144 attr != '__args__' and
1145 attr != '__slots__' and
1146 attr != '_get_protocol_attrs' and
1147 attr != '__next_in_mro__' and
1148 attr != '__parameters__' and
1149 attr != '__origin__' and
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001150 attr != '__orig_bases__' and
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001151 attr != '__extra__' and
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001152 attr != '__tree_hash__' and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001153 attr != '__module__'):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001154 attrs.add(attr)
1155
1156 return attrs
1157
1158
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001159class _Protocol(Generic, metaclass=_ProtocolMeta):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001160 """Internal base class for protocol classes.
1161
Guido van Rossumb24569a2016-11-20 18:01:29 -08001162 This implements a simple-minded structural issubclass check
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001163 (similar but more general than the one-offs in collections.abc
1164 such as Hashable).
1165 """
1166
Guido van Rossumd70fe632015-08-05 12:11:06 +02001167 __slots__ = ()
1168
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001169 _is_protocol = True
1170
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001171 def __class_getitem__(cls, params):
1172 return super().__class_getitem__(params)
1173
1174
1175# Some unconstrained type variables. These are used by the container types.
1176# (These are not for export.)
1177T = TypeVar('T') # Any type.
1178KT = TypeVar('KT') # Key type.
1179VT = TypeVar('VT') # Value type.
1180T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
1181V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
1182VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
1183T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
1184# Internal type variable used for Type[].
1185CT_co = TypeVar('CT_co', covariant=True, bound=type)
1186
1187# A useful type variable with constraints. This represents string types.
1188# (This one *is* for export!)
1189AnyStr = TypeVar('AnyStr', bytes, str)
1190
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001191
1192# Various ABCs mimicking those in collections.abc.
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001193def _alias(origin, params, inst=True):
1194 return _GenericAlias(origin, params, special=True, inst=inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001195
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001196Hashable = _alias(collections.abc.Hashable, ()) # Not generic.
1197Awaitable = _alias(collections.abc.Awaitable, T_co)
1198Coroutine = _alias(collections.abc.Coroutine, (T_co, T_contra, V_co))
1199AsyncIterable = _alias(collections.abc.AsyncIterable, T_co)
1200AsyncIterator = _alias(collections.abc.AsyncIterator, T_co)
1201Iterable = _alias(collections.abc.Iterable, T_co)
1202Iterator = _alias(collections.abc.Iterator, T_co)
1203Reversible = _alias(collections.abc.Reversible, T_co)
1204Sized = _alias(collections.abc.Sized, ()) # Not generic.
1205Container = _alias(collections.abc.Container, T_co)
1206Collection = _alias(collections.abc.Collection, T_co)
1207Callable = _VariadicGenericAlias(collections.abc.Callable, (), special=True)
1208Callable.__doc__ = \
1209 """Callable type; Callable[[int], str] is a function of (int) -> str.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001210
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001211 The subscription syntax must always be used with exactly two
1212 values: the argument list and the return type. The argument list
1213 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001214
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001215 There is no syntax to indicate optional or keyword arguments,
1216 such function types are rarely used as callback types.
1217 """
1218AbstractSet = _alias(collections.abc.Set, T_co)
1219MutableSet = _alias(collections.abc.MutableSet, T)
1220# NOTE: Mapping is only covariant in the value type.
1221Mapping = _alias(collections.abc.Mapping, (KT, VT_co))
1222MutableMapping = _alias(collections.abc.MutableMapping, (KT, VT))
1223Sequence = _alias(collections.abc.Sequence, T_co)
1224MutableSequence = _alias(collections.abc.MutableSequence, T)
1225ByteString = _alias(collections.abc.ByteString, ()) # Not generic
1226Tuple = _VariadicGenericAlias(tuple, (), inst=False, special=True)
1227Tuple.__doc__ = \
1228 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001229
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001230 Example: Tuple[T1, T2] is a tuple of two elements corresponding
1231 to type variables T1 and T2. Tuple[int, float, str] is a tuple
1232 of an int, a float and a string.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001233
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001234 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
1235 """
1236List = _alias(list, T, inst=False)
1237Deque = _alias(collections.deque, T)
1238Set = _alias(set, T, inst=False)
1239FrozenSet = _alias(frozenset, T_co, inst=False)
1240MappingView = _alias(collections.abc.MappingView, T_co)
1241KeysView = _alias(collections.abc.KeysView, KT)
1242ItemsView = _alias(collections.abc.ItemsView, (KT, VT_co))
1243ValuesView = _alias(collections.abc.ValuesView, VT_co)
1244ContextManager = _alias(contextlib.AbstractContextManager, T_co)
1245AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, T_co)
1246Dict = _alias(dict, (KT, VT), inst=False)
1247DefaultDict = _alias(collections.defaultdict, (KT, VT))
Miss Islington (bot)6cb04862018-12-02 08:14:44 -08001248OrderedDict = _alias(collections.OrderedDict, (KT, VT))
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001249Counter = _alias(collections.Counter, T)
1250ChainMap = _alias(collections.ChainMap, (KT, VT))
1251Generator = _alias(collections.abc.Generator, (T_co, T_contra, V_co))
1252AsyncGenerator = _alias(collections.abc.AsyncGenerator, (T_co, T_contra))
1253Type = _alias(type, CT_co, inst=False)
1254Type.__doc__ = \
1255 """A special construct usable to annotate class objects.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001256
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001257 For example, suppose we have the following classes::
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001258
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001259 class User: ... # Abstract base for User classes
1260 class BasicUser(User): ...
1261 class ProUser(User): ...
1262 class TeamUser(User): ...
Guido van Rossumf17c2002015-12-03 17:31:24 -08001263
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001264 And a function that takes a class argument that's a subclass of
1265 User and returns an instance of the corresponding class::
Guido van Rossumf17c2002015-12-03 17:31:24 -08001266
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001267 U = TypeVar('U', bound=User)
1268 def new_user(user_class: Type[U]) -> U:
1269 user = user_class()
1270 # (Here we could write the user object to a database)
1271 return user
Guido van Rossumf17c2002015-12-03 17:31:24 -08001272
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001273 joe = new_user(BasicUser)
Guido van Rossumf17c2002015-12-03 17:31:24 -08001274
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001275 At this point the type checker knows that joe has type BasicUser.
1276 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001277
1278
1279class SupportsInt(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001280 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001281
1282 @abstractmethod
1283 def __int__(self) -> int:
1284 pass
1285
1286
1287class SupportsFloat(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001288 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001289
1290 @abstractmethod
1291 def __float__(self) -> float:
1292 pass
1293
1294
1295class SupportsComplex(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001296 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001297
1298 @abstractmethod
1299 def __complex__(self) -> complex:
1300 pass
1301
1302
1303class SupportsBytes(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001304 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001305
1306 @abstractmethod
1307 def __bytes__(self) -> bytes:
1308 pass
1309
1310
Guido van Rossumd70fe632015-08-05 12:11:06 +02001311class SupportsAbs(_Protocol[T_co]):
1312 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001313
1314 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001315 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001316 pass
1317
1318
Guido van Rossumd70fe632015-08-05 12:11:06 +02001319class SupportsRound(_Protocol[T_co]):
1320 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001321
1322 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001323 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001324 pass
1325
1326
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001327def _make_nmtuple(name, types):
Guido van Rossum2f841442016-11-15 09:48:06 -08001328 msg = "NamedTuple('Name', [(f0, t0), (f1, t1), ...]); each t must be a type"
1329 types = [(n, _type_check(t, msg)) for n, t in types]
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001330 nm_tpl = collections.namedtuple(name, [n for n, t in types])
Guido van Rossum83ec3022017-01-17 20:43:28 -08001331 # Prior to PEP 526, only _field_types attribute was assigned.
1332 # Now, both __annotations__ and _field_types are used to maintain compatibility.
1333 nm_tpl.__annotations__ = nm_tpl._field_types = collections.OrderedDict(types)
Guido van Rossum557d1eb2015-11-19 08:16:31 -08001334 try:
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001335 nm_tpl.__module__ = sys._getframe(2).f_globals.get('__name__', '__main__')
Guido van Rossum557d1eb2015-11-19 08:16:31 -08001336 except (AttributeError, ValueError):
1337 pass
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001338 return nm_tpl
1339
1340
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001341# attributes prohibited to set in NamedTuple class syntax
1342_prohibited = ('__new__', '__init__', '__slots__', '__getnewargs__',
1343 '_fields', '_field_defaults', '_field_types',
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02001344 '_make', '_replace', '_asdict', '_source')
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001345
1346_special = ('__module__', '__name__', '__qualname__', '__annotations__')
1347
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001348
Guido van Rossum2f841442016-11-15 09:48:06 -08001349class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001350
Guido van Rossum2f841442016-11-15 09:48:06 -08001351 def __new__(cls, typename, bases, ns):
1352 if ns.get('_root', False):
1353 return super().__new__(cls, typename, bases, ns)
Guido van Rossum2f841442016-11-15 09:48:06 -08001354 types = ns.get('__annotations__', {})
Guido van Rossum3c268be2017-01-18 08:03:50 -08001355 nm_tpl = _make_nmtuple(typename, types.items())
1356 defaults = []
1357 defaults_dict = {}
1358 for field_name in types:
1359 if field_name in ns:
1360 default_value = ns[field_name]
1361 defaults.append(default_value)
1362 defaults_dict[field_name] = default_value
1363 elif defaults:
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001364 raise TypeError("Non-default namedtuple field {field_name} cannot "
1365 "follow default field(s) {default_names}"
Guido van Rossum3c268be2017-01-18 08:03:50 -08001366 .format(field_name=field_name,
1367 default_names=', '.join(defaults_dict.keys())))
Miss Islington (bot)3c28a632018-05-08 18:44:09 -07001368 nm_tpl.__new__.__annotations__ = collections.OrderedDict(types)
Guido van Rossum3c268be2017-01-18 08:03:50 -08001369 nm_tpl.__new__.__defaults__ = tuple(defaults)
1370 nm_tpl._field_defaults = defaults_dict
Guido van Rossum95919c02017-01-22 17:47:20 -08001371 # update from user namespace without overriding special namedtuple attributes
1372 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001373 if key in _prohibited:
1374 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
1375 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08001376 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08001377 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001378
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001379
Guido van Rossum2f841442016-11-15 09:48:06 -08001380class NamedTuple(metaclass=NamedTupleMeta):
1381 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001382
Guido van Rossum2f841442016-11-15 09:48:06 -08001383 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001384
Guido van Rossum2f841442016-11-15 09:48:06 -08001385 class Employee(NamedTuple):
1386 name: str
1387 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001388
Guido van Rossum2f841442016-11-15 09:48:06 -08001389 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001390
Guido van Rossum2f841442016-11-15 09:48:06 -08001391 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001392
Guido van Rossum83ec3022017-01-17 20:43:28 -08001393 The resulting class has extra __annotations__ and _field_types
1394 attributes, giving an ordered dict mapping field names to types.
1395 __annotations__ should be preferred, while _field_types
1396 is kept to maintain pre PEP 526 compatibility. (The field names
Guido van Rossum2f841442016-11-15 09:48:06 -08001397 are in the _fields attribute, which is part of the namedtuple
1398 API.) Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001399
Guido van Rossum2f841442016-11-15 09:48:06 -08001400 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001401
Guido van Rossum2f841442016-11-15 09:48:06 -08001402 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001403
Guido van Rossum2f841442016-11-15 09:48:06 -08001404 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
1405 """
1406 _root = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001407
Guido van Rossum2f841442016-11-15 09:48:06 -08001408 def __new__(self, typename, fields=None, **kwargs):
Guido van Rossum2f841442016-11-15 09:48:06 -08001409 if fields is None:
1410 fields = kwargs.items()
1411 elif kwargs:
1412 raise TypeError("Either list of fields or keywords"
1413 " can be provided to NamedTuple, not both")
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001414 return _make_nmtuple(typename, fields)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001415
1416
Guido van Rossum91185fe2016-06-08 11:19:11 -07001417def NewType(name, tp):
1418 """NewType creates simple unique types with almost zero
1419 runtime overhead. NewType(name, tp) is considered a subtype of tp
1420 by static type checkers. At runtime, NewType(name, tp) returns
1421 a dummy function that simply returns its argument. Usage::
1422
1423 UserId = NewType('UserId', int)
1424
1425 def name_by_id(user_id: UserId) -> str:
1426 ...
1427
1428 UserId('user') # Fails type check
1429
1430 name_by_id(42) # Fails type check
1431 name_by_id(UserId(42)) # OK
1432
1433 num = UserId(5) + 1 # type: int
1434 """
1435
1436 def new_type(x):
1437 return x
1438
1439 new_type.__name__ = name
1440 new_type.__supertype__ = tp
1441 return new_type
1442
1443
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001444# Python-version-specific alias (Python 2: unicode; Python 3: str)
1445Text = str
1446
1447
Guido van Rossum91185fe2016-06-08 11:19:11 -07001448# Constant that's True when type checking, but False here.
1449TYPE_CHECKING = False
1450
1451
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001452class IO(Generic[AnyStr]):
1453 """Generic base class for TextIO and BinaryIO.
1454
1455 This is an abstract, generic version of the return of open().
1456
1457 NOTE: This does not distinguish between the different possible
1458 classes (text vs. binary, read vs. write vs. read/write,
1459 append-only, unbuffered). The TextIO and BinaryIO subclasses
1460 below capture the distinctions between text vs. binary, which is
1461 pervasive in the interface; however we currently do not offer a
1462 way to track the other distinctions in the type system.
1463 """
1464
Guido van Rossumd70fe632015-08-05 12:11:06 +02001465 __slots__ = ()
1466
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001467 @abstractproperty
1468 def mode(self) -> str:
1469 pass
1470
1471 @abstractproperty
1472 def name(self) -> str:
1473 pass
1474
1475 @abstractmethod
1476 def close(self) -> None:
1477 pass
1478
1479 @abstractmethod
1480 def closed(self) -> bool:
1481 pass
1482
1483 @abstractmethod
1484 def fileno(self) -> int:
1485 pass
1486
1487 @abstractmethod
1488 def flush(self) -> None:
1489 pass
1490
1491 @abstractmethod
1492 def isatty(self) -> bool:
1493 pass
1494
1495 @abstractmethod
1496 def read(self, n: int = -1) -> AnyStr:
1497 pass
1498
1499 @abstractmethod
1500 def readable(self) -> bool:
1501 pass
1502
1503 @abstractmethod
1504 def readline(self, limit: int = -1) -> AnyStr:
1505 pass
1506
1507 @abstractmethod
1508 def readlines(self, hint: int = -1) -> List[AnyStr]:
1509 pass
1510
1511 @abstractmethod
1512 def seek(self, offset: int, whence: int = 0) -> int:
1513 pass
1514
1515 @abstractmethod
1516 def seekable(self) -> bool:
1517 pass
1518
1519 @abstractmethod
1520 def tell(self) -> int:
1521 pass
1522
1523 @abstractmethod
1524 def truncate(self, size: int = None) -> int:
1525 pass
1526
1527 @abstractmethod
1528 def writable(self) -> bool:
1529 pass
1530
1531 @abstractmethod
1532 def write(self, s: AnyStr) -> int:
1533 pass
1534
1535 @abstractmethod
1536 def writelines(self, lines: List[AnyStr]) -> None:
1537 pass
1538
1539 @abstractmethod
1540 def __enter__(self) -> 'IO[AnyStr]':
1541 pass
1542
1543 @abstractmethod
1544 def __exit__(self, type, value, traceback) -> None:
1545 pass
1546
1547
1548class BinaryIO(IO[bytes]):
1549 """Typed version of the return of open() in binary mode."""
1550
Guido van Rossumd70fe632015-08-05 12:11:06 +02001551 __slots__ = ()
1552
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001553 @abstractmethod
1554 def write(self, s: Union[bytes, bytearray]) -> int:
1555 pass
1556
1557 @abstractmethod
1558 def __enter__(self) -> 'BinaryIO':
1559 pass
1560
1561
1562class TextIO(IO[str]):
1563 """Typed version of the return of open() in text mode."""
1564
Guido van Rossumd70fe632015-08-05 12:11:06 +02001565 __slots__ = ()
1566
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001567 @abstractproperty
1568 def buffer(self) -> BinaryIO:
1569 pass
1570
1571 @abstractproperty
1572 def encoding(self) -> str:
1573 pass
1574
1575 @abstractproperty
Guido van Rossum991d14f2016-11-09 13:12:51 -08001576 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001577 pass
1578
1579 @abstractproperty
1580 def line_buffering(self) -> bool:
1581 pass
1582
1583 @abstractproperty
1584 def newlines(self) -> Any:
1585 pass
1586
1587 @abstractmethod
1588 def __enter__(self) -> 'TextIO':
1589 pass
1590
1591
1592class io:
1593 """Wrapper namespace for IO generic classes."""
1594
1595 __all__ = ['IO', 'TextIO', 'BinaryIO']
1596 IO = IO
1597 TextIO = TextIO
1598 BinaryIO = BinaryIO
1599
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001600
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001601io.__name__ = __name__ + '.io'
1602sys.modules[io.__name__] = io
1603
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001604Pattern = _alias(stdlib_re.Pattern, AnyStr)
1605Match = _alias(stdlib_re.Match, AnyStr)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001606
1607class re:
1608 """Wrapper namespace for re type aliases."""
1609
1610 __all__ = ['Pattern', 'Match']
1611 Pattern = Pattern
1612 Match = Match
1613
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001614
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001615re.__name__ = __name__ + '.re'
1616sys.modules[re.__name__] = re