this repo has no description
1#!/usr/bin/env python3
2# Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
3# WARNING: This is a temporary copy of code from the cpython library to
4# facilitate bringup. Please file a task for anything you change!
5# flake8: noqa
6# fmt: off
7"""
8The typing module: Support for gradual typing as defined by PEP 484.
9
10At large scale, the structure of the module is following:
11* Imports and exports, all public names should be explicitly added to __all__.
12* Internal helper functions: these should never be used in code outside this module.
13* _SpecialForm and its instances (special forms): Any, NoReturn, ClassVar, Union, Optional
14* Two classes whose instances can be type arguments in addition to types: ForwardRef and TypeVar
15* The core of internal generics API: _GenericAlias and _VariadicGenericAlias, the latter is
16 currently only used by Tuple and Callable. All subscripted types like X[int], Union[int, str],
17 etc., are instances of either of these classes.
18* The public counterpart of the generics API consists of two classes: Generic and Protocol.
19* Public helper functions: get_type_hints, overload, cast, no_type_check,
20 no_type_check_decorator.
21* Generic aliases for collections.abc ABCs and few additional protocols.
22* Special types: NewType, NamedTuple, TypedDict.
23* Wrapper submodules for re and io related types.
24"""
25
26import collections
27import collections.abc
28import contextlib
29import functools
30import operator
31import re as stdlib_re # Avoid confusion with the re we export.
32import sys
33import types
34from abc import abstractmethod, ABCMeta
35
36# Please keep __all__ alphabetized within each category.
37__all__ = [
38 # Super-special typing primitives.
39 'Any',
40 'Callable',
41 'ClassVar',
42 'Final',
43 'ForwardRef',
44 'Generic',
45 'Literal',
46 'Optional',
47 'Protocol',
48 'Tuple',
49 'Type',
50 'TypeVar',
51 'Union',
52
53 # ABCs (from collections.abc).
54 'AbstractSet', # collections.abc.Set.
55 'ByteString',
56 'Container',
57 'ContextManager',
58 'Hashable',
59 'ItemsView',
60 'Iterable',
61 'Iterator',
62 'KeysView',
63 'Mapping',
64 'MappingView',
65 'MutableMapping',
66 'MutableSequence',
67 'MutableSet',
68 'Sequence',
69 'Sized',
70 'ValuesView',
71 'Awaitable',
72 'AsyncIterator',
73 'AsyncIterable',
74 'Coroutine',
75 'Collection',
76 'AsyncGenerator',
77 'AsyncContextManager',
78
79 # Structural checks, a.k.a. protocols.
80 'Reversible',
81 'SupportsAbs',
82 'SupportsBytes',
83 'SupportsComplex',
84 'SupportsFloat',
85 'SupportsIndex',
86 'SupportsInt',
87 'SupportsRound',
88
89 # Concrete collection types.
90 'ChainMap',
91 'Counter',
92 'Deque',
93 'Dict',
94 'DefaultDict',
95 'List',
96 'OrderedDict',
97 'Set',
98 'FrozenSet',
99 'NamedTuple', # Not really a type.
100 'TypedDict', # Not really a type.
101 'Generator',
102
103 # One-off things.
104 'AnyStr',
105 'cast',
106 'final',
107 'get_args',
108 'get_origin',
109 'get_type_hints',
110 'NewType',
111 'no_type_check',
112 'no_type_check_decorator',
113 'NoReturn',
114 'overload',
115 'runtime_checkable',
116 'Text',
117 'TYPE_CHECKING',
118]
119
120# The pseudo-submodules 're' and 'io' are part of the public
121# namespace, but excluded from __all__ because they might stomp on
122# legitimate imports of those modules.
123
124
125def _type_check(arg, msg, is_argument=True):
126 """Check that the argument is a type, and return it (internal helper).
127
128 As a special case, accept None and return type(None) instead. Also wrap strings
129 into ForwardRef instances. Consider several corner cases, for example plain
130 special forms like Union are not valid, while Union[int, str] is OK, etc.
131 The msg argument is a human-readable error message, e.g::
132
133 "Union[arg, ...]: arg should be a type."
134
135 We append the repr() of the actual value (truncated to 100 chars).
136 """
137 invalid_generic_forms = (Generic, Protocol)
138 if is_argument:
139 invalid_generic_forms = invalid_generic_forms + (ClassVar, Final)
140
141 if arg is None:
142 return type(None)
143 if isinstance(arg, str):
144 return ForwardRef(arg)
145 if (isinstance(arg, _GenericAlias) and
146 arg.__origin__ in invalid_generic_forms):
147 raise TypeError(f"{arg} is not valid as type argument")
148 if (isinstance(arg, _SpecialForm) and arg not in (Any, NoReturn) or
149 arg in (Generic, Protocol)):
150 raise TypeError(f"Plain {arg} is not valid as type argument")
151 if isinstance(arg, (type, TypeVar, ForwardRef, types.Union)):
152 return arg
153 if not callable(arg):
154 raise TypeError(f"{msg} Got {arg!r:.100}.")
155 return arg
156
157
158def _type_repr(obj):
159 """Return the repr() of an object, special-casing types (internal helper).
160
161 If obj is a type, we return a shorter version than the default
162 type.__repr__, based on the module and qualified name, which is
163 typically enough to uniquely identify a type. For everything
164 else, we fall back on repr(obj).
165 """
166 if isinstance(obj, type):
167 if obj.__module__ == 'builtins':
168 return obj.__qualname__
169 return f'{obj.__module__}.{obj.__qualname__}'
170 if obj is ...:
171 return('...')
172 if isinstance(obj, types.FunctionType):
173 return obj.__name__
174 return repr(obj)
175
176
177def _collect_type_vars(types):
178 """Collect all type variable contained in types in order of
179 first appearance (lexicographic order). For example::
180
181 _collect_type_vars((T, List[S, T])) == (T, S)
182 """
183 tvars = []
184 for t in types:
185 if isinstance(t, TypeVar) and t not in tvars:
186 tvars.append(t)
187 if isinstance(t, _GenericAlias) and not t._special:
188 tvars.extend([t for t in t.__parameters__ if t not in tvars])
189 return tuple(tvars)
190
191
192def _subs_tvars(tp, tvars, subs):
193 """Substitute type variables 'tvars' with substitutions 'subs'.
194 These two must have the same length.
195 """
196 if not isinstance(tp, _GenericAlias):
197 return tp
198 new_args = list(tp.__args__)
199 for a, arg in enumerate(tp.__args__):
200 if isinstance(arg, TypeVar):
201 for i, tvar in enumerate(tvars):
202 if arg == tvar:
203 new_args[a] = subs[i]
204 else:
205 new_args[a] = _subs_tvars(arg, tvars, subs)
206 if tp.__origin__ is Union:
207 return Union[tuple(new_args)]
208 return tp.copy_with(tuple(new_args))
209
210
211def _check_generic(cls, parameters):
212 """Check correct count for parameters of a generic cls (internal helper).
213 This gives a nice error message in case of count mismatch.
214 """
215 if not cls.__parameters__:
216 raise TypeError(f"{cls} is not a generic class")
217 alen = len(parameters)
218 elen = len(cls.__parameters__)
219 if alen != elen:
220 raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
221 f" actual {alen}, expected {elen}")
222
223
224def _remove_dups_flatten(parameters):
225 """An internal helper for Union creation and substitution: flatten Unions
226 among parameters, then remove duplicates.
227 """
228 # Flatten out Union[Union[...], ...].
229 params = []
230 for p in parameters:
231 if isinstance(p, _GenericAlias) and p.__origin__ is Union or isinstance(p, types.Union):
232 params.extend(p.__args__)
233 elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
234 params.extend(p[1:])
235 else:
236 params.append(p)
237 # Weed out strict duplicates, preserving the first of each occurrence.
238 all_params = set(params)
239 if len(all_params) < len(params):
240 new_params = []
241 for t in params:
242 if t in all_params:
243 new_params.append(t)
244 all_params.remove(t)
245 params = new_params
246 assert not all_params, all_params
247 return tuple(params)
248
249
250_cleanups = []
251
252
253def _tp_cache(func):
254 """Internal wrapper caching __getitem__ of generic types with a fallback to
255 original function for non-hashable arguments.
256 """
257 cached = functools.lru_cache()(func)
258 _cleanups.append(cached.cache_clear)
259
260 @functools.wraps(func)
261 def inner(*args, **kwds):
262 try:
263 return cached(*args, **kwds)
264 except TypeError:
265 pass # All real errors (not unhashable args) are raised below.
266 return func(*args, **kwds)
267 return inner
268
269
270def _eval_type(t, globalns, localns):
271 """Evaluate all forward references in the given type t.
272 For use of globalns and localns see the docstring for get_type_hints().
273 """
274 if isinstance(t, ForwardRef):
275 return t._evaluate(globalns, localns)
276 if isinstance(t, _GenericAlias):
277 ev_args = tuple(_eval_type(a, globalns, localns) for a in t.__args__)
278 if ev_args == t.__args__:
279 return t
280 res = t.copy_with(ev_args)
281 res._special = t._special
282 return res
283 return t
284
285
286class _Final:
287 """Mixin to prohibit subclassing"""
288
289 __slots__ = ('__weakref__',)
290
291 # TODO(T59042197)
292 # This was updated from the original implementation to work with pyro
293 # - def __init_subclass__(self, /, *args, **kwds):
294 # + def __init_subclass__(self, *args, **kwds):
295 def __init_subclass__(self, *args, **kwds):
296 if '_root' not in kwds:
297 raise TypeError("Cannot subclass special typing classes")
298
299class _Immutable:
300 """Mixin to indicate that object should not be copied."""
301
302 def __copy__(self):
303 return self
304
305 def __deepcopy__(self, memo):
306 return self
307
308
309class _SpecialForm(_Final, _Immutable, _root=True):
310 """Internal indicator of special typing constructs.
311 See _doc instance attribute for specific docs.
312 """
313
314 __slots__ = ('_name', '_doc')
315
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.
326 raise TypeError(f"Cannot subclass {cls!r}")
327 return super().__new__(cls)
328
329 def __init__(self, name, doc):
330 self._name = name
331 self._doc = doc
332
333 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,))
340
341 def __repr__(self):
342 return 'typing.' + self._name
343
344 def __reduce__(self):
345 return self._name
346
347 def __call__(self, *args, **kwds):
348 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 in ('ClassVar', 'Final'):
359 item = _type_check(parameters, f'{self._name} 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 if self._name == 'Literal':
376 # There is no '_type_check' call because arguments to Literal[...] are
377 # values, not types.
378 return _GenericAlias(self, parameters)
379 raise TypeError(f"{self} is not subscriptable")
380
381
382Any = _SpecialForm('Any', doc=
383 """Special type indicating an unconstrained type.
384
385 - Any is compatible with every type.
386 - Any assumed to have all methods.
387 - All values assumed to be instances of Any.
388
389 Note that all the above statements are true from the point of view of
390 static type checkers. At runtime, Any should not be used with instance
391 or class checks.
392 """)
393
394NoReturn = _SpecialForm('NoReturn', doc=
395 """Special type indicating functions that never return.
396 Example::
397
398 from typing import NoReturn
399
400 def stop() -> NoReturn:
401 raise Exception('no way')
402
403 This type is invalid in other positions, e.g., ``List[NoReturn]``
404 will fail in static type checkers.
405 """)
406
407ClassVar = _SpecialForm('ClassVar', doc=
408 """Special type construct to mark class variables.
409
410 An annotation wrapped in ClassVar indicates that a given
411 attribute is intended to be used as a class variable and
412 should not be set on instances of that class. Usage::
413
414 class Starship:
415 stats: ClassVar[Dict[str, int]] = {} # class variable
416 damage: int = 10 # instance variable
417
418 ClassVar accepts only types and cannot be further subscribed.
419
420 Note that ClassVar is not a class itself, and should not
421 be used with isinstance() or issubclass().
422 """)
423
424Final = _SpecialForm('Final', doc=
425 """Special typing construct to indicate final names to type checkers.
426
427 A final name cannot be re-assigned or overridden in a subclass.
428 For example:
429
430 MAX_SIZE: Final = 9000
431 MAX_SIZE += 1 # Error reported by type checker
432
433 class Connection:
434 TIMEOUT: Final[int] = 10
435
436 class FastConnector(Connection):
437 TIMEOUT = 1 # Error reported by type checker
438
439 There is no runtime checking of these properties.
440 """)
441
442Union = _SpecialForm('Union', doc=
443 """Union type; Union[X, Y] means either X or Y.
444
445 To define a union, use e.g. Union[int, str]. Details:
446 - The arguments must be types and there must be at least one.
447 - None as an argument is a special case and is replaced by
448 type(None).
449 - Unions of unions are flattened, e.g.::
450
451 Union[Union[int, str], float] == Union[int, str, float]
452
453 - Unions of a single argument vanish, e.g.::
454
455 Union[int] == int # The constructor actually returns int
456
457 - Redundant arguments are skipped, e.g.::
458
459 Union[int, str, int] == Union[int, str]
460
461 - When comparing unions, the argument order is ignored, e.g.::
462
463 Union[int, str] == Union[str, int]
464
465 - You cannot subclass or instantiate a union.
466 - You can use Optional[X] as a shorthand for Union[X, None].
467 """)
468
469Optional = _SpecialForm('Optional', doc=
470 """Optional type.
471
472 Optional[X] is equivalent to Union[X, None].
473 """)
474
475Literal = _SpecialForm('Literal', doc=
476 """Special typing form to define literal types (a.k.a. value types).
477
478 This form can be used to indicate to type checkers that the corresponding
479 variable or function parameter has a value equivalent to the provided
480 literal (or one of several literals):
481
482 def validate_simple(data: Any) -> Literal[True]: # always returns True
483 ...
484
485 MODE = Literal['r', 'rb', 'w', 'wb']
486 def open_helper(file: str, mode: MODE) -> str:
487 ...
488
489 open_helper('/some/path', 'r') # Passes type check
490 open_helper('/other/path', 'typo') # Error in type checker
491
492 Literal[...] cannot be subclassed. At runtime, an arbitrary value
493 is allowed as type argument to Literal[...], but type checkers may
494 impose restrictions.
495 """)
496
497
498class ForwardRef(_Final, _root=True):
499 """Internal wrapper to hold a forward reference."""
500
501 __slots__ = ('__forward_arg__', '__forward_code__',
502 '__forward_evaluated__', '__forward_value__',
503 '__forward_is_argument__')
504
505 def __init__(self, arg, is_argument=True):
506 if not isinstance(arg, str):
507 raise TypeError(f"Forward reference must be a string -- got {arg!r}")
508 try:
509 code = compile(arg, '<string>', 'eval')
510 except SyntaxError:
511 raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
512 self.__forward_arg__ = arg
513 self.__forward_code__ = code
514 self.__forward_evaluated__ = False
515 self.__forward_value__ = None
516 self.__forward_is_argument__ = is_argument
517
518 def _evaluate(self, globalns, localns):
519 if not self.__forward_evaluated__ or localns is not globalns:
520 if globalns is None and localns is None:
521 globalns = localns = {}
522 elif globalns is None:
523 globalns = localns
524 elif localns is None:
525 localns = globalns
526 self.__forward_value__ = _type_check(
527 eval(self.__forward_code__, globalns, localns),
528 "Forward references must evaluate to types.",
529 is_argument=self.__forward_is_argument__)
530 self.__forward_evaluated__ = True
531 return self.__forward_value__
532
533 def __eq__(self, other):
534 if not isinstance(other, ForwardRef):
535 return NotImplemented
536 if self.__forward_evaluated__ and other.__forward_evaluated__:
537 return (self.__forward_arg__ == other.__forward_arg__ and
538 self.__forward_value__ == other.__forward_value__)
539 return self.__forward_arg__ == other.__forward_arg__
540
541 def __hash__(self):
542 return hash(self.__forward_arg__)
543
544 def __repr__(self):
545 return f'ForwardRef({self.__forward_arg__!r})'
546
547
548class TypeVar(_Final, _Immutable, _root=True):
549 """Type variable.
550
551 Usage::
552
553 T = TypeVar('T') # Can be anything
554 A = TypeVar('A', str, bytes) # Must be str or bytes
555
556 Type variables exist primarily for the benefit of static type
557 checkers. They serve as the parameters for generic types as well
558 as for generic function definitions. See class Generic for more
559 information on generic types. Generic functions work as follows:
560
561 def repeat(x: T, n: int) -> List[T]:
562 '''Return a list containing n references to x.'''
563 return [x]*n
564
565 def longest(x: A, y: A) -> A:
566 '''Return the longest of two strings.'''
567 return x if len(x) >= len(y) else y
568
569 The latter example's signature is essentially the overloading
570 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
571 that if the arguments are instances of some subclass of str,
572 the return type is still plain str.
573
574 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
575
576 Type variables defined with covariant=True or contravariant=True
577 can be used to declare covariant or contravariant generic types.
578 See PEP 484 for more details. By default generic types are invariant
579 in all type variables.
580
581 Type variables can be introspected. e.g.:
582
583 T.__name__ == 'T'
584 T.__constraints__ == ()
585 T.__covariant__ == False
586 T.__contravariant__ = False
587 A.__constraints__ == (str, bytes)
588
589 Note that only type variables defined in global scope can be pickled.
590 """
591
592 __slots__ = ('__name__', '__bound__', '__constraints__',
593 '__covariant__', '__contravariant__')
594
595 def __init__(self, name, *constraints, bound=None,
596 covariant=False, contravariant=False):
597 self.__name__ = name
598 if covariant and contravariant:
599 raise ValueError("Bivariant types are not supported.")
600 self.__covariant__ = bool(covariant)
601 self.__contravariant__ = bool(contravariant)
602 if constraints and bound is not None:
603 raise TypeError("Constraints cannot be combined with bound=...")
604 if constraints and len(constraints) == 1:
605 raise TypeError("A single constraint is not allowed")
606 msg = "TypeVar(name, constraint, ...): constraints must be types."
607 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
608 if bound:
609 self.__bound__ = _type_check(bound, "Bound must be a type.")
610 else:
611 self.__bound__ = None
612 try:
613 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling
614 except (AttributeError, ValueError):
615 def_mod = None
616 if def_mod != 'typing':
617 self.__module__ = def_mod
618
619 def __or__(self, right):
620 return Union[self, right]
621
622 def __repr__(self):
623 if self.__covariant__:
624 prefix = '+'
625 elif self.__contravariant__:
626 prefix = '-'
627 else:
628 prefix = '~'
629 return prefix + self.__name__
630
631 def __reduce__(self):
632 return self.__name__
633
634 def __ror__(self, right):
635 return Union[self, right]
636
637
638# Special typing constructs Union, Optional, Generic, Callable and Tuple
639# use three special attributes for internal bookkeeping of generic types:
640# * __parameters__ is a tuple of unique free type parameters of a generic
641# type, for example, Dict[T, T].__parameters__ == (T,);
642# * __origin__ keeps a reference to a type that was subscripted,
643# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
644# the type.
645# * __args__ is a tuple of all arguments used in subscripting,
646# e.g., Dict[T, int].__args__ == (T, int).
647
648
649# Mapping from non-generic type names that have a generic alias in typing
650# but with a different name.
651_normalize_alias = {'list': 'List',
652 'tuple': 'Tuple',
653 'dict': 'Dict',
654 'set': 'Set',
655 'frozenset': 'FrozenSet',
656 'deque': 'Deque',
657 'defaultdict': 'DefaultDict',
658 'type': 'Type',
659 'Set': 'AbstractSet'}
660
661def _is_dunder(attr):
662 return attr.startswith('__') and attr.endswith('__')
663
664
665class _GenericAlias(_Final, _root=True):
666 """The central part of internal API.
667
668 This represents a generic version of type 'origin' with type arguments 'params'.
669 There are two kind of these aliases: user defined and special. The special ones
670 are wrappers around builtin collections and ABCs in collections.abc. These must
671 have 'name' always set. If 'inst' is False, then the alias can't be instantiated,
672 this is used by e.g. typing.List and typing.Dict.
673 """
674 def __init__(self, origin, params, *, inst=True, special=False, name=None):
675 self._inst = inst
676 self._special = special
677 if special and name is None:
678 orig_name = origin.__name__
679 name = _normalize_alias.get(orig_name, orig_name)
680 self._name = name
681 if not isinstance(params, tuple):
682 params = (params,)
683 self.__origin__ = origin
684 self.__args__ = tuple(... if a is _TypingEllipsis else
685 () if a is _TypingEmpty else
686 a for a in params)
687 self.__parameters__ = _collect_type_vars(params)
688 self.__slots__ = None # This is not documented.
689 if not name:
690 self.__module__ = origin.__module__
691
692 @_tp_cache
693 def __getitem__(self, params):
694 if self.__origin__ in (Generic, Protocol):
695 # Can't subscript Generic[...] or Protocol[...].
696 raise TypeError(f"Cannot subscript already-subscripted {self}")
697 if not isinstance(params, tuple):
698 params = (params,)
699 msg = "Parameters to generic types must be types."
700 params = tuple(_type_check(p, msg) for p in params)
701 _check_generic(self, params)
702 return _subs_tvars(self, self.__parameters__, params)
703
704 def copy_with(self, params):
705 # We don't copy self._special.
706 return _GenericAlias(self.__origin__, params, name=self._name, inst=self._inst)
707
708 def __repr__(self):
709 if (self._name != 'Callable' or
710 len(self.__args__) == 2 and self.__args__[0] is Ellipsis):
711 if self._name:
712 name = 'typing.' + self._name
713 else:
714 name = _type_repr(self.__origin__)
715 if not self._special:
716 args = f'[{", ".join([_type_repr(a) for a in self.__args__])}]'
717 else:
718 args = ''
719 return (f'{name}{args}')
720 if self._special:
721 return 'typing.Callable'
722 return (f'typing.Callable'
723 f'[[{", ".join([_type_repr(a) for a in self.__args__[:-1]])}], '
724 f'{_type_repr(self.__args__[-1])}]')
725
726 def __eq__(self, other):
727 if not isinstance(other, _GenericAlias):
728 return NotImplemented
729 if self.__origin__ != other.__origin__:
730 return False
731 if self.__origin__ is Union and other.__origin__ is Union:
732 return frozenset(self.__args__) == frozenset(other.__args__)
733 return self.__args__ == other.__args__
734
735 def __hash__(self):
736 if self.__origin__ is Union:
737 return hash((Union, frozenset(self.__args__)))
738 return hash((self.__origin__, self.__args__))
739
740 def __call__(self, *args, **kwargs):
741 if not self._inst:
742 raise TypeError(f"Type {self._name} cannot be instantiated; "
743 f"use {self._name.lower()}() instead")
744 result = self.__origin__(*args, **kwargs)
745 try:
746 result.__orig_class__ = self
747 except AttributeError:
748 pass
749 return result
750
751 def __mro_entries__(self, bases):
752 if self._name: # generic version of an ABC or built-in class
753 res = []
754 if self.__origin__ not in bases:
755 res.append(self.__origin__)
756 i = bases.index(self)
757 if not any(isinstance(b, _GenericAlias) or issubclass(b, Generic)
758 for b in bases[i+1:]):
759 res.append(Generic)
760 return tuple(res)
761 if self.__origin__ is Generic:
762 if Protocol in bases:
763 return ()
764 i = bases.index(self)
765 for b in bases[i+1:]:
766 if isinstance(b, _GenericAlias) and b is not self:
767 return ()
768 return (self.__origin__,)
769
770 def __getattr__(self, attr):
771 # We are careful for copy and pickle.
772 # Also for simplicity we just don't relay all dunder names
773 if '__origin__' in self.__dict__ and not _is_dunder(attr):
774 return getattr(self.__origin__, attr)
775 raise AttributeError(attr)
776
777 def __or__(self, right):
778 return Union[self, right]
779
780 def __setattr__(self, attr, val):
781 if _is_dunder(attr) or attr in ('_name', '_inst', '_special'):
782 super().__setattr__(attr, val)
783 else:
784 setattr(self.__origin__, attr, val)
785
786 def __instancecheck__(self, obj):
787 return self.__subclasscheck__(type(obj))
788
789 def __subclasscheck__(self, cls):
790 if self._special:
791 if not isinstance(cls, _GenericAlias):
792 return issubclass(cls, self.__origin__)
793 if cls._special:
794 return issubclass(cls.__origin__, self.__origin__)
795 raise TypeError("Subscripted generics cannot be used with"
796 " class and instance checks")
797
798 def __reduce__(self):
799 if self._special:
800 return self._name
801
802 if self._name:
803 origin = globals()[self._name]
804 else:
805 origin = self.__origin__
806 if (origin is Callable and
807 not (len(self.__args__) == 2 and self.__args__[0] is Ellipsis)):
808 args = list(self.__args__[:-1]), self.__args__[-1]
809 else:
810 args = tuple(self.__args__)
811 if len(args) == 1 and not isinstance(args[0], tuple):
812 args, = args
813 return operator.getitem, (origin, args)
814
815 def __ror__(self, right):
816 return Union[self, right]
817
818
819class _VariadicGenericAlias(_GenericAlias, _root=True):
820 """Same as _GenericAlias above but for variadic aliases. Currently,
821 this is used only by special internal aliases: Tuple and Callable.
822 """
823 def __getitem__(self, params):
824 if self._name != 'Callable' or not self._special:
825 return self.__getitem_inner__(params)
826 if not isinstance(params, tuple) or len(params) != 2:
827 raise TypeError("Callable must be used as "
828 "Callable[[arg, ...], result].")
829 args, result = params
830 if args is Ellipsis:
831 params = (Ellipsis, result)
832 else:
833 if not isinstance(args, list):
834 raise TypeError(f"Callable[args, result]: args must be a list."
835 f" Got {args}")
836 params = (tuple(args), result)
837 return self.__getitem_inner__(params)
838
839 @_tp_cache
840 def __getitem_inner__(self, params):
841 if self.__origin__ is tuple and self._special:
842 if params == ():
843 return self.copy_with((_TypingEmpty,))
844 if not isinstance(params, tuple):
845 params = (params,)
846 if len(params) == 2 and params[1] is ...:
847 msg = "Tuple[t, ...]: t must be a type."
848 p = _type_check(params[0], msg)
849 return self.copy_with((p, _TypingEllipsis))
850 msg = "Tuple[t0, t1, ...]: each t must be a type."
851 params = tuple(_type_check(p, msg) for p in params)
852 return self.copy_with(params)
853 if self.__origin__ is collections.abc.Callable and self._special:
854 args, result = params
855 msg = "Callable[args, result]: result must be a type."
856 result = _type_check(result, msg)
857 if args is Ellipsis:
858 return self.copy_with((_TypingEllipsis, result))
859 msg = "Callable[[arg, ...], result]: each arg must be a type."
860 args = tuple(_type_check(arg, msg) for arg in args)
861 params = args + (result,)
862 return self.copy_with(params)
863 return super().__getitem__(params)
864
865
866class Generic:
867 """Abstract base class for generic types.
868
869 A generic type is typically declared by inheriting from
870 this class parameterized with one or more type variables.
871 For example, a generic mapping type might be defined as::
872
873 class Mapping(Generic[KT, VT]):
874 def __getitem__(self, key: KT) -> VT:
875 ...
876 # Etc.
877
878 This class can then be used as follows::
879
880 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
881 try:
882 return mapping[key]
883 except KeyError:
884 return default
885 """
886 __slots__ = ()
887 _is_protocol = False
888
889 def __new__(cls, *args, **kwds):
890 if cls in (Generic, Protocol):
891 raise TypeError(f"Type {cls.__name__} cannot be instantiated; "
892 "it can be used only as a base class")
893 if super().__new__ is object.__new__ and cls.__init__ is not object.__init__:
894 obj = super().__new__(cls)
895 else:
896 obj = super().__new__(cls, *args, **kwds)
897 return obj
898
899 @_tp_cache
900 def __class_getitem__(cls, params):
901 if not isinstance(params, tuple):
902 params = (params,)
903 if not params and cls is not Tuple:
904 raise TypeError(
905 f"Parameter list to {cls.__qualname__}[...] cannot be empty")
906 msg = "Parameters to generic types must be types."
907 params = tuple(_type_check(p, msg) for p in params)
908 if cls in (Generic, Protocol):
909 # Generic and Protocol can only be subscripted with unique type variables.
910 if not all(isinstance(p, TypeVar) for p in params):
911 raise TypeError(
912 f"Parameters to {cls.__name__}[...] must all be type variables")
913 if len(set(params)) != len(params):
914 raise TypeError(
915 f"Parameters to {cls.__name__}[...] must all be unique")
916 else:
917 # Subscripting a regular Generic subclass.
918 _check_generic(cls, params)
919 return _GenericAlias(cls, params)
920
921 def __init_subclass__(cls, *args, **kwargs):
922 super().__init_subclass__(*args, **kwargs)
923 tvars = []
924 if '__orig_bases__' in cls.__dict__:
925 error = Generic in cls.__orig_bases__
926 else:
927 error = Generic in cls.__bases__ and cls.__name__ != 'Protocol'
928 if error:
929 raise TypeError("Cannot inherit from plain Generic")
930 if '__orig_bases__' in cls.__dict__:
931 tvars = _collect_type_vars(cls.__orig_bases__)
932 # Look for Generic[T1, ..., Tn].
933 # If found, tvars must be a subset of it.
934 # If not found, tvars is it.
935 # Also check for and reject plain Generic,
936 # and reject multiple Generic[...].
937 gvars = None
938 for base in cls.__orig_bases__:
939 if (isinstance(base, _GenericAlias) and
940 base.__origin__ is Generic):
941 if gvars is not None:
942 raise TypeError(
943 "Cannot inherit from Generic[...] multiple types.")
944 gvars = base.__parameters__
945 if gvars is not None:
946 tvarset = set(tvars)
947 gvarset = set(gvars)
948 if not tvarset <= gvarset:
949 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
950 s_args = ', '.join(str(g) for g in gvars)
951 raise TypeError(f"Some type variables ({s_vars}) are"
952 f" not listed in Generic[{s_args}]")
953 tvars = gvars
954 cls.__parameters__ = tuple(tvars)
955
956
957class _TypingEmpty:
958 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
959 to allow empty list/tuple in specific places, without allowing them
960 to sneak in where prohibited.
961 """
962
963
964class _TypingEllipsis:
965 """Internal placeholder for ... (ellipsis)."""
966
967
968_TYPING_INTERNALS = ['__parameters__', '__orig_bases__', '__orig_class__',
969 '_is_protocol', '_is_runtime_protocol']
970
971_SPECIAL_NAMES = ['__abstractmethods__', '__annotations__', '__dict__', '__doc__',
972 '__init__', '__module__', '__new__', '__slots__',
973 '__subclasshook__', '__weakref__']
974
975# These special attributes will be not collected as protocol members.
976EXCLUDED_ATTRIBUTES = _TYPING_INTERNALS + _SPECIAL_NAMES + ['_MutableMapping__marker']
977
978
979def _get_protocol_attrs(cls):
980 """Collect protocol members from a protocol class objects.
981
982 This includes names actually defined in the class dictionary, as well
983 as names that appear in annotations. Special names (above) are skipped.
984 """
985 attrs = set()
986 for base in cls.__mro__[:-1]: # without object
987 if base.__name__ in ('Protocol', 'Generic'):
988 continue
989 annotations = getattr(base, '__annotations__', {})
990 for attr in list(base.__dict__.keys()) + list(annotations.keys()):
991 if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
992 attrs.add(attr)
993 return attrs
994
995
996def _is_callable_members_only(cls):
997 # PEP 544 prohibits using issubclass() with protocols that have non-method members.
998 return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls))
999
1000
1001def _no_init(self, *args, **kwargs):
1002 if type(self)._is_protocol:
1003 raise TypeError('Protocols cannot be instantiated')
1004
1005
1006def _allow_reckless_class_cheks():
1007 """Allow instnance and class checks for special stdlib modules.
1008
1009 The abc and functools modules indiscriminately call isinstance() and
1010 issubclass() on the whole MRO of a user class, which may contain protocols.
1011 """
1012 try:
1013 return sys._getframe(3).f_globals['__name__'] in ['abc', 'functools']
1014 except (AttributeError, ValueError): # For platforms without _getframe().
1015 return True
1016
1017
1018_PROTO_ALLOWLIST = {
1019 'collections.abc': [
1020 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
1021 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
1022 ],
1023 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
1024}
1025
1026
1027class _ProtocolMeta(ABCMeta):
1028 # This metaclass is really unfortunate and exists only because of
1029 # the lack of __instancehook__.
1030 def __instancecheck__(cls, instance):
1031 # We need this method for situations where attributes are
1032 # assigned in __init__.
1033 if ((not getattr(cls, '_is_protocol', False) or
1034 _is_callable_members_only(cls)) and
1035 issubclass(instance.__class__, cls)):
1036 return True
1037 if cls._is_protocol:
1038 if all(hasattr(instance, attr) and
1039 # All *methods* can be blocked by setting them to None.
1040 (not callable(getattr(cls, attr, None)) or
1041 getattr(instance, attr) is not None)
1042 for attr in _get_protocol_attrs(cls)):
1043 return True
1044 return super().__instancecheck__(instance)
1045
1046
1047class Protocol(Generic, metaclass=_ProtocolMeta):
1048 """Base class for protocol classes.
1049
1050 Protocol classes are defined as::
1051
1052 class Proto(Protocol):
1053 def meth(self) -> int:
1054 ...
1055
1056 Such classes are primarily used with static type checkers that recognize
1057 structural subtyping (static duck-typing), for example::
1058
1059 class C:
1060 def meth(self) -> int:
1061 return 0
1062
1063 def func(x: Proto) -> int:
1064 return x.meth()
1065
1066 func(C()) # Passes static type check
1067
1068 See PEP 544 for details. Protocol classes decorated with
1069 @typing.runtime_checkable act as simple-minded runtime protocols that check
1070 only the presence of given attributes, ignoring their type signatures.
1071 Protocol classes can be generic, they are defined as::
1072
1073 class GenProto(Protocol[T]):
1074 def meth(self) -> T:
1075 ...
1076 """
1077 __slots__ = ()
1078 _is_protocol = True
1079 _is_runtime_protocol = False
1080
1081 def __init_subclass__(cls, *args, **kwargs):
1082 super().__init_subclass__(*args, **kwargs)
1083
1084 # Determine if this is a protocol or a concrete subclass.
1085 if not cls.__dict__.get('_is_protocol', False):
1086 cls._is_protocol = any(b is Protocol for b in cls.__bases__)
1087
1088 # Set (or override) the protocol subclass hook.
1089 def _proto_hook(other):
1090 if not cls.__dict__.get('_is_protocol', False):
1091 return NotImplemented
1092
1093 # First, perform various sanity checks.
1094 if not getattr(cls, '_is_runtime_protocol', False):
1095 if _allow_reckless_class_cheks():
1096 return NotImplemented
1097 raise TypeError("Instance and class checks can only be used with"
1098 " @runtime_checkable protocols")
1099 if not _is_callable_members_only(cls):
1100 if _allow_reckless_class_cheks():
1101 return NotImplemented
1102 raise TypeError("Protocols with non-method members"
1103 " don't support issubclass()")
1104 if not isinstance(other, type):
1105 # Same error message as for issubclass(1, int).
1106 raise TypeError('issubclass() arg 1 must be a class')
1107
1108 # Second, perform the actual structural compatibility check.
1109 for attr in _get_protocol_attrs(cls):
1110 for base in other.__mro__:
1111 # Check if the members appears in the class dictionary...
1112 if attr in base.__dict__:
1113 if base.__dict__[attr] is None:
1114 return NotImplemented
1115 break
1116
1117 # ...or in annotations, if it is a sub-protocol.
1118 annotations = getattr(base, '__annotations__', {})
1119 if (isinstance(annotations, collections.abc.Mapping) and
1120 attr in annotations and
1121 issubclass(other, Generic) and other._is_protocol):
1122 break
1123 else:
1124 return NotImplemented
1125 return True
1126
1127 if '__subclasshook__' not in cls.__dict__:
1128 cls.__subclasshook__ = _proto_hook
1129
1130 # We have nothing more to do for non-protocols...
1131 if not cls._is_protocol:
1132 return
1133
1134 # ... otherwise check consistency of bases, and prohibit instantiation.
1135 for base in cls.__bases__:
1136 if not (base in (object, Generic) or
1137 base.__module__ in _PROTO_ALLOWLIST and
1138 base.__name__ in _PROTO_ALLOWLIST[base.__module__] or
1139 issubclass(base, Generic) and base._is_protocol):
1140 raise TypeError('Protocols can only inherit from other'
1141 ' protocols, got %r' % base)
1142 cls.__init__ = _no_init
1143
1144
1145def runtime_checkable(cls):
1146 """Mark a protocol class as a runtime protocol.
1147
1148 Such protocol can be used with isinstance() and issubclass().
1149 Raise TypeError if applied to a non-protocol class.
1150 This allows a simple-minded structural check very similar to
1151 one trick ponies in collections.abc such as Iterable.
1152 For example::
1153
1154 @runtime_checkable
1155 class Closable(Protocol):
1156 def close(self): ...
1157
1158 assert isinstance(open('/some/file'), Closable)
1159
1160 Warning: this will check only the presence of the required methods,
1161 not their type signatures!
1162 """
1163 if not issubclass(cls, Generic) or not cls._is_protocol:
1164 raise TypeError('@runtime_checkable can be only applied to protocol classes,'
1165 ' got %r' % cls)
1166 cls._is_runtime_protocol = True
1167 return cls
1168
1169
1170def cast(typ, val):
1171 """Cast a value to a type.
1172
1173 This returns the value unchanged. To the type checker this
1174 signals that the return value has the designated type, but at
1175 runtime we intentionally don't check anything (we want this
1176 to be as fast as possible).
1177 """
1178 return val
1179
1180
1181def _get_defaults(func):
1182 """Internal helper to extract the default arguments, by name."""
1183 try:
1184 code = func.__code__
1185 except AttributeError:
1186 # Some built-in functions don't have __code__, __defaults__, etc.
1187 return {}
1188 pos_count = code.co_argcount
1189 arg_names = code.co_varnames
1190 arg_names = arg_names[:pos_count]
1191 defaults = func.__defaults__ or ()
1192 kwdefaults = func.__kwdefaults__
1193 res = dict(kwdefaults) if kwdefaults else {}
1194 pos_offset = pos_count - len(defaults)
1195 for name, value in zip(arg_names[pos_offset:], defaults):
1196 assert name not in res
1197 res[name] = value
1198 return res
1199
1200
1201_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
1202 types.MethodType, types.ModuleType)
1203# TODO(T64356933): No plans to support MethodDescriptorType, MethodWrapperType,
1204# WrapperDescriptorType in pyro
1205if (
1206 hasattr(types, "MethodDescriptorType")
1207 and hasattr(types, "MethodWrapperType")
1208 and hasattr(types, "WrapperDescriptorType")
1209):
1210 _allowed_types += (
1211 types.MethodDescriptorType,
1212 types.MethodWrapperType,
1213 types.WrapperDescriptorType,
1214 )
1215
1216def get_type_hints(obj, globalns=None, localns=None):
1217 """Return type hints for an object.
1218
1219 This is often the same as obj.__annotations__, but it handles
1220 forward references encoded as string literals, and if necessary
1221 adds Optional[t] if a default value equal to None is set.
1222
1223 The argument may be a module, class, method, or function. The annotations
1224 are returned as a dictionary. For classes, annotations include also
1225 inherited members.
1226
1227 TypeError is raised if the argument is not of a type that can contain
1228 annotations, and an empty dictionary is returned if no annotations are
1229 present.
1230
1231 BEWARE -- the behavior of globalns and localns is counterintuitive
1232 (unless you are familiar with how eval() and exec() work). The
1233 search order is locals first, then globals.
1234
1235 - If no dict arguments are passed, an attempt is made to use the
1236 globals from obj (or the respective module's globals for classes),
1237 and these are also used as the locals. If the object does not appear
1238 to have globals, an empty dictionary is used.
1239
1240 - If one dict argument is passed, it is used for both globals and
1241 locals.
1242
1243 - If two dict arguments are passed, they specify globals and
1244 locals, respectively.
1245 """
1246
1247 if getattr(obj, '__no_type_check__', None):
1248 return {}
1249 # Classes require a special treatment.
1250 if isinstance(obj, type):
1251 hints = {}
1252 for base in reversed(obj.__mro__):
1253 if globalns is None:
1254 base_globals = sys.modules[base.__module__].__dict__
1255 else:
1256 base_globals = globalns
1257 ann = base.__dict__.get('__annotations__', {})
1258 for name, value in ann.items():
1259 if value is None:
1260 value = type(None)
1261 if isinstance(value, str):
1262 value = ForwardRef(value, is_argument=False)
1263 value = _eval_type(value, base_globals, localns)
1264 hints[name] = value
1265 return hints
1266
1267 if globalns is None:
1268 if isinstance(obj, types.ModuleType):
1269 globalns = obj.__dict__
1270 else:
1271 nsobj = obj
1272 # Find globalns for the unwrapped object.
1273 while hasattr(nsobj, '__wrapped__'):
1274 nsobj = nsobj.__wrapped__
1275 globalns = getattr(nsobj, '__globals__', {})
1276 if localns is None:
1277 localns = globalns
1278 elif localns is None:
1279 localns = globalns
1280 hints = getattr(obj, '__annotations__', None)
1281 if hints is None:
1282 # Return empty annotations for something that _could_ have them.
1283 if isinstance(obj, _allowed_types):
1284 return {}
1285 else:
1286 raise TypeError('{!r} is not a module, class, method, '
1287 'or function.'.format(obj))
1288 defaults = _get_defaults(obj)
1289 hints = dict(hints)
1290 for name, value in hints.items():
1291 if value is None:
1292 value = type(None)
1293 if isinstance(value, str):
1294 value = ForwardRef(value)
1295 value = _eval_type(value, globalns, localns)
1296 if name in defaults and defaults[name] is None:
1297 value = Optional[value]
1298 hints[name] = value
1299 return hints
1300
1301
1302def get_origin(tp):
1303 """Get the unsubscripted version of a type.
1304
1305 This supports generic types, Callable, Tuple, Union, Literal, Final and ClassVar.
1306 Return None for unsupported types. Examples::
1307
1308 get_origin(Literal[42]) is Literal
1309 get_origin(int) is None
1310 get_origin(ClassVar[int]) is ClassVar
1311 get_origin(Generic) is Generic
1312 get_origin(Generic[T]) is Generic
1313 get_origin(Union[T, int]) is Union
1314 get_origin(List[Tuple[T, T]][int]) == list
1315 """
1316 if isinstance(tp, _GenericAlias):
1317 return tp.__origin__
1318 if tp is Generic:
1319 return Generic
1320 return None
1321
1322
1323def get_args(tp):
1324 """Get type arguments with all substitutions performed.
1325
1326 For unions, basic simplifications used by Union constructor are performed.
1327 Examples::
1328 get_args(Dict[str, int]) == (str, int)
1329 get_args(int) == ()
1330 get_args(Union[int, Union[T, int], str][int]) == (int, str)
1331 get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
1332 get_args(Callable[[], T][int]) == ([], int)
1333 """
1334 if isinstance(tp, _GenericAlias) and not tp._special:
1335 res = tp.__args__
1336 if get_origin(tp) is collections.abc.Callable and res[0] is not Ellipsis:
1337 res = (list(res[:-1]), res[-1])
1338 return res
1339 return ()
1340
1341
1342def no_type_check(arg):
1343 """Decorator to indicate that annotations are not type hints.
1344
1345 The argument must be a class or function; if it is a class, it
1346 applies recursively to all methods and classes defined in that class
1347 (but not to methods defined in its superclasses or subclasses).
1348
1349 This mutates the function(s) or class(es) in place.
1350 """
1351 if isinstance(arg, type):
1352 arg_attrs = arg.__dict__.copy()
1353 for attr, val in arg.__dict__.items():
1354 if val in arg.__bases__ + (arg,):
1355 arg_attrs.pop(attr)
1356 for obj in arg_attrs.values():
1357 if isinstance(obj, types.FunctionType):
1358 obj.__no_type_check__ = True
1359 if isinstance(obj, type):
1360 no_type_check(obj)
1361 try:
1362 arg.__no_type_check__ = True
1363 except TypeError: # built-in classes
1364 pass
1365 return arg
1366
1367
1368def no_type_check_decorator(decorator):
1369 """Decorator to give another decorator the @no_type_check effect.
1370
1371 This wraps the decorator with something that wraps the decorated
1372 function in @no_type_check.
1373 """
1374
1375 @functools.wraps(decorator)
1376 def wrapped_decorator(*args, **kwds):
1377 func = decorator(*args, **kwds)
1378 func = no_type_check(func)
1379 return func
1380
1381 return wrapped_decorator
1382
1383
1384def _overload_dummy(*args, **kwds):
1385 """Helper for @overload to raise when called."""
1386 raise NotImplementedError(
1387 "You should not call an overloaded function. "
1388 "A series of @overload-decorated functions "
1389 "outside a stub module should always be followed "
1390 "by an implementation that is not @overload-ed.")
1391
1392
1393def overload(func):
1394 """Decorator for overloaded functions/methods.
1395
1396 In a stub file, place two or more stub definitions for the same
1397 function in a row, each decorated with @overload. For example:
1398
1399 @overload
1400 def utf8(value: None) -> None: ...
1401 @overload
1402 def utf8(value: bytes) -> bytes: ...
1403 @overload
1404 def utf8(value: str) -> bytes: ...
1405
1406 In a non-stub file (i.e. a regular .py file), do the same but
1407 follow it with an implementation. The implementation should *not*
1408 be decorated with @overload. For example:
1409
1410 @overload
1411 def utf8(value: None) -> None: ...
1412 @overload
1413 def utf8(value: bytes) -> bytes: ...
1414 @overload
1415 def utf8(value: str) -> bytes: ...
1416 def utf8(value):
1417 # implementation goes here
1418 """
1419 return _overload_dummy
1420
1421
1422def final(f):
1423 """A decorator to indicate final methods and final classes.
1424
1425 Use this decorator to indicate to type checkers that the decorated
1426 method cannot be overridden, and decorated class cannot be subclassed.
1427 For example:
1428
1429 class Base:
1430 @final
1431 def done(self) -> None:
1432 ...
1433 class Sub(Base):
1434 def done(self) -> None: # Error reported by type checker
1435 ...
1436
1437 @final
1438 class Leaf:
1439 ...
1440 class Other(Leaf): # Error reported by type checker
1441 ...
1442
1443 There is no runtime checking of these properties.
1444 """
1445 return f
1446
1447
1448# Some unconstrained type variables. These are used by the container types.
1449# (These are not for export.)
1450T = TypeVar('T') # Any type.
1451KT = TypeVar('KT') # Key type.
1452VT = TypeVar('VT') # Value type.
1453T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
1454V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
1455VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
1456T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
1457# Internal type variable used for Type[].
1458CT_co = TypeVar('CT_co', covariant=True, bound=type)
1459
1460# A useful type variable with constraints. This represents string types.
1461# (This one *is* for export!)
1462AnyStr = TypeVar('AnyStr', bytes, str)
1463
1464
1465# Various ABCs mimicking those in collections.abc.
1466def _alias(origin, params, inst=True):
1467 return _GenericAlias(origin, params, special=True, inst=inst)
1468
1469Hashable = _alias(collections.abc.Hashable, ()) # Not generic.
1470Awaitable = _alias(collections.abc.Awaitable, T_co)
1471Coroutine = _alias(collections.abc.Coroutine, (T_co, T_contra, V_co))
1472AsyncIterable = _alias(collections.abc.AsyncIterable, T_co)
1473AsyncIterator = _alias(collections.abc.AsyncIterator, T_co)
1474Iterable = _alias(collections.abc.Iterable, T_co)
1475Iterator = _alias(collections.abc.Iterator, T_co)
1476Reversible = _alias(collections.abc.Reversible, T_co)
1477Sized = _alias(collections.abc.Sized, ()) # Not generic.
1478Container = _alias(collections.abc.Container, T_co)
1479Collection = _alias(collections.abc.Collection, T_co)
1480Callable = _VariadicGenericAlias(collections.abc.Callable, (), special=True)
1481Callable.__doc__ = \
1482 """Callable type; Callable[[int], str] is a function of (int) -> str.
1483
1484 The subscription syntax must always be used with exactly two
1485 values: the argument list and the return type. The argument list
1486 must be a list of types or ellipsis; the return type must be a single type.
1487
1488 There is no syntax to indicate optional or keyword arguments,
1489 such function types are rarely used as callback types.
1490 """
1491AbstractSet = _alias(collections.abc.Set, T_co)
1492MutableSet = _alias(collections.abc.MutableSet, T)
1493# NOTE: Mapping is only covariant in the value type.
1494Mapping = _alias(collections.abc.Mapping, (KT, VT_co))
1495MutableMapping = _alias(collections.abc.MutableMapping, (KT, VT))
1496Sequence = _alias(collections.abc.Sequence, T_co)
1497MutableSequence = _alias(collections.abc.MutableSequence, T)
1498ByteString = _alias(collections.abc.ByteString, ()) # Not generic
1499Tuple = _VariadicGenericAlias(tuple, (), inst=False, special=True)
1500Tuple.__doc__ = \
1501 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
1502
1503 Example: Tuple[T1, T2] is a tuple of two elements corresponding
1504 to type variables T1 and T2. Tuple[int, float, str] is a tuple
1505 of an int, a float and a string.
1506
1507 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
1508 """
1509List = _alias(list, T, inst=False)
1510Deque = _alias(collections.deque, T)
1511Set = _alias(set, T, inst=False)
1512FrozenSet = _alias(frozenset, T_co, inst=False)
1513MappingView = _alias(collections.abc.MappingView, T_co)
1514KeysView = _alias(collections.abc.KeysView, KT)
1515ItemsView = _alias(collections.abc.ItemsView, (KT, VT_co))
1516ValuesView = _alias(collections.abc.ValuesView, VT_co)
1517ContextManager = _alias(contextlib.AbstractContextManager, T_co)
1518AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, T_co)
1519Dict = _alias(dict, (KT, VT), inst=False)
1520DefaultDict = _alias(collections.defaultdict, (KT, VT))
1521OrderedDict = _alias(collections.OrderedDict, (KT, VT))
1522Counter = _alias(collections.Counter, T)
1523ChainMap = _alias(collections.ChainMap, (KT, VT))
1524Generator = _alias(collections.abc.Generator, (T_co, T_contra, V_co))
1525AsyncGenerator = _alias(collections.abc.AsyncGenerator, (T_co, T_contra))
1526Type = _alias(type, CT_co, inst=False)
1527Type.__doc__ = \
1528 """A special construct usable to annotate class objects.
1529
1530 For example, suppose we have the following classes::
1531
1532 class User: ... # Abstract base for User classes
1533 class BasicUser(User): ...
1534 class ProUser(User): ...
1535 class TeamUser(User): ...
1536
1537 And a function that takes a class argument that's a subclass of
1538 User and returns an instance of the corresponding class::
1539
1540 U = TypeVar('U', bound=User)
1541 def new_user(user_class: Type[U]) -> U:
1542 user = user_class()
1543 # (Here we could write the user object to a database)
1544 return user
1545
1546 joe = new_user(BasicUser)
1547
1548 At this point the type checker knows that joe has type BasicUser.
1549 """
1550
1551
1552@runtime_checkable
1553class SupportsInt(Protocol):
1554 """An ABC with one abstract method __int__."""
1555 __slots__ = ()
1556
1557 @abstractmethod
1558 def __int__(self) -> int:
1559 pass
1560
1561
1562@runtime_checkable
1563class SupportsFloat(Protocol):
1564 """An ABC with one abstract method __float__."""
1565 __slots__ = ()
1566
1567 @abstractmethod
1568 def __float__(self) -> float:
1569 pass
1570
1571
1572@runtime_checkable
1573class SupportsComplex(Protocol):
1574 """An ABC with one abstract method __complex__."""
1575 __slots__ = ()
1576
1577 @abstractmethod
1578 def __complex__(self) -> complex:
1579 pass
1580
1581
1582@runtime_checkable
1583class SupportsBytes(Protocol):
1584 """An ABC with one abstract method __bytes__."""
1585 __slots__ = ()
1586
1587 @abstractmethod
1588 def __bytes__(self) -> bytes:
1589 pass
1590
1591
1592@runtime_checkable
1593class SupportsIndex(Protocol):
1594 """An ABC with one abstract method __index__."""
1595 __slots__ = ()
1596
1597 @abstractmethod
1598 def __index__(self) -> int:
1599 pass
1600
1601
1602@runtime_checkable
1603class SupportsAbs(Protocol[T_co]):
1604 """An ABC with one abstract method __abs__ that is covariant in its return type."""
1605 __slots__ = ()
1606
1607 @abstractmethod
1608 def __abs__(self) -> T_co:
1609 pass
1610
1611
1612@runtime_checkable
1613class SupportsRound(Protocol[T_co]):
1614 """An ABC with one abstract method __round__ that is covariant in its return type."""
1615 __slots__ = ()
1616
1617 @abstractmethod
1618 def __round__(self, ndigits: int = 0) -> T_co:
1619 pass
1620
1621
1622def _make_nmtuple(name, types):
1623 msg = "NamedTuple('Name', [(f0, t0), (f1, t1), ...]); each t must be a type"
1624 types = [(n, _type_check(t, msg)) for n, t in types]
1625 nm_tpl = collections.namedtuple(name, [n for n, t in types])
1626 # Prior to PEP 526, only _field_types attribute was assigned.
1627 # Now __annotations__ are used and _field_types is deprecated (remove in 3.9)
1628 nm_tpl.__annotations__ = nm_tpl._field_types = dict(types)
1629 try:
1630 nm_tpl.__module__ = sys._getframe(2).f_globals.get('__name__', '__main__')
1631 except (AttributeError, ValueError):
1632 pass
1633 return nm_tpl
1634
1635
1636# attributes prohibited to set in NamedTuple class syntax
1637_prohibited = ('__new__', '__init__', '__slots__', '__getnewargs__',
1638 '_fields', '_field_defaults', '_field_types',
1639 '_make', '_replace', '_asdict', '_source')
1640
1641_special = ('__module__', '__name__', '__annotations__')
1642
1643
1644class NamedTupleMeta(type):
1645
1646 def __new__(cls, typename, bases, ns):
1647 if ns.get('_root', False):
1648 return super().__new__(cls, typename, bases, ns)
1649 types = ns.get('__annotations__', {})
1650 nm_tpl = _make_nmtuple(typename, types.items())
1651 defaults = []
1652 defaults_dict = {}
1653 for field_name in types:
1654 if field_name in ns:
1655 default_value = ns[field_name]
1656 defaults.append(default_value)
1657 defaults_dict[field_name] = default_value
1658 elif defaults:
1659 raise TypeError("Non-default namedtuple field {field_name} cannot "
1660 "follow default field(s) {default_names}"
1661 .format(field_name=field_name,
1662 default_names=', '.join(defaults_dict.keys())))
1663 nm_tpl.__new__.__annotations__ = dict(types)
1664 nm_tpl.__new__.__defaults__ = tuple(defaults)
1665 nm_tpl._field_defaults = defaults_dict
1666 # update from user namespace without overriding special namedtuple attributes
1667 for key in ns:
1668 if key in _prohibited:
1669 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
1670 elif key not in _special and key not in nm_tpl._fields:
1671 setattr(nm_tpl, key, ns[key])
1672 return nm_tpl
1673
1674
1675class NamedTuple(metaclass=NamedTupleMeta):
1676 """Typed version of namedtuple.
1677
1678 Usage in Python versions >= 3.6::
1679
1680 class Employee(NamedTuple):
1681 name: str
1682 id: int
1683
1684 This is equivalent to::
1685
1686 Employee = collections.namedtuple('Employee', ['name', 'id'])
1687
1688 The resulting class has an extra __annotations__ attribute, giving a
1689 dict that maps field names to types. (The field names are also in
1690 the _fields attribute, which is part of the namedtuple API.)
1691 Alternative equivalent keyword syntax is also accepted::
1692
1693 Employee = NamedTuple('Employee', name=str, id=int)
1694
1695 In Python versions <= 3.5 use::
1696
1697 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
1698 """
1699 _root = True
1700
1701 def __new__(*args, **kwargs):
1702 if not args:
1703 raise TypeError('NamedTuple.__new__(): not enough arguments')
1704 cls, *args = args # allow the "cls" keyword be passed
1705 if args:
1706 typename, *args = args # allow the "typename" keyword be passed
1707 elif 'typename' in kwargs:
1708 typename = kwargs.pop('typename')
1709 import warnings
1710 warnings.warn("Passing 'typename' as keyword argument is deprecated",
1711 DeprecationWarning, stacklevel=2)
1712 else:
1713 raise TypeError("NamedTuple.__new__() missing 1 required positional "
1714 "argument: 'typename'")
1715 if args:
1716 try:
1717 fields, = args # allow the "fields" keyword be passed
1718 except ValueError:
1719 raise TypeError(f'NamedTuple.__new__() takes from 2 to 3 '
1720 f'positional arguments but {len(args) + 2} '
1721 f'were given') from None
1722 elif 'fields' in kwargs and len(kwargs) == 1:
1723 fields = kwargs.pop('fields')
1724 import warnings
1725 warnings.warn("Passing 'fields' as keyword argument is deprecated",
1726 DeprecationWarning, stacklevel=2)
1727 else:
1728 fields = None
1729
1730 if fields is None:
1731 fields = kwargs.items()
1732 elif kwargs:
1733 raise TypeError("Either list of fields or keywords"
1734 " can be provided to NamedTuple, not both")
1735 return _make_nmtuple(typename, fields)
1736 __new__.__text_signature__ = '($cls, typename, fields=None, /, **kwargs)'
1737
1738# TODO(T59042197)
1739# This was updated from the original implementation to work with pyro
1740# -def _dict_new(cls, /, *args, **kwargs):
1741# +def _dict_new(cls, *args, **kwargs):
1742def _dict_new(cls, *args, **kwargs):
1743 return dict(*args, **kwargs)
1744
1745
1746# TODO(T59042197)
1747# This was updated from the original implementation to work with pyro
1748# -def _typeddict_new(cls, typename, fields=None, /, *, total=True, **kwargs):
1749# +def _typeddict_new(cls, typename, fields=None, *args, total=True, **kwargs):
1750def _typeddict_new(cls, typename, fields=None, *args, total=True, **kwargs):
1751 if fields is None:
1752 fields = kwargs
1753 elif kwargs:
1754 raise TypeError("TypedDict takes either a dict or keyword arguments,"
1755 " but not both")
1756
1757 ns = {'__annotations__': dict(fields), '__total__': total}
1758 try:
1759 # Setting correct module is necessary to make typed dict classes pickleable.
1760 ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
1761 except (AttributeError, ValueError):
1762 pass
1763
1764 return _TypedDictMeta(typename, (), ns)
1765
1766
1767def _check_fails(cls, other):
1768 # Typed dicts are only for static structural subtyping.
1769 raise TypeError('TypedDict does not support instance and class checks')
1770
1771
1772class _TypedDictMeta(type):
1773 def __new__(cls, name, bases, ns, total=True):
1774 """Create new typed dict class object.
1775
1776 This method is called directly when TypedDict is subclassed,
1777 or via _typeddict_new when TypedDict is instantiated. This way
1778 TypedDict supports all three syntax forms described in its docstring.
1779 Subclasses and instances of TypedDict return actual dictionaries
1780 via _dict_new.
1781 """
1782 ns['__new__'] = _typeddict_new if name == 'TypedDict' else _dict_new
1783 tp_dict = super(_TypedDictMeta, cls).__new__(cls, name, (dict,), ns)
1784
1785 anns = ns.get('__annotations__', {})
1786 msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
1787 anns = {n: _type_check(tp, msg) for n, tp in anns.items()}
1788 for base in bases:
1789 anns.update(base.__dict__.get('__annotations__', {}))
1790 tp_dict.__annotations__ = anns
1791 if not hasattr(tp_dict, '__total__'):
1792 tp_dict.__total__ = total
1793 return tp_dict
1794
1795 __instancecheck__ = __subclasscheck__ = _check_fails
1796
1797
1798class TypedDict(dict, metaclass=_TypedDictMeta):
1799 """A simple typed namespace. At runtime it is equivalent to a plain dict.
1800
1801 TypedDict creates a dictionary type that expects all of its
1802 instances to have a certain set of keys, where each key is
1803 associated with a value of a consistent type. This expectation
1804 is not checked at runtime but is only enforced by type checkers.
1805 Usage::
1806
1807 class Point2D(TypedDict):
1808 x: int
1809 y: int
1810 label: str
1811
1812 a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
1813 b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
1814
1815 assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
1816
1817 The type info can be accessed via Point2D.__annotations__. TypedDict
1818 supports two additional equivalent forms::
1819
1820 Point2D = TypedDict('Point2D', x=int, y=int, label=str)
1821 Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
1822
1823 By default, all keys must be present in a TypedDict. It is possible
1824 to override this by specifying totality.
1825 Usage::
1826
1827 class point2D(TypedDict, total=False):
1828 x: int
1829 y: int
1830
1831 This means that a point2D TypedDict can have any of the keys omitted.A type
1832 checker is only expected to support a literal False or True as the value of
1833 the total argument. True is the default, and makes all items defined in the
1834 class body be required.
1835
1836 The class syntax is only supported in Python 3.6+, while two other
1837 syntax forms work for Python 2.7 and 3.2+
1838 """
1839
1840
1841def NewType(name, tp):
1842 """NewType creates simple unique types with almost zero
1843 runtime overhead. NewType(name, tp) is considered a subtype of tp
1844 by static type checkers. At runtime, NewType(name, tp) returns
1845 a dummy function that simply returns its argument. Usage::
1846
1847 UserId = NewType('UserId', int)
1848
1849 def name_by_id(user_id: UserId) -> str:
1850 ...
1851
1852 UserId('user') # Fails type check
1853
1854 name_by_id(42) # Fails type check
1855 name_by_id(UserId(42)) # OK
1856
1857 num = UserId(5) + 1 # type: int
1858 """
1859
1860 def new_type(x):
1861 return x
1862
1863 new_type.__name__ = name
1864 new_type.__supertype__ = tp
1865 return new_type
1866
1867
1868# Python-version-specific alias (Python 2: unicode; Python 3: str)
1869Text = str
1870
1871
1872# Constant that's True when type checking, but False here.
1873TYPE_CHECKING = False
1874
1875
1876class IO(Generic[AnyStr]):
1877 """Generic base class for TextIO and BinaryIO.
1878
1879 This is an abstract, generic version of the return of open().
1880
1881 NOTE: This does not distinguish between the different possible
1882 classes (text vs. binary, read vs. write vs. read/write,
1883 append-only, unbuffered). The TextIO and BinaryIO subclasses
1884 below capture the distinctions between text vs. binary, which is
1885 pervasive in the interface; however we currently do not offer a
1886 way to track the other distinctions in the type system.
1887 """
1888
1889 __slots__ = ()
1890
1891 @property
1892 @abstractmethod
1893 def mode(self) -> str:
1894 pass
1895
1896 @property
1897 @abstractmethod
1898 def name(self) -> str:
1899 pass
1900
1901 @abstractmethod
1902 def close(self) -> None:
1903 pass
1904
1905 @property
1906 @abstractmethod
1907 def closed(self) -> bool:
1908 pass
1909
1910 @abstractmethod
1911 def fileno(self) -> int:
1912 pass
1913
1914 @abstractmethod
1915 def flush(self) -> None:
1916 pass
1917
1918 @abstractmethod
1919 def isatty(self) -> bool:
1920 pass
1921
1922 @abstractmethod
1923 def read(self, n: int = -1) -> AnyStr:
1924 pass
1925
1926 @abstractmethod
1927 def readable(self) -> bool:
1928 pass
1929
1930 @abstractmethod
1931 def readline(self, limit: int = -1) -> AnyStr:
1932 pass
1933
1934 @abstractmethod
1935 def readlines(self, hint: int = -1) -> List[AnyStr]:
1936 pass
1937
1938 @abstractmethod
1939 def seek(self, offset: int, whence: int = 0) -> int:
1940 pass
1941
1942 @abstractmethod
1943 def seekable(self) -> bool:
1944 pass
1945
1946 @abstractmethod
1947 def tell(self) -> int:
1948 pass
1949
1950 @abstractmethod
1951 def truncate(self, size: int = None) -> int:
1952 pass
1953
1954 @abstractmethod
1955 def writable(self) -> bool:
1956 pass
1957
1958 @abstractmethod
1959 def write(self, s: AnyStr) -> int:
1960 pass
1961
1962 @abstractmethod
1963 def writelines(self, lines: List[AnyStr]) -> None:
1964 pass
1965
1966 @abstractmethod
1967 def __enter__(self) -> 'IO[AnyStr]':
1968 pass
1969
1970 @abstractmethod
1971 def __exit__(self, type, value, traceback) -> None:
1972 pass
1973
1974
1975class BinaryIO(IO[bytes]):
1976 """Typed version of the return of open() in binary mode."""
1977
1978 __slots__ = ()
1979
1980 @abstractmethod
1981 def write(self, s: Union[bytes, bytearray]) -> int:
1982 pass
1983
1984 @abstractmethod
1985 def __enter__(self) -> 'BinaryIO':
1986 pass
1987
1988
1989class TextIO(IO[str]):
1990 """Typed version of the return of open() in text mode."""
1991
1992 __slots__ = ()
1993
1994 @property
1995 @abstractmethod
1996 def buffer(self) -> BinaryIO:
1997 pass
1998
1999 @property
2000 @abstractmethod
2001 def encoding(self) -> str:
2002 pass
2003
2004 @property
2005 @abstractmethod
2006 def errors(self) -> Optional[str]:
2007 pass
2008
2009 @property
2010 @abstractmethod
2011 def line_buffering(self) -> bool:
2012 pass
2013
2014 @property
2015 @abstractmethod
2016 def newlines(self) -> Any:
2017 pass
2018
2019 @abstractmethod
2020 def __enter__(self) -> 'TextIO':
2021 pass
2022
2023
2024class io:
2025 """Wrapper namespace for IO generic classes."""
2026
2027 __all__ = ['IO', 'TextIO', 'BinaryIO']
2028 IO = IO
2029 TextIO = TextIO
2030 BinaryIO = BinaryIO
2031
2032
2033io.__name__ = __name__ + '.io'
2034sys.modules[io.__name__] = io
2035
2036Pattern = _alias(stdlib_re.Pattern, AnyStr)
2037Match = _alias(stdlib_re.Match, AnyStr)
2038
2039class re:
2040 """Wrapper namespace for re type aliases."""
2041
2042 __all__ = ['Pattern', 'Match']
2043 Pattern = Pattern
2044 Match = Match
2045
2046
2047re.__name__ = __name__ + '.re'
2048sys.modules[re.__name__] = re