blob: b10615c07fbdf93bed3f59577ee2a25110899f5c [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:
5* Imports and exports, all public names should be explicitelly added to __all__.
6* 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
27import re as stdlib_re # Avoid confusion with the re we export.
28import sys
29import types
Ivan Levkivskyid911e402018-01-20 11:23:59 +000030from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070031
32# Please keep __all__ alphabetized within each category.
33__all__ = [
34 # Super-special typing primitives.
35 'Any',
36 'Callable',
Guido van Rossum0a6976d2016-09-11 15:34:56 -070037 'ClassVar',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070038 'Generic',
39 'Optional',
Guido van Rossumeb9aca32016-05-24 16:38:22 -070040 'Tuple',
41 'Type',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070042 'TypeVar',
43 'Union',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070044
45 # ABCs (from collections.abc).
46 'AbstractSet', # collections.abc.Set.
47 'ByteString',
48 'Container',
Ivan Levkivskyi29fda8d2017-06-10 21:57:56 +020049 'ContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070050 'Hashable',
51 'ItemsView',
52 'Iterable',
53 'Iterator',
54 'KeysView',
55 'Mapping',
56 'MappingView',
57 'MutableMapping',
58 'MutableSequence',
59 'MutableSet',
60 'Sequence',
61 'Sized',
62 'ValuesView',
Ivan Levkivskyid911e402018-01-20 11:23:59 +000063 'Awaitable',
64 'AsyncIterator',
65 'AsyncIterable',
66 'Coroutine',
67 'Collection',
68 'AsyncGenerator',
69 'AsyncContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070070
71 # Structural checks, a.k.a. protocols.
72 'Reversible',
73 'SupportsAbs',
Ivan Levkivskyif06e0212017-05-02 19:14:07 +020074 'SupportsBytes',
75 'SupportsComplex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070076 'SupportsFloat',
77 'SupportsInt',
78 'SupportsRound',
79
80 # Concrete collection types.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +010081 'Counter',
Raymond Hettinger80490522017-01-16 22:42:37 -080082 'Deque',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070083 'Dict',
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070084 'DefaultDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070085 'List',
86 'Set',
Guido van Rossumefa798d2016-08-23 11:01:50 -070087 'FrozenSet',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070088 'NamedTuple', # Not really a type.
89 'Generator',
90
91 # One-off things.
92 'AnyStr',
93 'cast',
94 'get_type_hints',
Guido van Rossum91185fe2016-06-08 11:19:11 -070095 'NewType',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070096 'no_type_check',
97 'no_type_check_decorator',
Ivan Levkivskyiac560272018-03-23 21:44:54 +000098 'NoReturn',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070099 'overload',
Guido van Rossum0e0563c2016-04-05 14:54:25 -0700100 'Text',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700101 'TYPE_CHECKING',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700102]
103
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700104# The pseudo-submodules 're' and 'io' are part of the public
105# namespace, but excluded from __all__ because they might stomp on
106# legitimate imports of those modules.
107
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700108
Miss Islington (bot)9c17cd32018-05-16 15:04:39 -0700109def _type_check(arg, msg, is_argument=False):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000110 """Check that the argument is a type, and return it (internal helper).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700111
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000112 As a special case, accept None and return type(None) instead. Also wrap strings
113 into ForwardRef instances. Consider several corner cases, for example plain
114 special forms like Union are not valid, while Union[int, str] is OK, etc.
115 The msg argument is a human-readable error message, e.g::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700116
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000117 "Union[arg, ...]: arg should be a type."
Guido van Rossum4cefe742016-09-27 15:20:12 -0700118
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000119 We append the repr() of the actual value (truncated to 100 chars).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700120 """
Miss Islington (bot)9c17cd32018-05-16 15:04:39 -0700121 invalid_generic_forms = (Generic, _Protocol)
122 if not is_argument:
123 invalid_generic_forms = invalid_generic_forms + (ClassVar, )
124
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000125 if arg is None:
126 return type(None)
127 if isinstance(arg, str):
128 return ForwardRef(arg)
129 if (isinstance(arg, _GenericAlias) and
Miss Islington (bot)9c17cd32018-05-16 15:04:39 -0700130 arg.__origin__ in invalid_generic_forms):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000131 raise TypeError(f"{arg} is not valid as type argument")
132 if (isinstance(arg, _SpecialForm) and arg is not Any or
133 arg in (Generic, _Protocol)):
134 raise TypeError(f"Plain {arg} is not valid as type argument")
135 if isinstance(arg, (type, TypeVar, ForwardRef)):
136 return arg
137 if not callable(arg):
138 raise TypeError(f"{msg} Got {arg!r:.100}.")
139 return arg
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700140
141
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000142def _type_repr(obj):
143 """Return the repr() of an object, special-casing types (internal helper).
144
145 If obj is a type, we return a shorter version than the default
146 type.__repr__, based on the module and qualified name, which is
147 typically enough to uniquely identify a type. For everything
148 else, we fall back on repr(obj).
149 """
150 if isinstance(obj, type):
151 if obj.__module__ == 'builtins':
152 return obj.__qualname__
153 return f'{obj.__module__}.{obj.__qualname__}'
154 if obj is ...:
155 return('...')
156 if isinstance(obj, types.FunctionType):
157 return obj.__name__
158 return repr(obj)
159
160
161def _collect_type_vars(types):
162 """Collect all type variable contained in types in order of
163 first appearance (lexicographic order). For example::
164
165 _collect_type_vars((T, List[S, T])) == (T, S)
166 """
167 tvars = []
168 for t in types:
169 if isinstance(t, TypeVar) and t not in tvars:
170 tvars.append(t)
171 if isinstance(t, _GenericAlias) and not t._special:
172 tvars.extend([t for t in t.__parameters__ if t not in tvars])
173 return tuple(tvars)
174
175
176def _subs_tvars(tp, tvars, subs):
177 """Substitute type variables 'tvars' with substitutions 'subs'.
178 These two must have the same length.
179 """
180 if not isinstance(tp, _GenericAlias):
181 return tp
182 new_args = list(tp.__args__)
183 for a, arg in enumerate(tp.__args__):
184 if isinstance(arg, TypeVar):
185 for i, tvar in enumerate(tvars):
186 if arg == tvar:
187 new_args[a] = subs[i]
188 else:
189 new_args[a] = _subs_tvars(arg, tvars, subs)
190 if tp.__origin__ is Union:
191 return Union[tuple(new_args)]
192 return tp.copy_with(tuple(new_args))
193
194
195def _check_generic(cls, parameters):
196 """Check correct count for parameters of a generic cls (internal helper).
197 This gives a nice error message in case of count mismatch.
198 """
199 if not cls.__parameters__:
200 raise TypeError(f"{cls} is not a generic class")
201 alen = len(parameters)
202 elen = len(cls.__parameters__)
203 if alen != elen:
204 raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
205 f" actual {alen}, expected {elen}")
206
207
208def _remove_dups_flatten(parameters):
209 """An internal helper for Union creation and substitution: flatten Union's
210 among parameters, then remove duplicates and strict subclasses.
211 """
212 # Flatten out Union[Union[...], ...].
213 params = []
214 for p in parameters:
215 if isinstance(p, _GenericAlias) and p.__origin__ is Union:
216 params.extend(p.__args__)
217 elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
218 params.extend(p[1:])
219 else:
220 params.append(p)
221 # Weed out strict duplicates, preserving the first of each occurrence.
222 all_params = set(params)
223 if len(all_params) < len(params):
224 new_params = []
225 for t in params:
226 if t in all_params:
227 new_params.append(t)
228 all_params.remove(t)
229 params = new_params
230 assert not all_params, all_params
231 # Weed out subclasses.
232 # E.g. Union[int, Employee, Manager] == Union[int, Employee].
233 # If object is present it will be sole survivor among proper classes.
234 # Never discard type variables.
235 # (In particular, Union[str, AnyStr] != AnyStr.)
236 all_params = set(params)
237 for t1 in params:
238 if not isinstance(t1, type):
239 continue
240 if any((isinstance(t2, type) or
241 isinstance(t2, _GenericAlias) and t2._special) and issubclass(t1, t2)
242 for t2 in all_params - {t1}):
243 all_params.remove(t1)
244 return tuple(t for t in params if t in all_params)
245
246
247_cleanups = []
248
249
250def _tp_cache(func):
251 """Internal wrapper caching __getitem__ of generic types with a fallback to
252 original function for non-hashable arguments.
253 """
254 cached = functools.lru_cache()(func)
255 _cleanups.append(cached.cache_clear)
256
257 @functools.wraps(func)
258 def inner(*args, **kwds):
259 try:
260 return cached(*args, **kwds)
261 except TypeError:
262 pass # All real errors (not unhashable args) are raised below.
263 return func(*args, **kwds)
264 return inner
265
266
267def _eval_type(t, globalns, localns):
268 """Evaluate all forward reverences in the given type t.
269 For use of globalns and localns see the docstring for get_type_hints().
270 """
271 if isinstance(t, ForwardRef):
272 return t._evaluate(globalns, localns)
273 if isinstance(t, _GenericAlias):
274 ev_args = tuple(_eval_type(a, globalns, localns) for a in t.__args__)
275 if ev_args == t.__args__:
276 return t
277 res = t.copy_with(ev_args)
278 res._special = t._special
279 return res
280 return t
281
282
283class _Final:
284 """Mixin to prohibit subclassing"""
Guido van Rossum4cefe742016-09-27 15:20:12 -0700285
Guido van Rossum83ec3022017-01-17 20:43:28 -0800286 __slots__ = ('__weakref__',)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700287
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000288 def __init_subclass__(self, *args, **kwds):
289 if '_root' not in kwds:
290 raise TypeError("Cannot subclass special typing classes")
291
Miss Islington (bot)d0e04c82018-03-26 15:29:06 -0700292class _Immutable:
293 """Mixin to indicate that object should not be copied."""
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000294
Miss Islington (bot)d0e04c82018-03-26 15:29:06 -0700295 def __copy__(self):
296 return self
297
298 def __deepcopy__(self, memo):
299 return self
300
301
302class _SpecialForm(_Final, _Immutable, _root=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000303 """Internal indicator of special typing constructs.
304 See _doc instance attribute for specific docs.
305 """
306
307 __slots__ = ('_name', '_doc')
308
309 def __getstate__(self):
310 return {'name': self._name, 'doc': self._doc}
311
312 def __setstate__(self, state):
313 self._name = state['name']
314 self._doc = state['doc']
Guido van Rossum4cefe742016-09-27 15:20:12 -0700315
316 def __new__(cls, *args, **kwds):
317 """Constructor.
318
319 This only exists to give a better error message in case
320 someone tries to subclass a special typing object (not a good idea).
321 """
322 if (len(args) == 3 and
323 isinstance(args[0], str) and
324 isinstance(args[1], tuple)):
325 # Close enough.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000326 raise TypeError(f"Cannot subclass {cls!r}")
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700327 return super().__new__(cls)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700328
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000329 def __init__(self, name, doc):
330 self._name = name
331 self._doc = doc
Guido van Rossum4cefe742016-09-27 15:20:12 -0700332
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000333 def __eq__(self, other):
334 if not isinstance(other, _SpecialForm):
335 return NotImplemented
336 return self._name == other._name
337
338 def __hash__(self):
339 return hash((self._name,))
Guido van Rossum4cefe742016-09-27 15:20:12 -0700340
341 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000342 return 'typing.' + self._name
343
Miss Islington (bot)d0e04c82018-03-26 15:29:06 -0700344 def __reduce__(self):
345 return self._name
Guido van Rossum4cefe742016-09-27 15:20:12 -0700346
347 def __call__(self, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000348 raise TypeError(f"Cannot instantiate {self!r}")
349
350 def __instancecheck__(self, obj):
351 raise TypeError(f"{self} cannot be used with isinstance()")
352
353 def __subclasscheck__(self, cls):
354 raise TypeError(f"{self} cannot be used with issubclass()")
355
356 @_tp_cache
357 def __getitem__(self, parameters):
358 if self._name == 'ClassVar':
359 item = _type_check(parameters, 'ClassVar accepts only single type.')
360 return _GenericAlias(self, (item,))
361 if self._name == 'Union':
362 if parameters == ():
363 raise TypeError("Cannot take a Union of no types.")
364 if not isinstance(parameters, tuple):
365 parameters = (parameters,)
366 msg = "Union[arg, ...]: each arg must be a type."
367 parameters = tuple(_type_check(p, msg) for p in parameters)
368 parameters = _remove_dups_flatten(parameters)
369 if len(parameters) == 1:
370 return parameters[0]
371 return _GenericAlias(self, parameters)
372 if self._name == 'Optional':
373 arg = _type_check(parameters, "Optional[t] requires a single type.")
374 return Union[arg, type(None)]
375 raise TypeError(f"{self} is not subscriptable")
Guido van Rossum4cefe742016-09-27 15:20:12 -0700376
377
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000378Any = _SpecialForm('Any', doc=
379 """Special type indicating an unconstrained type.
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700380
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000381 - Any is compatible with every type.
382 - Any assumed to have all methods.
383 - All values assumed to be instances of Any.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700384
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000385 Note that all the above statements are true from the point of view of
386 static type checkers. At runtime, Any should not be used with instance
387 or class checks.
388 """)
Guido van Rossumd70fe632015-08-05 12:11:06 +0200389
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000390NoReturn = _SpecialForm('NoReturn', doc=
391 """Special type indicating functions that never return.
392 Example::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700393
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000394 from typing import NoReturn
395
396 def stop() -> NoReturn:
397 raise Exception('no way')
398
399 This type is invalid in other positions, e.g., ``List[NoReturn]``
400 will fail in static type checkers.
401 """)
402
403ClassVar = _SpecialForm('ClassVar', doc=
404 """Special type construct to mark class variables.
405
406 An annotation wrapped in ClassVar indicates that a given
407 attribute is intended to be used as a class variable and
408 should not be set on instances of that class. Usage::
409
410 class Starship:
411 stats: ClassVar[Dict[str, int]] = {} # class variable
412 damage: int = 10 # instance variable
413
414 ClassVar accepts only types and cannot be further subscribed.
415
416 Note that ClassVar is not a class itself, and should not
417 be used with isinstance() or issubclass().
418 """)
419
420Union = _SpecialForm('Union', doc=
421 """Union type; Union[X, Y] means either X or Y.
422
423 To define a union, use e.g. Union[int, str]. Details:
424 - The arguments must be types and there must be at least one.
425 - None as an argument is a special case and is replaced by
426 type(None).
427 - Unions of unions are flattened, e.g.::
428
429 Union[Union[int, str], float] == Union[int, str, float]
430
431 - Unions of a single argument vanish, e.g.::
432
433 Union[int] == int # The constructor actually returns int
434
435 - Redundant arguments are skipped, e.g.::
436
437 Union[int, str, int] == Union[int, str]
438
439 - When comparing unions, the argument order is ignored, e.g.::
440
441 Union[int, str] == Union[str, int]
442
443 - When two arguments have a subclass relationship, the least
444 derived argument is kept, e.g.::
445
446 class Employee: pass
447 class Manager(Employee): pass
448 Union[int, Employee, Manager] == Union[int, Employee]
449 Union[Manager, int, Employee] == Union[int, Employee]
450 Union[Employee, Manager] == Employee
451
452 - Similar for object::
453
454 Union[int, object] == object
455
456 - You cannot subclass or instantiate a union.
457 - You can use Optional[X] as a shorthand for Union[X, None].
458 """)
459
460Optional = _SpecialForm('Optional', doc=
461 """Optional type.
462
463 Optional[X] is equivalent to Union[X, None].
464 """)
Guido van Rossumb7dedc82016-10-29 12:44:29 -0700465
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700466
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000467class ForwardRef(_Final, _root=True):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800468 """Internal wrapper to hold a forward reference."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700469
Guido van Rossum4cefe742016-09-27 15:20:12 -0700470 __slots__ = ('__forward_arg__', '__forward_code__',
Miss Islington (bot)9c17cd32018-05-16 15:04:39 -0700471 '__forward_evaluated__', '__forward_value__',
472 '__forward_is_argument__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700473
Miss Islington (bot)9c17cd32018-05-16 15:04:39 -0700474 def __init__(self, arg, is_argument=False):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700475 if not isinstance(arg, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000476 raise TypeError(f"Forward reference must be a string -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700477 try:
478 code = compile(arg, '<string>', 'eval')
479 except SyntaxError:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000480 raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700481 self.__forward_arg__ = arg
482 self.__forward_code__ = code
483 self.__forward_evaluated__ = False
484 self.__forward_value__ = None
Miss Islington (bot)9c17cd32018-05-16 15:04:39 -0700485 self.__forward_is_argument__ = is_argument
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700486
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000487 def _evaluate(self, globalns, localns):
Guido van Rossumdad17902016-11-10 08:29:18 -0800488 if not self.__forward_evaluated__ or localns is not globalns:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700489 if globalns is None and localns is None:
490 globalns = localns = {}
491 elif globalns is None:
492 globalns = localns
493 elif localns is None:
494 localns = globalns
495 self.__forward_value__ = _type_check(
496 eval(self.__forward_code__, globalns, localns),
Miss Islington (bot)9c17cd32018-05-16 15:04:39 -0700497 "Forward references must evaluate to types.",
498 is_argument=self.__forward_is_argument__)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700499 self.__forward_evaluated__ = True
500 return self.__forward_value__
501
Guido van Rossum4cefe742016-09-27 15:20:12 -0700502 def __eq__(self, other):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000503 if not isinstance(other, ForwardRef):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700504 return NotImplemented
505 return (self.__forward_arg__ == other.__forward_arg__ and
Guido van Rossumc7b92952016-11-10 08:24:06 -0800506 self.__forward_value__ == other.__forward_value__)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700507
508 def __hash__(self):
Guido van Rossumc7b92952016-11-10 08:24:06 -0800509 return hash((self.__forward_arg__, self.__forward_value__))
Guido van Rossum4cefe742016-09-27 15:20:12 -0700510
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700511 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000512 return f'ForwardRef({self.__forward_arg__!r})'
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700513
514
Miss Islington (bot)d0e04c82018-03-26 15:29:06 -0700515def _find_name(mod, name):
516 return getattr(sys.modules[mod], name)
517
518
519class TypeVar(_Final, _Immutable, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700520 """Type variable.
521
522 Usage::
523
524 T = TypeVar('T') # Can be anything
525 A = TypeVar('A', str, bytes) # Must be str or bytes
526
527 Type variables exist primarily for the benefit of static type
528 checkers. They serve as the parameters for generic types as well
529 as for generic function definitions. See class Generic for more
530 information on generic types. Generic functions work as follows:
531
Guido van Rossumb24569a2016-11-20 18:01:29 -0800532 def repeat(x: T, n: int) -> List[T]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700533 '''Return a list containing n references to x.'''
534 return [x]*n
535
536 def longest(x: A, y: A) -> A:
537 '''Return the longest of two strings.'''
538 return x if len(x) >= len(y) else y
539
540 The latter example's signature is essentially the overloading
541 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
542 that if the arguments are instances of some subclass of str,
543 the return type is still plain str.
544
Guido van Rossumb24569a2016-11-20 18:01:29 -0800545 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700546
Guido van Rossumefa798d2016-08-23 11:01:50 -0700547 Type variables defined with covariant=True or contravariant=True
548 can be used do declare covariant or contravariant generic types.
549 See PEP 484 for more details. By default generic types are invariant
550 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700551
552 Type variables can be introspected. e.g.:
553
554 T.__name__ == 'T'
555 T.__constraints__ == ()
556 T.__covariant__ == False
557 T.__contravariant__ = False
558 A.__constraints__ == (str, bytes)
Miss Islington (bot)d0e04c82018-03-26 15:29:06 -0700559
560 Note that only type variables defined in global scope can be pickled.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700561 """
562
Guido van Rossum4cefe742016-09-27 15:20:12 -0700563 __slots__ = ('__name__', '__bound__', '__constraints__',
Miss Islington (bot)d0e04c82018-03-26 15:29:06 -0700564 '__covariant__', '__contravariant__', '_def_mod')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700565
566 def __init__(self, name, *constraints, bound=None,
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800567 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700568 self.__name__ = name
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700569 if covariant and contravariant:
Guido van Rossumefa798d2016-08-23 11:01:50 -0700570 raise ValueError("Bivariant types are not supported.")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700571 self.__covariant__ = bool(covariant)
572 self.__contravariant__ = bool(contravariant)
573 if constraints and bound is not None:
574 raise TypeError("Constraints cannot be combined with bound=...")
575 if constraints and len(constraints) == 1:
576 raise TypeError("A single constraint is not allowed")
577 msg = "TypeVar(name, constraint, ...): constraints must be types."
578 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
579 if bound:
580 self.__bound__ = _type_check(bound, "Bound must be a type.")
581 else:
582 self.__bound__ = None
Miss Islington (bot)d0e04c82018-03-26 15:29:06 -0700583 self._def_mod = sys._getframe(1).f_globals['__name__'] # for pickling
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700584
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000585 def __getstate__(self):
586 return {'name': self.__name__,
587 'bound': self.__bound__,
588 'constraints': self.__constraints__,
589 'co': self.__covariant__,
590 'contra': self.__contravariant__}
591
592 def __setstate__(self, state):
593 self.__name__ = state['name']
594 self.__bound__ = state['bound']
595 self.__constraints__ = state['constraints']
596 self.__covariant__ = state['co']
597 self.__contravariant__ = state['contra']
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700598
599 def __repr__(self):
600 if self.__covariant__:
601 prefix = '+'
602 elif self.__contravariant__:
603 prefix = '-'
604 else:
605 prefix = '~'
606 return prefix + self.__name__
607
Miss Islington (bot)d0e04c82018-03-26 15:29:06 -0700608 def __reduce__(self):
609 return (_find_name, (self._def_mod, self.__name__))
610
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700611
Guido van Rossum83ec3022017-01-17 20:43:28 -0800612# Special typing constructs Union, Optional, Generic, Callable and Tuple
613# use three special attributes for internal bookkeeping of generic types:
614# * __parameters__ is a tuple of unique free type parameters of a generic
615# type, for example, Dict[T, T].__parameters__ == (T,);
616# * __origin__ keeps a reference to a type that was subscripted,
Miss Islington (bot)3c28a632018-05-08 18:44:09 -0700617# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
618# the type.
Guido van Rossum83ec3022017-01-17 20:43:28 -0800619# * __args__ is a tuple of all arguments used in subscripting,
620# e.g., Dict[T, int].__args__ == (T, int).
621
Miss Islington (bot)04eac022018-04-04 17:46:40 -0700622
623# Mapping from non-generic type names that have a generic alias in typing
624# but with a different name.
625_normalize_alias = {'list': 'List',
626 'tuple': 'Tuple',
627 'dict': 'Dict',
628 'set': 'Set',
629 'frozenset': 'FrozenSet',
630 'deque': 'Deque',
631 'defaultdict': 'DefaultDict',
632 'type': 'Type',
633 'Set': 'AbstractSet'}
634
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000635def _is_dunder(attr):
636 return attr.startswith('__') and attr.endswith('__')
Guido van Rossum83ec3022017-01-17 20:43:28 -0800637
Guido van Rossumb24569a2016-11-20 18:01:29 -0800638
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000639class _GenericAlias(_Final, _root=True):
640 """The central part of internal API.
641
642 This represents a generic version of type 'origin' with type arguments 'params'.
643 There are two kind of these aliases: user defined and special. The special ones
644 are wrappers around builtin collections and ABCs in collections.abc. These must
645 have 'name' always set. If 'inst' is False, then the alias can't be instantiated,
646 this is used by e.g. typing.List and typing.Dict.
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700647 """
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000648 def __init__(self, origin, params, *, inst=True, special=False, name=None):
649 self._inst = inst
650 self._special = special
651 if special and name is None:
652 orig_name = origin.__name__
Miss Islington (bot)04eac022018-04-04 17:46:40 -0700653 name = _normalize_alias.get(orig_name, orig_name)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000654 self._name = name
655 if not isinstance(params, tuple):
656 params = (params,)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700657 self.__origin__ = origin
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700658 self.__args__ = tuple(... if a is _TypingEllipsis else
659 () if a is _TypingEmpty else
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000660 a for a in params)
661 self.__parameters__ = _collect_type_vars(params)
662 self.__slots__ = None # This is not documented.
663 if not name:
664 self.__module__ = origin.__module__
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700665
Guido van Rossum4cefe742016-09-27 15:20:12 -0700666 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700667 def __getitem__(self, params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000668 if self.__origin__ in (Generic, _Protocol):
669 # Can't subscript Generic[...] or _Protocol[...].
670 raise TypeError(f"Cannot subscript already-subscripted {self}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700671 if not isinstance(params, tuple):
672 params = (params,)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700673 msg = "Parameters to generic types must be types."
674 params = tuple(_type_check(p, msg) for p in params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000675 _check_generic(self, params)
676 return _subs_tvars(self, self.__parameters__, params)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100677
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000678 def copy_with(self, params):
679 # We don't copy self._special.
680 return _GenericAlias(self.__origin__, params, name=self._name, inst=self._inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700681
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000682 def __repr__(self):
683 if (self._name != 'Callable' or
684 len(self.__args__) == 2 and self.__args__[0] is Ellipsis):
685 if self._name:
686 name = 'typing.' + self._name
687 else:
688 name = _type_repr(self.__origin__)
689 if not self._special:
690 args = f'[{", ".join([_type_repr(a) for a in self.__args__])}]'
691 else:
692 args = ''
693 return (f'{name}{args}')
694 if self._special:
695 return 'typing.Callable'
696 return (f'typing.Callable'
697 f'[[{", ".join([_type_repr(a) for a in self.__args__[:-1]])}], '
698 f'{_type_repr(self.__args__[-1])}]')
699
700 def __eq__(self, other):
701 if not isinstance(other, _GenericAlias):
702 return NotImplemented
703 if self.__origin__ != other.__origin__:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100704 return False
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000705 if self.__origin__ is Union and other.__origin__ is Union:
706 return frozenset(self.__args__) == frozenset(other.__args__)
707 return self.__args__ == other.__args__
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100708
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000709 def __hash__(self):
710 if self.__origin__ is Union:
711 return hash((Union, frozenset(self.__args__)))
712 return hash((self.__origin__, self.__args__))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700713
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000714 def __call__(self, *args, **kwargs):
715 if not self._inst:
716 raise TypeError(f"Type {self._name} cannot be instantiated; "
717 f"use {self._name.lower()}() instead")
718 result = self.__origin__(*args, **kwargs)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700719 try:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000720 result.__orig_class__ = self
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700721 except AttributeError:
722 pass
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000723 return result
724
725 def __mro_entries__(self, bases):
726 if self._name: # generic version of an ABC or built-in class
727 res = []
728 if self.__origin__ not in bases:
729 res.append(self.__origin__)
730 i = bases.index(self)
731 if not any(isinstance(b, _GenericAlias) or issubclass(b, Generic)
732 for b in bases[i+1:]):
733 res.append(Generic)
734 return tuple(res)
735 if self.__origin__ is Generic:
736 i = bases.index(self)
737 for b in bases[i+1:]:
738 if isinstance(b, _GenericAlias) and b is not self:
739 return ()
740 return (self.__origin__,)
741
742 def __getattr__(self, attr):
Miss Islington (bot)32955292018-04-20 14:00:41 -0700743 # We are careful for copy and pickle.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000744 # Also for simplicity we just don't relay all dunder names
745 if '__origin__' in self.__dict__ and not _is_dunder(attr):
746 return getattr(self.__origin__, attr)
747 raise AttributeError(attr)
748
749 def __setattr__(self, attr, val):
750 if _is_dunder(attr) or attr in ('_name', '_inst', '_special'):
751 super().__setattr__(attr, val)
752 else:
753 setattr(self.__origin__, attr, val)
754
755 def __instancecheck__(self, obj):
756 return self.__subclasscheck__(type(obj))
757
758 def __subclasscheck__(self, cls):
759 if self._special:
760 if not isinstance(cls, _GenericAlias):
761 return issubclass(cls, self.__origin__)
762 if cls._special:
763 return issubclass(cls.__origin__, self.__origin__)
764 raise TypeError("Subscripted generics cannot be used with"
765 " class and instance checks")
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700766
Miss Islington (bot)d0e04c82018-03-26 15:29:06 -0700767 def __reduce__(self):
768 if self._special:
769 return self._name
770 return super().__reduce__()
771
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700772
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000773class _VariadicGenericAlias(_GenericAlias, _root=True):
774 """Same as _GenericAlias above but for variadic aliases. Currently,
775 this is used only by special internal aliases: Tuple and Callable.
776 """
777 def __getitem__(self, params):
778 if self._name != 'Callable' or not self._special:
779 return self.__getitem_inner__(params)
780 if not isinstance(params, tuple) or len(params) != 2:
781 raise TypeError("Callable must be used as "
782 "Callable[[arg, ...], result].")
783 args, result = params
784 if args is Ellipsis:
785 params = (Ellipsis, result)
786 else:
787 if not isinstance(args, list):
788 raise TypeError(f"Callable[args, result]: args must be a list."
789 f" Got {args}")
790 params = (tuple(args), result)
791 return self.__getitem_inner__(params)
792
793 @_tp_cache
794 def __getitem_inner__(self, params):
795 if self.__origin__ is tuple and self._special:
796 if params == ():
797 return self.copy_with((_TypingEmpty,))
798 if not isinstance(params, tuple):
799 params = (params,)
800 if len(params) == 2 and params[1] is ...:
801 msg = "Tuple[t, ...]: t must be a type."
802 p = _type_check(params[0], msg)
803 return self.copy_with((p, _TypingEllipsis))
804 msg = "Tuple[t0, t1, ...]: each t must be a type."
805 params = tuple(_type_check(p, msg) for p in params)
806 return self.copy_with(params)
807 if self.__origin__ is collections.abc.Callable and self._special:
808 args, result = params
809 msg = "Callable[args, result]: result must be a type."
810 result = _type_check(result, msg)
811 if args is Ellipsis:
812 return self.copy_with((_TypingEllipsis, result))
813 msg = "Callable[[arg, ...], result]: each arg must be a type."
814 args = tuple(_type_check(arg, msg) for arg in args)
815 params = args + (result,)
816 return self.copy_with(params)
817 return super().__getitem__(params)
818
819
820class Generic:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700821 """Abstract base class for generic types.
822
Guido van Rossumb24569a2016-11-20 18:01:29 -0800823 A generic type is typically declared by inheriting from
824 this class parameterized with one or more type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700825 For example, a generic mapping type might be defined as::
826
827 class Mapping(Generic[KT, VT]):
828 def __getitem__(self, key: KT) -> VT:
829 ...
830 # Etc.
831
832 This class can then be used as follows::
833
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700834 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700835 try:
836 return mapping[key]
837 except KeyError:
838 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700839 """
Guido van Rossumd70fe632015-08-05 12:11:06 +0200840 __slots__ = ()
841
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700842 def __new__(cls, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000843 if cls is Generic:
Guido van Rossum62fe1bb2016-10-29 16:05:26 -0700844 raise TypeError("Type Generic cannot be instantiated; "
845 "it can be used only as a base class")
Miss Islington (bot)c5444b32018-05-10 20:30:47 -0700846 if super().__new__ is object.__new__ and cls.__init__ is not object.__init__:
Miss Islington (bot)3c28a632018-05-08 18:44:09 -0700847 obj = super().__new__(cls)
848 else:
849 obj = super().__new__(cls, *args, **kwds)
850 return obj
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000851
852 @_tp_cache
853 def __class_getitem__(cls, params):
854 if not isinstance(params, tuple):
855 params = (params,)
856 if not params and cls is not Tuple:
857 raise TypeError(
858 f"Parameter list to {cls.__qualname__}[...] cannot be empty")
859 msg = "Parameters to generic types must be types."
860 params = tuple(_type_check(p, msg) for p in params)
861 if cls is Generic:
862 # Generic can only be subscripted with unique type variables.
863 if not all(isinstance(p, TypeVar) for p in params):
864 raise TypeError(
865 "Parameters to Generic[...] must all be type variables")
866 if len(set(params)) != len(params):
867 raise TypeError(
868 "Parameters to Generic[...] must all be unique")
869 elif cls is _Protocol:
870 # _Protocol is internal at the moment, just skip the check
871 pass
872 else:
873 # Subscripting a regular Generic subclass.
874 _check_generic(cls, params)
875 return _GenericAlias(cls, params)
876
877 def __init_subclass__(cls, *args, **kwargs):
Miss Islington (bot)bd85c972018-04-04 09:51:34 -0700878 super().__init_subclass__(*args, **kwargs)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000879 tvars = []
880 if '__orig_bases__' in cls.__dict__:
881 error = Generic in cls.__orig_bases__
882 else:
883 error = Generic in cls.__bases__ and cls.__name__ != '_Protocol'
884 if error:
885 raise TypeError("Cannot inherit from plain Generic")
886 if '__orig_bases__' in cls.__dict__:
887 tvars = _collect_type_vars(cls.__orig_bases__)
888 # Look for Generic[T1, ..., Tn].
889 # If found, tvars must be a subset of it.
890 # If not found, tvars is it.
891 # Also check for and reject plain Generic,
892 # and reject multiple Generic[...].
893 gvars = None
894 for base in cls.__orig_bases__:
895 if (isinstance(base, _GenericAlias) and
896 base.__origin__ is Generic):
897 if gvars is not None:
898 raise TypeError(
899 "Cannot inherit from Generic[...] multiple types.")
900 gvars = base.__parameters__
901 if gvars is None:
902 gvars = tvars
903 else:
904 tvarset = set(tvars)
905 gvarset = set(gvars)
906 if not tvarset <= gvarset:
907 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
908 s_args = ', '.join(str(g) for g in gvars)
909 raise TypeError(f"Some type variables ({s_vars}) are"
910 f" not listed in Generic[{s_args}]")
911 tvars = gvars
912 cls.__parameters__ = tuple(tvars)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700913
914
915class _TypingEmpty:
Guido van Rossumb24569a2016-11-20 18:01:29 -0800916 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
917 to allow empty list/tuple in specific places, without allowing them
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700918 to sneak in where prohibited.
919 """
920
921
922class _TypingEllipsis:
Guido van Rossumb24569a2016-11-20 18:01:29 -0800923 """Internal placeholder for ... (ellipsis)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700924
925
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700926def cast(typ, val):
927 """Cast a value to a type.
928
929 This returns the value unchanged. To the type checker this
930 signals that the return value has the designated type, but at
931 runtime we intentionally don't check anything (we want this
932 to be as fast as possible).
933 """
934 return val
935
936
937def _get_defaults(func):
938 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -0800939 try:
940 code = func.__code__
941 except AttributeError:
942 # Some built-in functions don't have __code__, __defaults__, etc.
943 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700944 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700945 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700946 arg_names = arg_names[:pos_count]
947 defaults = func.__defaults__ or ()
948 kwdefaults = func.__kwdefaults__
949 res = dict(kwdefaults) if kwdefaults else {}
950 pos_offset = pos_count - len(defaults)
951 for name, value in zip(arg_names[pos_offset:], defaults):
952 assert name not in res
953 res[name] = value
954 return res
955
956
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100957_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
958 types.MethodType, types.ModuleType,
Ivan Levkivskyif06e0212017-05-02 19:14:07 +0200959 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100960
961
Guido van Rossum991d14f2016-11-09 13:12:51 -0800962def get_type_hints(obj, globalns=None, localns=None):
963 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700964
Guido van Rossum991d14f2016-11-09 13:12:51 -0800965 This is often the same as obj.__annotations__, but it handles
966 forward references encoded as string literals, and if necessary
967 adds Optional[t] if a default value equal to None is set.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700968
Guido van Rossum991d14f2016-11-09 13:12:51 -0800969 The argument may be a module, class, method, or function. The annotations
970 are returned as a dictionary. For classes, annotations include also
971 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700972
Guido van Rossum991d14f2016-11-09 13:12:51 -0800973 TypeError is raised if the argument is not of a type that can contain
974 annotations, and an empty dictionary is returned if no annotations are
975 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700976
Guido van Rossum991d14f2016-11-09 13:12:51 -0800977 BEWARE -- the behavior of globalns and localns is counterintuitive
978 (unless you are familiar with how eval() and exec() work). The
979 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700980
Guido van Rossum991d14f2016-11-09 13:12:51 -0800981 - If no dict arguments are passed, an attempt is made to use the
Łukasz Langaf350a262017-09-14 14:33:00 -0400982 globals from obj (or the respective module's globals for classes),
983 and these are also used as the locals. If the object does not appear
984 to have globals, an empty dictionary is used.
Guido van Rossum0a6976d2016-09-11 15:34:56 -0700985
Guido van Rossum991d14f2016-11-09 13:12:51 -0800986 - If one dict argument is passed, it is used for both globals and
987 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -0700988
Guido van Rossum991d14f2016-11-09 13:12:51 -0800989 - If two dict arguments are passed, they specify globals and
990 locals, respectively.
991 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -0700992
Guido van Rossum991d14f2016-11-09 13:12:51 -0800993 if getattr(obj, '__no_type_check__', None):
994 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -0800995 # Classes require a special treatment.
996 if isinstance(obj, type):
997 hints = {}
998 for base in reversed(obj.__mro__):
Łukasz Langaf350a262017-09-14 14:33:00 -0400999 if globalns is None:
1000 base_globals = sys.modules[base.__module__].__dict__
1001 else:
1002 base_globals = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001003 ann = base.__dict__.get('__annotations__', {})
1004 for name, value in ann.items():
1005 if value is None:
1006 value = type(None)
1007 if isinstance(value, str):
Miss Islington (bot)9c17cd32018-05-16 15:04:39 -07001008 value = ForwardRef(value, is_argument=True)
Łukasz Langaf350a262017-09-14 14:33:00 -04001009 value = _eval_type(value, base_globals, localns)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001010 hints[name] = value
1011 return hints
Łukasz Langaf350a262017-09-14 14:33:00 -04001012
1013 if globalns is None:
1014 if isinstance(obj, types.ModuleType):
1015 globalns = obj.__dict__
1016 else:
1017 globalns = getattr(obj, '__globals__', {})
1018 if localns is None:
1019 localns = globalns
1020 elif localns is None:
1021 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001022 hints = getattr(obj, '__annotations__', None)
1023 if hints is None:
1024 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001025 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001026 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001027 else:
1028 raise TypeError('{!r} is not a module, class, method, '
1029 'or function.'.format(obj))
1030 defaults = _get_defaults(obj)
1031 hints = dict(hints)
1032 for name, value in hints.items():
1033 if value is None:
1034 value = type(None)
1035 if isinstance(value, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001036 value = ForwardRef(value)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001037 value = _eval_type(value, globalns, localns)
1038 if name in defaults and defaults[name] is None:
1039 value = Optional[value]
1040 hints[name] = value
1041 return hints
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001042
1043
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001044def no_type_check(arg):
1045 """Decorator to indicate that annotations are not type hints.
1046
1047 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001048 applies recursively to all methods and classes defined in that class
1049 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001050
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001051 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001052 """
1053 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001054 arg_attrs = arg.__dict__.copy()
1055 for attr, val in arg.__dict__.items():
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001056 if val in arg.__bases__ + (arg,):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001057 arg_attrs.pop(attr)
1058 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001059 if isinstance(obj, types.FunctionType):
1060 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001061 if isinstance(obj, type):
1062 no_type_check(obj)
1063 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001064 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001065 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001066 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001067 return arg
1068
1069
1070def no_type_check_decorator(decorator):
1071 """Decorator to give another decorator the @no_type_check effect.
1072
1073 This wraps the decorator with something that wraps the decorated
1074 function in @no_type_check.
1075 """
1076
1077 @functools.wraps(decorator)
1078 def wrapped_decorator(*args, **kwds):
1079 func = decorator(*args, **kwds)
1080 func = no_type_check(func)
1081 return func
1082
1083 return wrapped_decorator
1084
1085
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001086def _overload_dummy(*args, **kwds):
1087 """Helper for @overload to raise when called."""
1088 raise NotImplementedError(
1089 "You should not call an overloaded function. "
1090 "A series of @overload-decorated functions "
1091 "outside a stub module should always be followed "
1092 "by an implementation that is not @overload-ed.")
1093
1094
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001095def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001096 """Decorator for overloaded functions/methods.
1097
1098 In a stub file, place two or more stub definitions for the same
1099 function in a row, each decorated with @overload. For example:
1100
1101 @overload
1102 def utf8(value: None) -> None: ...
1103 @overload
1104 def utf8(value: bytes) -> bytes: ...
1105 @overload
1106 def utf8(value: str) -> bytes: ...
1107
1108 In a non-stub file (i.e. a regular .py file), do the same but
1109 follow it with an implementation. The implementation should *not*
1110 be decorated with @overload. For example:
1111
1112 @overload
1113 def utf8(value: None) -> None: ...
1114 @overload
1115 def utf8(value: bytes) -> bytes: ...
1116 @overload
1117 def utf8(value: str) -> bytes: ...
1118 def utf8(value):
1119 # implementation goes here
1120 """
1121 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001122
1123
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001124class _ProtocolMeta(type):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001125 """Internal metaclass for _Protocol.
1126
1127 This exists so _Protocol classes can be generic without deriving
1128 from Generic.
1129 """
1130
Guido van Rossumd70fe632015-08-05 12:11:06 +02001131 def __instancecheck__(self, obj):
Guido van Rossumca4b2522016-11-19 10:32:41 -08001132 if _Protocol not in self.__bases__:
1133 return super().__instancecheck__(obj)
Guido van Rossumd70fe632015-08-05 12:11:06 +02001134 raise TypeError("Protocols cannot be used with isinstance().")
1135
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001136 def __subclasscheck__(self, cls):
1137 if not self._is_protocol:
1138 # No structural checks since this isn't a protocol.
1139 return NotImplemented
1140
1141 if self is _Protocol:
1142 # Every class is a subclass of the empty protocol.
1143 return True
1144
1145 # Find all attributes defined in the protocol.
1146 attrs = self._get_protocol_attrs()
1147
1148 for attr in attrs:
1149 if not any(attr in d.__dict__ for d in cls.__mro__):
1150 return False
1151 return True
1152
1153 def _get_protocol_attrs(self):
1154 # Get all Protocol base classes.
1155 protocol_bases = []
1156 for c in self.__mro__:
1157 if getattr(c, '_is_protocol', False) and c.__name__ != '_Protocol':
1158 protocol_bases.append(c)
1159
1160 # Get attributes included in protocol.
1161 attrs = set()
1162 for base in protocol_bases:
1163 for attr in base.__dict__.keys():
1164 # Include attributes not defined in any non-protocol bases.
1165 for c in self.__mro__:
1166 if (c is not base and attr in c.__dict__ and
1167 not getattr(c, '_is_protocol', False)):
1168 break
1169 else:
1170 if (not attr.startswith('_abc_') and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001171 attr != '__abstractmethods__' and
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001172 attr != '__annotations__' and
1173 attr != '__weakref__' and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001174 attr != '_is_protocol' and
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001175 attr != '_gorg' and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001176 attr != '__dict__' and
1177 attr != '__args__' and
1178 attr != '__slots__' and
1179 attr != '_get_protocol_attrs' and
1180 attr != '__next_in_mro__' and
1181 attr != '__parameters__' and
1182 attr != '__origin__' and
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001183 attr != '__orig_bases__' and
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001184 attr != '__extra__' and
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001185 attr != '__tree_hash__' and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001186 attr != '__module__'):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001187 attrs.add(attr)
1188
1189 return attrs
1190
1191
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001192class _Protocol(Generic, metaclass=_ProtocolMeta):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001193 """Internal base class for protocol classes.
1194
Guido van Rossumb24569a2016-11-20 18:01:29 -08001195 This implements a simple-minded structural issubclass check
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001196 (similar but more general than the one-offs in collections.abc
1197 such as Hashable).
1198 """
1199
Guido van Rossumd70fe632015-08-05 12:11:06 +02001200 __slots__ = ()
1201
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001202 _is_protocol = True
1203
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001204 def __class_getitem__(cls, params):
1205 return super().__class_getitem__(params)
1206
1207
1208# Some unconstrained type variables. These are used by the container types.
1209# (These are not for export.)
1210T = TypeVar('T') # Any type.
1211KT = TypeVar('KT') # Key type.
1212VT = TypeVar('VT') # Value type.
1213T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
1214V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
1215VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
1216T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
1217# Internal type variable used for Type[].
1218CT_co = TypeVar('CT_co', covariant=True, bound=type)
1219
1220# A useful type variable with constraints. This represents string types.
1221# (This one *is* for export!)
1222AnyStr = TypeVar('AnyStr', bytes, str)
1223
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001224
1225# Various ABCs mimicking those in collections.abc.
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001226def _alias(origin, params, inst=True):
1227 return _GenericAlias(origin, params, special=True, inst=inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001228
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001229Hashable = _alias(collections.abc.Hashable, ()) # Not generic.
1230Awaitable = _alias(collections.abc.Awaitable, T_co)
1231Coroutine = _alias(collections.abc.Coroutine, (T_co, T_contra, V_co))
1232AsyncIterable = _alias(collections.abc.AsyncIterable, T_co)
1233AsyncIterator = _alias(collections.abc.AsyncIterator, T_co)
1234Iterable = _alias(collections.abc.Iterable, T_co)
1235Iterator = _alias(collections.abc.Iterator, T_co)
1236Reversible = _alias(collections.abc.Reversible, T_co)
1237Sized = _alias(collections.abc.Sized, ()) # Not generic.
1238Container = _alias(collections.abc.Container, T_co)
1239Collection = _alias(collections.abc.Collection, T_co)
1240Callable = _VariadicGenericAlias(collections.abc.Callable, (), special=True)
1241Callable.__doc__ = \
1242 """Callable type; Callable[[int], str] is a function of (int) -> str.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001243
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001244 The subscription syntax must always be used with exactly two
1245 values: the argument list and the return type. The argument list
1246 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001247
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001248 There is no syntax to indicate optional or keyword arguments,
1249 such function types are rarely used as callback types.
1250 """
1251AbstractSet = _alias(collections.abc.Set, T_co)
1252MutableSet = _alias(collections.abc.MutableSet, T)
1253# NOTE: Mapping is only covariant in the value type.
1254Mapping = _alias(collections.abc.Mapping, (KT, VT_co))
1255MutableMapping = _alias(collections.abc.MutableMapping, (KT, VT))
1256Sequence = _alias(collections.abc.Sequence, T_co)
1257MutableSequence = _alias(collections.abc.MutableSequence, T)
1258ByteString = _alias(collections.abc.ByteString, ()) # Not generic
1259Tuple = _VariadicGenericAlias(tuple, (), inst=False, special=True)
1260Tuple.__doc__ = \
1261 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001262
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001263 Example: Tuple[T1, T2] is a tuple of two elements corresponding
1264 to type variables T1 and T2. Tuple[int, float, str] is a tuple
1265 of an int, a float and a string.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001266
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001267 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
1268 """
1269List = _alias(list, T, inst=False)
1270Deque = _alias(collections.deque, T)
1271Set = _alias(set, T, inst=False)
1272FrozenSet = _alias(frozenset, T_co, inst=False)
1273MappingView = _alias(collections.abc.MappingView, T_co)
1274KeysView = _alias(collections.abc.KeysView, KT)
1275ItemsView = _alias(collections.abc.ItemsView, (KT, VT_co))
1276ValuesView = _alias(collections.abc.ValuesView, VT_co)
1277ContextManager = _alias(contextlib.AbstractContextManager, T_co)
1278AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, T_co)
1279Dict = _alias(dict, (KT, VT), inst=False)
1280DefaultDict = _alias(collections.defaultdict, (KT, VT))
1281Counter = _alias(collections.Counter, T)
1282ChainMap = _alias(collections.ChainMap, (KT, VT))
1283Generator = _alias(collections.abc.Generator, (T_co, T_contra, V_co))
1284AsyncGenerator = _alias(collections.abc.AsyncGenerator, (T_co, T_contra))
1285Type = _alias(type, CT_co, inst=False)
1286Type.__doc__ = \
1287 """A special construct usable to annotate class objects.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001288
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001289 For example, suppose we have the following classes::
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001290
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001291 class User: ... # Abstract base for User classes
1292 class BasicUser(User): ...
1293 class ProUser(User): ...
1294 class TeamUser(User): ...
Guido van Rossumf17c2002015-12-03 17:31:24 -08001295
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001296 And a function that takes a class argument that's a subclass of
1297 User and returns an instance of the corresponding class::
Guido van Rossumf17c2002015-12-03 17:31:24 -08001298
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001299 U = TypeVar('U', bound=User)
1300 def new_user(user_class: Type[U]) -> U:
1301 user = user_class()
1302 # (Here we could write the user object to a database)
1303 return user
Guido van Rossumf17c2002015-12-03 17:31:24 -08001304
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001305 joe = new_user(BasicUser)
Guido van Rossumf17c2002015-12-03 17:31:24 -08001306
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001307 At this point the type checker knows that joe has type BasicUser.
1308 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001309
1310
1311class SupportsInt(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001312 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001313
1314 @abstractmethod
1315 def __int__(self) -> int:
1316 pass
1317
1318
1319class SupportsFloat(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001320 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001321
1322 @abstractmethod
1323 def __float__(self) -> float:
1324 pass
1325
1326
1327class SupportsComplex(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001328 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001329
1330 @abstractmethod
1331 def __complex__(self) -> complex:
1332 pass
1333
1334
1335class SupportsBytes(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001336 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001337
1338 @abstractmethod
1339 def __bytes__(self) -> bytes:
1340 pass
1341
1342
Guido van Rossumd70fe632015-08-05 12:11:06 +02001343class SupportsAbs(_Protocol[T_co]):
1344 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001345
1346 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001347 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001348 pass
1349
1350
Guido van Rossumd70fe632015-08-05 12:11:06 +02001351class SupportsRound(_Protocol[T_co]):
1352 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001353
1354 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001355 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001356 pass
1357
1358
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001359def _make_nmtuple(name, types):
Guido van Rossum2f841442016-11-15 09:48:06 -08001360 msg = "NamedTuple('Name', [(f0, t0), (f1, t1), ...]); each t must be a type"
1361 types = [(n, _type_check(t, msg)) for n, t in types]
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001362 nm_tpl = collections.namedtuple(name, [n for n, t in types])
Guido van Rossum83ec3022017-01-17 20:43:28 -08001363 # Prior to PEP 526, only _field_types attribute was assigned.
1364 # Now, both __annotations__ and _field_types are used to maintain compatibility.
1365 nm_tpl.__annotations__ = nm_tpl._field_types = collections.OrderedDict(types)
Guido van Rossum557d1eb2015-11-19 08:16:31 -08001366 try:
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001367 nm_tpl.__module__ = sys._getframe(2).f_globals.get('__name__', '__main__')
Guido van Rossum557d1eb2015-11-19 08:16:31 -08001368 except (AttributeError, ValueError):
1369 pass
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001370 return nm_tpl
1371
1372
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001373# attributes prohibited to set in NamedTuple class syntax
1374_prohibited = ('__new__', '__init__', '__slots__', '__getnewargs__',
1375 '_fields', '_field_defaults', '_field_types',
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02001376 '_make', '_replace', '_asdict', '_source')
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001377
1378_special = ('__module__', '__name__', '__qualname__', '__annotations__')
1379
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001380
Guido van Rossum2f841442016-11-15 09:48:06 -08001381class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001382
Guido van Rossum2f841442016-11-15 09:48:06 -08001383 def __new__(cls, typename, bases, ns):
1384 if ns.get('_root', False):
1385 return super().__new__(cls, typename, bases, ns)
Guido van Rossum2f841442016-11-15 09:48:06 -08001386 types = ns.get('__annotations__', {})
Guido van Rossum3c268be2017-01-18 08:03:50 -08001387 nm_tpl = _make_nmtuple(typename, types.items())
1388 defaults = []
1389 defaults_dict = {}
1390 for field_name in types:
1391 if field_name in ns:
1392 default_value = ns[field_name]
1393 defaults.append(default_value)
1394 defaults_dict[field_name] = default_value
1395 elif defaults:
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001396 raise TypeError("Non-default namedtuple field {field_name} cannot "
1397 "follow default field(s) {default_names}"
Guido van Rossum3c268be2017-01-18 08:03:50 -08001398 .format(field_name=field_name,
1399 default_names=', '.join(defaults_dict.keys())))
Miss Islington (bot)3c28a632018-05-08 18:44:09 -07001400 nm_tpl.__new__.__annotations__ = collections.OrderedDict(types)
Guido van Rossum3c268be2017-01-18 08:03:50 -08001401 nm_tpl.__new__.__defaults__ = tuple(defaults)
1402 nm_tpl._field_defaults = defaults_dict
Guido van Rossum95919c02017-01-22 17:47:20 -08001403 # update from user namespace without overriding special namedtuple attributes
1404 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001405 if key in _prohibited:
1406 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
1407 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08001408 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08001409 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001410
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001411
Guido van Rossum2f841442016-11-15 09:48:06 -08001412class NamedTuple(metaclass=NamedTupleMeta):
1413 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001414
Guido van Rossum2f841442016-11-15 09:48:06 -08001415 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001416
Guido van Rossum2f841442016-11-15 09:48:06 -08001417 class Employee(NamedTuple):
1418 name: str
1419 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001420
Guido van Rossum2f841442016-11-15 09:48:06 -08001421 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001422
Guido van Rossum2f841442016-11-15 09:48:06 -08001423 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001424
Guido van Rossum83ec3022017-01-17 20:43:28 -08001425 The resulting class has extra __annotations__ and _field_types
1426 attributes, giving an ordered dict mapping field names to types.
1427 __annotations__ should be preferred, while _field_types
1428 is kept to maintain pre PEP 526 compatibility. (The field names
Guido van Rossum2f841442016-11-15 09:48:06 -08001429 are in the _fields attribute, which is part of the namedtuple
1430 API.) Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001431
Guido van Rossum2f841442016-11-15 09:48:06 -08001432 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001433
Guido van Rossum2f841442016-11-15 09:48:06 -08001434 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001435
Guido van Rossum2f841442016-11-15 09:48:06 -08001436 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
1437 """
1438 _root = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001439
Guido van Rossum2f841442016-11-15 09:48:06 -08001440 def __new__(self, typename, fields=None, **kwargs):
Guido van Rossum2f841442016-11-15 09:48:06 -08001441 if fields is None:
1442 fields = kwargs.items()
1443 elif kwargs:
1444 raise TypeError("Either list of fields or keywords"
1445 " can be provided to NamedTuple, not both")
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001446 return _make_nmtuple(typename, fields)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001447
1448
Guido van Rossum91185fe2016-06-08 11:19:11 -07001449def NewType(name, tp):
1450 """NewType creates simple unique types with almost zero
1451 runtime overhead. NewType(name, tp) is considered a subtype of tp
1452 by static type checkers. At runtime, NewType(name, tp) returns
1453 a dummy function that simply returns its argument. Usage::
1454
1455 UserId = NewType('UserId', int)
1456
1457 def name_by_id(user_id: UserId) -> str:
1458 ...
1459
1460 UserId('user') # Fails type check
1461
1462 name_by_id(42) # Fails type check
1463 name_by_id(UserId(42)) # OK
1464
1465 num = UserId(5) + 1 # type: int
1466 """
1467
1468 def new_type(x):
1469 return x
1470
1471 new_type.__name__ = name
1472 new_type.__supertype__ = tp
1473 return new_type
1474
1475
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001476# Python-version-specific alias (Python 2: unicode; Python 3: str)
1477Text = str
1478
1479
Guido van Rossum91185fe2016-06-08 11:19:11 -07001480# Constant that's True when type checking, but False here.
1481TYPE_CHECKING = False
1482
1483
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001484class IO(Generic[AnyStr]):
1485 """Generic base class for TextIO and BinaryIO.
1486
1487 This is an abstract, generic version of the return of open().
1488
1489 NOTE: This does not distinguish between the different possible
1490 classes (text vs. binary, read vs. write vs. read/write,
1491 append-only, unbuffered). The TextIO and BinaryIO subclasses
1492 below capture the distinctions between text vs. binary, which is
1493 pervasive in the interface; however we currently do not offer a
1494 way to track the other distinctions in the type system.
1495 """
1496
Guido van Rossumd70fe632015-08-05 12:11:06 +02001497 __slots__ = ()
1498
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001499 @abstractproperty
1500 def mode(self) -> str:
1501 pass
1502
1503 @abstractproperty
1504 def name(self) -> str:
1505 pass
1506
1507 @abstractmethod
1508 def close(self) -> None:
1509 pass
1510
1511 @abstractmethod
1512 def closed(self) -> bool:
1513 pass
1514
1515 @abstractmethod
1516 def fileno(self) -> int:
1517 pass
1518
1519 @abstractmethod
1520 def flush(self) -> None:
1521 pass
1522
1523 @abstractmethod
1524 def isatty(self) -> bool:
1525 pass
1526
1527 @abstractmethod
1528 def read(self, n: int = -1) -> AnyStr:
1529 pass
1530
1531 @abstractmethod
1532 def readable(self) -> bool:
1533 pass
1534
1535 @abstractmethod
1536 def readline(self, limit: int = -1) -> AnyStr:
1537 pass
1538
1539 @abstractmethod
1540 def readlines(self, hint: int = -1) -> List[AnyStr]:
1541 pass
1542
1543 @abstractmethod
1544 def seek(self, offset: int, whence: int = 0) -> int:
1545 pass
1546
1547 @abstractmethod
1548 def seekable(self) -> bool:
1549 pass
1550
1551 @abstractmethod
1552 def tell(self) -> int:
1553 pass
1554
1555 @abstractmethod
1556 def truncate(self, size: int = None) -> int:
1557 pass
1558
1559 @abstractmethod
1560 def writable(self) -> bool:
1561 pass
1562
1563 @abstractmethod
1564 def write(self, s: AnyStr) -> int:
1565 pass
1566
1567 @abstractmethod
1568 def writelines(self, lines: List[AnyStr]) -> None:
1569 pass
1570
1571 @abstractmethod
1572 def __enter__(self) -> 'IO[AnyStr]':
1573 pass
1574
1575 @abstractmethod
1576 def __exit__(self, type, value, traceback) -> None:
1577 pass
1578
1579
1580class BinaryIO(IO[bytes]):
1581 """Typed version of the return of open() in binary mode."""
1582
Guido van Rossumd70fe632015-08-05 12:11:06 +02001583 __slots__ = ()
1584
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001585 @abstractmethod
1586 def write(self, s: Union[bytes, bytearray]) -> int:
1587 pass
1588
1589 @abstractmethod
1590 def __enter__(self) -> 'BinaryIO':
1591 pass
1592
1593
1594class TextIO(IO[str]):
1595 """Typed version of the return of open() in text mode."""
1596
Guido van Rossumd70fe632015-08-05 12:11:06 +02001597 __slots__ = ()
1598
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001599 @abstractproperty
1600 def buffer(self) -> BinaryIO:
1601 pass
1602
1603 @abstractproperty
1604 def encoding(self) -> str:
1605 pass
1606
1607 @abstractproperty
Guido van Rossum991d14f2016-11-09 13:12:51 -08001608 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001609 pass
1610
1611 @abstractproperty
1612 def line_buffering(self) -> bool:
1613 pass
1614
1615 @abstractproperty
1616 def newlines(self) -> Any:
1617 pass
1618
1619 @abstractmethod
1620 def __enter__(self) -> 'TextIO':
1621 pass
1622
1623
1624class io:
1625 """Wrapper namespace for IO generic classes."""
1626
1627 __all__ = ['IO', 'TextIO', 'BinaryIO']
1628 IO = IO
1629 TextIO = TextIO
1630 BinaryIO = BinaryIO
1631
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001632
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001633io.__name__ = __name__ + '.io'
1634sys.modules[io.__name__] = io
1635
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001636Pattern = _alias(stdlib_re.Pattern, AnyStr)
1637Match = _alias(stdlib_re.Match, AnyStr)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001638
1639class re:
1640 """Wrapper namespace for re type aliases."""
1641
1642 __all__ = ['Pattern', 'Match']
1643 Pattern = Pattern
1644 Match = Match
1645
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001646
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001647re.__name__ = __name__ + '.re'
1648sys.modules[re.__name__] = re