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