1""" 2The typing module: Support for gradual typing as defined by PEP 484. 3 4At large scale, the structure of the module is following: 5* Imports and exports, all public names should be explicitly added to __all__. 6* Internal helper functions: these should never be used in code outside this module. 7* _SpecialForm and its instances (special forms): 8 Any, NoReturn, ClassVar, Union, Optional, Concatenate 9* Classes whose instances can be type arguments in addition to types: 10 ForwardRef, TypeVar and ParamSpec 11* The core of internal generics API: _GenericAlias and _VariadicGenericAlias, the latter is 12 currently only used by Tuple and Callable. All subscripted types like X[int], Union[int, str], 13 etc., are instances of either of these classes. 14* The public counterpart of the generics API consists of two classes: Generic and Protocol. 15* Public helper functions: get_type_hints, overload, cast, no_type_check, 16 no_type_check_decorator. 17* Generic aliases for collections.abc ABCs and few additional protocols. 18* Special types: NewType, NamedTuple, TypedDict. 19* Wrapper submodules for re and io related types. 20""" 21 22from abc import abstractmethod, ABCMeta 23import collections 24import collections.abc 25import contextlib 26import functools 27import operator 28import re as stdlib_re # Avoid confusion with the re we export. 29import sys 30import types 31from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType, GenericAlias 32 33# Please keep __all__ alphabetized within each category. 34__all__ = [ 35 # Super-special typing primitives. 36 'Annotated', 37 'Any', 38 'Callable', 39 'ClassVar', 40 'Concatenate', 41 'Final', 42 'ForwardRef', 43 'Generic', 44 'Literal', 45 'Optional', 46 'ParamSpec', 47 'Protocol', 48 'Tuple', 49 'Type', 50 'TypeVar', 51 'Union', 52 53 # ABCs (from collections.abc). 54 'AbstractSet', # collections.abc.Set. 55 'ByteString', 56 'Container', 57 'ContextManager', 58 'Hashable', 59 'ItemsView', 60 'Iterable', 61 'Iterator', 62 'KeysView', 63 'Mapping', 64 'MappingView', 65 'MutableMapping', 66 'MutableSequence', 67 'MutableSet', 68 'Sequence', 69 'Sized', 70 'ValuesView', 71 'Awaitable', 72 'AsyncIterator', 73 'AsyncIterable', 74 'Coroutine', 75 'Collection', 76 'AsyncGenerator', 77 'AsyncContextManager', 78 79 # Structural checks, a.k.a. protocols. 80 'Reversible', 81 'SupportsAbs', 82 'SupportsBytes', 83 'SupportsComplex', 84 'SupportsFloat', 85 'SupportsIndex', 86 'SupportsInt', 87 'SupportsRound', 88 89 # Concrete collection types. 90 'ChainMap', 91 'Counter', 92 'Deque', 93 'Dict', 94 'DefaultDict', 95 'List', 96 'OrderedDict', 97 'Set', 98 'FrozenSet', 99 'NamedTuple', # Not really a type. 100 'TypedDict', # Not really a type. 101 'Generator', 102 103 # Other concrete types. 104 'BinaryIO', 105 'IO', 106 'Match', 107 'Pattern', 108 'TextIO', 109 110 # One-off things. 111 'AnyStr', 112 'cast', 113 'final', 114 'get_args', 115 'get_origin', 116 'get_type_hints', 117 'is_typeddict', 118 'NewType', 119 'no_type_check', 120 'no_type_check_decorator', 121 'NoReturn', 122 'overload', 123 'ParamSpecArgs', 124 'ParamSpecKwargs', 125 'runtime_checkable', 126 'Text', 127 'TYPE_CHECKING', 128 'TypeAlias', 129 'TypeGuard', 130] 131 132# The pseudo-submodules 're' and 'io' are part of the public 133# namespace, but excluded from __all__ because they might stomp on 134# legitimate imports of those modules. 135 136 137def _type_convert(arg, module=None): 138 """For converting None to type(None), and strings to ForwardRef.""" 139 if arg is None: 140 return type(None) 141 if isinstance(arg, str): 142 return ForwardRef(arg, module=module) 143 return arg 144 145 146def _type_check(arg, msg, is_argument=True, module=None, *, is_class=False): 147 """Check that the argument is a type, and return it (internal helper). 148 149 As a special case, accept None and return type(None) instead. Also wrap strings 150 into ForwardRef instances. Consider several corner cases, for example plain 151 special forms like Union are not valid, while Union[int, str] is OK, etc. 152 The msg argument is a human-readable error message, e.g:: 153 154 "Union[arg, ...]: arg should be a type." 155 156 We append the repr() of the actual value (truncated to 100 chars). 157 """ 158 invalid_generic_forms = (Generic, Protocol) 159 if not is_class: 160 invalid_generic_forms += (ClassVar,) 161 if is_argument: 162 invalid_generic_forms += (Final,) 163 164 arg = _type_convert(arg, module=module) 165 if (isinstance(arg, _GenericAlias) and 166 arg.__origin__ in invalid_generic_forms): 167 raise TypeError(f"{arg} is not valid as type argument") 168 if arg in (Any, NoReturn, Final): 169 return arg 170 if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol): 171 raise TypeError(f"Plain {arg} is not valid as type argument") 172 if isinstance(arg, (type, TypeVar, ForwardRef, types.UnionType, ParamSpec)): 173 return arg 174 if not callable(arg): 175 raise TypeError(f"{msg} Got {arg!r:.100}.") 176 return arg 177 178 179def _is_param_expr(arg): 180 return arg is ... or isinstance(arg, 181 (tuple, list, ParamSpec, _ConcatenateGenericAlias)) 182 183 184def _type_repr(obj): 185 """Return the repr() of an object, special-casing types (internal helper). 186 187 If obj is a type, we return a shorter version than the default 188 type.__repr__, based on the module and qualified name, which is 189 typically enough to uniquely identify a type. For everything 190 else, we fall back on repr(obj). 191 """ 192 if isinstance(obj, types.GenericAlias): 193 return repr(obj) 194 if isinstance(obj, type): 195 if obj.__module__ == 'builtins': 196 return obj.__qualname__ 197 return f'{obj.__module__}.{obj.__qualname__}' 198 if obj is ...: 199 return('...') 200 if isinstance(obj, types.FunctionType): 201 return obj.__name__ 202 return repr(obj) 203 204 205def _collect_type_vars(types_, typevar_types=None): 206 """Collect all type variable contained 207 in types in order of first appearance (lexicographic order). For example:: 208 209 _collect_type_vars((T, List[S, T])) == (T, S) 210 """ 211 if typevar_types is None: 212 typevar_types = TypeVar 213 tvars = [] 214 for t in types_: 215 if isinstance(t, typevar_types) and t not in tvars: 216 tvars.append(t) 217 if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)): 218 tvars.extend([t for t in t.__parameters__ if t not in tvars]) 219 return tuple(tvars) 220 221 222def _check_generic(cls, parameters, elen): 223 """Check correct count for parameters of a generic cls (internal helper). 224 This gives a nice error message in case of count mismatch. 225 """ 226 if not elen: 227 raise TypeError(f"{cls} is not a generic class") 228 alen = len(parameters) 229 if alen != elen: 230 raise TypeError(f"Too {'many' if alen > elen else 'few'} arguments for {cls};" 231 f" actual {alen}, expected {elen}") 232 233def _prepare_paramspec_params(cls, params): 234 """Prepares the parameters for a Generic containing ParamSpec 235 variables (internal helper). 236 """ 237 # Special case where Z[[int, str, bool]] == Z[int, str, bool] in PEP 612. 238 if (len(cls.__parameters__) == 1 239 and params and not _is_param_expr(params[0])): 240 assert isinstance(cls.__parameters__[0], ParamSpec) 241 return (params,) 242 else: 243 _check_generic(cls, params, len(cls.__parameters__)) 244 _params = [] 245 # Convert lists to tuples to help other libraries cache the results. 246 for p, tvar in zip(params, cls.__parameters__): 247 if isinstance(tvar, ParamSpec) and isinstance(p, list): 248 p = tuple(p) 249 _params.append(p) 250 return tuple(_params) 251 252def _deduplicate(params): 253 # Weed out strict duplicates, preserving the first of each occurrence. 254 all_params = set(params) 255 if len(all_params) < len(params): 256 new_params = [] 257 for t in params: 258 if t in all_params: 259 new_params.append(t) 260 all_params.remove(t) 261 params = new_params 262 assert not all_params, all_params 263 return params 264 265 266def _remove_dups_flatten(parameters): 267 """An internal helper for Union creation and substitution: flatten Unions 268 among parameters, then remove duplicates. 269 """ 270 # Flatten out Union[Union[...], ...]. 271 params = [] 272 for p in parameters: 273 if isinstance(p, (_UnionGenericAlias, types.UnionType)): 274 params.extend(p.__args__) 275 elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union: 276 params.extend(p[1:]) 277 else: 278 params.append(p) 279 280 return tuple(_deduplicate(params)) 281 282 283def _flatten_literal_params(parameters): 284 """An internal helper for Literal creation: flatten Literals among parameters""" 285 params = [] 286 for p in parameters: 287 if isinstance(p, _LiteralGenericAlias): 288 params.extend(p.__args__) 289 else: 290 params.append(p) 291 return tuple(params) 292 293 294_cleanups = [] 295 296 297def _tp_cache(func=None, /, *, typed=False): 298 """Internal wrapper caching __getitem__ of generic types with a fallback to 299 original function for non-hashable arguments. 300 """ 301 def decorator(func): 302 cached = functools.lru_cache(typed=typed)(func) 303 _cleanups.append(cached.cache_clear) 304 305 @functools.wraps(func) 306 def inner(*args, **kwds): 307 try: 308 return cached(*args, **kwds) 309 except TypeError: 310 pass # All real errors (not unhashable args) are raised below. 311 return func(*args, **kwds) 312 return inner 313 314 if func is not None: 315 return decorator(func) 316 317 return decorator 318 319def _eval_type(t, globalns, localns, recursive_guard=frozenset()): 320 """Evaluate all forward references in the given type t. 321 For use of globalns and localns see the docstring for get_type_hints(). 322 recursive_guard is used to prevent prevent infinite recursion 323 with recursive ForwardRef. 324 """ 325 if isinstance(t, ForwardRef): 326 return t._evaluate(globalns, localns, recursive_guard) 327 if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)): 328 ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__) 329 if ev_args == t.__args__: 330 return t 331 if isinstance(t, GenericAlias): 332 return GenericAlias(t.__origin__, ev_args) 333 if isinstance(t, types.UnionType): 334 return functools.reduce(operator.or_, ev_args) 335 else: 336 return t.copy_with(ev_args) 337 return t 338 339 340class _Final: 341 """Mixin to prohibit subclassing""" 342 343 __slots__ = ('__weakref__',) 344 345 def __init_subclass__(self, /, *args, **kwds): 346 if '_root' not in kwds: 347 raise TypeError("Cannot subclass special typing classes") 348 349class _Immutable: 350 """Mixin to indicate that object should not be copied.""" 351 __slots__ = () 352 353 def __copy__(self): 354 return self 355 356 def __deepcopy__(self, memo): 357 return self 358 359 360# Internal indicator of special typing constructs. 361# See __doc__ instance attribute for specific docs. 362class _SpecialForm(_Final, _root=True): 363 __slots__ = ('_name', '__doc__', '_getitem') 364 365 def __init__(self, getitem): 366 self._getitem = getitem 367 self._name = getitem.__name__ 368 self.__doc__ = getitem.__doc__ 369 370 def __getattr__(self, item): 371 if item in {'__name__', '__qualname__'}: 372 return self._name 373 374 raise AttributeError(item) 375 376 def __mro_entries__(self, bases): 377 raise TypeError(f"Cannot subclass {self!r}") 378 379 def __repr__(self): 380 return 'typing.' + self._name 381 382 def __reduce__(self): 383 return self._name 384 385 def __call__(self, *args, **kwds): 386 raise TypeError(f"Cannot instantiate {self!r}") 387 388 def __or__(self, other): 389 return Union[self, other] 390 391 def __ror__(self, other): 392 return Union[other, self] 393 394 def __instancecheck__(self, obj): 395 raise TypeError(f"{self} cannot be used with isinstance()") 396 397 def __subclasscheck__(self, cls): 398 raise TypeError(f"{self} cannot be used with issubclass()") 399 400 @_tp_cache 401 def __getitem__(self, parameters): 402 return self._getitem(self, parameters) 403 404 405class _LiteralSpecialForm(_SpecialForm, _root=True): 406 def __getitem__(self, parameters): 407 if not isinstance(parameters, tuple): 408 parameters = (parameters,) 409 return self._getitem(self, *parameters) 410 411 412@_SpecialForm 413def Any(self, parameters): 414 """Special type indicating an unconstrained type. 415 416 - Any is compatible with every type. 417 - Any assumed to have all methods. 418 - All values assumed to be instances of Any. 419 420 Note that all the above statements are true from the point of view of 421 static type checkers. At runtime, Any should not be used with instance 422 or class checks. 423 """ 424 raise TypeError(f"{self} is not subscriptable") 425 426@_SpecialForm 427def NoReturn(self, parameters): 428 """Special type indicating functions that never return. 429 Example:: 430 431 from typing import NoReturn 432 433 def stop() -> NoReturn: 434 raise Exception('no way') 435 436 This type is invalid in other positions, e.g., ``List[NoReturn]`` 437 will fail in static type checkers. 438 """ 439 raise TypeError(f"{self} is not subscriptable") 440 441@_SpecialForm 442def ClassVar(self, parameters): 443 """Special type construct to mark class variables. 444 445 An annotation wrapped in ClassVar indicates that a given 446 attribute is intended to be used as a class variable and 447 should not be set on instances of that class. Usage:: 448 449 class Starship: 450 stats: ClassVar[Dict[str, int]] = {} # class variable 451 damage: int = 10 # instance variable 452 453 ClassVar accepts only types and cannot be further subscribed. 454 455 Note that ClassVar is not a class itself, and should not 456 be used with isinstance() or issubclass(). 457 """ 458 item = _type_check(parameters, f'{self} accepts only single type.') 459 return _GenericAlias(self, (item,)) 460 461@_SpecialForm 462def Final(self, parameters): 463 """Special typing construct to indicate final names to type checkers. 464 465 A final name cannot be re-assigned or overridden in a subclass. 466 For example: 467 468 MAX_SIZE: Final = 9000 469 MAX_SIZE += 1 # Error reported by type checker 470 471 class Connection: 472 TIMEOUT: Final[int] = 10 473 474 class FastConnector(Connection): 475 TIMEOUT = 1 # Error reported by type checker 476 477 There is no runtime checking of these properties. 478 """ 479 item = _type_check(parameters, f'{self} accepts only single type.') 480 return _GenericAlias(self, (item,)) 481 482@_SpecialForm 483def Union(self, parameters): 484 """Union type; Union[X, Y] means either X or Y. 485 486 To define a union, use e.g. Union[int, str]. Details: 487 - The arguments must be types and there must be at least one. 488 - None as an argument is a special case and is replaced by 489 type(None). 490 - Unions of unions are flattened, e.g.:: 491 492 Union[Union[int, str], float] == Union[int, str, float] 493 494 - Unions of a single argument vanish, e.g.:: 495 496 Union[int] == int # The constructor actually returns int 497 498 - Redundant arguments are skipped, e.g.:: 499 500 Union[int, str, int] == Union[int, str] 501 502 - When comparing unions, the argument order is ignored, e.g.:: 503 504 Union[int, str] == Union[str, int] 505 506 - You cannot subclass or instantiate a union. 507 - You can use Optional[X] as a shorthand for Union[X, None]. 508 """ 509 if parameters == (): 510 raise TypeError("Cannot take a Union of no types.") 511 if not isinstance(parameters, tuple): 512 parameters = (parameters,) 513 msg = "Union[arg, ...]: each arg must be a type." 514 parameters = tuple(_type_check(p, msg) for p in parameters) 515 parameters = _remove_dups_flatten(parameters) 516 if len(parameters) == 1: 517 return parameters[0] 518 if len(parameters) == 2 and type(None) in parameters: 519 return _UnionGenericAlias(self, parameters, name="Optional") 520 return _UnionGenericAlias(self, parameters) 521 522@_SpecialForm 523def Optional(self, parameters): 524 """Optional type. 525 526 Optional[X] is equivalent to Union[X, None]. 527 """ 528 arg = _type_check(parameters, f"{self} requires a single type.") 529 return Union[arg, type(None)] 530 531@_LiteralSpecialForm 532@_tp_cache(typed=True) 533def Literal(self, *parameters): 534 """Special typing form to define literal types (a.k.a. value types). 535 536 This form can be used to indicate to type checkers that the corresponding 537 variable or function parameter has a value equivalent to the provided 538 literal (or one of several literals): 539 540 def validate_simple(data: Any) -> Literal[True]: # always returns True 541 ... 542 543 MODE = Literal['r', 'rb', 'w', 'wb'] 544 def open_helper(file: str, mode: MODE) -> str: 545 ... 546 547 open_helper('/some/path', 'r') # Passes type check 548 open_helper('/other/path', 'typo') # Error in type checker 549 550 Literal[...] cannot be subclassed. At runtime, an arbitrary value 551 is allowed as type argument to Literal[...], but type checkers may 552 impose restrictions. 553 """ 554 # There is no '_type_check' call because arguments to Literal[...] are 555 # values, not types. 556 parameters = _flatten_literal_params(parameters) 557 558 try: 559 parameters = tuple(p for p, _ in _deduplicate(list(_value_and_type_iter(parameters)))) 560 except TypeError: # unhashable parameters 561 pass 562 563 return _LiteralGenericAlias(self, parameters) 564 565 566@_SpecialForm 567def TypeAlias(self, parameters): 568 """Special marker indicating that an assignment should 569 be recognized as a proper type alias definition by type 570 checkers. 571 572 For example:: 573 574 Predicate: TypeAlias = Callable[..., bool] 575 576 It's invalid when used anywhere except as in the example above. 577 """ 578 raise TypeError(f"{self} is not subscriptable") 579 580 581@_SpecialForm 582def Concatenate(self, parameters): 583 """Used in conjunction with ``ParamSpec`` and ``Callable`` to represent a 584 higher order function which adds, removes or transforms parameters of a 585 callable. 586 587 For example:: 588 589 Callable[Concatenate[int, P], int] 590 591 See PEP 612 for detailed information. 592 """ 593 if parameters == (): 594 raise TypeError("Cannot take a Concatenate of no types.") 595 if not isinstance(parameters, tuple): 596 parameters = (parameters,) 597 if not isinstance(parameters[-1], ParamSpec): 598 raise TypeError("The last parameter to Concatenate should be a " 599 "ParamSpec variable.") 600 msg = "Concatenate[arg, ...]: each arg must be a type." 601 parameters = tuple(_type_check(p, msg) for p in parameters) 602 return _ConcatenateGenericAlias(self, parameters) 603 604 605@_SpecialForm 606def TypeGuard(self, parameters): 607 """Special typing form used to annotate the return type of a user-defined 608 type guard function. ``TypeGuard`` only accepts a single type argument. 609 At runtime, functions marked this way should return a boolean. 610 611 ``TypeGuard`` aims to benefit *type narrowing* -- a technique used by static 612 type checkers to determine a more precise type of an expression within a 613 program's code flow. Usually type narrowing is done by analyzing 614 conditional code flow and applying the narrowing to a block of code. The 615 conditional expression here is sometimes referred to as a "type guard". 616 617 Sometimes it would be convenient to use a user-defined boolean function 618 as a type guard. Such a function should use ``TypeGuard[...]`` as its 619 return type to alert static type checkers to this intention. 620 621 Using ``-> TypeGuard`` tells the static type checker that for a given 622 function: 623 624 1. The return value is a boolean. 625 2. If the return value is ``True``, the type of its argument 626 is the type inside ``TypeGuard``. 627 628 For example:: 629 630 def is_str(val: Union[str, float]): 631 # "isinstance" type guard 632 if isinstance(val, str): 633 # Type of ``val`` is narrowed to ``str`` 634 ... 635 else: 636 # Else, type of ``val`` is narrowed to ``float``. 637 ... 638 639 Strict type narrowing is not enforced -- ``TypeB`` need not be a narrower 640 form of ``TypeA`` (it can even be a wider form) and this may lead to 641 type-unsafe results. The main reason is to allow for things like 642 narrowing ``List[object]`` to ``List[str]`` even though the latter is not 643 a subtype of the former, since ``List`` is invariant. The responsibility of 644 writing type-safe type guards is left to the user. 645 646 ``TypeGuard`` also works with type variables. For more information, see 647 PEP 647 (User-Defined Type Guards). 648 """ 649 item = _type_check(parameters, f'{self} accepts only single type.') 650 return _GenericAlias(self, (item,)) 651 652 653class ForwardRef(_Final, _root=True): 654 """Internal wrapper to hold a forward reference.""" 655 656 __slots__ = ('__forward_arg__', '__forward_code__', 657 '__forward_evaluated__', '__forward_value__', 658 '__forward_is_argument__', '__forward_is_class__', 659 '__forward_module__') 660 661 def __init__(self, arg, is_argument=True, module=None, *, is_class=False): 662 if not isinstance(arg, str): 663 raise TypeError(f"Forward reference must be a string -- got {arg!r}") 664 try: 665 code = compile(arg, '<string>', 'eval') 666 except SyntaxError: 667 raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}") 668 self.__forward_arg__ = arg 669 self.__forward_code__ = code 670 self.__forward_evaluated__ = False 671 self.__forward_value__ = None 672 self.__forward_is_argument__ = is_argument 673 self.__forward_is_class__ = is_class 674 self.__forward_module__ = module 675 676 def _evaluate(self, globalns, localns, recursive_guard): 677 if self.__forward_arg__ in recursive_guard: 678 return self 679 if not self.__forward_evaluated__ or localns is not globalns: 680 if globalns is None and localns is None: 681 globalns = localns = {} 682 elif globalns is None: 683 globalns = localns 684 elif localns is None: 685 localns = globalns 686 if self.__forward_module__ is not None: 687 globalns = getattr( 688 sys.modules.get(self.__forward_module__, None), '__dict__', globalns 689 ) 690 type_ = _type_check( 691 eval(self.__forward_code__, globalns, localns), 692 "Forward references must evaluate to types.", 693 is_argument=self.__forward_is_argument__, 694 is_class=self.__forward_is_class__, 695 ) 696 self.__forward_value__ = _eval_type( 697 type_, globalns, localns, recursive_guard | {self.__forward_arg__} 698 ) 699 self.__forward_evaluated__ = True 700 return self.__forward_value__ 701 702 def __eq__(self, other): 703 if not isinstance(other, ForwardRef): 704 return NotImplemented 705 if self.__forward_evaluated__ and other.__forward_evaluated__: 706 return (self.__forward_arg__ == other.__forward_arg__ and 707 self.__forward_value__ == other.__forward_value__) 708 return self.__forward_arg__ == other.__forward_arg__ 709 710 def __hash__(self): 711 return hash(self.__forward_arg__) 712 713 def __repr__(self): 714 return f'ForwardRef({self.__forward_arg__!r})' 715 716class _TypeVarLike: 717 """Mixin for TypeVar-like types (TypeVar and ParamSpec).""" 718 def __init__(self, bound, covariant, contravariant): 719 """Used to setup TypeVars and ParamSpec's bound, covariant and 720 contravariant attributes. 721 """ 722 if covariant and contravariant: 723 raise ValueError("Bivariant types are not supported.") 724 self.__covariant__ = bool(covariant) 725 self.__contravariant__ = bool(contravariant) 726 if bound: 727 self.__bound__ = _type_check(bound, "Bound must be a type.") 728 else: 729 self.__bound__ = None 730 731 def __or__(self, right): 732 return Union[self, right] 733 734 def __ror__(self, left): 735 return Union[left, self] 736 737 def __repr__(self): 738 if self.__covariant__: 739 prefix = '+' 740 elif self.__contravariant__: 741 prefix = '-' 742 else: 743 prefix = '~' 744 return prefix + self.__name__ 745 746 def __reduce__(self): 747 return self.__name__ 748 749 750class TypeVar( _Final, _Immutable, _TypeVarLike, _root=True): 751 """Type variable. 752 753 Usage:: 754 755 T = TypeVar('T') # Can be anything 756 A = TypeVar('A', str, bytes) # Must be str or bytes 757 758 Type variables exist primarily for the benefit of static type 759 checkers. They serve as the parameters for generic types as well 760 as for generic function definitions. See class Generic for more 761 information on generic types. Generic functions work as follows: 762 763 def repeat(x: T, n: int) -> List[T]: 764 '''Return a list containing n references to x.''' 765 return [x]*n 766 767 def longest(x: A, y: A) -> A: 768 '''Return the longest of two strings.''' 769 return x if len(x) >= len(y) else y 770 771 The latter example's signature is essentially the overloading 772 of (str, str) -> str and (bytes, bytes) -> bytes. Also note 773 that if the arguments are instances of some subclass of str, 774 the return type is still plain str. 775 776 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError. 777 778 Type variables defined with covariant=True or contravariant=True 779 can be used to declare covariant or contravariant generic types. 780 See PEP 484 for more details. By default generic types are invariant 781 in all type variables. 782 783 Type variables can be introspected. e.g.: 784 785 T.__name__ == 'T' 786 T.__constraints__ == () 787 T.__covariant__ == False 788 T.__contravariant__ = False 789 A.__constraints__ == (str, bytes) 790 791 Note that only type variables defined in global scope can be pickled. 792 """ 793 794 __slots__ = ('__name__', '__bound__', '__constraints__', 795 '__covariant__', '__contravariant__', '__dict__') 796 797 def __init__(self, name, *constraints, bound=None, 798 covariant=False, contravariant=False): 799 self.__name__ = name 800 super().__init__(bound, covariant, contravariant) 801 if constraints and bound is not None: 802 raise TypeError("Constraints cannot be combined with bound=...") 803 if constraints and len(constraints) == 1: 804 raise TypeError("A single constraint is not allowed") 805 msg = "TypeVar(name, constraint, ...): constraints must be types." 806 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints) 807 try: 808 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling 809 except (AttributeError, ValueError): 810 def_mod = None 811 if def_mod != 'typing': 812 self.__module__ = def_mod 813 814 815class ParamSpecArgs(_Final, _Immutable, _root=True): 816 """The args for a ParamSpec object. 817 818 Given a ParamSpec object P, P.args is an instance of ParamSpecArgs. 819 820 ParamSpecArgs objects have a reference back to their ParamSpec: 821 822 P.args.__origin__ is P 823 824 This type is meant for runtime introspection and has no special meaning to 825 static type checkers. 826 """ 827 def __init__(self, origin): 828 self.__origin__ = origin 829 830 def __repr__(self): 831 return f"{self.__origin__.__name__}.args" 832 833 834class ParamSpecKwargs(_Final, _Immutable, _root=True): 835 """The kwargs for a ParamSpec object. 836 837 Given a ParamSpec object P, P.kwargs is an instance of ParamSpecKwargs. 838 839 ParamSpecKwargs objects have a reference back to their ParamSpec: 840 841 P.kwargs.__origin__ is P 842 843 This type is meant for runtime introspection and has no special meaning to 844 static type checkers. 845 """ 846 def __init__(self, origin): 847 self.__origin__ = origin 848 849 def __repr__(self): 850 return f"{self.__origin__.__name__}.kwargs" 851 852 853class ParamSpec(_Final, _Immutable, _TypeVarLike, _root=True): 854 """Parameter specification variable. 855 856 Usage:: 857 858 P = ParamSpec('P') 859 860 Parameter specification variables exist primarily for the benefit of static 861 type checkers. They are used to forward the parameter types of one 862 callable to another callable, a pattern commonly found in higher order 863 functions and decorators. They are only valid when used in ``Concatenate``, 864 or s the first argument to ``Callable``, or as parameters for user-defined 865 Generics. See class Generic for more information on generic types. An 866 example for annotating a decorator:: 867 868 T = TypeVar('T') 869 P = ParamSpec('P') 870 871 def add_logging(f: Callable[P, T]) -> Callable[P, T]: 872 '''A type-safe decorator to add logging to a function.''' 873 def inner(*args: P.args, **kwargs: P.kwargs) -> T: 874 logging.info(f'{f.__name__} was called') 875 return f(*args, **kwargs) 876 return inner 877 878 @add_logging 879 def add_two(x: float, y: float) -> float: 880 '''Add two numbers together.''' 881 return x + y 882 883 Parameter specification variables defined with covariant=True or 884 contravariant=True can be used to declare covariant or contravariant 885 generic types. These keyword arguments are valid, but their actual semantics 886 are yet to be decided. See PEP 612 for details. 887 888 Parameter specification variables can be introspected. e.g.: 889 890 P.__name__ == 'T' 891 P.__bound__ == None 892 P.__covariant__ == False 893 P.__contravariant__ == False 894 895 Note that only parameter specification variables defined in global scope can 896 be pickled. 897 """ 898 899 __slots__ = ('__name__', '__bound__', '__covariant__', '__contravariant__', 900 '__dict__') 901 902 @property 903 def args(self): 904 return ParamSpecArgs(self) 905 906 @property 907 def kwargs(self): 908 return ParamSpecKwargs(self) 909 910 def __init__(self, name, *, bound=None, covariant=False, contravariant=False): 911 self.__name__ = name 912 super().__init__(bound, covariant, contravariant) 913 try: 914 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') 915 except (AttributeError, ValueError): 916 def_mod = None 917 if def_mod != 'typing': 918 self.__module__ = def_mod 919 920 921def _is_dunder(attr): 922 return attr.startswith('__') and attr.endswith('__') 923 924class _BaseGenericAlias(_Final, _root=True): 925 """The central part of internal API. 926 927 This represents a generic version of type 'origin' with type arguments 'params'. 928 There are two kind of these aliases: user defined and special. The special ones 929 are wrappers around builtin collections and ABCs in collections.abc. These must 930 have 'name' always set. If 'inst' is False, then the alias can't be instantiated, 931 this is used by e.g. typing.List and typing.Dict. 932 """ 933 def __init__(self, origin, *, inst=True, name=None): 934 self._inst = inst 935 self._name = name 936 self.__origin__ = origin 937 self.__slots__ = None # This is not documented. 938 939 def __call__(self, *args, **kwargs): 940 if not self._inst: 941 raise TypeError(f"Type {self._name} cannot be instantiated; " 942 f"use {self.__origin__.__name__}() instead") 943 result = self.__origin__(*args, **kwargs) 944 try: 945 result.__orig_class__ = self 946 except AttributeError: 947 pass 948 return result 949 950 def __mro_entries__(self, bases): 951 res = [] 952 if self.__origin__ not in bases: 953 res.append(self.__origin__) 954 i = bases.index(self) 955 for b in bases[i+1:]: 956 if isinstance(b, _BaseGenericAlias) or issubclass(b, Generic): 957 break 958 else: 959 res.append(Generic) 960 return tuple(res) 961 962 def __getattr__(self, attr): 963 if attr in {'__name__', '__qualname__'}: 964 return self._name or self.__origin__.__name__ 965 966 # We are careful for copy and pickle. 967 # Also for simplicity we just don't relay all dunder names 968 if '__origin__' in self.__dict__ and not _is_dunder(attr): 969 return getattr(self.__origin__, attr) 970 raise AttributeError(attr) 971 972 def __setattr__(self, attr, val): 973 if _is_dunder(attr) or attr in {'_name', '_inst', '_nparams', 974 '_typevar_types', '_paramspec_tvars'}: 975 super().__setattr__(attr, val) 976 else: 977 setattr(self.__origin__, attr, val) 978 979 def __instancecheck__(self, obj): 980 return self.__subclasscheck__(type(obj)) 981 982 def __subclasscheck__(self, cls): 983 raise TypeError("Subscripted generics cannot be used with" 984 " class and instance checks") 985 986 def __dir__(self): 987 return list(set(super().__dir__() 988 + [attr for attr in dir(self.__origin__) if not _is_dunder(attr)])) 989 990# Special typing constructs Union, Optional, Generic, Callable and Tuple 991# use three special attributes for internal bookkeeping of generic types: 992# * __parameters__ is a tuple of unique free type parameters of a generic 993# type, for example, Dict[T, T].__parameters__ == (T,); 994# * __origin__ keeps a reference to a type that was subscripted, 995# e.g., Union[T, int].__origin__ == Union, or the non-generic version of 996# the type. 997# * __args__ is a tuple of all arguments used in subscripting, 998# e.g., Dict[T, int].__args__ == (T, int). 999 1000 1001class _GenericAlias(_BaseGenericAlias, _root=True): 1002 def __init__(self, origin, params, *, inst=True, name=None, 1003 _typevar_types=TypeVar, 1004 _paramspec_tvars=False): 1005 super().__init__(origin, inst=inst, name=name) 1006 if not isinstance(params, tuple): 1007 params = (params,) 1008 self.__args__ = tuple(... if a is _TypingEllipsis else 1009 () if a is _TypingEmpty else 1010 a for a in params) 1011 self.__parameters__ = _collect_type_vars(params, typevar_types=_typevar_types) 1012 self._typevar_types = _typevar_types 1013 self._paramspec_tvars = _paramspec_tvars 1014 if not name: 1015 self.__module__ = origin.__module__ 1016 1017 def __eq__(self, other): 1018 if not isinstance(other, _GenericAlias): 1019 return NotImplemented 1020 return (self.__origin__ == other.__origin__ 1021 and self.__args__ == other.__args__) 1022 1023 def __hash__(self): 1024 return hash((self.__origin__, self.__args__)) 1025 1026 def __or__(self, right): 1027 return Union[self, right] 1028 1029 def __ror__(self, left): 1030 return Union[left, self] 1031 1032 @_tp_cache 1033 def __getitem__(self, params): 1034 if self.__origin__ in (Generic, Protocol): 1035 # Can't subscript Generic[...] or Protocol[...]. 1036 raise TypeError(f"Cannot subscript already-subscripted {self}") 1037 if not isinstance(params, tuple): 1038 params = (params,) 1039 params = tuple(_type_convert(p) for p in params) 1040 if (self._paramspec_tvars 1041 and any(isinstance(t, ParamSpec) for t in self.__parameters__)): 1042 params = _prepare_paramspec_params(self, params) 1043 else: 1044 _check_generic(self, params, len(self.__parameters__)) 1045 1046 subst = dict(zip(self.__parameters__, params)) 1047 new_args = [] 1048 for arg in self.__args__: 1049 if isinstance(arg, self._typevar_types): 1050 if isinstance(arg, ParamSpec): 1051 arg = subst[arg] 1052 if not _is_param_expr(arg): 1053 raise TypeError(f"Expected a list of types, an ellipsis, " 1054 f"ParamSpec, or Concatenate. Got {arg}") 1055 else: 1056 arg = subst[arg] 1057 elif isinstance(arg, (_GenericAlias, GenericAlias, types.UnionType)): 1058 subparams = arg.__parameters__ 1059 if subparams: 1060 subargs = tuple(subst[x] for x in subparams) 1061 arg = arg[subargs] 1062 # Required to flatten out the args for CallableGenericAlias 1063 if self.__origin__ == collections.abc.Callable and isinstance(arg, tuple): 1064 new_args.extend(arg) 1065 else: 1066 new_args.append(arg) 1067 return self.copy_with(tuple(new_args)) 1068 1069 def copy_with(self, params): 1070 return self.__class__(self.__origin__, params, name=self._name, inst=self._inst) 1071 1072 def __repr__(self): 1073 if self._name: 1074 name = 'typing.' + self._name 1075 else: 1076 name = _type_repr(self.__origin__) 1077 args = ", ".join([_type_repr(a) for a in self.__args__]) 1078 return f'{name}[{args}]' 1079 1080 def __reduce__(self): 1081 if self._name: 1082 origin = globals()[self._name] 1083 else: 1084 origin = self.__origin__ 1085 args = tuple(self.__args__) 1086 if len(args) == 1 and not isinstance(args[0], tuple): 1087 args, = args 1088 return operator.getitem, (origin, args) 1089 1090 def __mro_entries__(self, bases): 1091 if isinstance(self.__origin__, _SpecialForm): 1092 raise TypeError(f"Cannot subclass {self!r}") 1093 1094 if self._name: # generic version of an ABC or built-in class 1095 return super().__mro_entries__(bases) 1096 if self.__origin__ is Generic: 1097 if Protocol in bases: 1098 return () 1099 i = bases.index(self) 1100 for b in bases[i+1:]: 1101 if isinstance(b, _BaseGenericAlias) and b is not self: 1102 return () 1103 return (self.__origin__,) 1104 1105 1106# _nparams is the number of accepted parameters, e.g. 0 for Hashable, 1107# 1 for List and 2 for Dict. It may be -1 if variable number of 1108# parameters are accepted (needs custom __getitem__). 1109 1110class _SpecialGenericAlias(_BaseGenericAlias, _root=True): 1111 def __init__(self, origin, nparams, *, inst=True, name=None): 1112 if name is None: 1113 name = origin.__name__ 1114 super().__init__(origin, inst=inst, name=name) 1115 self._nparams = nparams 1116 if origin.__module__ == 'builtins': 1117 self.__doc__ = f'A generic version of {origin.__qualname__}.' 1118 else: 1119 self.__doc__ = f'A generic version of {origin.__module__}.{origin.__qualname__}.' 1120 1121 @_tp_cache 1122 def __getitem__(self, params): 1123 if not isinstance(params, tuple): 1124 params = (params,) 1125 msg = "Parameters to generic types must be types." 1126 params = tuple(_type_check(p, msg) for p in params) 1127 _check_generic(self, params, self._nparams) 1128 return self.copy_with(params) 1129 1130 def copy_with(self, params): 1131 return _GenericAlias(self.__origin__, params, 1132 name=self._name, inst=self._inst) 1133 1134 def __repr__(self): 1135 return 'typing.' + self._name 1136 1137 def __subclasscheck__(self, cls): 1138 if isinstance(cls, _SpecialGenericAlias): 1139 return issubclass(cls.__origin__, self.__origin__) 1140 if not isinstance(cls, _GenericAlias): 1141 return issubclass(cls, self.__origin__) 1142 return super().__subclasscheck__(cls) 1143 1144 def __reduce__(self): 1145 return self._name 1146 1147 def __or__(self, right): 1148 return Union[self, right] 1149 1150 def __ror__(self, left): 1151 return Union[left, self] 1152 1153class _CallableGenericAlias(_GenericAlias, _root=True): 1154 def __repr__(self): 1155 assert self._name == 'Callable' 1156 args = self.__args__ 1157 if len(args) == 2 and _is_param_expr(args[0]): 1158 return super().__repr__() 1159 return (f'typing.Callable' 1160 f'[[{", ".join([_type_repr(a) for a in args[:-1]])}], ' 1161 f'{_type_repr(args[-1])}]') 1162 1163 def __reduce__(self): 1164 args = self.__args__ 1165 if not (len(args) == 2 and _is_param_expr(args[0])): 1166 args = list(args[:-1]), args[-1] 1167 return operator.getitem, (Callable, args) 1168 1169 1170class _CallableType(_SpecialGenericAlias, _root=True): 1171 def copy_with(self, params): 1172 return _CallableGenericAlias(self.__origin__, params, 1173 name=self._name, inst=self._inst, 1174 _typevar_types=(TypeVar, ParamSpec), 1175 _paramspec_tvars=True) 1176 1177 def __getitem__(self, params): 1178 if not isinstance(params, tuple) or len(params) != 2: 1179 raise TypeError("Callable must be used as " 1180 "Callable[[arg, ...], result].") 1181 args, result = params 1182 # This relaxes what args can be on purpose to allow things like 1183 # PEP 612 ParamSpec. Responsibility for whether a user is using 1184 # Callable[...] properly is deferred to static type checkers. 1185 if isinstance(args, list): 1186 params = (tuple(args), result) 1187 else: 1188 params = (args, result) 1189 return self.__getitem_inner__(params) 1190 1191 @_tp_cache 1192 def __getitem_inner__(self, params): 1193 args, result = params 1194 msg = "Callable[args, result]: result must be a type." 1195 result = _type_check(result, msg) 1196 if args is Ellipsis: 1197 return self.copy_with((_TypingEllipsis, result)) 1198 if not isinstance(args, tuple): 1199 args = (args,) 1200 args = tuple(_type_convert(arg) for arg in args) 1201 params = args + (result,) 1202 return self.copy_with(params) 1203 1204 1205class _TupleType(_SpecialGenericAlias, _root=True): 1206 @_tp_cache 1207 def __getitem__(self, params): 1208 if params == (): 1209 return self.copy_with((_TypingEmpty,)) 1210 if not isinstance(params, tuple): 1211 params = (params,) 1212 if len(params) == 2 and params[1] is ...: 1213 msg = "Tuple[t, ...]: t must be a type." 1214 p = _type_check(params[0], msg) 1215 return self.copy_with((p, _TypingEllipsis)) 1216 msg = "Tuple[t0, t1, ...]: each t must be a type." 1217 params = tuple(_type_check(p, msg) for p in params) 1218 return self.copy_with(params) 1219 1220 1221class _UnionGenericAlias(_GenericAlias, _root=True): 1222 def copy_with(self, params): 1223 return Union[params] 1224 1225 def __eq__(self, other): 1226 if not isinstance(other, (_UnionGenericAlias, types.UnionType)): 1227 return NotImplemented 1228 return set(self.__args__) == set(other.__args__) 1229 1230 def __hash__(self): 1231 return hash(frozenset(self.__args__)) 1232 1233 def __repr__(self): 1234 args = self.__args__ 1235 if len(args) == 2: 1236 if args[0] is type(None): 1237 return f'typing.Optional[{_type_repr(args[1])}]' 1238 elif args[1] is type(None): 1239 return f'typing.Optional[{_type_repr(args[0])}]' 1240 return super().__repr__() 1241 1242 def __instancecheck__(self, obj): 1243 return self.__subclasscheck__(type(obj)) 1244 1245 def __subclasscheck__(self, cls): 1246 for arg in self.__args__: 1247 if issubclass(cls, arg): 1248 return True 1249 1250 def __reduce__(self): 1251 func, (origin, args) = super().__reduce__() 1252 return func, (Union, args) 1253 1254 1255def _value_and_type_iter(parameters): 1256 return ((p, type(p)) for p in parameters) 1257 1258 1259class _LiteralGenericAlias(_GenericAlias, _root=True): 1260 1261 def __eq__(self, other): 1262 if not isinstance(other, _LiteralGenericAlias): 1263 return NotImplemented 1264 1265 return set(_value_and_type_iter(self.__args__)) == set(_value_and_type_iter(other.__args__)) 1266 1267 def __hash__(self): 1268 return hash(frozenset(_value_and_type_iter(self.__args__))) 1269 1270 1271class _ConcatenateGenericAlias(_GenericAlias, _root=True): 1272 def __init__(self, *args, **kwargs): 1273 super().__init__(*args, **kwargs, 1274 _typevar_types=(TypeVar, ParamSpec), 1275 _paramspec_tvars=True) 1276 1277 1278class Generic: 1279 """Abstract base class for generic types. 1280 1281 A generic type is typically declared by inheriting from 1282 this class parameterized with one or more type variables. 1283 For example, a generic mapping type might be defined as:: 1284 1285 class Mapping(Generic[KT, VT]): 1286 def __getitem__(self, key: KT) -> VT: 1287 ... 1288 # Etc. 1289 1290 This class can then be used as follows:: 1291 1292 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT: 1293 try: 1294 return mapping[key] 1295 except KeyError: 1296 return default 1297 """ 1298 __slots__ = () 1299 _is_protocol = False 1300 1301 @_tp_cache 1302 def __class_getitem__(cls, params): 1303 if not isinstance(params, tuple): 1304 params = (params,) 1305 if not params and cls is not Tuple: 1306 raise TypeError( 1307 f"Parameter list to {cls.__qualname__}[...] cannot be empty") 1308 params = tuple(_type_convert(p) for p in params) 1309 if cls in (Generic, Protocol): 1310 # Generic and Protocol can only be subscripted with unique type variables. 1311 if not all(isinstance(p, (TypeVar, ParamSpec)) for p in params): 1312 raise TypeError( 1313 f"Parameters to {cls.__name__}[...] must all be type variables " 1314 f"or parameter specification variables.") 1315 if len(set(params)) != len(params): 1316 raise TypeError( 1317 f"Parameters to {cls.__name__}[...] must all be unique") 1318 else: 1319 # Subscripting a regular Generic subclass. 1320 if any(isinstance(t, ParamSpec) for t in cls.__parameters__): 1321 params = _prepare_paramspec_params(cls, params) 1322 else: 1323 _check_generic(cls, params, len(cls.__parameters__)) 1324 return _GenericAlias(cls, params, 1325 _typevar_types=(TypeVar, ParamSpec), 1326 _paramspec_tvars=True) 1327 1328 def __init_subclass__(cls, *args, **kwargs): 1329 super().__init_subclass__(*args, **kwargs) 1330 tvars = [] 1331 if '__orig_bases__' in cls.__dict__: 1332 error = Generic in cls.__orig_bases__ 1333 else: 1334 error = Generic in cls.__bases__ and cls.__name__ != 'Protocol' 1335 if error: 1336 raise TypeError("Cannot inherit from plain Generic") 1337 if '__orig_bases__' in cls.__dict__: 1338 tvars = _collect_type_vars(cls.__orig_bases__, (TypeVar, ParamSpec)) 1339 # Look for Generic[T1, ..., Tn]. 1340 # If found, tvars must be a subset of it. 1341 # If not found, tvars is it. 1342 # Also check for and reject plain Generic, 1343 # and reject multiple Generic[...]. 1344 gvars = None 1345 for base in cls.__orig_bases__: 1346 if (isinstance(base, _GenericAlias) and 1347 base.__origin__ is Generic): 1348 if gvars is not None: 1349 raise TypeError( 1350 "Cannot inherit from Generic[...] multiple types.") 1351 gvars = base.__parameters__ 1352 if gvars is not None: 1353 tvarset = set(tvars) 1354 gvarset = set(gvars) 1355 if not tvarset <= gvarset: 1356 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset) 1357 s_args = ', '.join(str(g) for g in gvars) 1358 raise TypeError(f"Some type variables ({s_vars}) are" 1359 f" not listed in Generic[{s_args}]") 1360 tvars = gvars 1361 cls.__parameters__ = tuple(tvars) 1362 1363 1364class _TypingEmpty: 1365 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta 1366 to allow empty list/tuple in specific places, without allowing them 1367 to sneak in where prohibited. 1368 """ 1369 1370 1371class _TypingEllipsis: 1372 """Internal placeholder for ... (ellipsis).""" 1373 1374 1375_TYPING_INTERNALS = ['__parameters__', '__orig_bases__', '__orig_class__', 1376 '_is_protocol', '_is_runtime_protocol'] 1377 1378_SPECIAL_NAMES = ['__abstractmethods__', '__annotations__', '__dict__', '__doc__', 1379 '__init__', '__module__', '__new__', '__slots__', 1380 '__subclasshook__', '__weakref__', '__class_getitem__'] 1381 1382# These special attributes will be not collected as protocol members. 1383EXCLUDED_ATTRIBUTES = _TYPING_INTERNALS + _SPECIAL_NAMES + ['_MutableMapping__marker'] 1384 1385 1386def _get_protocol_attrs(cls): 1387 """Collect protocol members from a protocol class objects. 1388 1389 This includes names actually defined in the class dictionary, as well 1390 as names that appear in annotations. Special names (above) are skipped. 1391 """ 1392 attrs = set() 1393 for base in cls.__mro__[:-1]: # without object 1394 if base.__name__ in ('Protocol', 'Generic'): 1395 continue 1396 annotations = getattr(base, '__annotations__', {}) 1397 for attr in list(base.__dict__.keys()) + list(annotations.keys()): 1398 if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES: 1399 attrs.add(attr) 1400 return attrs 1401 1402 1403def _is_callable_members_only(cls): 1404 # PEP 544 prohibits using issubclass() with protocols that have non-method members. 1405 return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls)) 1406 1407 1408def _no_init_or_replace_init(self, *args, **kwargs): 1409 cls = type(self) 1410 1411 if cls._is_protocol: 1412 raise TypeError('Protocols cannot be instantiated') 1413 1414 # Already using a custom `__init__`. No need to calculate correct 1415 # `__init__` to call. This can lead to RecursionError. See bpo-45121. 1416 if cls.__init__ is not _no_init_or_replace_init: 1417 return 1418 1419 # Initially, `__init__` of a protocol subclass is set to `_no_init_or_replace_init`. 1420 # The first instantiation of the subclass will call `_no_init_or_replace_init` which 1421 # searches for a proper new `__init__` in the MRO. The new `__init__` 1422 # replaces the subclass' old `__init__` (ie `_no_init_or_replace_init`). Subsequent 1423 # instantiation of the protocol subclass will thus use the new 1424 # `__init__` and no longer call `_no_init_or_replace_init`. 1425 for base in cls.__mro__: 1426 init = base.__dict__.get('__init__', _no_init_or_replace_init) 1427 if init is not _no_init_or_replace_init: 1428 cls.__init__ = init 1429 break 1430 else: 1431 # should not happen 1432 cls.__init__ = object.__init__ 1433 1434 cls.__init__(self, *args, **kwargs) 1435 1436 1437def _caller(depth=1, default='__main__'): 1438 try: 1439 return sys._getframe(depth + 1).f_globals.get('__name__', default) 1440 except (AttributeError, ValueError): # For platforms without _getframe() 1441 return None 1442 1443 1444def _allow_reckless_class_checks(depth=3): 1445 """Allow instance and class checks for special stdlib modules. 1446 1447 The abc and functools modules indiscriminately call isinstance() and 1448 issubclass() on the whole MRO of a user class, which may contain protocols. 1449 """ 1450 try: 1451 return sys._getframe(depth).f_globals['__name__'] in ['abc', 'functools'] 1452 except (AttributeError, ValueError): # For platforms without _getframe(). 1453 return True 1454 1455 1456_PROTO_ALLOWLIST = { 1457 'collections.abc': [ 1458 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable', 1459 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible', 1460 ], 1461 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'], 1462} 1463 1464 1465class _ProtocolMeta(ABCMeta): 1466 # This metaclass is really unfortunate and exists only because of 1467 # the lack of __instancehook__. 1468 def __instancecheck__(cls, instance): 1469 # We need this method for situations where attributes are 1470 # assigned in __init__. 1471 if ( 1472 getattr(cls, '_is_protocol', False) and 1473 not getattr(cls, '_is_runtime_protocol', False) and 1474 not _allow_reckless_class_checks(depth=2) 1475 ): 1476 raise TypeError("Instance and class checks can only be used with" 1477 " @runtime_checkable protocols") 1478 1479 if ((not getattr(cls, '_is_protocol', False) or 1480 _is_callable_members_only(cls)) and 1481 issubclass(instance.__class__, cls)): 1482 return True 1483 if cls._is_protocol: 1484 if all(hasattr(instance, attr) and 1485 # All *methods* can be blocked by setting them to None. 1486 (not callable(getattr(cls, attr, None)) or 1487 getattr(instance, attr) is not None) 1488 for attr in _get_protocol_attrs(cls)): 1489 return True 1490 return super().__instancecheck__(instance) 1491 1492 1493class Protocol(Generic, metaclass=_ProtocolMeta): 1494 """Base class for protocol classes. 1495 1496 Protocol classes are defined as:: 1497 1498 class Proto(Protocol): 1499 def meth(self) -> int: 1500 ... 1501 1502 Such classes are primarily used with static type checkers that recognize 1503 structural subtyping (static duck-typing), for example:: 1504 1505 class C: 1506 def meth(self) -> int: 1507 return 0 1508 1509 def func(x: Proto) -> int: 1510 return x.meth() 1511 1512 func(C()) # Passes static type check 1513 1514 See PEP 544 for details. Protocol classes decorated with 1515 @typing.runtime_checkable act as simple-minded runtime protocols that check 1516 only the presence of given attributes, ignoring their type signatures. 1517 Protocol classes can be generic, they are defined as:: 1518 1519 class GenProto(Protocol[T]): 1520 def meth(self) -> T: 1521 ... 1522 """ 1523 __slots__ = () 1524 _is_protocol = True 1525 _is_runtime_protocol = False 1526 1527 def __init_subclass__(cls, *args, **kwargs): 1528 super().__init_subclass__(*args, **kwargs) 1529 1530 # Determine if this is a protocol or a concrete subclass. 1531 if not cls.__dict__.get('_is_protocol', False): 1532 cls._is_protocol = any(b is Protocol for b in cls.__bases__) 1533 1534 # Set (or override) the protocol subclass hook. 1535 def _proto_hook(other): 1536 if not cls.__dict__.get('_is_protocol', False): 1537 return NotImplemented 1538 1539 # First, perform various sanity checks. 1540 if not getattr(cls, '_is_runtime_protocol', False): 1541 if _allow_reckless_class_checks(): 1542 return NotImplemented 1543 raise TypeError("Instance and class checks can only be used with" 1544 " @runtime_checkable protocols") 1545 if not _is_callable_members_only(cls): 1546 if _allow_reckless_class_checks(): 1547 return NotImplemented 1548 raise TypeError("Protocols with non-method members" 1549 " don't support issubclass()") 1550 if not isinstance(other, type): 1551 # Same error message as for issubclass(1, int). 1552 raise TypeError('issubclass() arg 1 must be a class') 1553 1554 # Second, perform the actual structural compatibility check. 1555 for attr in _get_protocol_attrs(cls): 1556 for base in other.__mro__: 1557 # Check if the members appears in the class dictionary... 1558 if attr in base.__dict__: 1559 if base.__dict__[attr] is None: 1560 return NotImplemented 1561 break 1562 1563 # ...or in annotations, if it is a sub-protocol. 1564 annotations = getattr(base, '__annotations__', {}) 1565 if (isinstance(annotations, collections.abc.Mapping) and 1566 attr in annotations and 1567 issubclass(other, Generic) and other._is_protocol): 1568 break 1569 else: 1570 return NotImplemented 1571 return True 1572 1573 if '__subclasshook__' not in cls.__dict__: 1574 cls.__subclasshook__ = _proto_hook 1575 1576 # We have nothing more to do for non-protocols... 1577 if not cls._is_protocol: 1578 return 1579 1580 # ... otherwise check consistency of bases, and prohibit instantiation. 1581 for base in cls.__bases__: 1582 if not (base in (object, Generic) or 1583 base.__module__ in _PROTO_ALLOWLIST and 1584 base.__name__ in _PROTO_ALLOWLIST[base.__module__] or 1585 issubclass(base, Generic) and base._is_protocol): 1586 raise TypeError('Protocols can only inherit from other' 1587 ' protocols, got %r' % base) 1588 cls.__init__ = _no_init_or_replace_init 1589 1590 1591class _AnnotatedAlias(_GenericAlias, _root=True): 1592 """Runtime representation of an annotated type. 1593 1594 At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't' 1595 with extra annotations. The alias behaves like a normal typing alias, 1596 instantiating is the same as instantiating the underlying type, binding 1597 it to types is also the same. 1598 """ 1599 def __init__(self, origin, metadata): 1600 if isinstance(origin, _AnnotatedAlias): 1601 metadata = origin.__metadata__ + metadata 1602 origin = origin.__origin__ 1603 super().__init__(origin, origin) 1604 self.__metadata__ = metadata 1605 1606 def copy_with(self, params): 1607 assert len(params) == 1 1608 new_type = params[0] 1609 return _AnnotatedAlias(new_type, self.__metadata__) 1610 1611 def __repr__(self): 1612 return "typing.Annotated[{}, {}]".format( 1613 _type_repr(self.__origin__), 1614 ", ".join(repr(a) for a in self.__metadata__) 1615 ) 1616 1617 def __reduce__(self): 1618 return operator.getitem, ( 1619 Annotated, (self.__origin__,) + self.__metadata__ 1620 ) 1621 1622 def __eq__(self, other): 1623 if not isinstance(other, _AnnotatedAlias): 1624 return NotImplemented 1625 return (self.__origin__ == other.__origin__ 1626 and self.__metadata__ == other.__metadata__) 1627 1628 def __hash__(self): 1629 return hash((self.__origin__, self.__metadata__)) 1630 1631 def __getattr__(self, attr): 1632 if attr in {'__name__', '__qualname__'}: 1633 return 'Annotated' 1634 return super().__getattr__(attr) 1635 1636 1637class Annotated: 1638 """Add context specific metadata to a type. 1639 1640 Example: Annotated[int, runtime_check.Unsigned] indicates to the 1641 hypothetical runtime_check module that this type is an unsigned int. 1642 Every other consumer of this type can ignore this metadata and treat 1643 this type as int. 1644 1645 The first argument to Annotated must be a valid type. 1646 1647 Details: 1648 1649 - It's an error to call `Annotated` with less than two arguments. 1650 - Nested Annotated are flattened:: 1651 1652 Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3] 1653 1654 - Instantiating an annotated type is equivalent to instantiating the 1655 underlying type:: 1656 1657 Annotated[C, Ann1](5) == C(5) 1658 1659 - Annotated can be used as a generic type alias:: 1660 1661 Optimized = Annotated[T, runtime.Optimize()] 1662 Optimized[int] == Annotated[int, runtime.Optimize()] 1663 1664 OptimizedList = Annotated[List[T], runtime.Optimize()] 1665 OptimizedList[int] == Annotated[List[int], runtime.Optimize()] 1666 """ 1667 1668 __slots__ = () 1669 1670 def __new__(cls, *args, **kwargs): 1671 raise TypeError("Type Annotated cannot be instantiated.") 1672 1673 @_tp_cache 1674 def __class_getitem__(cls, params): 1675 if not isinstance(params, tuple) or len(params) < 2: 1676 raise TypeError("Annotated[...] should be used " 1677 "with at least two arguments (a type and an " 1678 "annotation).") 1679 msg = "Annotated[t, ...]: t must be a type." 1680 origin = _type_check(params[0], msg) 1681 metadata = tuple(params[1:]) 1682 return _AnnotatedAlias(origin, metadata) 1683 1684 def __init_subclass__(cls, *args, **kwargs): 1685 raise TypeError( 1686 "Cannot subclass {}.Annotated".format(cls.__module__) 1687 ) 1688 1689 1690def runtime_checkable(cls): 1691 """Mark a protocol class as a runtime protocol. 1692 1693 Such protocol can be used with isinstance() and issubclass(). 1694 Raise TypeError if applied to a non-protocol class. 1695 This allows a simple-minded structural check very similar to 1696 one trick ponies in collections.abc such as Iterable. 1697 For example:: 1698 1699 @runtime_checkable 1700 class Closable(Protocol): 1701 def close(self): ... 1702 1703 assert isinstance(open('/some/file'), Closable) 1704 1705 Warning: this will check only the presence of the required methods, 1706 not their type signatures! 1707 """ 1708 if not issubclass(cls, Generic) or not cls._is_protocol: 1709 raise TypeError('@runtime_checkable can be only applied to protocol classes,' 1710 ' got %r' % cls) 1711 cls._is_runtime_protocol = True 1712 return cls 1713 1714 1715def cast(typ, val): 1716 """Cast a value to a type. 1717 1718 This returns the value unchanged. To the type checker this 1719 signals that the return value has the designated type, but at 1720 runtime we intentionally don't check anything (we want this 1721 to be as fast as possible). 1722 """ 1723 return val 1724 1725 1726def _get_defaults(func): 1727 """Internal helper to extract the default arguments, by name.""" 1728 try: 1729 code = func.__code__ 1730 except AttributeError: 1731 # Some built-in functions don't have __code__, __defaults__, etc. 1732 return {} 1733 pos_count = code.co_argcount 1734 arg_names = code.co_varnames 1735 arg_names = arg_names[:pos_count] 1736 defaults = func.__defaults__ or () 1737 kwdefaults = func.__kwdefaults__ 1738 res = dict(kwdefaults) if kwdefaults else {} 1739 pos_offset = pos_count - len(defaults) 1740 for name, value in zip(arg_names[pos_offset:], defaults): 1741 assert name not in res 1742 res[name] = value 1743 return res 1744 1745 1746_allowed_types = (types.FunctionType, types.BuiltinFunctionType, 1747 types.MethodType, types.ModuleType, 1748 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType) 1749 1750 1751def get_type_hints(obj, globalns=None, localns=None, include_extras=False): 1752 """Return type hints for an object. 1753 1754 This is often the same as obj.__annotations__, but it handles 1755 forward references encoded as string literals, adds Optional[t] if a 1756 default value equal to None is set and recursively replaces all 1757 'Annotated[T, ...]' with 'T' (unless 'include_extras=True'). 1758 1759 The argument may be a module, class, method, or function. The annotations 1760 are returned as a dictionary. For classes, annotations include also 1761 inherited members. 1762 1763 TypeError is raised if the argument is not of a type that can contain 1764 annotations, and an empty dictionary is returned if no annotations are 1765 present. 1766 1767 BEWARE -- the behavior of globalns and localns is counterintuitive 1768 (unless you are familiar with how eval() and exec() work). The 1769 search order is locals first, then globals. 1770 1771 - If no dict arguments are passed, an attempt is made to use the 1772 globals from obj (or the respective module's globals for classes), 1773 and these are also used as the locals. If the object does not appear 1774 to have globals, an empty dictionary is used. For classes, the search 1775 order is globals first then locals. 1776 1777 - If one dict argument is passed, it is used for both globals and 1778 locals. 1779 1780 - If two dict arguments are passed, they specify globals and 1781 locals, respectively. 1782 """ 1783 1784 if getattr(obj, '__no_type_check__', None): 1785 return {} 1786 # Classes require a special treatment. 1787 if isinstance(obj, type): 1788 hints = {} 1789 for base in reversed(obj.__mro__): 1790 if globalns is None: 1791 base_globals = getattr(sys.modules.get(base.__module__, None), '__dict__', {}) 1792 else: 1793 base_globals = globalns 1794 ann = base.__dict__.get('__annotations__', {}) 1795 if isinstance(ann, types.GetSetDescriptorType): 1796 ann = {} 1797 base_locals = dict(vars(base)) if localns is None else localns 1798 if localns is None and globalns is None: 1799 # This is surprising, but required. Before Python 3.10, 1800 # get_type_hints only evaluated the globalns of 1801 # a class. To maintain backwards compatibility, we reverse 1802 # the globalns and localns order so that eval() looks into 1803 # *base_globals* first rather than *base_locals*. 1804 # This only affects ForwardRefs. 1805 base_globals, base_locals = base_locals, base_globals 1806 for name, value in ann.items(): 1807 if value is None: 1808 value = type(None) 1809 if isinstance(value, str): 1810 value = ForwardRef(value, is_argument=False, is_class=True) 1811 value = _eval_type(value, base_globals, base_locals) 1812 hints[name] = value 1813 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()} 1814 1815 if globalns is None: 1816 if isinstance(obj, types.ModuleType): 1817 globalns = obj.__dict__ 1818 else: 1819 nsobj = obj 1820 # Find globalns for the unwrapped object. 1821 while hasattr(nsobj, '__wrapped__'): 1822 nsobj = nsobj.__wrapped__ 1823 globalns = getattr(nsobj, '__globals__', {}) 1824 if localns is None: 1825 localns = globalns 1826 elif localns is None: 1827 localns = globalns 1828 hints = getattr(obj, '__annotations__', None) 1829 if hints is None: 1830 # Return empty annotations for something that _could_ have them. 1831 if isinstance(obj, _allowed_types): 1832 return {} 1833 else: 1834 raise TypeError('{!r} is not a module, class, method, ' 1835 'or function.'.format(obj)) 1836 defaults = _get_defaults(obj) 1837 hints = dict(hints) 1838 for name, value in hints.items(): 1839 if value is None: 1840 value = type(None) 1841 if isinstance(value, str): 1842 # class-level forward refs were handled above, this must be either 1843 # a module-level annotation or a function argument annotation 1844 value = ForwardRef( 1845 value, 1846 is_argument=not isinstance(obj, types.ModuleType), 1847 is_class=False, 1848 ) 1849 value = _eval_type(value, globalns, localns) 1850 if name in defaults and defaults[name] is None: 1851 value = Optional[value] 1852 hints[name] = value 1853 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()} 1854 1855 1856def _strip_annotations(t): 1857 """Strips the annotations from a given type. 1858 """ 1859 if isinstance(t, _AnnotatedAlias): 1860 return _strip_annotations(t.__origin__) 1861 if isinstance(t, _GenericAlias): 1862 stripped_args = tuple(_strip_annotations(a) for a in t.__args__) 1863 if stripped_args == t.__args__: 1864 return t 1865 return t.copy_with(stripped_args) 1866 if isinstance(t, GenericAlias): 1867 stripped_args = tuple(_strip_annotations(a) for a in t.__args__) 1868 if stripped_args == t.__args__: 1869 return t 1870 return GenericAlias(t.__origin__, stripped_args) 1871 if isinstance(t, types.UnionType): 1872 stripped_args = tuple(_strip_annotations(a) for a in t.__args__) 1873 if stripped_args == t.__args__: 1874 return t 1875 return functools.reduce(operator.or_, stripped_args) 1876 1877 return t 1878 1879 1880def get_origin(tp): 1881 """Get the unsubscripted version of a type. 1882 1883 This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar 1884 and Annotated. Return None for unsupported types. Examples:: 1885 1886 get_origin(Literal[42]) is Literal 1887 get_origin(int) is None 1888 get_origin(ClassVar[int]) is ClassVar 1889 get_origin(Generic) is Generic 1890 get_origin(Generic[T]) is Generic 1891 get_origin(Union[T, int]) is Union 1892 get_origin(List[Tuple[T, T]][int]) == list 1893 get_origin(P.args) is P 1894 """ 1895 if isinstance(tp, _AnnotatedAlias): 1896 return Annotated 1897 if isinstance(tp, (_BaseGenericAlias, GenericAlias, 1898 ParamSpecArgs, ParamSpecKwargs)): 1899 return tp.__origin__ 1900 if tp is Generic: 1901 return Generic 1902 if isinstance(tp, types.UnionType): 1903 return types.UnionType 1904 return None 1905 1906 1907def get_args(tp): 1908 """Get type arguments with all substitutions performed. 1909 1910 For unions, basic simplifications used by Union constructor are performed. 1911 Examples:: 1912 get_args(Dict[str, int]) == (str, int) 1913 get_args(int) == () 1914 get_args(Union[int, Union[T, int], str][int]) == (int, str) 1915 get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int]) 1916 get_args(Callable[[], T][int]) == ([], int) 1917 """ 1918 if isinstance(tp, _AnnotatedAlias): 1919 return (tp.__origin__,) + tp.__metadata__ 1920 if isinstance(tp, (_GenericAlias, GenericAlias)): 1921 res = tp.__args__ 1922 if (tp.__origin__ is collections.abc.Callable 1923 and not (len(res) == 2 and _is_param_expr(res[0]))): 1924 res = (list(res[:-1]), res[-1]) 1925 return res 1926 if isinstance(tp, types.UnionType): 1927 return tp.__args__ 1928 return () 1929 1930 1931def is_typeddict(tp): 1932 """Check if an annotation is a TypedDict class 1933 1934 For example:: 1935 class Film(TypedDict): 1936 title: str 1937 year: int 1938 1939 is_typeddict(Film) # => True 1940 is_typeddict(Union[list, str]) # => False 1941 """ 1942 return isinstance(tp, _TypedDictMeta) 1943 1944 1945def no_type_check(arg): 1946 """Decorator to indicate that annotations are not type hints. 1947 1948 The argument must be a class or function; if it is a class, it 1949 applies recursively to all methods and classes defined in that class 1950 (but not to methods defined in its superclasses or subclasses). 1951 1952 This mutates the function(s) or class(es) in place. 1953 """ 1954 if isinstance(arg, type): 1955 arg_attrs = arg.__dict__.copy() 1956 for attr, val in arg.__dict__.items(): 1957 if val in arg.__bases__ + (arg,): 1958 arg_attrs.pop(attr) 1959 for obj in arg_attrs.values(): 1960 if isinstance(obj, types.FunctionType): 1961 obj.__no_type_check__ = True 1962 if isinstance(obj, type): 1963 no_type_check(obj) 1964 try: 1965 arg.__no_type_check__ = True 1966 except TypeError: # built-in classes 1967 pass 1968 return arg 1969 1970 1971def no_type_check_decorator(decorator): 1972 """Decorator to give another decorator the @no_type_check effect. 1973 1974 This wraps the decorator with something that wraps the decorated 1975 function in @no_type_check. 1976 """ 1977 1978 @functools.wraps(decorator) 1979 def wrapped_decorator(*args, **kwds): 1980 func = decorator(*args, **kwds) 1981 func = no_type_check(func) 1982 return func 1983 1984 return wrapped_decorator 1985 1986 1987def _overload_dummy(*args, **kwds): 1988 """Helper for @overload to raise when called.""" 1989 raise NotImplementedError( 1990 "You should not call an overloaded function. " 1991 "A series of @overload-decorated functions " 1992 "outside a stub module should always be followed " 1993 "by an implementation that is not @overload-ed.") 1994 1995 1996def overload(func): 1997 """Decorator for overloaded functions/methods. 1998 1999 In a stub file, place two or more stub definitions for the same 2000 function in a row, each decorated with @overload. For example: 2001 2002 @overload 2003 def utf8(value: None) -> None: ... 2004 @overload 2005 def utf8(value: bytes) -> bytes: ... 2006 @overload 2007 def utf8(value: str) -> bytes: ... 2008 2009 In a non-stub file (i.e. a regular .py file), do the same but 2010 follow it with an implementation. The implementation should *not* 2011 be decorated with @overload. For example: 2012 2013 @overload 2014 def utf8(value: None) -> None: ... 2015 @overload 2016 def utf8(value: bytes) -> bytes: ... 2017 @overload 2018 def utf8(value: str) -> bytes: ... 2019 def utf8(value): 2020 # implementation goes here 2021 """ 2022 return _overload_dummy 2023 2024 2025def final(f): 2026 """A decorator to indicate final methods and final classes. 2027 2028 Use this decorator to indicate to type checkers that the decorated 2029 method cannot be overridden, and decorated class cannot be subclassed. 2030 For example: 2031 2032 class Base: 2033 @final 2034 def done(self) -> None: 2035 ... 2036 class Sub(Base): 2037 def done(self) -> None: # Error reported by type checker 2038 ... 2039 2040 @final 2041 class Leaf: 2042 ... 2043 class Other(Leaf): # Error reported by type checker 2044 ... 2045 2046 There is no runtime checking of these properties. 2047 """ 2048 return f 2049 2050 2051# Some unconstrained type variables. These are used by the container types. 2052# (These are not for export.) 2053T = TypeVar('T') # Any type. 2054KT = TypeVar('KT') # Key type. 2055VT = TypeVar('VT') # Value type. 2056T_co = TypeVar('T_co', covariant=True) # Any type covariant containers. 2057V_co = TypeVar('V_co', covariant=True) # Any type covariant containers. 2058VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers. 2059T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant. 2060# Internal type variable used for Type[]. 2061CT_co = TypeVar('CT_co', covariant=True, bound=type) 2062 2063# A useful type variable with constraints. This represents string types. 2064# (This one *is* for export!) 2065AnyStr = TypeVar('AnyStr', bytes, str) 2066 2067 2068# Various ABCs mimicking those in collections.abc. 2069_alias = _SpecialGenericAlias 2070 2071Hashable = _alias(collections.abc.Hashable, 0) # Not generic. 2072Awaitable = _alias(collections.abc.Awaitable, 1) 2073Coroutine = _alias(collections.abc.Coroutine, 3) 2074AsyncIterable = _alias(collections.abc.AsyncIterable, 1) 2075AsyncIterator = _alias(collections.abc.AsyncIterator, 1) 2076Iterable = _alias(collections.abc.Iterable, 1) 2077Iterator = _alias(collections.abc.Iterator, 1) 2078Reversible = _alias(collections.abc.Reversible, 1) 2079Sized = _alias(collections.abc.Sized, 0) # Not generic. 2080Container = _alias(collections.abc.Container, 1) 2081Collection = _alias(collections.abc.Collection, 1) 2082Callable = _CallableType(collections.abc.Callable, 2) 2083Callable.__doc__ = \ 2084 """Callable type; Callable[[int], str] is a function of (int) -> str. 2085 2086 The subscription syntax must always be used with exactly two 2087 values: the argument list and the return type. The argument list 2088 must be a list of types or ellipsis; the return type must be a single type. 2089 2090 There is no syntax to indicate optional or keyword arguments, 2091 such function types are rarely used as callback types. 2092 """ 2093AbstractSet = _alias(collections.abc.Set, 1, name='AbstractSet') 2094MutableSet = _alias(collections.abc.MutableSet, 1) 2095# NOTE: Mapping is only covariant in the value type. 2096Mapping = _alias(collections.abc.Mapping, 2) 2097MutableMapping = _alias(collections.abc.MutableMapping, 2) 2098Sequence = _alias(collections.abc.Sequence, 1) 2099MutableSequence = _alias(collections.abc.MutableSequence, 1) 2100ByteString = _alias(collections.abc.ByteString, 0) # Not generic 2101# Tuple accepts variable number of parameters. 2102Tuple = _TupleType(tuple, -1, inst=False, name='Tuple') 2103Tuple.__doc__ = \ 2104 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y. 2105 2106 Example: Tuple[T1, T2] is a tuple of two elements corresponding 2107 to type variables T1 and T2. Tuple[int, float, str] is a tuple 2108 of an int, a float and a string. 2109 2110 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...]. 2111 """ 2112List = _alias(list, 1, inst=False, name='List') 2113Deque = _alias(collections.deque, 1, name='Deque') 2114Set = _alias(set, 1, inst=False, name='Set') 2115FrozenSet = _alias(frozenset, 1, inst=False, name='FrozenSet') 2116MappingView = _alias(collections.abc.MappingView, 1) 2117KeysView = _alias(collections.abc.KeysView, 1) 2118ItemsView = _alias(collections.abc.ItemsView, 2) 2119ValuesView = _alias(collections.abc.ValuesView, 1) 2120ContextManager = _alias(contextlib.AbstractContextManager, 1, name='ContextManager') 2121AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, 1, name='AsyncContextManager') 2122Dict = _alias(dict, 2, inst=False, name='Dict') 2123DefaultDict = _alias(collections.defaultdict, 2, name='DefaultDict') 2124OrderedDict = _alias(collections.OrderedDict, 2) 2125Counter = _alias(collections.Counter, 1) 2126ChainMap = _alias(collections.ChainMap, 2) 2127Generator = _alias(collections.abc.Generator, 3) 2128AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2) 2129Type = _alias(type, 1, inst=False, name='Type') 2130Type.__doc__ = \ 2131 """A special construct usable to annotate class objects. 2132 2133 For example, suppose we have the following classes:: 2134 2135 class User: ... # Abstract base for User classes 2136 class BasicUser(User): ... 2137 class ProUser(User): ... 2138 class TeamUser(User): ... 2139 2140 And a function that takes a class argument that's a subclass of 2141 User and returns an instance of the corresponding class:: 2142 2143 U = TypeVar('U', bound=User) 2144 def new_user(user_class: Type[U]) -> U: 2145 user = user_class() 2146 # (Here we could write the user object to a database) 2147 return user 2148 2149 joe = new_user(BasicUser) 2150 2151 At this point the type checker knows that joe has type BasicUser. 2152 """ 2153 2154 2155@runtime_checkable 2156class SupportsInt(Protocol): 2157 """An ABC with one abstract method __int__.""" 2158 __slots__ = () 2159 2160 @abstractmethod 2161 def __int__(self) -> int: 2162 pass 2163 2164 2165@runtime_checkable 2166class SupportsFloat(Protocol): 2167 """An ABC with one abstract method __float__.""" 2168 __slots__ = () 2169 2170 @abstractmethod 2171 def __float__(self) -> float: 2172 pass 2173 2174 2175@runtime_checkable 2176class SupportsComplex(Protocol): 2177 """An ABC with one abstract method __complex__.""" 2178 __slots__ = () 2179 2180 @abstractmethod 2181 def __complex__(self) -> complex: 2182 pass 2183 2184 2185@runtime_checkable 2186class SupportsBytes(Protocol): 2187 """An ABC with one abstract method __bytes__.""" 2188 __slots__ = () 2189 2190 @abstractmethod 2191 def __bytes__(self) -> bytes: 2192 pass 2193 2194 2195@runtime_checkable 2196class SupportsIndex(Protocol): 2197 """An ABC with one abstract method __index__.""" 2198 __slots__ = () 2199 2200 @abstractmethod 2201 def __index__(self) -> int: 2202 pass 2203 2204 2205@runtime_checkable 2206class SupportsAbs(Protocol[T_co]): 2207 """An ABC with one abstract method __abs__ that is covariant in its return type.""" 2208 __slots__ = () 2209 2210 @abstractmethod 2211 def __abs__(self) -> T_co: 2212 pass 2213 2214 2215@runtime_checkable 2216class SupportsRound(Protocol[T_co]): 2217 """An ABC with one abstract method __round__ that is covariant in its return type.""" 2218 __slots__ = () 2219 2220 @abstractmethod 2221 def __round__(self, ndigits: int = 0) -> T_co: 2222 pass 2223 2224 2225def _make_nmtuple(name, types, module, defaults = ()): 2226 fields = [n for n, t in types] 2227 types = {n: _type_check(t, f"field {n} annotation must be a type") 2228 for n, t in types} 2229 nm_tpl = collections.namedtuple(name, fields, 2230 defaults=defaults, module=module) 2231 nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types 2232 return nm_tpl 2233 2234 2235# attributes prohibited to set in NamedTuple class syntax 2236_prohibited = frozenset({'__new__', '__init__', '__slots__', '__getnewargs__', 2237 '_fields', '_field_defaults', 2238 '_make', '_replace', '_asdict', '_source'}) 2239 2240_special = frozenset({'__module__', '__name__', '__annotations__'}) 2241 2242 2243class NamedTupleMeta(type): 2244 2245 def __new__(cls, typename, bases, ns): 2246 assert bases[0] is _NamedTuple 2247 types = ns.get('__annotations__', {}) 2248 default_names = [] 2249 for field_name in types: 2250 if field_name in ns: 2251 default_names.append(field_name) 2252 elif default_names: 2253 raise TypeError(f"Non-default namedtuple field {field_name} " 2254 f"cannot follow default field" 2255 f"{'s' if len(default_names) > 1 else ''} " 2256 f"{', '.join(default_names)}") 2257 nm_tpl = _make_nmtuple(typename, types.items(), 2258 defaults=[ns[n] for n in default_names], 2259 module=ns['__module__']) 2260 # update from user namespace without overriding special namedtuple attributes 2261 for key in ns: 2262 if key in _prohibited: 2263 raise AttributeError("Cannot overwrite NamedTuple attribute " + key) 2264 elif key not in _special and key not in nm_tpl._fields: 2265 setattr(nm_tpl, key, ns[key]) 2266 return nm_tpl 2267 2268 2269def NamedTuple(typename, fields=None, /, **kwargs): 2270 """Typed version of namedtuple. 2271 2272 Usage in Python versions >= 3.6:: 2273 2274 class Employee(NamedTuple): 2275 name: str 2276 id: int 2277 2278 This is equivalent to:: 2279 2280 Employee = collections.namedtuple('Employee', ['name', 'id']) 2281 2282 The resulting class has an extra __annotations__ attribute, giving a 2283 dict that maps field names to types. (The field names are also in 2284 the _fields attribute, which is part of the namedtuple API.) 2285 Alternative equivalent keyword syntax is also accepted:: 2286 2287 Employee = NamedTuple('Employee', name=str, id=int) 2288 2289 In Python versions <= 3.5 use:: 2290 2291 Employee = NamedTuple('Employee', [('name', str), ('id', int)]) 2292 """ 2293 if fields is None: 2294 fields = kwargs.items() 2295 elif kwargs: 2296 raise TypeError("Either list of fields or keywords" 2297 " can be provided to NamedTuple, not both") 2298 try: 2299 module = sys._getframe(1).f_globals.get('__name__', '__main__') 2300 except (AttributeError, ValueError): 2301 module = None 2302 return _make_nmtuple(typename, fields, module=module) 2303 2304_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {}) 2305 2306def _namedtuple_mro_entries(bases): 2307 if len(bases) > 1: 2308 raise TypeError("Multiple inheritance with NamedTuple is not supported") 2309 assert bases[0] is NamedTuple 2310 return (_NamedTuple,) 2311 2312NamedTuple.__mro_entries__ = _namedtuple_mro_entries 2313 2314 2315class _TypedDictMeta(type): 2316 def __new__(cls, name, bases, ns, total=True): 2317 """Create new typed dict class object. 2318 2319 This method is called when TypedDict is subclassed, 2320 or when TypedDict is instantiated. This way 2321 TypedDict supports all three syntax forms described in its docstring. 2322 Subclasses and instances of TypedDict return actual dictionaries. 2323 """ 2324 for base in bases: 2325 if type(base) is not _TypedDictMeta: 2326 raise TypeError('cannot inherit from both a TypedDict type ' 2327 'and a non-TypedDict base class') 2328 tp_dict = type.__new__(_TypedDictMeta, name, (dict,), ns) 2329 2330 annotations = {} 2331 own_annotations = ns.get('__annotations__', {}) 2332 own_annotation_keys = set(own_annotations.keys()) 2333 msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type" 2334 own_annotations = { 2335 n: _type_check(tp, msg, module=tp_dict.__module__) 2336 for n, tp in own_annotations.items() 2337 } 2338 required_keys = set() 2339 optional_keys = set() 2340 2341 for base in bases: 2342 annotations.update(base.__dict__.get('__annotations__', {})) 2343 required_keys.update(base.__dict__.get('__required_keys__', ())) 2344 optional_keys.update(base.__dict__.get('__optional_keys__', ())) 2345 2346 annotations.update(own_annotations) 2347 if total: 2348 required_keys.update(own_annotation_keys) 2349 else: 2350 optional_keys.update(own_annotation_keys) 2351 2352 tp_dict.__annotations__ = annotations 2353 tp_dict.__required_keys__ = frozenset(required_keys) 2354 tp_dict.__optional_keys__ = frozenset(optional_keys) 2355 if not hasattr(tp_dict, '__total__'): 2356 tp_dict.__total__ = total 2357 return tp_dict 2358 2359 __call__ = dict # static method 2360 2361 def __subclasscheck__(cls, other): 2362 # Typed dicts are only for static structural subtyping. 2363 raise TypeError('TypedDict does not support instance and class checks') 2364 2365 __instancecheck__ = __subclasscheck__ 2366 2367 2368def TypedDict(typename, fields=None, /, *, total=True, **kwargs): 2369 """A simple typed namespace. At runtime it is equivalent to a plain dict. 2370 2371 TypedDict creates a dictionary type that expects all of its 2372 instances to have a certain set of keys, where each key is 2373 associated with a value of a consistent type. This expectation 2374 is not checked at runtime but is only enforced by type checkers. 2375 Usage:: 2376 2377 class Point2D(TypedDict): 2378 x: int 2379 y: int 2380 label: str 2381 2382 a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK 2383 b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check 2384 2385 assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first') 2386 2387 The type info can be accessed via the Point2D.__annotations__ dict, and 2388 the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets. 2389 TypedDict supports two additional equivalent forms:: 2390 2391 Point2D = TypedDict('Point2D', x=int, y=int, label=str) 2392 Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str}) 2393 2394 By default, all keys must be present in a TypedDict. It is possible 2395 to override this by specifying totality. 2396 Usage:: 2397 2398 class point2D(TypedDict, total=False): 2399 x: int 2400 y: int 2401 2402 This means that a point2D TypedDict can have any of the keys omitted.A type 2403 checker is only expected to support a literal False or True as the value of 2404 the total argument. True is the default, and makes all items defined in the 2405 class body be required. 2406 2407 The class syntax is only supported in Python 3.6+, while two other 2408 syntax forms work for Python 2.7 and 3.2+ 2409 """ 2410 if fields is None: 2411 fields = kwargs 2412 elif kwargs: 2413 raise TypeError("TypedDict takes either a dict or keyword arguments," 2414 " but not both") 2415 2416 ns = {'__annotations__': dict(fields)} 2417 try: 2418 # Setting correct module is necessary to make typed dict classes pickleable. 2419 ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__') 2420 except (AttributeError, ValueError): 2421 pass 2422 2423 return _TypedDictMeta(typename, (), ns, total=total) 2424 2425_TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {}) 2426TypedDict.__mro_entries__ = lambda bases: (_TypedDict,) 2427 2428 2429class NewType: 2430 """NewType creates simple unique types with almost zero 2431 runtime overhead. NewType(name, tp) is considered a subtype of tp 2432 by static type checkers. At runtime, NewType(name, tp) returns 2433 a dummy callable that simply returns its argument. Usage:: 2434 2435 UserId = NewType('UserId', int) 2436 2437 def name_by_id(user_id: UserId) -> str: 2438 ... 2439 2440 UserId('user') # Fails type check 2441 2442 name_by_id(42) # Fails type check 2443 name_by_id(UserId(42)) # OK 2444 2445 num = UserId(5) + 1 # type: int 2446 """ 2447 2448 def __init__(self, name, tp): 2449 self.__qualname__ = name 2450 if '.' in name: 2451 name = name.rpartition('.')[-1] 2452 self.__name__ = name 2453 self.__supertype__ = tp 2454 def_mod = _caller() 2455 if def_mod != 'typing': 2456 self.__module__ = def_mod 2457 2458 def __repr__(self): 2459 return f'{self.__module__}.{self.__qualname__}' 2460 2461 def __call__(self, x): 2462 return x 2463 2464 def __reduce__(self): 2465 return self.__qualname__ 2466 2467 def __or__(self, other): 2468 return Union[self, other] 2469 2470 def __ror__(self, other): 2471 return Union[other, self] 2472 2473 2474# Python-version-specific alias (Python 2: unicode; Python 3: str) 2475Text = str 2476 2477 2478# Constant that's True when type checking, but False here. 2479TYPE_CHECKING = False 2480 2481 2482class IO(Generic[AnyStr]): 2483 """Generic base class for TextIO and BinaryIO. 2484 2485 This is an abstract, generic version of the return of open(). 2486 2487 NOTE: This does not distinguish between the different possible 2488 classes (text vs. binary, read vs. write vs. read/write, 2489 append-only, unbuffered). The TextIO and BinaryIO subclasses 2490 below capture the distinctions between text vs. binary, which is 2491 pervasive in the interface; however we currently do not offer a 2492 way to track the other distinctions in the type system. 2493 """ 2494 2495 __slots__ = () 2496 2497 @property 2498 @abstractmethod 2499 def mode(self) -> str: 2500 pass 2501 2502 @property 2503 @abstractmethod 2504 def name(self) -> str: 2505 pass 2506 2507 @abstractmethod 2508 def close(self) -> None: 2509 pass 2510 2511 @property 2512 @abstractmethod 2513 def closed(self) -> bool: 2514 pass 2515 2516 @abstractmethod 2517 def fileno(self) -> int: 2518 pass 2519 2520 @abstractmethod 2521 def flush(self) -> None: 2522 pass 2523 2524 @abstractmethod 2525 def isatty(self) -> bool: 2526 pass 2527 2528 @abstractmethod 2529 def read(self, n: int = -1) -> AnyStr: 2530 pass 2531 2532 @abstractmethod 2533 def readable(self) -> bool: 2534 pass 2535 2536 @abstractmethod 2537 def readline(self, limit: int = -1) -> AnyStr: 2538 pass 2539 2540 @abstractmethod 2541 def readlines(self, hint: int = -1) -> List[AnyStr]: 2542 pass 2543 2544 @abstractmethod 2545 def seek(self, offset: int, whence: int = 0) -> int: 2546 pass 2547 2548 @abstractmethod 2549 def seekable(self) -> bool: 2550 pass 2551 2552 @abstractmethod 2553 def tell(self) -> int: 2554 pass 2555 2556 @abstractmethod 2557 def truncate(self, size: int = None) -> int: 2558 pass 2559 2560 @abstractmethod 2561 def writable(self) -> bool: 2562 pass 2563 2564 @abstractmethod 2565 def write(self, s: AnyStr) -> int: 2566 pass 2567 2568 @abstractmethod 2569 def writelines(self, lines: List[AnyStr]) -> None: 2570 pass 2571 2572 @abstractmethod 2573 def __enter__(self) -> 'IO[AnyStr]': 2574 pass 2575 2576 @abstractmethod 2577 def __exit__(self, type, value, traceback) -> None: 2578 pass 2579 2580 2581class BinaryIO(IO[bytes]): 2582 """Typed version of the return of open() in binary mode.""" 2583 2584 __slots__ = () 2585 2586 @abstractmethod 2587 def write(self, s: Union[bytes, bytearray]) -> int: 2588 pass 2589 2590 @abstractmethod 2591 def __enter__(self) -> 'BinaryIO': 2592 pass 2593 2594 2595class TextIO(IO[str]): 2596 """Typed version of the return of open() in text mode.""" 2597 2598 __slots__ = () 2599 2600 @property 2601 @abstractmethod 2602 def buffer(self) -> BinaryIO: 2603 pass 2604 2605 @property 2606 @abstractmethod 2607 def encoding(self) -> str: 2608 pass 2609 2610 @property 2611 @abstractmethod 2612 def errors(self) -> Optional[str]: 2613 pass 2614 2615 @property 2616 @abstractmethod 2617 def line_buffering(self) -> bool: 2618 pass 2619 2620 @property 2621 @abstractmethod 2622 def newlines(self) -> Any: 2623 pass 2624 2625 @abstractmethod 2626 def __enter__(self) -> 'TextIO': 2627 pass 2628 2629 2630class io: 2631 """Wrapper namespace for IO generic classes.""" 2632 2633 __all__ = ['IO', 'TextIO', 'BinaryIO'] 2634 IO = IO 2635 TextIO = TextIO 2636 BinaryIO = BinaryIO 2637 2638 2639io.__name__ = __name__ + '.io' 2640sys.modules[io.__name__] = io 2641 2642Pattern = _alias(stdlib_re.Pattern, 1) 2643Match = _alias(stdlib_re.Match, 1) 2644 2645class re: 2646 """Wrapper namespace for re type aliases.""" 2647 2648 __all__ = ['Pattern', 'Match'] 2649 Pattern = Pattern 2650 Match = Match 2651 2652 2653re.__name__ = __name__ + '.re' 2654sys.modules[re.__name__] = re 2655