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