1"""
2The typing module: Support for gradual typing as defined by PEP 484 and subsequent PEPs.
3
4Among other things, the module includes the following:
5* Generic, Protocol, and internal machinery to support generic aliases.
6 All subscripted types like X[int], Union[int, str] are generic aliases.
7* Various "special forms" that have unique meanings in type annotations:
8 NoReturn, Never, ClassVar, Self, Concatenate, Unpack, and others.
9* Classes whose instances can be type arguments to generic classes and functions:
10 TypeVar, ParamSpec, TypeVarTuple.
11* Public helper functions: get_type_hints, overload, cast, final, and others.
12* Several protocols to support duck-typing:
13 SupportsFloat, SupportsIndex, SupportsAbs, and others.
14* Special types: NewType, NamedTuple, TypedDict.
15* Deprecated wrapper submodules for re and io related types.
16* Deprecated aliases for builtin types and collections.abc ABCs.
17
18Any name not present in __all__ is an implementation detail
19that may be changed without notice. Use at your own risk!
20"""
21
22from abc import abstractmethod, ABCMeta
23import collections
24from collections import defaultdict
25import collections.abc
26import contextlib
27import functools
28import operator
29import re as stdlib_re # Avoid confusion with the re we export.
30import sys
31import types
32import warnings
33from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType, GenericAlias
34
35
36try:
37 from _typing import _idfunc
38except ImportError:
39 def _idfunc(_, x):
40 return x
41
42# Please keep __all__ alphabetized within each category.
43__all__ = [
44 # Super-special typing primitives.
45 'Annotated',
46 'Any',
47 'Callable',
48 'ClassVar',
49 'Concatenate',
50 'Final',
51 'ForwardRef',
52 'Generic',
53 'Literal',
54 'Optional',
55 'ParamSpec',
56 'Protocol',
57 'Tuple',
58 'Type',
59 'TypeVar',
60 'TypeVarTuple',
61 'Union',
62
63 # ABCs (from collections.abc).
64 'AbstractSet', # collections.abc.Set.
65 'ByteString',
66 'Container',
67 'ContextManager',
68 'Hashable',
69 'ItemsView',
70 'Iterable',
71 'Iterator',
72 'KeysView',
73 'Mapping',
74 'MappingView',
75 'MutableMapping',
76 'MutableSequence',
77 'MutableSet',
78 'Sequence',
79 'Sized',
80 'ValuesView',
81 'Awaitable',
82 'AsyncIterator',
83 'AsyncIterable',
84 'Coroutine',
85 'Collection',
86 'AsyncGenerator',
87 'AsyncContextManager',
88
89 # Structural checks, a.k.a. protocols.
90 'Reversible',
91 'SupportsAbs',
92 'SupportsBytes',
93 'SupportsComplex',
94 'SupportsFloat',
95 'SupportsIndex',
96 'SupportsInt',
97 'SupportsRound',
98
99 # Concrete collection types.
100 'ChainMap',
101 'Counter',
102 'Deque',
103 'Dict',
104 'DefaultDict',
105 'List',
106 'OrderedDict',
107 'Set',
108 'FrozenSet',
109 'NamedTuple', # Not really a type.
110 'TypedDict', # Not really a type.
111 'Generator',
112
113 # Other concrete types.
114 'BinaryIO',
115 'IO',
116 'Match',
117 'Pattern',
118 'TextIO',
119
120 # One-off things.
121 'AnyStr',
122 'assert_type',
123 'assert_never',
124 'cast',
125 'clear_overloads',
126 'dataclass_transform',
127 'final',
128 'get_args',
129 'get_origin',
130 'get_overloads',
131 'get_type_hints',
132 'is_typeddict',
133 'LiteralString',
134 'Never',
135 'NewType',
136 'no_type_check',
137 'no_type_check_decorator',
138 'NoReturn',
139 'NotRequired',
140 'overload',
141 'ParamSpecArgs',
142 'ParamSpecKwargs',
143 'Required',
144 'reveal_type',
145 'runtime_checkable',
146 'Self',
147 'Text',
148 'TYPE_CHECKING',
149 'TypeAlias',
150 'TypeGuard',
151 'Unpack',
152]
153
154# The pseudo-submodules 're' and 'io' are part of the public
155# namespace, but excluded from __all__ because they might stomp on
156# legitimate imports of those modules.
157
158
159def _type_convert(arg, module=None, *, allow_special_forms=False):
160 """For converting None to type(None), and strings to ForwardRef."""
161 if arg is None:
162 return type(None)
163 if isinstance(arg, str):
164 return ForwardRef(arg, module=module, is_class=allow_special_forms)
165 return arg
166
167
168def _type_check(arg, msg, is_argument=True, module=None, *, allow_special_forms=False):
169 """Check that the argument is a type, and return it (internal helper).
170
171 As a special case, accept None and return type(None) instead. Also wrap strings
172 into ForwardRef instances. Consider several corner cases, for example plain
173 special forms like Union are not valid, while Union[int, str] is OK, etc.
174 The msg argument is a human-readable error message, e.g.::
175
176 "Union[arg, ...]: arg should be a type."
177
178 We append the repr() of the actual value (truncated to 100 chars).
179 """
180 invalid_generic_forms = (Generic, Protocol)
181 if not allow_special_forms:
182 invalid_generic_forms += (ClassVar,)
183 if is_argument:
184 invalid_generic_forms += (Final,)
185
186 arg = _type_convert(arg, module=module, allow_special_forms=allow_special_forms)
187 if (isinstance(arg, _GenericAlias) and
188 arg.__origin__ in invalid_generic_forms):
189 raise TypeError(f"{arg} is not valid as type argument")
190 if arg in (Any, LiteralString, NoReturn, Never, Self, TypeAlias):
191 return arg
192 if allow_special_forms and arg in (ClassVar, Final):
193 return arg
194 if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
195 raise TypeError(f"Plain {arg} is not valid as type argument")
196 if type(arg) is tuple:
197 raise TypeError(f"{msg} Got {arg!r:.100}.")
198 return arg
199
200
201def _is_param_expr(arg):
202 return arg is ... or isinstance(arg,
203 (tuple, list, ParamSpec, _ConcatenateGenericAlias))
204
205
206def _should_unflatten_callable_args(typ, args):
207 """Internal helper for munging collections.abc.Callable's __args__.
208
209 The canonical representation for a Callable's __args__ flattens the
210 argument types, see https://github.com/python/cpython/issues/86361.
211
212 For example::
213
214 >>> import collections.abc
215 >>> P = ParamSpec('P')
216 >>> collections.abc.Callable[[int, int], str].__args__ == (int, int, str)
217 True
218 >>> collections.abc.Callable[P, str].__args__ == (P, str)
219 True
220
221 As a result, if we need to reconstruct the Callable from its __args__,
222 we need to unflatten it.
223 """
224 return (
225 typ.__origin__ is collections.abc.Callable
226 and not (len(args) == 2 and _is_param_expr(args[0]))
227 )
228
229
230def _type_repr(obj):
231 """Return the repr() of an object, special-casing types (internal helper).
232
233 If obj is a type, we return a shorter version than the default
234 type.__repr__, based on the module and qualified name, which is
235 typically enough to uniquely identify a type. For everything
236 else, we fall back on repr(obj).
237 """
238 if isinstance(obj, types.GenericAlias):
239 return repr(obj)
240 if isinstance(obj, type):
241 if obj.__module__ == 'builtins':
242 return obj.__qualname__
243 return f'{obj.__module__}.{obj.__qualname__}'
244 if obj is ...:
245 return('...')
246 if isinstance(obj, types.FunctionType):
247 return obj.__name__
248 return repr(obj)
249
250
251def _collect_parameters(args):
252 """Collect all type variables and parameter specifications in args
253 in order of first appearance (lexicographic order).
254
255 For example::
256
257 >>> P = ParamSpec('P')
258 >>> T = TypeVar('T')
259 >>> _collect_parameters((T, Callable[P, T]))
260 (~T, ~P)
261 """
262 parameters = []
263 for t in args:
264 if isinstance(t, type):
265 # We don't want __parameters__ descriptor of a bare Python class.
266 pass
267 elif isinstance(t, tuple):
268 # `t` might be a tuple, when `ParamSpec` is substituted with
269 # `[T, int]`, or `[int, *Ts]`, etc.
270 for x in t:
271 for collected in _collect_parameters([x]):
272 if collected not in parameters:
273 parameters.append(collected)
274 elif hasattr(t, '__typing_subst__'):
275 if t not in parameters:
276 parameters.append(t)
277 else:
278 for x in getattr(t, '__parameters__', ()):
279 if x not in parameters:
280 parameters.append(x)
281 return tuple(parameters)
282
283
284def _check_generic(cls, parameters, elen):
285 """Check correct count for parameters of a generic cls (internal helper).
286
287 This gives a nice error message in case of count mismatch.
288 """
289 if not elen:
290 raise TypeError(f"{cls} is not a generic class")
291 alen = len(parameters)
292 if alen != elen:
293 raise TypeError(f"Too {'many' if alen > elen else 'few'} arguments for {cls};"
294 f" actual {alen}, expected {elen}")
295
296def _unpack_args(args):
297 newargs = []
298 for arg in args:
299 subargs = getattr(arg, '__typing_unpacked_tuple_args__', None)
300 if subargs is not None and not (subargs and subargs[-1] is ...):
301 newargs.extend(subargs)
302 else:
303 newargs.append(arg)
304 return newargs
305
306def _deduplicate(params, *, unhashable_fallback=False):
307 # Weed out strict duplicates, preserving the first of each occurrence.
308 try:
309 return dict.fromkeys(params)
310 except TypeError:
311 if not unhashable_fallback:
312 raise
313 # Happens for cases like `Annotated[dict, {'x': IntValidator()}]`
314 return _deduplicate_unhashable(params)
315
316def _deduplicate_unhashable(unhashable_params):
317 new_unhashable = []
318 for t in unhashable_params:
319 if t not in new_unhashable:
320 new_unhashable.append(t)
321 return new_unhashable
322
323def _compare_args_orderless(first_args, second_args):
324 first_unhashable = _deduplicate_unhashable(first_args)
325 second_unhashable = _deduplicate_unhashable(second_args)
326 t = list(second_unhashable)
327 try:
328 for elem in first_unhashable:
329 t.remove(elem)
330 except ValueError:
331 return False
332 return not t
333
334def _remove_dups_flatten(parameters):
335 """Internal helper for Union creation and substitution.
336
337 Flatten Unions among parameters, then remove duplicates.
338 """
339 # Flatten out Union[Union[...], ...].
340 params = []
341 for p in parameters:
342 if isinstance(p, (_UnionGenericAlias, types.UnionType)):
343 params.extend(p.__args__)
344 else:
345 params.append(p)
346
347 return tuple(_deduplicate(params, unhashable_fallback=True))
348
349
350def _flatten_literal_params(parameters):
351 """Internal helper for Literal creation: flatten Literals among parameters."""
352 params = []
353 for p in parameters:
354 if isinstance(p, _LiteralGenericAlias):
355 params.extend(p.__args__)
356 else:
357 params.append(p)
358 return tuple(params)
359
360
361_cleanups = []
362
363
364def _tp_cache(func=None, /, *, typed=False):
365 """Internal wrapper caching __getitem__ of generic types.
366
367 For non-hashable arguments, the original function is used as a fallback.
368 """
369 def decorator(func):
370 cached = functools.lru_cache(typed=typed)(func)
371 _cleanups.append(cached.cache_clear)
372
373 @functools.wraps(func)
374 def inner(*args, **kwds):
375 try:
376 return cached(*args, **kwds)
377 except TypeError:
378 pass # All real errors (not unhashable args) are raised below.
379 return func(*args, **kwds)
380 return inner
381
382 if func is not None:
383 return decorator(func)
384
385 return decorator
386
387def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
388 """Evaluate all forward references in the given type t.
389
390 For use of globalns and localns see the docstring for get_type_hints().
391 recursive_guard is used to prevent infinite recursion with a recursive
392 ForwardRef.
393 """
394 if isinstance(t, ForwardRef):
395 return t._evaluate(globalns, localns, recursive_guard)
396 if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):
397 if isinstance(t, GenericAlias):
398 args = tuple(
399 ForwardRef(arg) if isinstance(arg, str) else arg
400 for arg in t.__args__
401 )
402 is_unpacked = t.__unpacked__
403 if _should_unflatten_callable_args(t, args):
404 t = t.__origin__[(args[:-1], args[-1])]
405 else:
406 t = t.__origin__[args]
407 if is_unpacked:
408 t = Unpack[t]
409 ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
410 if ev_args == t.__args__:
411 return t
412 if isinstance(t, GenericAlias):
413 return GenericAlias(t.__origin__, ev_args)
414 if isinstance(t, types.UnionType):
415 return functools.reduce(operator.or_, ev_args)
416 else:
417 return t.copy_with(ev_args)
418 return t
419
420
421class _Final:
422 """Mixin to prohibit subclassing."""
423
424 __slots__ = ('__weakref__',)
425
426 def __init_subclass__(cls, /, *args, **kwds):
427 if '_root' not in kwds:
428 raise TypeError("Cannot subclass special typing classes")
429
430class _Immutable:
431 """Mixin to indicate that object should not be copied."""
432
433 __slots__ = ()
434
435 def __copy__(self):
436 return self
437
438 def __deepcopy__(self, memo):
439 return self
440
441
442class _NotIterable:
443 """Mixin to prevent iteration, without being compatible with Iterable.
444
445 That is, we could do::
446
447 def __iter__(self): raise TypeError()
448
449 But this would make users of this mixin duck type-compatible with
450 collections.abc.Iterable - isinstance(foo, Iterable) would be True.
451
452 Luckily, we can instead prevent iteration by setting __iter__ to None, which
453 is treated specially.
454 """
455
456 __slots__ = ()
457 __iter__ = None
458
459
460# Internal indicator of special typing constructs.
461# See __doc__ instance attribute for specific docs.
462class _SpecialForm(_Final, _NotIterable, _root=True):
463 __slots__ = ('_name', '__doc__', '_getitem')
464
465 def __init__(self, getitem):
466 self._getitem = getitem
467 self._name = getitem.__name__
468 self.__doc__ = getitem.__doc__
469
470 def __getattr__(self, item):
471 if item in {'__name__', '__qualname__'}:
472 return self._name
473
474 raise AttributeError(item)
475
476 def __mro_entries__(self, bases):
477 raise TypeError(f"Cannot subclass {self!r}")
478
479 def __repr__(self):
480 return 'typing.' + self._name
481
482 def __reduce__(self):
483 return self._name
484
485 def __call__(self, *args, **kwds):
486 raise TypeError(f"Cannot instantiate {self!r}")
487
488 def __or__(self, other):
489 return Union[self, other]
490
491 def __ror__(self, other):
492 return Union[other, self]
493
494 def __instancecheck__(self, obj):
495 raise TypeError(f"{self} cannot be used with isinstance()")
496
497 def __subclasscheck__(self, cls):
498 raise TypeError(f"{self} cannot be used with issubclass()")
499
500 @_tp_cache
501 def __getitem__(self, parameters):
502 return self._getitem(self, parameters)
503
504
505class _LiteralSpecialForm(_SpecialForm, _root=True):
506 def __getitem__(self, parameters):
507 if not isinstance(parameters, tuple):
508 parameters = (parameters,)
509 return self._getitem(self, *parameters)
510
511
512class _AnyMeta(type):
513 def __instancecheck__(self, obj):
514 if self is Any:
515 raise TypeError("typing.Any cannot be used with isinstance()")
516 return super().__instancecheck__(obj)
517
518 def __repr__(self):
519 if self is Any:
520 return "typing.Any"
521 return super().__repr__() # respect to subclasses
522
523
[docs]
524class Any(metaclass=_AnyMeta):
525 """Special type indicating an unconstrained type.
526
527 - Any is compatible with every type.
528 - Any assumed to have all methods.
529 - All values assumed to be instances of Any.
530
531 Note that all the above statements are true from the point of view of
532 static type checkers. At runtime, Any should not be used with instance
533 checks.
534 """
535
536 def __new__(cls, *args, **kwargs):
537 if cls is Any:
538 raise TypeError("Any cannot be instantiated")
539 return super().__new__(cls)
540
541
542@_SpecialForm
543def NoReturn(self, parameters):
544 """Special type indicating functions that never return.
545
546 Example::
547
548 from typing import NoReturn
549
550 def stop() -> NoReturn:
551 raise Exception('no way')
552
553 NoReturn can also be used as a bottom type, a type that
554 has no values. Starting in Python 3.11, the Never type should
555 be used for this concept instead. Type checkers should treat the two
556 equivalently.
557 """
558 raise TypeError(f"{self} is not subscriptable")
559
560# This is semantically identical to NoReturn, but it is implemented
561# separately so that type checkers can distinguish between the two
562# if they want.
563@_SpecialForm
564def Never(self, parameters):
565 """The bottom type, a type that has no members.
566
567 This can be used to define a function that should never be
568 called, or a function that never returns::
569
570 from typing import Never
571
572 def never_call_me(arg: Never) -> None:
573 pass
574
575 def int_or_str(arg: int | str) -> None:
576 never_call_me(arg) # type checker error
577 match arg:
578 case int():
579 print("It's an int")
580 case str():
581 print("It's a str")
582 case _:
583 never_call_me(arg) # OK, arg is of type Never
584 """
585 raise TypeError(f"{self} is not subscriptable")
586
587
588@_SpecialForm
589def Self(self, parameters):
590 """Used to spell the type of "self" in classes.
591
592 Example::
593
594 from typing import Self
595
596 class Foo:
597 def return_self(self) -> Self:
598 ...
599 return self
600
601 This is especially useful for:
602 - classmethods that are used as alternative constructors
603 - annotating an `__enter__` method which returns self
604 """
605 raise TypeError(f"{self} is not subscriptable")
606
607
608@_SpecialForm
609def LiteralString(self, parameters):
610 """Represents an arbitrary literal string.
611
612 Example::
613
614 from typing import LiteralString
615
616 def run_query(sql: LiteralString) -> None:
617 ...
618
619 def caller(arbitrary_string: str, literal_string: LiteralString) -> None:
620 run_query("SELECT * FROM students") # OK
621 run_query(literal_string) # OK
622 run_query("SELECT * FROM " + literal_string) # OK
623 run_query(arbitrary_string) # type checker error
624 run_query( # type checker error
625 f"SELECT * FROM students WHERE name = {arbitrary_string}"
626 )
627
628 Only string literals and other LiteralStrings are compatible
629 with LiteralString. This provides a tool to help prevent
630 security issues such as SQL injection.
631 """
632 raise TypeError(f"{self} is not subscriptable")
633
634
635@_SpecialForm
636def ClassVar(self, parameters):
637 """Special type construct to mark class variables.
638
639 An annotation wrapped in ClassVar indicates that a given
640 attribute is intended to be used as a class variable and
641 should not be set on instances of that class.
642
643 Usage::
644
645 class Starship:
646 stats: ClassVar[dict[str, int]] = {} # class variable
647 damage: int = 10 # instance variable
648
649 ClassVar accepts only types and cannot be further subscribed.
650
651 Note that ClassVar is not a class itself, and should not
652 be used with isinstance() or issubclass().
653 """
654 item = _type_check(parameters, f'{self} accepts only single type.')
655 return _GenericAlias(self, (item,))
656
657@_SpecialForm
658def Final(self, parameters):
659 """Special typing construct to indicate final names to type checkers.
660
661 A final name cannot be re-assigned or overridden in a subclass.
662
663 For example::
664
665 MAX_SIZE: Final = 9000
666 MAX_SIZE += 1 # Error reported by type checker
667
668 class Connection:
669 TIMEOUT: Final[int] = 10
670
671 class FastConnector(Connection):
672 TIMEOUT = 1 # Error reported by type checker
673
674 There is no runtime checking of these properties.
675 """
676 item = _type_check(parameters, f'{self} accepts only single type.')
677 return _GenericAlias(self, (item,))
678
679@_SpecialForm
680def Union(self, parameters):
681 """Union type; Union[X, Y] means either X or Y.
682
683 On Python 3.10 and higher, the | operator
684 can also be used to denote unions;
685 X | Y means the same thing to the type checker as Union[X, Y].
686
687 To define a union, use e.g. Union[int, str]. Details:
688 - The arguments must be types and there must be at least one.
689 - None as an argument is a special case and is replaced by
690 type(None).
691 - Unions of unions are flattened, e.g.::
692
693 assert Union[Union[int, str], float] == Union[int, str, float]
694
695 - Unions of a single argument vanish, e.g.::
696
697 assert Union[int] == int # The constructor actually returns int
698
699 - Redundant arguments are skipped, e.g.::
700
701 assert Union[int, str, int] == Union[int, str]
702
703 - When comparing unions, the argument order is ignored, e.g.::
704
705 assert Union[int, str] == Union[str, int]
706
707 - You cannot subclass or instantiate a union.
708 - You can use Optional[X] as a shorthand for Union[X, None].
709 """
710 if parameters == ():
711 raise TypeError("Cannot take a Union of no types.")
712 if not isinstance(parameters, tuple):
713 parameters = (parameters,)
714 msg = "Union[arg, ...]: each arg must be a type."
715 parameters = tuple(_type_check(p, msg) for p in parameters)
716 parameters = _remove_dups_flatten(parameters)
717 if len(parameters) == 1:
718 return parameters[0]
719 if len(parameters) == 2 and type(None) in parameters:
720 return _UnionGenericAlias(self, parameters, name="Optional")
721 return _UnionGenericAlias(self, parameters)
722
723@_SpecialForm
724def Optional(self, parameters):
725 """Optional[X] is equivalent to Union[X, None]."""
726 arg = _type_check(parameters, f"{self} requires a single type.")
727 return Union[arg, type(None)]
728
729@_LiteralSpecialForm
730@_tp_cache(typed=True)
731def Literal(self, *parameters):
732 """Special typing form to define literal types (a.k.a. value types).
733
734 This form can be used to indicate to type checkers that the corresponding
735 variable or function parameter has a value equivalent to the provided
736 literal (or one of several literals)::
737
738 def validate_simple(data: Any) -> Literal[True]: # always returns True
739 ...
740
741 MODE = Literal['r', 'rb', 'w', 'wb']
742 def open_helper(file: str, mode: MODE) -> str:
743 ...
744
745 open_helper('/some/path', 'r') # Passes type check
746 open_helper('/other/path', 'typo') # Error in type checker
747
748 Literal[...] cannot be subclassed. At runtime, an arbitrary value
749 is allowed as type argument to Literal[...], but type checkers may
750 impose restrictions.
751 """
752 # There is no '_type_check' call because arguments to Literal[...] are
753 # values, not types.
754 parameters = _flatten_literal_params(parameters)
755
756 try:
757 parameters = tuple(p for p, _ in _deduplicate(list(_value_and_type_iter(parameters))))
758 except TypeError: # unhashable parameters
759 pass
760
761 return _LiteralGenericAlias(self, parameters)
762
763
764@_SpecialForm
765def TypeAlias(self, parameters):
766 """Special form for marking type aliases.
767
768 Use TypeAlias to indicate that an assignment should
769 be recognized as a proper type alias definition by type
770 checkers.
771
772 For example::
773
774 Predicate: TypeAlias = Callable[..., bool]
775
776 It's invalid when used anywhere except as in the example above.
777 """
778 raise TypeError(f"{self} is not subscriptable")
779
780
781@_SpecialForm
782def Concatenate(self, parameters):
783 """Special form for annotating higher-order functions.
784
785 ``Concatenate`` can be used in conjunction with ``ParamSpec`` and
786 ``Callable`` to represent a higher-order function which adds, removes or
787 transforms the parameters of a callable.
788
789 For example::
790
791 Callable[Concatenate[int, P], int]
792
793 See PEP 612 for detailed information.
794 """
795 if parameters == ():
796 raise TypeError("Cannot take a Concatenate of no types.")
797 if not isinstance(parameters, tuple):
798 parameters = (parameters,)
799 if not (parameters[-1] is ... or isinstance(parameters[-1], ParamSpec)):
800 raise TypeError("The last parameter to Concatenate should be a "
801 "ParamSpec variable or ellipsis.")
802 msg = "Concatenate[arg, ...]: each arg must be a type."
803 parameters = (*(_type_check(p, msg) for p in parameters[:-1]), parameters[-1])
804 return _ConcatenateGenericAlias(self, parameters,
805 _paramspec_tvars=True)
806
807
808@_SpecialForm
809def TypeGuard(self, parameters):
810 """Special typing construct for marking user-defined type guard functions.
811
812 ``TypeGuard`` can be used to annotate the return type of a user-defined
813 type guard function. ``TypeGuard`` only accepts a single type argument.
814 At runtime, functions marked this way should return a boolean.
815
816 ``TypeGuard`` aims to benefit *type narrowing* -- a technique used by static
817 type checkers to determine a more precise type of an expression within a
818 program's code flow. Usually type narrowing is done by analyzing
819 conditional code flow and applying the narrowing to a block of code. The
820 conditional expression here is sometimes referred to as a "type guard".
821
822 Sometimes it would be convenient to use a user-defined boolean function
823 as a type guard. Such a function should use ``TypeGuard[...]`` as its
824 return type to alert static type checkers to this intention.
825
826 Using ``-> TypeGuard`` tells the static type checker that for a given
827 function:
828
829 1. The return value is a boolean.
830 2. If the return value is ``True``, the type of its argument
831 is the type inside ``TypeGuard``.
832
833 For example::
834
835 def is_str(val: Union[str, float]):
836 # "isinstance" type guard
837 if isinstance(val, str):
838 # Type of ``val`` is narrowed to ``str``
839 ...
840 else:
841 # Else, type of ``val`` is narrowed to ``float``.
842 ...
843
844 Strict type narrowing is not enforced -- ``TypeB`` need not be a narrower
845 form of ``TypeA`` (it can even be a wider form) and this may lead to
846 type-unsafe results. The main reason is to allow for things like
847 narrowing ``List[object]`` to ``List[str]`` even though the latter is not
848 a subtype of the former, since ``List`` is invariant. The responsibility of
849 writing type-safe type guards is left to the user.
850
851 ``TypeGuard`` also works with type variables. For more information, see
852 PEP 647 (User-Defined Type Guards).
853 """
854 item = _type_check(parameters, f'{self} accepts only single type.')
855 return _GenericAlias(self, (item,))
856
857
858class ForwardRef(_Final, _root=True):
859 """Internal wrapper to hold a forward reference."""
860
861 __slots__ = ('__forward_arg__', '__forward_code__',
862 '__forward_evaluated__', '__forward_value__',
863 '__forward_is_argument__', '__forward_is_class__',
864 '__forward_module__')
865
866 def __init__(self, arg, is_argument=True, module=None, *, is_class=False):
867 if not isinstance(arg, str):
868 raise TypeError(f"Forward reference must be a string -- got {arg!r}")
869
870 # If we do `def f(*args: *Ts)`, then we'll have `arg = '*Ts'`.
871 # Unfortunately, this isn't a valid expression on its own, so we
872 # do the unpacking manually.
873 if arg.startswith('*'):
874 arg_to_compile = f'({arg},)[0]' # E.g. (*Ts,)[0] or (*tuple[int, int],)[0]
875 else:
876 arg_to_compile = arg
877 try:
878 code = compile(arg_to_compile, '<string>', 'eval')
879 except SyntaxError:
880 raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
881
882 self.__forward_arg__ = arg
883 self.__forward_code__ = code
884 self.__forward_evaluated__ = False
885 self.__forward_value__ = None
886 self.__forward_is_argument__ = is_argument
887 self.__forward_is_class__ = is_class
888 self.__forward_module__ = module
889
890 def _evaluate(self, globalns, localns, recursive_guard):
891 if self.__forward_arg__ in recursive_guard:
892 return self
893 if not self.__forward_evaluated__ or localns is not globalns:
894 if globalns is None and localns is None:
895 globalns = localns = {}
896 elif globalns is None:
897 globalns = localns
898 elif localns is None:
899 localns = globalns
900 if self.__forward_module__ is not None:
901 globalns = getattr(
902 sys.modules.get(self.__forward_module__, None), '__dict__', globalns
903 )
904 type_ = _type_check(
905 eval(self.__forward_code__, globalns, localns),
906 "Forward references must evaluate to types.",
907 is_argument=self.__forward_is_argument__,
908 allow_special_forms=self.__forward_is_class__,
909 )
910 self.__forward_value__ = _eval_type(
911 type_, globalns, localns, recursive_guard | {self.__forward_arg__}
912 )
913 self.__forward_evaluated__ = True
914 return self.__forward_value__
915
916 def __eq__(self, other):
917 if not isinstance(other, ForwardRef):
918 return NotImplemented
919 if self.__forward_evaluated__ and other.__forward_evaluated__:
920 return (self.__forward_arg__ == other.__forward_arg__ and
921 self.__forward_value__ == other.__forward_value__)
922 return (self.__forward_arg__ == other.__forward_arg__ and
923 self.__forward_module__ == other.__forward_module__)
924
925 def __hash__(self):
926 return hash((self.__forward_arg__, self.__forward_module__))
927
928 def __or__(self, other):
929 return Union[self, other]
930
931 def __ror__(self, other):
932 return Union[other, self]
933
934 def __repr__(self):
935 if self.__forward_module__ is None:
936 module_repr = ''
937 else:
938 module_repr = f', module={self.__forward_module__!r}'
939 return f'ForwardRef({self.__forward_arg__!r}{module_repr})'
940
941
942def _is_unpacked_typevartuple(x: Any) -> bool:
943 return ((not isinstance(x, type)) and
944 getattr(x, '__typing_is_unpacked_typevartuple__', False))
945
946
947def _is_typevar_like(x: Any) -> bool:
948 return isinstance(x, (TypeVar, ParamSpec)) or _is_unpacked_typevartuple(x)
949
950
951class _PickleUsingNameMixin:
952 """Mixin enabling pickling based on self.__name__."""
953
954 def __reduce__(self):
955 return self.__name__
956
957
958class _BoundVarianceMixin:
959 """Mixin giving __init__ bound and variance arguments.
960
961 This is used by TypeVar and ParamSpec, which both employ the notions of
962 a type 'bound' (restricting type arguments to be a subtype of some
963 specified type) and type 'variance' (determining subtype relations between
964 generic types).
965 """
966 def __init__(self, bound, covariant, contravariant):
967 """Used to setup TypeVars and ParamSpec's bound, covariant and
968 contravariant attributes.
969 """
970 if covariant and contravariant:
971 raise ValueError("Bivariant types are not supported.")
972 self.__covariant__ = bool(covariant)
973 self.__contravariant__ = bool(contravariant)
974 if bound:
975 self.__bound__ = _type_check(bound, "Bound must be a type.")
976 else:
977 self.__bound__ = None
978
979 def __or__(self, right):
980 return Union[self, right]
981
982 def __ror__(self, left):
983 return Union[left, self]
984
985 def __repr__(self):
986 if self.__covariant__:
987 prefix = '+'
988 elif self.__contravariant__:
989 prefix = '-'
990 else:
991 prefix = '~'
992 return prefix + self.__name__
993
994
995class TypeVar(_Final, _Immutable, _BoundVarianceMixin, _PickleUsingNameMixin,
996 _root=True):
997 """Type variable.
998
999 Usage::
1000
1001 T = TypeVar('T') # Can be anything
1002 A = TypeVar('A', str, bytes) # Must be str or bytes
1003
1004 Type variables exist primarily for the benefit of static type
1005 checkers. They serve as the parameters for generic types as well
1006 as for generic function definitions. See class Generic for more
1007 information on generic types. Generic functions work as follows:
1008
1009 def repeat(x: T, n: int) -> List[T]:
1010 '''Return a list containing n references to x.'''
1011 return [x]*n
1012
1013 def longest(x: A, y: A) -> A:
1014 '''Return the longest of two strings.'''
1015 return x if len(x) >= len(y) else y
1016
1017 The latter example's signature is essentially the overloading
1018 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
1019 that if the arguments are instances of some subclass of str,
1020 the return type is still plain str.
1021
1022 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
1023
1024 Type variables defined with covariant=True or contravariant=True
1025 can be used to declare covariant or contravariant generic types.
1026 See PEP 484 for more details. By default generic types are invariant
1027 in all type variables.
1028
1029 Type variables can be introspected. e.g.:
1030
1031 T.__name__ == 'T'
1032 T.__constraints__ == ()
1033 T.__covariant__ == False
1034 T.__contravariant__ = False
1035 A.__constraints__ == (str, bytes)
1036
1037 Note that only type variables defined in global scope can be pickled.
1038 """
1039
1040 def __init__(self, name, *constraints, bound=None,
1041 covariant=False, contravariant=False):
1042 self.__name__ = name
1043 super().__init__(bound, covariant, contravariant)
1044 if constraints and bound is not None:
1045 raise TypeError("Constraints cannot be combined with bound=...")
1046 if constraints and len(constraints) == 1:
1047 raise TypeError("A single constraint is not allowed")
1048 msg = "TypeVar(name, constraint, ...): constraints must be types."
1049 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
1050 def_mod = _caller()
1051 if def_mod != 'typing':
1052 self.__module__ = def_mod
1053
1054 def __typing_subst__(self, arg):
1055 msg = "Parameters to generic types must be types."
1056 arg = _type_check(arg, msg, is_argument=True)
1057 if ((isinstance(arg, _GenericAlias) and arg.__origin__ is Unpack) or
1058 (isinstance(arg, GenericAlias) and getattr(arg, '__unpacked__', False))):
1059 raise TypeError(f"{arg} is not valid as type argument")
1060 return arg
1061
1062
1063class TypeVarTuple(_Final, _Immutable, _PickleUsingNameMixin, _root=True):
1064 """Type variable tuple.
1065
1066 Usage:
1067
1068 Ts = TypeVarTuple('Ts') # Can be given any name
1069
1070 Just as a TypeVar (type variable) is a placeholder for a single type,
1071 a TypeVarTuple is a placeholder for an *arbitrary* number of types. For
1072 example, if we define a generic class using a TypeVarTuple:
1073
1074 class C(Generic[*Ts]): ...
1075
1076 Then we can parameterize that class with an arbitrary number of type
1077 arguments:
1078
1079 C[int] # Fine
1080 C[int, str] # Also fine
1081 C[()] # Even this is fine
1082
1083 For more details, see PEP 646.
1084
1085 Note that only TypeVarTuples defined in global scope can be pickled.
1086 """
1087
1088 def __init__(self, name):
1089 self.__name__ = name
1090
1091 # Used for pickling.
1092 def_mod = _caller()
1093 if def_mod != 'typing':
1094 self.__module__ = def_mod
1095
1096 def __iter__(self):
1097 yield Unpack[self]
1098
1099 def __repr__(self):
1100 return self.__name__
1101
1102 def __typing_subst__(self, arg):
1103 raise TypeError("Substitution of bare TypeVarTuple is not supported")
1104
1105 def __typing_prepare_subst__(self, alias, args):
1106 params = alias.__parameters__
1107 typevartuple_index = params.index(self)
1108 for param in params[typevartuple_index + 1:]:
1109 if isinstance(param, TypeVarTuple):
1110 raise TypeError(f"More than one TypeVarTuple parameter in {alias}")
1111
1112 alen = len(args)
1113 plen = len(params)
1114 left = typevartuple_index
1115 right = plen - typevartuple_index - 1
1116 var_tuple_index = None
1117 fillarg = None
1118 for k, arg in enumerate(args):
1119 if not isinstance(arg, type):
1120 subargs = getattr(arg, '__typing_unpacked_tuple_args__', None)
1121 if subargs and len(subargs) == 2 and subargs[-1] is ...:
1122 if var_tuple_index is not None:
1123 raise TypeError("More than one unpacked arbitrary-length tuple argument")
1124 var_tuple_index = k
1125 fillarg = subargs[0]
1126 if var_tuple_index is not None:
1127 left = min(left, var_tuple_index)
1128 right = min(right, alen - var_tuple_index - 1)
1129 elif left + right > alen:
1130 raise TypeError(f"Too few arguments for {alias};"
1131 f" actual {alen}, expected at least {plen-1}")
1132
1133 return (
1134 *args[:left],
1135 *([fillarg]*(typevartuple_index - left)),
1136 tuple(args[left: alen - right]),
1137 *([fillarg]*(plen - right - left - typevartuple_index - 1)),
1138 *args[alen - right:],
1139 )
1140
1141
1142class ParamSpecArgs(_Final, _Immutable, _root=True):
1143 """The args for a ParamSpec object.
1144
1145 Given a ParamSpec object P, P.args is an instance of ParamSpecArgs.
1146
1147 ParamSpecArgs objects have a reference back to their ParamSpec:
1148
1149 P.args.__origin__ is P
1150
1151 This type is meant for runtime introspection and has no special meaning to
1152 static type checkers.
1153 """
1154 def __init__(self, origin):
1155 self.__origin__ = origin
1156
1157 def __repr__(self):
1158 return f"{self.__origin__.__name__}.args"
1159
1160 def __eq__(self, other):
1161 if not isinstance(other, ParamSpecArgs):
1162 return NotImplemented
1163 return self.__origin__ == other.__origin__
1164
1165
1166class ParamSpecKwargs(_Final, _Immutable, _root=True):
1167 """The kwargs for a ParamSpec object.
1168
1169 Given a ParamSpec object P, P.kwargs is an instance of ParamSpecKwargs.
1170
1171 ParamSpecKwargs objects have a reference back to their ParamSpec:
1172
1173 P.kwargs.__origin__ is P
1174
1175 This type is meant for runtime introspection and has no special meaning to
1176 static type checkers.
1177 """
1178 def __init__(self, origin):
1179 self.__origin__ = origin
1180
1181 def __repr__(self):
1182 return f"{self.__origin__.__name__}.kwargs"
1183
1184 def __eq__(self, other):
1185 if not isinstance(other, ParamSpecKwargs):
1186 return NotImplemented
1187 return self.__origin__ == other.__origin__
1188
1189
1190class ParamSpec(_Final, _Immutable, _BoundVarianceMixin, _PickleUsingNameMixin,
1191 _root=True):
1192 """Parameter specification variable.
1193
1194 Usage::
1195
1196 P = ParamSpec('P')
1197
1198 Parameter specification variables exist primarily for the benefit of static
1199 type checkers. They are used to forward the parameter types of one
1200 callable to another callable, a pattern commonly found in higher order
1201 functions and decorators. They are only valid when used in ``Concatenate``,
1202 or as the first argument to ``Callable``, or as parameters for user-defined
1203 Generics. See class Generic for more information on generic types. An
1204 example for annotating a decorator::
1205
1206 T = TypeVar('T')
1207 P = ParamSpec('P')
1208
1209 def add_logging(f: Callable[P, T]) -> Callable[P, T]:
1210 '''A type-safe decorator to add logging to a function.'''
1211 def inner(*args: P.args, **kwargs: P.kwargs) -> T:
1212 logging.info(f'{f.__name__} was called')
1213 return f(*args, **kwargs)
1214 return inner
1215
1216 @add_logging
1217 def add_two(x: float, y: float) -> float:
1218 '''Add two numbers together.'''
1219 return x + y
1220
1221 Parameter specification variables can be introspected. e.g.:
1222
1223 P.__name__ == 'P'
1224
1225 Note that only parameter specification variables defined in global scope can
1226 be pickled.
1227 """
1228
1229 @property
1230 def args(self):
1231 return ParamSpecArgs(self)
1232
1233 @property
1234 def kwargs(self):
1235 return ParamSpecKwargs(self)
1236
1237 def __init__(self, name, *, bound=None, covariant=False, contravariant=False):
1238 self.__name__ = name
1239 super().__init__(bound, covariant, contravariant)
1240 def_mod = _caller()
1241 if def_mod != 'typing':
1242 self.__module__ = def_mod
1243
1244 def __typing_subst__(self, arg):
1245 if isinstance(arg, (list, tuple)):
1246 arg = tuple(_type_check(a, "Expected a type.") for a in arg)
1247 elif not _is_param_expr(arg):
1248 raise TypeError(f"Expected a list of types, an ellipsis, "
1249 f"ParamSpec, or Concatenate. Got {arg}")
1250 return arg
1251
1252 def __typing_prepare_subst__(self, alias, args):
1253 params = alias.__parameters__
1254 i = params.index(self)
1255 if i >= len(args):
1256 raise TypeError(f"Too few arguments for {alias}")
1257 # Special case where Z[[int, str, bool]] == Z[int, str, bool] in PEP 612.
1258 if len(params) == 1 and not _is_param_expr(args[0]):
1259 assert i == 0
1260 args = (args,)
1261 # Convert lists to tuples to help other libraries cache the results.
1262 elif isinstance(args[i], list):
1263 args = (*args[:i], tuple(args[i]), *args[i+1:])
1264 return args
1265
1266def _is_dunder(attr):
1267 return attr.startswith('__') and attr.endswith('__')
1268
1269class _BaseGenericAlias(_Final, _root=True):
1270 """The central part of the internal API.
1271
1272 This represents a generic version of type 'origin' with type arguments 'params'.
1273 There are two kind of these aliases: user defined and special. The special ones
1274 are wrappers around builtin collections and ABCs in collections.abc. These must
1275 have 'name' always set. If 'inst' is False, then the alias can't be instantiated;
1276 this is used by e.g. typing.List and typing.Dict.
1277 """
1278
1279 def __init__(self, origin, *, inst=True, name=None):
1280 self._inst = inst
1281 self._name = name
1282 self.__origin__ = origin
1283 self.__slots__ = None # This is not documented.
1284
1285 def __call__(self, *args, **kwargs):
1286 if not self._inst:
1287 raise TypeError(f"Type {self._name} cannot be instantiated; "
1288 f"use {self.__origin__.__name__}() instead")
1289 result = self.__origin__(*args, **kwargs)
1290 try:
1291 result.__orig_class__ = self
1292 # Some objects raise TypeError (or something even more exotic)
1293 # if you try to set attributes on them; we guard against that here
1294 except Exception:
1295 pass
1296 return result
1297
1298 def __mro_entries__(self, bases):
1299 res = []
1300 if self.__origin__ not in bases:
1301 res.append(self.__origin__)
1302 i = bases.index(self)
1303 for b in bases[i+1:]:
1304 if isinstance(b, _BaseGenericAlias) or issubclass(b, Generic):
1305 break
1306 else:
1307 res.append(Generic)
1308 return tuple(res)
1309
1310 def __getattr__(self, attr):
1311 if attr in {'__name__', '__qualname__'}:
1312 return self._name or self.__origin__.__name__
1313
1314 # We are careful for copy and pickle.
1315 # Also for simplicity we don't relay any dunder names
1316 if '__origin__' in self.__dict__ and not _is_dunder(attr):
1317 return getattr(self.__origin__, attr)
1318 raise AttributeError(attr)
1319
1320 def __setattr__(self, attr, val):
1321 if _is_dunder(attr) or attr in {'_name', '_inst', '_nparams',
1322 '_paramspec_tvars'}:
1323 super().__setattr__(attr, val)
1324 else:
1325 setattr(self.__origin__, attr, val)
1326
1327 def __instancecheck__(self, obj):
1328 return self.__subclasscheck__(type(obj))
1329
1330 def __subclasscheck__(self, cls):
1331 raise TypeError("Subscripted generics cannot be used with"
1332 " class and instance checks")
1333
1334 def __dir__(self):
1335 return list(set(super().__dir__()
1336 + [attr for attr in dir(self.__origin__) if not _is_dunder(attr)]))
1337
1338
1339# Special typing constructs Union, Optional, Generic, Callable and Tuple
1340# use three special attributes for internal bookkeeping of generic types:
1341# * __parameters__ is a tuple of unique free type parameters of a generic
1342# type, for example, Dict[T, T].__parameters__ == (T,);
1343# * __origin__ keeps a reference to a type that was subscripted,
1344# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
1345# the type.
1346# * __args__ is a tuple of all arguments used in subscripting,
1347# e.g., Dict[T, int].__args__ == (T, int).
1348
1349
1350class _GenericAlias(_BaseGenericAlias, _root=True):
1351 # The type of parameterized generics.
1352 #
1353 # That is, for example, `type(List[int])` is `_GenericAlias`.
1354 #
1355 # Objects which are instances of this class include:
1356 # * Parameterized container types, e.g. `Tuple[int]`, `List[int]`.
1357 # * Note that native container types, e.g. `tuple`, `list`, use
1358 # `types.GenericAlias` instead.
1359 # * Parameterized classes:
1360 # T = TypeVar('T')
1361 # class C(Generic[T]): pass
1362 # # C[int] is a _GenericAlias
1363 # * `Callable` aliases, generic `Callable` aliases, and
1364 # parameterized `Callable` aliases:
1365 # T = TypeVar('T')
1366 # # _CallableGenericAlias inherits from _GenericAlias.
1367 # A = Callable[[], None] # _CallableGenericAlias
1368 # B = Callable[[T], None] # _CallableGenericAlias
1369 # C = B[int] # _CallableGenericAlias
1370 # * Parameterized `Final`, `ClassVar` and `TypeGuard`:
1371 # # All _GenericAlias
1372 # Final[int]
1373 # ClassVar[float]
1374 # TypeVar[bool]
1375
1376 def __init__(self, origin, args, *, inst=True, name=None,
1377 _paramspec_tvars=False):
1378 super().__init__(origin, inst=inst, name=name)
1379 if not isinstance(args, tuple):
1380 args = (args,)
1381 self.__args__ = tuple(... if a is _TypingEllipsis else
1382 a for a in args)
1383 self.__parameters__ = _collect_parameters(args)
1384 self._paramspec_tvars = _paramspec_tvars
1385 if not name:
1386 self.__module__ = origin.__module__
1387
1388 def __eq__(self, other):
1389 if not isinstance(other, _GenericAlias):
1390 return NotImplemented
1391 return (self.__origin__ == other.__origin__
1392 and self.__args__ == other.__args__)
1393
1394 def __hash__(self):
1395 return hash((self.__origin__, self.__args__))
1396
1397 def __or__(self, right):
1398 return Union[self, right]
1399
1400 def __ror__(self, left):
1401 return Union[left, self]
1402
1403 @_tp_cache
1404 def __getitem__(self, args):
1405 # Parameterizes an already-parameterized object.
1406 #
1407 # For example, we arrive here doing something like:
1408 # T1 = TypeVar('T1')
1409 # T2 = TypeVar('T2')
1410 # T3 = TypeVar('T3')
1411 # class A(Generic[T1]): pass
1412 # B = A[T2] # B is a _GenericAlias
1413 # C = B[T3] # Invokes _GenericAlias.__getitem__
1414 #
1415 # We also arrive here when parameterizing a generic `Callable` alias:
1416 # T = TypeVar('T')
1417 # C = Callable[[T], None]
1418 # C[int] # Invokes _GenericAlias.__getitem__
1419
1420 if self.__origin__ in (Generic, Protocol):
1421 # Can't subscript Generic[...] or Protocol[...].
1422 raise TypeError(f"Cannot subscript already-subscripted {self}")
1423 if not self.__parameters__:
1424 raise TypeError(f"{self} is not a generic class")
1425
1426 # Preprocess `args`.
1427 if not isinstance(args, tuple):
1428 args = (args,)
1429 args = tuple(_type_convert(p) for p in args)
1430 args = _unpack_args(args)
1431 new_args = self._determine_new_args(args)
1432 r = self.copy_with(new_args)
1433 return r
1434
1435 def _determine_new_args(self, args):
1436 # Determines new __args__ for __getitem__.
1437 #
1438 # For example, suppose we had:
1439 # T1 = TypeVar('T1')
1440 # T2 = TypeVar('T2')
1441 # class A(Generic[T1, T2]): pass
1442 # T3 = TypeVar('T3')
1443 # B = A[int, T3]
1444 # C = B[str]
1445 # `B.__args__` is `(int, T3)`, so `C.__args__` should be `(int, str)`.
1446 # Unfortunately, this is harder than it looks, because if `T3` is
1447 # anything more exotic than a plain `TypeVar`, we need to consider
1448 # edge cases.
1449
1450 params = self.__parameters__
1451 # In the example above, this would be {T3: str}
1452 for param in params:
1453 prepare = getattr(param, '__typing_prepare_subst__', None)
1454 if prepare is not None:
1455 args = prepare(self, args)
1456 alen = len(args)
1457 plen = len(params)
1458 if alen != plen:
1459 raise TypeError(f"Too {'many' if alen > plen else 'few'} arguments for {self};"
1460 f" actual {alen}, expected {plen}")
1461 new_arg_by_param = dict(zip(params, args))
1462 return tuple(self._make_substitution(self.__args__, new_arg_by_param))
1463
1464 def _make_substitution(self, args, new_arg_by_param):
1465 """Create a list of new type arguments."""
1466 new_args = []
1467 for old_arg in args:
1468 if isinstance(old_arg, type):
1469 new_args.append(old_arg)
1470 continue
1471
1472 substfunc = getattr(old_arg, '__typing_subst__', None)
1473 if substfunc:
1474 new_arg = substfunc(new_arg_by_param[old_arg])
1475 else:
1476 subparams = getattr(old_arg, '__parameters__', ())
1477 if not subparams:
1478 new_arg = old_arg
1479 else:
1480 subargs = []
1481 for x in subparams:
1482 if isinstance(x, TypeVarTuple):
1483 subargs.extend(new_arg_by_param[x])
1484 else:
1485 subargs.append(new_arg_by_param[x])
1486 new_arg = old_arg[tuple(subargs)]
1487
1488 if self.__origin__ == collections.abc.Callable and isinstance(new_arg, tuple):
1489 # Consider the following `Callable`.
1490 # C = Callable[[int], str]
1491 # Here, `C.__args__` should be (int, str) - NOT ([int], str).
1492 # That means that if we had something like...
1493 # P = ParamSpec('P')
1494 # T = TypeVar('T')
1495 # C = Callable[P, T]
1496 # D = C[[int, str], float]
1497 # ...we need to be careful; `new_args` should end up as
1498 # `(int, str, float)` rather than `([int, str], float)`.
1499 new_args.extend(new_arg)
1500 elif _is_unpacked_typevartuple(old_arg):
1501 # Consider the following `_GenericAlias`, `B`:
1502 # class A(Generic[*Ts]): ...
1503 # B = A[T, *Ts]
1504 # If we then do:
1505 # B[float, int, str]
1506 # The `new_arg` corresponding to `T` will be `float`, and the
1507 # `new_arg` corresponding to `*Ts` will be `(int, str)`. We
1508 # should join all these types together in a flat list
1509 # `(float, int, str)` - so again, we should `extend`.
1510 new_args.extend(new_arg)
1511 elif isinstance(old_arg, tuple):
1512 # Corner case:
1513 # P = ParamSpec('P')
1514 # T = TypeVar('T')
1515 # class Base(Generic[P]): ...
1516 # Can be substituted like this:
1517 # X = Base[[int, T]]
1518 # In this case, `old_arg` will be a tuple:
1519 new_args.append(
1520 tuple(self._make_substitution(old_arg, new_arg_by_param)),
1521 )
1522 else:
1523 new_args.append(new_arg)
1524 return new_args
1525
1526 def copy_with(self, args):
1527 return self.__class__(self.__origin__, args, name=self._name, inst=self._inst,
1528 _paramspec_tvars=self._paramspec_tvars)
1529
1530 def __repr__(self):
1531 if self._name:
1532 name = 'typing.' + self._name
1533 else:
1534 name = _type_repr(self.__origin__)
1535 if self.__args__:
1536 args = ", ".join([_type_repr(a) for a in self.__args__])
1537 else:
1538 # To ensure the repr is eval-able.
1539 args = "()"
1540 return f'{name}[{args}]'
1541
1542 def __reduce__(self):
1543 if self._name:
1544 origin = globals()[self._name]
1545 else:
1546 origin = self.__origin__
1547 args = tuple(self.__args__)
1548 if len(args) == 1 and not isinstance(args[0], tuple):
1549 args, = args
1550 return operator.getitem, (origin, args)
1551
1552 def __mro_entries__(self, bases):
1553 if isinstance(self.__origin__, _SpecialForm):
1554 raise TypeError(f"Cannot subclass {self!r}")
1555
1556 if self._name: # generic version of an ABC or built-in class
1557 return super().__mro_entries__(bases)
1558 if self.__origin__ is Generic:
1559 if Protocol in bases:
1560 return ()
1561 i = bases.index(self)
1562 for b in bases[i+1:]:
1563 if isinstance(b, _BaseGenericAlias) and b is not self:
1564 return ()
1565 return (self.__origin__,)
1566
1567 def __iter__(self):
1568 yield Unpack[self]
1569
1570
1571# _nparams is the number of accepted parameters, e.g. 0 for Hashable,
1572# 1 for List and 2 for Dict. It may be -1 if variable number of
1573# parameters are accepted (needs custom __getitem__).
1574
1575class _SpecialGenericAlias(_NotIterable, _BaseGenericAlias, _root=True):
1576 def __init__(self, origin, nparams, *, inst=True, name=None):
1577 if name is None:
1578 name = origin.__name__
1579 super().__init__(origin, inst=inst, name=name)
1580 self._nparams = nparams
1581 if origin.__module__ == 'builtins':
1582 self.__doc__ = f'A generic version of {origin.__qualname__}.'
1583 else:
1584 self.__doc__ = f'A generic version of {origin.__module__}.{origin.__qualname__}.'
1585
1586 @_tp_cache
1587 def __getitem__(self, params):
1588 if not isinstance(params, tuple):
1589 params = (params,)
1590 msg = "Parameters to generic types must be types."
1591 params = tuple(_type_check(p, msg) for p in params)
1592 _check_generic(self, params, self._nparams)
1593 return self.copy_with(params)
1594
1595 def copy_with(self, params):
1596 return _GenericAlias(self.__origin__, params,
1597 name=self._name, inst=self._inst)
1598
1599 def __repr__(self):
1600 return 'typing.' + self._name
1601
1602 def __subclasscheck__(self, cls):
1603 if isinstance(cls, _SpecialGenericAlias):
1604 return issubclass(cls.__origin__, self.__origin__)
1605 if not isinstance(cls, _GenericAlias):
1606 return issubclass(cls, self.__origin__)
1607 return super().__subclasscheck__(cls)
1608
1609 def __reduce__(self):
1610 return self._name
1611
1612 def __or__(self, right):
1613 return Union[self, right]
1614
1615 def __ror__(self, left):
1616 return Union[left, self]
1617
1618class _CallableGenericAlias(_NotIterable, _GenericAlias, _root=True):
1619 def __repr__(self):
1620 assert self._name == 'Callable'
1621 args = self.__args__
1622 if len(args) == 2 and _is_param_expr(args[0]):
1623 return super().__repr__()
1624 return (f'typing.Callable'
1625 f'[[{", ".join([_type_repr(a) for a in args[:-1]])}], '
1626 f'{_type_repr(args[-1])}]')
1627
1628 def __reduce__(self):
1629 args = self.__args__
1630 if not (len(args) == 2 and _is_param_expr(args[0])):
1631 args = list(args[:-1]), args[-1]
1632 return operator.getitem, (Callable, args)
1633
1634
1635class _CallableType(_SpecialGenericAlias, _root=True):
1636 def copy_with(self, params):
1637 return _CallableGenericAlias(self.__origin__, params,
1638 name=self._name, inst=self._inst,
1639 _paramspec_tvars=True)
1640
1641 def __getitem__(self, params):
1642 if not isinstance(params, tuple) or len(params) != 2:
1643 raise TypeError("Callable must be used as "
1644 "Callable[[arg, ...], result].")
1645 args, result = params
1646 # This relaxes what args can be on purpose to allow things like
1647 # PEP 612 ParamSpec. Responsibility for whether a user is using
1648 # Callable[...] properly is deferred to static type checkers.
1649 if isinstance(args, list):
1650 params = (tuple(args), result)
1651 else:
1652 params = (args, result)
1653 return self.__getitem_inner__(params)
1654
1655 @_tp_cache
1656 def __getitem_inner__(self, params):
1657 args, result = params
1658 msg = "Callable[args, result]: result must be a type."
1659 result = _type_check(result, msg)
1660 if args is Ellipsis:
1661 return self.copy_with((_TypingEllipsis, result))
1662 if not isinstance(args, tuple):
1663 args = (args,)
1664 args = tuple(_type_convert(arg) for arg in args)
1665 params = args + (result,)
1666 return self.copy_with(params)
1667
1668
1669class _TupleType(_SpecialGenericAlias, _root=True):
1670 @_tp_cache
1671 def __getitem__(self, params):
1672 if not isinstance(params, tuple):
1673 params = (params,)
1674 if len(params) >= 2 and params[-1] is ...:
1675 msg = "Tuple[t, ...]: t must be a type."
1676 params = tuple(_type_check(p, msg) for p in params[:-1])
1677 return self.copy_with((*params, _TypingEllipsis))
1678 msg = "Tuple[t0, t1, ...]: each t must be a type."
1679 params = tuple(_type_check(p, msg) for p in params)
1680 return self.copy_with(params)
1681
1682
1683class _UnionGenericAlias(_NotIterable, _GenericAlias, _root=True):
1684 def copy_with(self, params):
1685 return Union[params]
1686
1687 def __eq__(self, other):
1688 if not isinstance(other, (_UnionGenericAlias, types.UnionType)):
1689 return NotImplemented
1690 try: # fast path
1691 return set(self.__args__) == set(other.__args__)
1692 except TypeError: # not hashable, slow path
1693 return _compare_args_orderless(self.__args__, other.__args__)
1694
1695 def __hash__(self):
1696 return hash(frozenset(self.__args__))
1697
1698 def __repr__(self):
1699 args = self.__args__
1700 if len(args) == 2:
1701 if args[0] is type(None):
1702 return f'typing.Optional[{_type_repr(args[1])}]'
1703 elif args[1] is type(None):
1704 return f'typing.Optional[{_type_repr(args[0])}]'
1705 return super().__repr__()
1706
1707 def __instancecheck__(self, obj):
1708 return self.__subclasscheck__(type(obj))
1709
1710 def __subclasscheck__(self, cls):
1711 for arg in self.__args__:
1712 if issubclass(cls, arg):
1713 return True
1714
1715 def __reduce__(self):
1716 func, (origin, args) = super().__reduce__()
1717 return func, (Union, args)
1718
1719
1720def _value_and_type_iter(parameters):
1721 return ((p, type(p)) for p in parameters)
1722
1723
1724class _LiteralGenericAlias(_GenericAlias, _root=True):
1725 def __eq__(self, other):
1726 if not isinstance(other, _LiteralGenericAlias):
1727 return NotImplemented
1728
1729 return set(_value_and_type_iter(self.__args__)) == set(_value_and_type_iter(other.__args__))
1730
1731 def __hash__(self):
1732 return hash(frozenset(_value_and_type_iter(self.__args__)))
1733
1734
1735class _ConcatenateGenericAlias(_GenericAlias, _root=True):
1736 def copy_with(self, params):
1737 if isinstance(params[-1], (list, tuple)):
1738 return (*params[:-1], *params[-1])
1739 if isinstance(params[-1], _ConcatenateGenericAlias):
1740 params = (*params[:-1], *params[-1].__args__)
1741 return super().copy_with(params)
1742
1743
1744@_SpecialForm
1745def Unpack(self, parameters):
1746 """Type unpack operator.
1747
1748 The type unpack operator takes the child types from some container type,
1749 such as `tuple[int, str]` or a `TypeVarTuple`, and 'pulls them out'.
1750
1751 For example::
1752
1753 # For some generic class `Foo`:
1754 Foo[Unpack[tuple[int, str]]] # Equivalent to Foo[int, str]
1755
1756 Ts = TypeVarTuple('Ts')
1757 # Specifies that `Bar` is generic in an arbitrary number of types.
1758 # (Think of `Ts` as a tuple of an arbitrary number of individual
1759 # `TypeVar`s, which the `Unpack` is 'pulling out' directly into the
1760 # `Generic[]`.)
1761 class Bar(Generic[Unpack[Ts]]): ...
1762 Bar[int] # Valid
1763 Bar[int, str] # Also valid
1764
1765 From Python 3.11, this can also be done using the `*` operator::
1766
1767 Foo[*tuple[int, str]]
1768 class Bar(Generic[*Ts]): ...
1769
1770 Note that there is only some runtime checking of this operator. Not
1771 everything the runtime allows may be accepted by static type checkers.
1772
1773 For more information, see PEP 646.
1774 """
1775 item = _type_check(parameters, f'{self} accepts only single type.')
1776 return _UnpackGenericAlias(origin=self, args=(item,))
1777
1778
1779class _UnpackGenericAlias(_GenericAlias, _root=True):
1780 def __repr__(self):
1781 # `Unpack` only takes one argument, so __args__ should contain only
1782 # a single item.
1783 return '*' + repr(self.__args__[0])
1784
1785 def __getitem__(self, args):
1786 if self.__typing_is_unpacked_typevartuple__:
1787 return args
1788 return super().__getitem__(args)
1789
1790 @property
1791 def __typing_unpacked_tuple_args__(self):
1792 assert self.__origin__ is Unpack
1793 assert len(self.__args__) == 1
1794 arg, = self.__args__
1795 if isinstance(arg, _GenericAlias):
1796 assert arg.__origin__ is tuple
1797 return arg.__args__
1798 return None
1799
1800 @property
1801 def __typing_is_unpacked_typevartuple__(self):
1802 assert self.__origin__ is Unpack
1803 assert len(self.__args__) == 1
1804 return isinstance(self.__args__[0], TypeVarTuple)
1805
1806
1807class Generic:
1808 """Abstract base class for generic types.
1809
1810 A generic type is typically declared by inheriting from
1811 this class parameterized with one or more type variables.
1812 For example, a generic mapping type might be defined as::
1813
1814 class Mapping(Generic[KT, VT]):
1815 def __getitem__(self, key: KT) -> VT:
1816 ...
1817 # Etc.
1818
1819 This class can then be used as follows::
1820
1821 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
1822 try:
1823 return mapping[key]
1824 except KeyError:
1825 return default
1826 """
1827 __slots__ = ()
1828 _is_protocol = False
1829
1830 @_tp_cache
1831 def __class_getitem__(cls, params):
1832 """Parameterizes a generic class.
1833
1834 At least, parameterizing a generic class is the *main* thing this method
1835 does. For example, for some generic class `Foo`, this is called when we
1836 do `Foo[int]` - there, with `cls=Foo` and `params=int`.
1837
1838 However, note that this method is also called when defining generic
1839 classes in the first place with `class Foo(Generic[T]): ...`.
1840 """
1841 if not isinstance(params, tuple):
1842 params = (params,)
1843
1844 params = tuple(_type_convert(p) for p in params)
1845 if cls in (Generic, Protocol):
1846 # Generic and Protocol can only be subscripted with unique type variables.
1847 if not params:
1848 raise TypeError(
1849 f"Parameter list to {cls.__qualname__}[...] cannot be empty"
1850 )
1851 if not all(_is_typevar_like(p) for p in params):
1852 raise TypeError(
1853 f"Parameters to {cls.__name__}[...] must all be type variables "
1854 f"or parameter specification variables.")
1855 if len(set(params)) != len(params):
1856 raise TypeError(
1857 f"Parameters to {cls.__name__}[...] must all be unique")
1858 else:
1859 # Subscripting a regular Generic subclass.
1860 for param in cls.__parameters__:
1861 prepare = getattr(param, '__typing_prepare_subst__', None)
1862 if prepare is not None:
1863 params = prepare(cls, params)
1864 _check_generic(cls, params, len(cls.__parameters__))
1865
1866 new_args = []
1867 for param, new_arg in zip(cls.__parameters__, params):
1868 if isinstance(param, TypeVarTuple):
1869 new_args.extend(new_arg)
1870 else:
1871 new_args.append(new_arg)
1872 params = tuple(new_args)
1873
1874 return _GenericAlias(cls, params,
1875 _paramspec_tvars=True)
1876
1877 def __init_subclass__(cls, *args, **kwargs):
1878 super().__init_subclass__(*args, **kwargs)
1879 tvars = []
1880 if '__orig_bases__' in cls.__dict__:
1881 error = Generic in cls.__orig_bases__
1882 else:
1883 error = (Generic in cls.__bases__ and
1884 cls.__name__ != 'Protocol' and
1885 type(cls) != _TypedDictMeta)
1886 if error:
1887 raise TypeError("Cannot inherit from plain Generic")
1888 if '__orig_bases__' in cls.__dict__:
1889 tvars = _collect_parameters(cls.__orig_bases__)
1890 # Look for Generic[T1, ..., Tn].
1891 # If found, tvars must be a subset of it.
1892 # If not found, tvars is it.
1893 # Also check for and reject plain Generic,
1894 # and reject multiple Generic[...].
1895 gvars = None
1896 for base in cls.__orig_bases__:
1897 if (isinstance(base, _GenericAlias) and
1898 base.__origin__ is Generic):
1899 if gvars is not None:
1900 raise TypeError(
1901 "Cannot inherit from Generic[...] multiple times.")
1902 gvars = base.__parameters__
1903 if gvars is not None:
1904 tvarset = set(tvars)
1905 gvarset = set(gvars)
1906 if not tvarset <= gvarset:
1907 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
1908 s_args = ', '.join(str(g) for g in gvars)
1909 raise TypeError(f"Some type variables ({s_vars}) are"
1910 f" not listed in Generic[{s_args}]")
1911 tvars = gvars
1912 cls.__parameters__ = tuple(tvars)
1913
1914
1915class _TypingEllipsis:
1916 """Internal placeholder for ... (ellipsis)."""
1917
1918
1919_TYPING_INTERNALS = ['__parameters__', '__orig_bases__', '__orig_class__',
1920 '_is_protocol', '_is_runtime_protocol', '__final__']
1921
1922_SPECIAL_NAMES = ['__abstractmethods__', '__annotations__', '__dict__', '__doc__',
1923 '__init__', '__module__', '__new__', '__slots__',
1924 '__subclasshook__', '__weakref__', '__class_getitem__']
1925
1926# These special attributes will be not collected as protocol members.
1927EXCLUDED_ATTRIBUTES = _TYPING_INTERNALS + _SPECIAL_NAMES + ['_MutableMapping__marker']
1928
1929
1930def _get_protocol_attrs(cls):
1931 """Collect protocol members from a protocol class objects.
1932
1933 This includes names actually defined in the class dictionary, as well
1934 as names that appear in annotations. Special names (above) are skipped.
1935 """
1936 attrs = set()
1937 for base in cls.__mro__[:-1]: # without object
1938 if base.__name__ in ('Protocol', 'Generic'):
1939 continue
1940 annotations = getattr(base, '__annotations__', {})
1941 for attr in list(base.__dict__.keys()) + list(annotations.keys()):
1942 if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
1943 attrs.add(attr)
1944 return attrs
1945
1946
1947def _is_callable_members_only(cls):
1948 # PEP 544 prohibits using issubclass() with protocols that have non-method members.
1949 return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls))
1950
1951
1952def _no_init_or_replace_init(self, *args, **kwargs):
1953 cls = type(self)
1954
1955 if cls._is_protocol:
1956 raise TypeError('Protocols cannot be instantiated')
1957
1958 # Already using a custom `__init__`. No need to calculate correct
1959 # `__init__` to call. This can lead to RecursionError. See bpo-45121.
1960 if cls.__init__ is not _no_init_or_replace_init:
1961 return
1962
1963 # Initially, `__init__` of a protocol subclass is set to `_no_init_or_replace_init`.
1964 # The first instantiation of the subclass will call `_no_init_or_replace_init` which
1965 # searches for a proper new `__init__` in the MRO. The new `__init__`
1966 # replaces the subclass' old `__init__` (ie `_no_init_or_replace_init`). Subsequent
1967 # instantiation of the protocol subclass will thus use the new
1968 # `__init__` and no longer call `_no_init_or_replace_init`.
1969 for base in cls.__mro__:
1970 init = base.__dict__.get('__init__', _no_init_or_replace_init)
1971 if init is not _no_init_or_replace_init:
1972 cls.__init__ = init
1973 break
1974 else:
1975 # should not happen
1976 cls.__init__ = object.__init__
1977
1978 cls.__init__(self, *args, **kwargs)
1979
1980
1981def _caller(depth=1, default='__main__'):
1982 try:
1983 return sys._getframe(depth + 1).f_globals.get('__name__', default)
1984 except (AttributeError, ValueError): # For platforms without _getframe()
1985 return None
1986
1987
1988def _allow_reckless_class_checks(depth=3):
1989 """Allow instance and class checks for special stdlib modules.
1990
1991 The abc and functools modules indiscriminately call isinstance() and
1992 issubclass() on the whole MRO of a user class, which may contain protocols.
1993 """
1994 return _caller(depth) in {'abc', 'functools', None}
1995
1996
1997_PROTO_ALLOWLIST = {
1998 'collections.abc': [
1999 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
2000 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
2001 ],
2002 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
2003}
2004
2005
2006class _ProtocolMeta(ABCMeta):
2007 # This metaclass is really unfortunate and exists only because of
2008 # the lack of __instancehook__.
2009 def __instancecheck__(cls, instance):
2010 # We need this method for situations where attributes are
2011 # assigned in __init__.
2012 if (
2013 getattr(cls, '_is_protocol', False) and
2014 not getattr(cls, '_is_runtime_protocol', False) and
2015 not _allow_reckless_class_checks(depth=2)
2016 ):
2017 raise TypeError("Instance and class checks can only be used with"
2018 " @runtime_checkable protocols")
2019
2020 if ((not getattr(cls, '_is_protocol', False) or
2021 _is_callable_members_only(cls)) and
2022 issubclass(instance.__class__, cls)):
2023 return True
2024 if cls._is_protocol:
2025 if all(hasattr(instance, attr) and
2026 # All *methods* can be blocked by setting them to None.
2027 (not callable(getattr(cls, attr, None)) or
2028 getattr(instance, attr) is not None)
2029 for attr in _get_protocol_attrs(cls)):
2030 return True
2031 return super().__instancecheck__(instance)
2032
2033
2034class Protocol(Generic, metaclass=_ProtocolMeta):
2035 """Base class for protocol classes.
2036
2037 Protocol classes are defined as::
2038
2039 class Proto(Protocol):
2040 def meth(self) -> int:
2041 ...
2042
2043 Such classes are primarily used with static type checkers that recognize
2044 structural subtyping (static duck-typing).
2045
2046 For example::
2047
2048 class C:
2049 def meth(self) -> int:
2050 return 0
2051
2052 def func(x: Proto) -> int:
2053 return x.meth()
2054
2055 func(C()) # Passes static type check
2056
2057 See PEP 544 for details. Protocol classes decorated with
2058 @typing.runtime_checkable act as simple-minded runtime protocols that check
2059 only the presence of given attributes, ignoring their type signatures.
2060 Protocol classes can be generic, they are defined as::
2061
2062 class GenProto(Protocol[T]):
2063 def meth(self) -> T:
2064 ...
2065 """
2066
2067 __slots__ = ()
2068 _is_protocol = True
2069 _is_runtime_protocol = False
2070
2071 def __init_subclass__(cls, *args, **kwargs):
2072 super().__init_subclass__(*args, **kwargs)
2073
2074 # Determine if this is a protocol or a concrete subclass.
2075 if not cls.__dict__.get('_is_protocol', False):
2076 cls._is_protocol = any(b is Protocol for b in cls.__bases__)
2077
2078 # Set (or override) the protocol subclass hook.
2079 def _proto_hook(other):
2080 if not cls.__dict__.get('_is_protocol', False):
2081 return NotImplemented
2082
2083 # First, perform various sanity checks.
2084 if not getattr(cls, '_is_runtime_protocol', False):
2085 if _allow_reckless_class_checks():
2086 return NotImplemented
2087 raise TypeError("Instance and class checks can only be used with"
2088 " @runtime_checkable protocols")
2089 if not _is_callable_members_only(cls):
2090 if _allow_reckless_class_checks():
2091 return NotImplemented
2092 raise TypeError("Protocols with non-method members"
2093 " don't support issubclass()")
2094 if not isinstance(other, type):
2095 # Same error message as for issubclass(1, int).
2096 raise TypeError('issubclass() arg 1 must be a class')
2097
2098 # Second, perform the actual structural compatibility check.
2099 for attr in _get_protocol_attrs(cls):
2100 for base in other.__mro__:
2101 # Check if the members appears in the class dictionary...
2102 if attr in base.__dict__:
2103 if base.__dict__[attr] is None:
2104 return NotImplemented
2105 break
2106
2107 # ...or in annotations, if it is a sub-protocol.
2108 annotations = getattr(base, '__annotations__', {})
2109 if (isinstance(annotations, collections.abc.Mapping) and
2110 attr in annotations and
2111 issubclass(other, Generic) and other._is_protocol):
2112 break
2113 else:
2114 return NotImplemented
2115 return True
2116
2117 if '__subclasshook__' not in cls.__dict__:
2118 cls.__subclasshook__ = _proto_hook
2119
2120 # We have nothing more to do for non-protocols...
2121 if not cls._is_protocol:
2122 return
2123
2124 # ... otherwise check consistency of bases, and prohibit instantiation.
2125 for base in cls.__bases__:
2126 if not (base in (object, Generic) or
2127 base.__module__ in _PROTO_ALLOWLIST and
2128 base.__name__ in _PROTO_ALLOWLIST[base.__module__] or
2129 issubclass(base, Generic) and base._is_protocol):
2130 raise TypeError('Protocols can only inherit from other'
2131 ' protocols, got %r' % base)
2132 if cls.__init__ is Protocol.__init__:
2133 cls.__init__ = _no_init_or_replace_init
2134
2135
2136class _AnnotatedAlias(_NotIterable, _GenericAlias, _root=True):
2137 """Runtime representation of an annotated type.
2138
2139 At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't'
2140 with extra annotations. The alias behaves like a normal typing alias.
2141 Instantiating is the same as instantiating the underlying type; binding
2142 it to types is also the same.
2143
2144 The metadata itself is stored in a '__metadata__' attribute as a tuple.
2145 """
2146
2147 def __init__(self, origin, metadata):
2148 if isinstance(origin, _AnnotatedAlias):
2149 metadata = origin.__metadata__ + metadata
2150 origin = origin.__origin__
2151 super().__init__(origin, origin)
2152 self.__metadata__ = metadata
2153
2154 def copy_with(self, params):
2155 assert len(params) == 1
2156 new_type = params[0]
2157 return _AnnotatedAlias(new_type, self.__metadata__)
2158
2159 def __repr__(self):
2160 return "typing.Annotated[{}, {}]".format(
2161 _type_repr(self.__origin__),
2162 ", ".join(repr(a) for a in self.__metadata__)
2163 )
2164
2165 def __reduce__(self):
2166 return operator.getitem, (
2167 Annotated, (self.__origin__,) + self.__metadata__
2168 )
2169
2170 def __eq__(self, other):
2171 if not isinstance(other, _AnnotatedAlias):
2172 return NotImplemented
2173 return (self.__origin__ == other.__origin__
2174 and self.__metadata__ == other.__metadata__)
2175
2176 def __hash__(self):
2177 return hash((self.__origin__, self.__metadata__))
2178
2179 def __getattr__(self, attr):
2180 if attr in {'__name__', '__qualname__'}:
2181 return 'Annotated'
2182 return super().__getattr__(attr)
2183
2184
2185class Annotated:
2186 """Add context-specific metadata to a type.
2187
2188 Example: Annotated[int, runtime_check.Unsigned] indicates to the
2189 hypothetical runtime_check module that this type is an unsigned int.
2190 Every other consumer of this type can ignore this metadata and treat
2191 this type as int.
2192
2193 The first argument to Annotated must be a valid type.
2194
2195 Details:
2196
2197 - It's an error to call `Annotated` with less than two arguments.
2198 - Access the metadata via the ``__metadata__`` attribute::
2199
2200 assert Annotated[int, '$'].__metadata__ == ('$',)
2201
2202 - Nested Annotated types are flattened::
2203
2204 assert Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
2205
2206 - Instantiating an annotated type is equivalent to instantiating the
2207 underlying type::
2208
2209 assert Annotated[C, Ann1](5) == C(5)
2210
2211 - Annotated can be used as a generic type alias::
2212
2213 Optimized: TypeAlias = Annotated[T, runtime.Optimize()]
2214 assert Optimized[int] == Annotated[int, runtime.Optimize()]
2215
2216 OptimizedList: TypeAlias = Annotated[list[T], runtime.Optimize()]
2217 assert OptimizedList[int] == Annotated[list[int], runtime.Optimize()]
2218
2219 - Annotated cannot be used with an unpacked TypeVarTuple::
2220
2221 Variadic: TypeAlias = Annotated[*Ts, Ann1] # NOT valid
2222
2223 This would be equivalent to::
2224
2225 Annotated[T1, T2, T3, ..., Ann1]
2226
2227 where T1, T2 etc. are TypeVars, which would be invalid, because
2228 only one type should be passed to Annotated.
2229 """
2230
2231 __slots__ = ()
2232
2233 def __new__(cls, *args, **kwargs):
2234 raise TypeError("Type Annotated cannot be instantiated.")
2235
2236 def __class_getitem__(cls, params):
2237 if not isinstance(params, tuple):
2238 params = (params,)
2239 return cls._class_getitem_inner(cls, *params)
2240
2241 @_tp_cache(typed=True)
2242 def _class_getitem_inner(cls, *params):
2243 if len(params) < 2:
2244 raise TypeError("Annotated[...] should be used "
2245 "with at least two arguments (a type and an "
2246 "annotation).")
2247 if _is_unpacked_typevartuple(params[0]):
2248 raise TypeError("Annotated[...] should not be used with an "
2249 "unpacked TypeVarTuple")
2250 msg = "Annotated[t, ...]: t must be a type."
2251 origin = _type_check(params[0], msg, allow_special_forms=True)
2252 metadata = tuple(params[1:])
2253 return _AnnotatedAlias(origin, metadata)
2254
2255 def __init_subclass__(cls, *args, **kwargs):
2256 raise TypeError(
2257 "Cannot subclass {}.Annotated".format(cls.__module__)
2258 )
2259
2260
2261def runtime_checkable(cls):
2262 """Mark a protocol class as a runtime protocol.
2263
2264 Such protocol can be used with isinstance() and issubclass().
2265 Raise TypeError if applied to a non-protocol class.
2266 This allows a simple-minded structural check very similar to
2267 one trick ponies in collections.abc such as Iterable.
2268
2269 For example::
2270
2271 @runtime_checkable
2272 class Closable(Protocol):
2273 def close(self): ...
2274
2275 assert isinstance(open('/some/file'), Closable)
2276
2277 Warning: this will check only the presence of the required methods,
2278 not their type signatures!
2279 """
2280 if not issubclass(cls, Generic) or not cls._is_protocol:
2281 raise TypeError('@runtime_checkable can be only applied to protocol classes,'
2282 ' got %r' % cls)
2283 cls._is_runtime_protocol = True
2284 return cls
2285
2286
2287def cast(typ, val):
2288 """Cast a value to a type.
2289
2290 This returns the value unchanged. To the type checker this
2291 signals that the return value has the designated type, but at
2292 runtime we intentionally don't check anything (we want this
2293 to be as fast as possible).
2294 """
2295 return val
2296
2297
2298def assert_type(val, typ, /):
2299 """Ask a static type checker to confirm that the value is of the given type.
2300
2301 At runtime this does nothing: it returns the first argument unchanged with no
2302 checks or side effects, no matter the actual type of the argument.
2303
2304 When a static type checker encounters a call to assert_type(), it
2305 emits an error if the value is not of the specified type::
2306
2307 def greet(name: str) -> None:
2308 assert_type(name, str) # OK
2309 assert_type(name, int) # type checker error
2310 """
2311 return val
2312
2313
2314_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
2315 types.MethodType, types.ModuleType,
2316 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)
2317
2318
2319def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
2320 """Return type hints for an object.
2321
2322 This is often the same as obj.__annotations__, but it handles
2323 forward references encoded as string literals and recursively replaces all
2324 'Annotated[T, ...]' with 'T' (unless 'include_extras=True').
2325
2326 The argument may be a module, class, method, or function. The annotations
2327 are returned as a dictionary. For classes, annotations include also
2328 inherited members.
2329
2330 TypeError is raised if the argument is not of a type that can contain
2331 annotations, and an empty dictionary is returned if no annotations are
2332 present.
2333
2334 BEWARE -- the behavior of globalns and localns is counterintuitive
2335 (unless you are familiar with how eval() and exec() work). The
2336 search order is locals first, then globals.
2337
2338 - If no dict arguments are passed, an attempt is made to use the
2339 globals from obj (or the respective module's globals for classes),
2340 and these are also used as the locals. If the object does not appear
2341 to have globals, an empty dictionary is used. For classes, the search
2342 order is globals first then locals.
2343
2344 - If one dict argument is passed, it is used for both globals and
2345 locals.
2346
2347 - If two dict arguments are passed, they specify globals and
2348 locals, respectively.
2349 """
2350 if getattr(obj, '__no_type_check__', None):
2351 return {}
2352 # Classes require a special treatment.
2353 if isinstance(obj, type):
2354 hints = {}
2355 for base in reversed(obj.__mro__):
2356 if globalns is None:
2357 base_globals = getattr(sys.modules.get(base.__module__, None), '__dict__', {})
2358 else:
2359 base_globals = globalns
2360 ann = base.__dict__.get('__annotations__', {})
2361 if isinstance(ann, types.GetSetDescriptorType):
2362 ann = {}
2363 base_locals = dict(vars(base)) if localns is None else localns
2364 if localns is None and globalns is None:
2365 # This is surprising, but required. Before Python 3.10,
2366 # get_type_hints only evaluated the globalns of
2367 # a class. To maintain backwards compatibility, we reverse
2368 # the globalns and localns order so that eval() looks into
2369 # *base_globals* first rather than *base_locals*.
2370 # This only affects ForwardRefs.
2371 base_globals, base_locals = base_locals, base_globals
2372 for name, value in ann.items():
2373 if value is None:
2374 value = type(None)
2375 if isinstance(value, str):
2376 value = ForwardRef(value, is_argument=False, is_class=True)
2377 value = _eval_type(value, base_globals, base_locals)
2378 hints[name] = value
2379 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
2380
2381 if globalns is None:
2382 if isinstance(obj, types.ModuleType):
2383 globalns = obj.__dict__
2384 else:
2385 nsobj = obj
2386 # Find globalns for the unwrapped object.
2387 while hasattr(nsobj, '__wrapped__'):
2388 nsobj = nsobj.__wrapped__
2389 globalns = getattr(nsobj, '__globals__', {})
2390 if localns is None:
2391 localns = globalns
2392 elif localns is None:
2393 localns = globalns
2394 hints = getattr(obj, '__annotations__', None)
2395 if hints is None:
2396 # Return empty annotations for something that _could_ have them.
2397 if isinstance(obj, _allowed_types):
2398 return {}
2399 else:
2400 raise TypeError('{!r} is not a module, class, method, '
2401 'or function.'.format(obj))
2402 hints = dict(hints)
2403 for name, value in hints.items():
2404 if value is None:
2405 value = type(None)
2406 if isinstance(value, str):
2407 # class-level forward refs were handled above, this must be either
2408 # a module-level annotation or a function argument annotation
2409 value = ForwardRef(
2410 value,
2411 is_argument=not isinstance(obj, types.ModuleType),
2412 is_class=False,
2413 )
2414 hints[name] = _eval_type(value, globalns, localns)
2415 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
2416
2417
2418def _strip_annotations(t):
2419 """Strip the annotations from a given type."""
2420 if isinstance(t, _AnnotatedAlias):
2421 return _strip_annotations(t.__origin__)
2422 if hasattr(t, "__origin__") and t.__origin__ in (Required, NotRequired):
2423 return _strip_annotations(t.__args__[0])
2424 if isinstance(t, _GenericAlias):
2425 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
2426 if stripped_args == t.__args__:
2427 return t
2428 return t.copy_with(stripped_args)
2429 if isinstance(t, GenericAlias):
2430 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
2431 if stripped_args == t.__args__:
2432 return t
2433 return GenericAlias(t.__origin__, stripped_args)
2434 if isinstance(t, types.UnionType):
2435 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
2436 if stripped_args == t.__args__:
2437 return t
2438 return functools.reduce(operator.or_, stripped_args)
2439
2440 return t
2441
2442
2443def get_origin(tp):
2444 """Get the unsubscripted version of a type.
2445
2446 This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar,
2447 Annotated, and others. Return None for unsupported types.
2448
2449 Examples::
2450
2451 >>> P = ParamSpec('P')
2452 >>> assert get_origin(Literal[42]) is Literal
2453 >>> assert get_origin(int) is None
2454 >>> assert get_origin(ClassVar[int]) is ClassVar
2455 >>> assert get_origin(Generic) is Generic
2456 >>> assert get_origin(Generic[T]) is Generic
2457 >>> assert get_origin(Union[T, int]) is Union
2458 >>> assert get_origin(List[Tuple[T, T]][int]) is list
2459 >>> assert get_origin(P.args) is P
2460 """
2461 if isinstance(tp, _AnnotatedAlias):
2462 return Annotated
2463 if isinstance(tp, (_BaseGenericAlias, GenericAlias,
2464 ParamSpecArgs, ParamSpecKwargs)):
2465 return tp.__origin__
2466 if tp is Generic:
2467 return Generic
2468 if isinstance(tp, types.UnionType):
2469 return types.UnionType
2470 return None
2471
2472
2473def get_args(tp):
2474 """Get type arguments with all substitutions performed.
2475
2476 For unions, basic simplifications used by Union constructor are performed.
2477
2478 Examples::
2479
2480 >>> T = TypeVar('T')
2481 >>> assert get_args(Dict[str, int]) == (str, int)
2482 >>> assert get_args(int) == ()
2483 >>> assert get_args(Union[int, Union[T, int], str][int]) == (int, str)
2484 >>> assert get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
2485 >>> assert get_args(Callable[[], T][int]) == ([], int)
2486 """
2487 if isinstance(tp, _AnnotatedAlias):
2488 return (tp.__origin__,) + tp.__metadata__
2489 if isinstance(tp, (_GenericAlias, GenericAlias)):
2490 res = tp.__args__
2491 if _should_unflatten_callable_args(tp, res):
2492 res = (list(res[:-1]), res[-1])
2493 return res
2494 if isinstance(tp, types.UnionType):
2495 return tp.__args__
2496 return ()
2497
2498
2499def is_typeddict(tp):
2500 """Check if an annotation is a TypedDict class.
2501
2502 For example::
2503
2504 >>> from typing import TypedDict
2505 >>> class Film(TypedDict):
2506 ... title: str
2507 ... year: int
2508 ...
2509 >>> is_typeddict(Film)
2510 True
2511 >>> is_typeddict(dict)
2512 False
2513 """
2514 return isinstance(tp, _TypedDictMeta)
2515
2516
2517_ASSERT_NEVER_REPR_MAX_LENGTH = 100
2518
2519
2520def assert_never(arg: Never, /) -> Never:
2521 """Statically assert that a line of code is unreachable.
2522
2523 Example::
2524
2525 def int_or_str(arg: int | str) -> None:
2526 match arg:
2527 case int():
2528 print("It's an int")
2529 case str():
2530 print("It's a str")
2531 case _:
2532 assert_never(arg)
2533
2534 If a type checker finds that a call to assert_never() is
2535 reachable, it will emit an error.
2536
2537 At runtime, this throws an exception when called.
2538 """
2539 value = repr(arg)
2540 if len(value) > _ASSERT_NEVER_REPR_MAX_LENGTH:
2541 value = value[:_ASSERT_NEVER_REPR_MAX_LENGTH] + '...'
2542 raise AssertionError(f"Expected code to be unreachable, but got: {value}")
2543
2544
2545def no_type_check(arg):
2546 """Decorator to indicate that annotations are not type hints.
2547
2548 The argument must be a class or function; if it is a class, it
2549 applies recursively to all methods and classes defined in that class
2550 (but not to methods defined in its superclasses or subclasses).
2551
2552 This mutates the function(s) or class(es) in place.
2553 """
2554 if isinstance(arg, type):
2555 for key in dir(arg):
2556 obj = getattr(arg, key)
2557 if (
2558 not hasattr(obj, '__qualname__')
2559 or obj.__qualname__ != f'{arg.__qualname__}.{obj.__name__}'
2560 or getattr(obj, '__module__', None) != arg.__module__
2561 ):
2562 # We only modify objects that are defined in this type directly.
2563 # If classes / methods are nested in multiple layers,
2564 # we will modify them when processing their direct holders.
2565 continue
2566 # Instance, class, and static methods:
2567 if isinstance(obj, types.FunctionType):
2568 obj.__no_type_check__ = True
2569 if isinstance(obj, types.MethodType):
2570 obj.__func__.__no_type_check__ = True
2571 # Nested types:
2572 if isinstance(obj, type):
2573 no_type_check(obj)
2574 try:
2575 arg.__no_type_check__ = True
2576 except TypeError: # built-in classes
2577 pass
2578 return arg
2579
2580
2581def no_type_check_decorator(decorator):
2582 """Decorator to give another decorator the @no_type_check effect.
2583
2584 This wraps the decorator with something that wraps the decorated
2585 function in @no_type_check.
2586 """
2587 @functools.wraps(decorator)
2588 def wrapped_decorator(*args, **kwds):
2589 func = decorator(*args, **kwds)
2590 func = no_type_check(func)
2591 return func
2592
2593 return wrapped_decorator
2594
2595
2596def _overload_dummy(*args, **kwds):
2597 """Helper for @overload to raise when called."""
2598 raise NotImplementedError(
2599 "You should not call an overloaded function. "
2600 "A series of @overload-decorated functions "
2601 "outside a stub module should always be followed "
2602 "by an implementation that is not @overload-ed.")
2603
2604
2605# {module: {qualname: {firstlineno: func}}}
2606_overload_registry = defaultdict(functools.partial(defaultdict, dict))
2607
2608
[docs]
2609def overload(func):
2610 """Decorator for overloaded functions/methods.
2611
2612 In a stub file, place two or more stub definitions for the same
2613 function in a row, each decorated with @overload.
2614
2615 For example::
2616
2617 @overload
2618 def utf8(value: None) -> None: ...
2619 @overload
2620 def utf8(value: bytes) -> bytes: ...
2621 @overload
2622 def utf8(value: str) -> bytes: ...
2623
2624 In a non-stub file (i.e. a regular .py file), do the same but
2625 follow it with an implementation. The implementation should *not*
2626 be decorated with @overload::
2627
2628 @overload
2629 def utf8(value: None) -> None: ...
2630 @overload
2631 def utf8(value: bytes) -> bytes: ...
2632 @overload
2633 def utf8(value: str) -> bytes: ...
2634 def utf8(value):
2635 ... # implementation goes here
2636
2637 The overloads for a function can be retrieved at runtime using the
2638 get_overloads() function.
2639 """
2640 # classmethod and staticmethod
2641 f = getattr(func, "__func__", func)
2642 try:
2643 _overload_registry[f.__module__][f.__qualname__][f.__code__.co_firstlineno] = func
2644 except AttributeError:
2645 # Not a normal function; ignore.
2646 pass
2647 return _overload_dummy
2648
2649
2650def get_overloads(func):
2651 """Return all defined overloads for *func* as a sequence."""
2652 # classmethod and staticmethod
2653 f = getattr(func, "__func__", func)
2654 if f.__module__ not in _overload_registry:
2655 return []
2656 mod_dict = _overload_registry[f.__module__]
2657 if f.__qualname__ not in mod_dict:
2658 return []
2659 return list(mod_dict[f.__qualname__].values())
2660
2661
2662def clear_overloads():
2663 """Clear all overloads in the registry."""
2664 _overload_registry.clear()
2665
2666
[docs]
2667def final(f):
2668 """Decorator to indicate final methods and final classes.
2669
2670 Use this decorator to indicate to type checkers that the decorated
2671 method cannot be overridden, and decorated class cannot be subclassed.
2672
2673 For example::
2674
2675 class Base:
2676 @final
2677 def done(self) -> None:
2678 ...
2679 class Sub(Base):
2680 def done(self) -> None: # Error reported by type checker
2681 ...
2682
2683 @final
2684 class Leaf:
2685 ...
2686 class Other(Leaf): # Error reported by type checker
2687 ...
2688
2689 There is no runtime checking of these properties. The decorator
2690 attempts to set the ``__final__`` attribute to ``True`` on the decorated
2691 object to allow runtime introspection.
2692 """
2693 try:
2694 f.__final__ = True
2695 except (AttributeError, TypeError):
2696 # Skip the attribute silently if it is not writable.
2697 # AttributeError happens if the object has __slots__ or a
2698 # read-only property, TypeError if it's a builtin class.
2699 pass
2700 return f
2701
2702
2703# Some unconstrained type variables. These are used by the container types.
2704# (These are not for export.)
2705T = TypeVar('T') # Any type.
2706KT = TypeVar('KT') # Key type.
2707VT = TypeVar('VT') # Value type.
2708T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
2709V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
2710VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
2711T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
2712# Internal type variable used for Type[].
2713CT_co = TypeVar('CT_co', covariant=True, bound=type)
2714
2715# A useful type variable with constraints. This represents string types.
2716# (This one *is* for export!)
2717AnyStr = TypeVar('AnyStr', bytes, str)
2718
2719
2720# Various ABCs mimicking those in collections.abc.
2721_alias = _SpecialGenericAlias
2722
2723Hashable = _alias(collections.abc.Hashable, 0) # Not generic.
2724Awaitable = _alias(collections.abc.Awaitable, 1)
2725Coroutine = _alias(collections.abc.Coroutine, 3)
2726AsyncIterable = _alias(collections.abc.AsyncIterable, 1)
2727AsyncIterator = _alias(collections.abc.AsyncIterator, 1)
2728Iterable = _alias(collections.abc.Iterable, 1)
2729Iterator = _alias(collections.abc.Iterator, 1)
2730Reversible = _alias(collections.abc.Reversible, 1)
2731Sized = _alias(collections.abc.Sized, 0) # Not generic.
2732Container = _alias(collections.abc.Container, 1)
2733Collection = _alias(collections.abc.Collection, 1)
2734Callable = _CallableType(collections.abc.Callable, 2)
2735Callable.__doc__ = \
2736 """Deprecated alias to collections.abc.Callable.
2737
2738 Callable[[int], str] signifies a function that takes a single
2739 parameter of type int and returns a str.
2740
2741 The subscription syntax must always be used with exactly two
2742 values: the argument list and the return type.
2743 The argument list must be a list of types, a ParamSpec,
2744 Concatenate or ellipsis. The return type must be a single type.
2745
2746 There is no syntax to indicate optional or keyword arguments;
2747 such function types are rarely used as callback types.
2748 """
2749AbstractSet = _alias(collections.abc.Set, 1, name='AbstractSet')
2750MutableSet = _alias(collections.abc.MutableSet, 1)
2751# NOTE: Mapping is only covariant in the value type.
2752Mapping = _alias(collections.abc.Mapping, 2)
2753MutableMapping = _alias(collections.abc.MutableMapping, 2)
2754Sequence = _alias(collections.abc.Sequence, 1)
2755MutableSequence = _alias(collections.abc.MutableSequence, 1)
2756ByteString = _alias(collections.abc.ByteString, 0) # Not generic
2757# Tuple accepts variable number of parameters.
2758Tuple = _TupleType(tuple, -1, inst=False, name='Tuple')
2759Tuple.__doc__ = \
2760 """Deprecated alias to builtins.tuple.
2761
2762 Tuple[X, Y] is the cross-product type of X and Y.
2763
2764 Example: Tuple[T1, T2] is a tuple of two elements corresponding
2765 to type variables T1 and T2. Tuple[int, float, str] is a tuple
2766 of an int, a float and a string.
2767
2768 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
2769 """
2770List = _alias(list, 1, inst=False, name='List')
2771Deque = _alias(collections.deque, 1, name='Deque')
2772Set = _alias(set, 1, inst=False, name='Set')
2773FrozenSet = _alias(frozenset, 1, inst=False, name='FrozenSet')
2774MappingView = _alias(collections.abc.MappingView, 1)
2775KeysView = _alias(collections.abc.KeysView, 1)
2776ItemsView = _alias(collections.abc.ItemsView, 2)
2777ValuesView = _alias(collections.abc.ValuesView, 1)
2778ContextManager = _alias(contextlib.AbstractContextManager, 1, name='ContextManager')
2779AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, 1, name='AsyncContextManager')
2780Dict = _alias(dict, 2, inst=False, name='Dict')
2781DefaultDict = _alias(collections.defaultdict, 2, name='DefaultDict')
2782OrderedDict = _alias(collections.OrderedDict, 2)
2783Counter = _alias(collections.Counter, 1)
2784ChainMap = _alias(collections.ChainMap, 2)
2785Generator = _alias(collections.abc.Generator, 3)
2786AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2)
2787Type = _alias(type, 1, inst=False, name='Type')
2788Type.__doc__ = \
2789 """Deprecated alias to builtins.type.
2790
2791 builtins.type or typing.Type can be used to annotate class objects.
2792 For example, suppose we have the following classes::
2793
2794 class User: ... # Abstract base for User classes
2795 class BasicUser(User): ...
2796 class ProUser(User): ...
2797 class TeamUser(User): ...
2798
2799 And a function that takes a class argument that's a subclass of
2800 User and returns an instance of the corresponding class::
2801
2802 U = TypeVar('U', bound=User)
2803 def new_user(user_class: Type[U]) -> U:
2804 user = user_class()
2805 # (Here we could write the user object to a database)
2806 return user
2807
2808 joe = new_user(BasicUser)
2809
2810 At this point the type checker knows that joe has type BasicUser.
2811 """
2812
2813
2814@runtime_checkable
2815class SupportsInt(Protocol):
2816 """An ABC with one abstract method __int__."""
2817
2818 __slots__ = ()
2819
2820 @abstractmethod
2821 def __int__(self) -> int:
2822 pass
2823
2824
2825@runtime_checkable
2826class SupportsFloat(Protocol):
2827 """An ABC with one abstract method __float__."""
2828
2829 __slots__ = ()
2830
2831 @abstractmethod
2832 def __float__(self) -> float:
2833 pass
2834
2835
2836@runtime_checkable
2837class SupportsComplex(Protocol):
2838 """An ABC with one abstract method __complex__."""
2839
2840 __slots__ = ()
2841
2842 @abstractmethod
2843 def __complex__(self) -> complex:
2844 pass
2845
2846
2847@runtime_checkable
2848class SupportsBytes(Protocol):
2849 """An ABC with one abstract method __bytes__."""
2850
2851 __slots__ = ()
2852
2853 @abstractmethod
2854 def __bytes__(self) -> bytes:
2855 pass
2856
2857
2858@runtime_checkable
2859class SupportsIndex(Protocol):
2860 """An ABC with one abstract method __index__."""
2861
2862 __slots__ = ()
2863
2864 @abstractmethod
2865 def __index__(self) -> int:
2866 pass
2867
2868
2869@runtime_checkable
2870class SupportsAbs(Protocol[T_co]):
2871 """An ABC with one abstract method __abs__ that is covariant in its return type."""
2872
2873 __slots__ = ()
2874
2875 @abstractmethod
2876 def __abs__(self) -> T_co:
2877 pass
2878
2879
2880@runtime_checkable
2881class SupportsRound(Protocol[T_co]):
2882 """An ABC with one abstract method __round__ that is covariant in its return type."""
2883
2884 __slots__ = ()
2885
2886 @abstractmethod
2887 def __round__(self, ndigits: int = 0) -> T_co:
2888 pass
2889
2890
2891def _make_nmtuple(name, types, module, defaults = ()):
2892 fields = [n for n, t in types]
2893 types = {n: _type_check(t, f"field {n} annotation must be a type")
2894 for n, t in types}
2895 nm_tpl = collections.namedtuple(name, fields,
2896 defaults=defaults, module=module)
2897 nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types
2898 return nm_tpl
2899
2900
2901# attributes prohibited to set in NamedTuple class syntax
2902_prohibited = frozenset({'__new__', '__init__', '__slots__', '__getnewargs__',
2903 '_fields', '_field_defaults',
2904 '_make', '_replace', '_asdict', '_source'})
2905
2906_special = frozenset({'__module__', '__name__', '__annotations__'})
2907
2908
2909class NamedTupleMeta(type):
2910 def __new__(cls, typename, bases, ns):
2911 assert _NamedTuple in bases
2912 for base in bases:
2913 if base is not _NamedTuple and base is not Generic:
2914 raise TypeError(
2915 'can only inherit from a NamedTuple type and Generic')
2916 bases = tuple(tuple if base is _NamedTuple else base for base in bases)
2917 types = ns.get('__annotations__', {})
2918 default_names = []
2919 for field_name in types:
2920 if field_name in ns:
2921 default_names.append(field_name)
2922 elif default_names:
2923 raise TypeError(f"Non-default namedtuple field {field_name} "
2924 f"cannot follow default field"
2925 f"{'s' if len(default_names) > 1 else ''} "
2926 f"{', '.join(default_names)}")
2927 nm_tpl = _make_nmtuple(typename, types.items(),
2928 defaults=[ns[n] for n in default_names],
2929 module=ns['__module__'])
2930 nm_tpl.__bases__ = bases
2931 if Generic in bases:
2932 class_getitem = Generic.__class_getitem__.__func__
2933 nm_tpl.__class_getitem__ = classmethod(class_getitem)
2934 # update from user namespace without overriding special namedtuple attributes
2935 for key in ns:
2936 if key in _prohibited:
2937 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
2938 elif key not in _special and key not in nm_tpl._fields:
2939 setattr(nm_tpl, key, ns[key])
2940 if Generic in bases:
2941 nm_tpl.__init_subclass__()
2942 return nm_tpl
2943
2944
2945def NamedTuple(typename, fields=None, /, **kwargs):
2946 """Typed version of namedtuple.
2947
2948 Usage::
2949
2950 class Employee(NamedTuple):
2951 name: str
2952 id: int
2953
2954 This is equivalent to::
2955
2956 Employee = collections.namedtuple('Employee', ['name', 'id'])
2957
2958 The resulting class has an extra __annotations__ attribute, giving a
2959 dict that maps field names to types. (The field names are also in
2960 the _fields attribute, which is part of the namedtuple API.)
2961 An alternative equivalent functional syntax is also accepted::
2962
2963 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
2964 """
2965 if fields is None:
2966 fields = kwargs.items()
2967 elif kwargs:
2968 raise TypeError("Either list of fields or keywords"
2969 " can be provided to NamedTuple, not both")
2970 return _make_nmtuple(typename, fields, module=_caller())
2971
2972_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})
2973
2974def _namedtuple_mro_entries(bases):
2975 assert NamedTuple in bases
2976 return (_NamedTuple,)
2977
2978NamedTuple.__mro_entries__ = _namedtuple_mro_entries
2979
2980
2981class _TypedDictMeta(type):
2982 def __new__(cls, name, bases, ns, total=True):
2983 """Create a new typed dict class object.
2984
2985 This method is called when TypedDict is subclassed,
2986 or when TypedDict is instantiated. This way
2987 TypedDict supports all three syntax forms described in its docstring.
2988 Subclasses and instances of TypedDict return actual dictionaries.
2989 """
2990 for base in bases:
2991 if type(base) is not _TypedDictMeta and base is not Generic:
2992 raise TypeError('cannot inherit from both a TypedDict type '
2993 'and a non-TypedDict base class')
2994
2995 if any(issubclass(b, Generic) for b in bases):
2996 generic_base = (Generic,)
2997 else:
2998 generic_base = ()
2999
3000 tp_dict = type.__new__(_TypedDictMeta, name, (*generic_base, dict), ns)
3001
3002 annotations = {}
3003 own_annotations = ns.get('__annotations__', {})
3004 msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
3005 own_annotations = {
3006 n: _type_check(tp, msg, module=tp_dict.__module__)
3007 for n, tp in own_annotations.items()
3008 }
3009 required_keys = set()
3010 optional_keys = set()
3011
3012 for base in bases:
3013 annotations.update(base.__dict__.get('__annotations__', {}))
3014
3015 base_required = base.__dict__.get('__required_keys__', set())
3016 required_keys |= base_required
3017 optional_keys -= base_required
3018
3019 base_optional = base.__dict__.get('__optional_keys__', set())
3020 required_keys -= base_optional
3021 optional_keys |= base_optional
3022
3023 annotations.update(own_annotations)
3024 for annotation_key, annotation_type in own_annotations.items():
3025 annotation_origin = get_origin(annotation_type)
3026 if annotation_origin is Annotated:
3027 annotation_args = get_args(annotation_type)
3028 if annotation_args:
3029 annotation_type = annotation_args[0]
3030 annotation_origin = get_origin(annotation_type)
3031
3032 if annotation_origin is Required:
3033 is_required = True
3034 elif annotation_origin is NotRequired:
3035 is_required = False
3036 else:
3037 is_required = total
3038
3039 if is_required:
3040 required_keys.add(annotation_key)
3041 optional_keys.discard(annotation_key)
3042 else:
3043 optional_keys.add(annotation_key)
3044 required_keys.discard(annotation_key)
3045
3046 assert required_keys.isdisjoint(optional_keys), (
3047 f"Required keys overlap with optional keys in {name}:"
3048 f" {required_keys=}, {optional_keys=}"
3049 )
3050 tp_dict.__annotations__ = annotations
3051 tp_dict.__required_keys__ = frozenset(required_keys)
3052 tp_dict.__optional_keys__ = frozenset(optional_keys)
3053 if not hasattr(tp_dict, '__total__'):
3054 tp_dict.__total__ = total
3055 return tp_dict
3056
3057 __call__ = dict # static method
3058
3059 def __subclasscheck__(cls, other):
3060 # Typed dicts are only for static structural subtyping.
3061 raise TypeError('TypedDict does not support instance and class checks')
3062
3063 __instancecheck__ = __subclasscheck__
3064
3065
3066def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
3067 """A simple typed namespace. At runtime it is equivalent to a plain dict.
3068
3069 TypedDict creates a dictionary type such that a type checker will expect all
3070 instances to have a certain set of keys, where each key is
3071 associated with a value of a consistent type. This expectation
3072 is not checked at runtime.
3073
3074 Usage::
3075
3076 >>> class Point2D(TypedDict):
3077 ... x: int
3078 ... y: int
3079 ... label: str
3080 ...
3081 >>> a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
3082 >>> b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
3083 >>> Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
3084 True
3085
3086 The type info can be accessed via the Point2D.__annotations__ dict, and
3087 the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
3088 TypedDict supports an additional equivalent form::
3089
3090 Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
3091
3092 By default, all keys must be present in a TypedDict. It is possible
3093 to override this by specifying totality::
3094
3095 class Point2D(TypedDict, total=False):
3096 x: int
3097 y: int
3098
3099 This means that a Point2D TypedDict can have any of the keys omitted. A type
3100 checker is only expected to support a literal False or True as the value of
3101 the total argument. True is the default, and makes all items defined in the
3102 class body be required.
3103
3104 The Required and NotRequired special forms can also be used to mark
3105 individual keys as being required or not required::
3106
3107 class Point2D(TypedDict):
3108 x: int # the "x" key must always be present (Required is the default)
3109 y: NotRequired[int] # the "y" key can be omitted
3110
3111 See PEP 655 for more details on Required and NotRequired.
3112 """
3113 if fields is None:
3114 fields = kwargs
3115 elif kwargs:
3116 raise TypeError("TypedDict takes either a dict or keyword arguments,"
3117 " but not both")
3118 if kwargs:
3119 warnings.warn(
3120 "The kwargs-based syntax for TypedDict definitions is deprecated "
3121 "in Python 3.11, will be removed in Python 3.13, and may not be "
3122 "understood by third-party type checkers.",
3123 DeprecationWarning,
3124 stacklevel=2,
3125 )
3126
3127 ns = {'__annotations__': dict(fields)}
3128 module = _caller()
3129 if module is not None:
3130 # Setting correct module is necessary to make typed dict classes pickleable.
3131 ns['__module__'] = module
3132
3133 return _TypedDictMeta(typename, (), ns, total=total)
3134
3135_TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {})
3136TypedDict.__mro_entries__ = lambda bases: (_TypedDict,)
3137
3138
3139@_SpecialForm
3140def Required(self, parameters):
3141 """Special typing construct to mark a TypedDict key as required.
3142
3143 This is mainly useful for total=False TypedDicts.
3144
3145 For example::
3146
3147 class Movie(TypedDict, total=False):
3148 title: Required[str]
3149 year: int
3150
3151 m = Movie(
3152 title='The Matrix', # typechecker error if key is omitted
3153 year=1999,
3154 )
3155
3156 There is no runtime checking that a required key is actually provided
3157 when instantiating a related TypedDict.
3158 """
3159 item = _type_check(parameters, f'{self._name} accepts only a single type.')
3160 return _GenericAlias(self, (item,))
3161
3162
3163@_SpecialForm
3164def NotRequired(self, parameters):
3165 """Special typing construct to mark a TypedDict key as potentially missing.
3166
3167 For example::
3168
3169 class Movie(TypedDict):
3170 title: str
3171 year: NotRequired[int]
3172
3173 m = Movie(
3174 title='The Matrix', # typechecker error if key is omitted
3175 year=1999,
3176 )
3177 """
3178 item = _type_check(parameters, f'{self._name} accepts only a single type.')
3179 return _GenericAlias(self, (item,))
3180
3181
3182class NewType:
3183 """NewType creates simple unique types with almost zero runtime overhead.
3184
3185 NewType(name, tp) is considered a subtype of tp
3186 by static type checkers. At runtime, NewType(name, tp) returns
3187 a dummy callable that simply returns its argument.
3188
3189 Usage::
3190
3191 UserId = NewType('UserId', int)
3192
3193 def name_by_id(user_id: UserId) -> str:
3194 ...
3195
3196 UserId('user') # Fails type check
3197
3198 name_by_id(42) # Fails type check
3199 name_by_id(UserId(42)) # OK
3200
3201 num = UserId(5) + 1 # type: int
3202 """
3203
3204 __call__ = _idfunc
3205
3206 def __init__(self, name, tp):
3207 self.__qualname__ = name
3208 if '.' in name:
3209 name = name.rpartition('.')[-1]
3210 self.__name__ = name
3211 self.__supertype__ = tp
3212 def_mod = _caller()
3213 if def_mod != 'typing':
3214 self.__module__ = def_mod
3215
3216 def __mro_entries__(self, bases):
3217 # We defined __mro_entries__ to get a better error message
3218 # if a user attempts to subclass a NewType instance. bpo-46170
3219 superclass_name = self.__name__
3220
3221 class Dummy:
3222 def __init_subclass__(cls):
3223 subclass_name = cls.__name__
3224 raise TypeError(
3225 f"Cannot subclass an instance of NewType. Perhaps you were looking for: "
3226 f"`{subclass_name} = NewType({subclass_name!r}, {superclass_name})`"
3227 )
3228
3229 return (Dummy,)
3230
3231 def __repr__(self):
3232 return f'{self.__module__}.{self.__qualname__}'
3233
3234 def __reduce__(self):
3235 return self.__qualname__
3236
3237 def __or__(self, other):
3238 return Union[self, other]
3239
3240 def __ror__(self, other):
3241 return Union[other, self]
3242
3243
3244# Python-version-specific alias (Python 2: unicode; Python 3: str)
3245Text = str
3246
3247
3248# Constant that's True when type checking, but False here.
3249TYPE_CHECKING = False
3250
3251
3252class IO(Generic[AnyStr]):
3253 """Generic base class for TextIO and BinaryIO.
3254
3255 This is an abstract, generic version of the return of open().
3256
3257 NOTE: This does not distinguish between the different possible
3258 classes (text vs. binary, read vs. write vs. read/write,
3259 append-only, unbuffered). The TextIO and BinaryIO subclasses
3260 below capture the distinctions between text vs. binary, which is
3261 pervasive in the interface; however we currently do not offer a
3262 way to track the other distinctions in the type system.
3263 """
3264
3265 __slots__ = ()
3266
3267 @property
3268 @abstractmethod
3269 def mode(self) -> str:
3270 pass
3271
3272 @property
3273 @abstractmethod
3274 def name(self) -> str:
3275 pass
3276
3277 @abstractmethod
3278 def close(self) -> None:
3279 pass
3280
3281 @property
3282 @abstractmethod
3283 def closed(self) -> bool:
3284 pass
3285
3286 @abstractmethod
3287 def fileno(self) -> int:
3288 pass
3289
3290 @abstractmethod
3291 def flush(self) -> None:
3292 pass
3293
3294 @abstractmethod
3295 def isatty(self) -> bool:
3296 pass
3297
3298 @abstractmethod
3299 def read(self, n: int = -1) -> AnyStr:
3300 pass
3301
3302 @abstractmethod
3303 def readable(self) -> bool:
3304 pass
3305
3306 @abstractmethod
3307 def readline(self, limit: int = -1) -> AnyStr:
3308 pass
3309
3310 @abstractmethod
3311 def readlines(self, hint: int = -1) -> List[AnyStr]:
3312 pass
3313
3314 @abstractmethod
3315 def seek(self, offset: int, whence: int = 0) -> int:
3316 pass
3317
3318 @abstractmethod
3319 def seekable(self) -> bool:
3320 pass
3321
3322 @abstractmethod
3323 def tell(self) -> int:
3324 pass
3325
3326 @abstractmethod
3327 def truncate(self, size: int = None) -> int:
3328 pass
3329
3330 @abstractmethod
3331 def writable(self) -> bool:
3332 pass
3333
3334 @abstractmethod
3335 def write(self, s: AnyStr) -> int:
3336 pass
3337
3338 @abstractmethod
3339 def writelines(self, lines: List[AnyStr]) -> None:
3340 pass
3341
3342 @abstractmethod
3343 def __enter__(self) -> 'IO[AnyStr]':
3344 pass
3345
3346 @abstractmethod
3347 def __exit__(self, type, value, traceback) -> None:
3348 pass
3349
3350
3351class BinaryIO(IO[bytes]):
3352 """Typed version of the return of open() in binary mode."""
3353
3354 __slots__ = ()
3355
3356 @abstractmethod
3357 def write(self, s: Union[bytes, bytearray]) -> int:
3358 pass
3359
3360 @abstractmethod
3361 def __enter__(self) -> 'BinaryIO':
3362 pass
3363
3364
3365class TextIO(IO[str]):
3366 """Typed version of the return of open() in text mode."""
3367
3368 __slots__ = ()
3369
3370 @property
3371 @abstractmethod
3372 def buffer(self) -> BinaryIO:
3373 pass
3374
3375 @property
3376 @abstractmethod
3377 def encoding(self) -> str:
3378 pass
3379
3380 @property
3381 @abstractmethod
3382 def errors(self) -> Optional[str]:
3383 pass
3384
3385 @property
3386 @abstractmethod
3387 def line_buffering(self) -> bool:
3388 pass
3389
3390 @property
3391 @abstractmethod
3392 def newlines(self) -> Any:
3393 pass
3394
3395 @abstractmethod
3396 def __enter__(self) -> 'TextIO':
3397 pass
3398
3399
3400class _DeprecatedType(type):
3401 def __getattribute__(cls, name):
3402 if name not in {"__dict__", "__module__", "__doc__"} and name in cls.__dict__:
3403 warnings.warn(
3404 f"{cls.__name__} is deprecated, import directly "
3405 f"from typing instead. {cls.__name__} will be removed "
3406 "in Python 3.12.",
3407 DeprecationWarning,
3408 stacklevel=2,
3409 )
3410 return super().__getattribute__(name)
3411
3412
3413class io(metaclass=_DeprecatedType):
3414 """Wrapper namespace for IO generic classes."""
3415
3416 __all__ = ['IO', 'TextIO', 'BinaryIO']
3417 IO = IO
3418 TextIO = TextIO
3419 BinaryIO = BinaryIO
3420
3421
3422io.__name__ = __name__ + '.io'
3423sys.modules[io.__name__] = io
3424
3425Pattern = _alias(stdlib_re.Pattern, 1)
3426Match = _alias(stdlib_re.Match, 1)
3427
3428class re(metaclass=_DeprecatedType):
3429 """Wrapper namespace for re type aliases."""
3430
3431 __all__ = ['Pattern', 'Match']
3432 Pattern = Pattern
3433 Match = Match
3434
3435
3436re.__name__ = __name__ + '.re'
3437sys.modules[re.__name__] = re
3438
3439
3440def reveal_type(obj: T, /) -> T:
3441 """Ask a static type checker to reveal the inferred type of an expression.
3442
3443 When a static type checker encounters a call to ``reveal_type()``,
3444 it will emit the inferred type of the argument::
3445
3446 x: int = 1
3447 reveal_type(x)
3448
3449 Running a static type checker (e.g., mypy) on this example
3450 will produce output similar to 'Revealed type is "builtins.int"'.
3451
3452 At runtime, the function prints the runtime type of the
3453 argument and returns the argument unchanged.
3454 """
3455 print(f"Runtime type is {type(obj).__name__!r}", file=sys.stderr)
3456 return obj
3457
3458
3459def dataclass_transform(
3460 *,
3461 eq_default: bool = True,
3462 order_default: bool = False,
3463 kw_only_default: bool = False,
3464 field_specifiers: tuple[type[Any] | Callable[..., Any], ...] = (),
3465 **kwargs: Any,
3466) -> Callable[[T], T]:
3467 """Decorator to mark an object as providing dataclass-like behaviour.
3468
3469 The decorator can be applied to a function, class, or metaclass.
3470
3471 Example usage with a decorator function::
3472
3473 T = TypeVar("T")
3474
3475 @dataclass_transform()
3476 def create_model(cls: type[T]) -> type[T]:
3477 ...
3478 return cls
3479
3480 @create_model
3481 class CustomerModel:
3482 id: int
3483 name: str
3484
3485 On a base class::
3486
3487 @dataclass_transform()
3488 class ModelBase: ...
3489
3490 class CustomerModel(ModelBase):
3491 id: int
3492 name: str
3493
3494 On a metaclass::
3495
3496 @dataclass_transform()
3497 class ModelMeta(type): ...
3498
3499 class ModelBase(metaclass=ModelMeta): ...
3500
3501 class CustomerModel(ModelBase):
3502 id: int
3503 name: str
3504
3505 The ``CustomerModel`` classes defined above will
3506 be treated by type checkers similarly to classes created with
3507 ``@dataclasses.dataclass``.
3508 For example, type checkers will assume these classes have
3509 ``__init__`` methods that accept ``id`` and ``name``.
3510
3511 The arguments to this decorator can be used to customize this behavior:
3512 - ``eq_default`` indicates whether the ``eq`` parameter is assumed to be
3513 ``True`` or ``False`` if it is omitted by the caller.
3514 - ``order_default`` indicates whether the ``order`` parameter is
3515 assumed to be True or False if it is omitted by the caller.
3516 - ``kw_only_default`` indicates whether the ``kw_only`` parameter is
3517 assumed to be True or False if it is omitted by the caller.
3518 - ``field_specifiers`` specifies a static list of supported classes
3519 or functions that describe fields, similar to ``dataclasses.field()``.
3520 - Arbitrary other keyword arguments are accepted in order to allow for
3521 possible future extensions.
3522
3523 At runtime, this decorator records its arguments in the
3524 ``__dataclass_transform__`` attribute on the decorated object.
3525 It has no other runtime effect.
3526
3527 See PEP 681 for more details.
3528 """
3529 def decorator(cls_or_fn):
3530 cls_or_fn.__dataclass_transform__ = {
3531 "eq_default": eq_default,
3532 "order_default": order_default,
3533 "kw_only_default": kw_only_default,
3534 "field_specifiers": field_specifiers,
3535 "kwargs": kwargs,
3536 }
3537 return cls_or_fn
3538 return decorator