You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1945 lines
74 KiB

4 years ago
  1. import abc
  2. import collections
  3. import contextlib
  4. import sys
  5. import typing
  6. import collections.abc as collections_abc
  7. import operator
  8. # These are used by Protocol implementation
  9. # We use internal typing helpers here, but this significantly reduces
  10. # code duplication. (Also this is only until Protocol is in typing.)
  11. from typing import Generic, Callable, TypeVar, Tuple
  12. # After PEP 560, internal typing API was substantially reworked.
  13. # This is especially important for Protocol class which uses internal APIs
  14. # quite extensivelly.
  15. PEP_560 = sys.version_info[:3] >= (3, 7, 0)
  16. if PEP_560:
  17. GenericMeta = TypingMeta = type
  18. else:
  19. from typing import GenericMeta, TypingMeta
  20. OLD_GENERICS = False
  21. try:
  22. from typing import _type_vars, _next_in_mro, _type_check
  23. except ImportError:
  24. OLD_GENERICS = True
  25. try:
  26. from typing import _subs_tree # noqa
  27. SUBS_TREE = True
  28. except ImportError:
  29. SUBS_TREE = False
  30. try:
  31. from typing import _tp_cache
  32. except ImportError:
  33. def _tp_cache(x):
  34. return x
  35. try:
  36. from typing import _TypingEllipsis, _TypingEmpty
  37. except ImportError:
  38. class _TypingEllipsis:
  39. pass
  40. class _TypingEmpty:
  41. pass
  42. # The two functions below are copies of typing internal helpers.
  43. # They are needed by _ProtocolMeta
  44. def _no_slots_copy(dct):
  45. dict_copy = dict(dct)
  46. if '__slots__' in dict_copy:
  47. for slot in dict_copy['__slots__']:
  48. dict_copy.pop(slot, None)
  49. return dict_copy
  50. def _check_generic(cls, parameters):
  51. if not cls.__parameters__:
  52. raise TypeError("%s is not a generic class" % repr(cls))
  53. alen = len(parameters)
  54. elen = len(cls.__parameters__)
  55. if alen != elen:
  56. raise TypeError("Too %s parameters for %s; actual %s, expected %s" %
  57. ("many" if alen > elen else "few", repr(cls), alen, elen))
  58. if hasattr(typing, '_generic_new'):
  59. _generic_new = typing._generic_new
  60. else:
  61. # Note: The '_generic_new(...)' function is used as a part of the
  62. # process of creating a generic type and was added to the typing module
  63. # as of Python 3.5.3.
  64. #
  65. # We've defined '_generic_new(...)' below to exactly match the behavior
  66. # implemented in older versions of 'typing' bundled with Python 3.5.0 to
  67. # 3.5.2. This helps eliminate redundancy when defining collection types
  68. # like 'Deque' later.
  69. #
  70. # See https://github.com/python/typing/pull/308 for more details -- in
  71. # particular, compare and contrast the definition of types like
  72. # 'typing.List' before and after the merge.
  73. def _generic_new(base_cls, cls, *args, **kwargs):
  74. return base_cls.__new__(cls, *args, **kwargs)
  75. # See https://github.com/python/typing/pull/439
  76. if hasattr(typing, '_geqv'):
  77. from typing import _geqv
  78. _geqv_defined = True
  79. else:
  80. _geqv = None
  81. _geqv_defined = False
  82. if sys.version_info[:2] >= (3, 6):
  83. import _collections_abc
  84. _check_methods_in_mro = _collections_abc._check_methods
  85. else:
  86. def _check_methods_in_mro(C, *methods):
  87. mro = C.__mro__
  88. for method in methods:
  89. for B in mro:
  90. if method in B.__dict__:
  91. if B.__dict__[method] is None:
  92. return NotImplemented
  93. break
  94. else:
  95. return NotImplemented
  96. return True
  97. # Please keep __all__ alphabetized within each category.
  98. __all__ = [
  99. # Super-special typing primitives.
  100. 'ClassVar',
  101. 'Final',
  102. 'Type',
  103. # ABCs (from collections.abc).
  104. # The following are added depending on presence
  105. # of their non-generic counterparts in stdlib:
  106. # 'Awaitable',
  107. # 'AsyncIterator',
  108. # 'AsyncIterable',
  109. # 'Coroutine',
  110. # 'AsyncGenerator',
  111. # 'AsyncContextManager',
  112. # 'ChainMap',
  113. # Concrete collection types.
  114. 'ContextManager',
  115. 'Counter',
  116. 'Deque',
  117. 'DefaultDict',
  118. 'TypedDict',
  119. # One-off things.
  120. 'final',
  121. 'IntVar',
  122. 'Literal',
  123. 'NewType',
  124. 'overload',
  125. 'Text',
  126. 'TYPE_CHECKING',
  127. ]
  128. # Annotated relies on substitution trees of pep 560. It will not work for
  129. # versions of typing older than 3.5.3
  130. HAVE_ANNOTATED = PEP_560 or SUBS_TREE
  131. if PEP_560:
  132. __all__.append("get_type_hints")
  133. if HAVE_ANNOTATED:
  134. __all__.append("Annotated")
  135. # Protocols are hard to backport to the original version of typing 3.5.0
  136. HAVE_PROTOCOLS = sys.version_info[:3] != (3, 5, 0)
  137. if HAVE_PROTOCOLS:
  138. __all__.extend(['Protocol', 'runtime', 'runtime_checkable'])
  139. # TODO
  140. if hasattr(typing, 'NoReturn'):
  141. NoReturn = typing.NoReturn
  142. elif hasattr(typing, '_FinalTypingBase'):
  143. class _NoReturn(typing._FinalTypingBase, _root=True):
  144. """Special type indicating functions that never return.
  145. Example::
  146. from typing import NoReturn
  147. def stop() -> NoReturn:
  148. raise Exception('no way')
  149. This type is invalid in other positions, e.g., ``List[NoReturn]``
  150. will fail in static type checkers.
  151. """
  152. __slots__ = ()
  153. def __instancecheck__(self, obj):
  154. raise TypeError("NoReturn cannot be used with isinstance().")
  155. def __subclasscheck__(self, cls):
  156. raise TypeError("NoReturn cannot be used with issubclass().")
  157. NoReturn = _NoReturn(_root=True)
  158. else:
  159. class _NoReturnMeta(typing.TypingMeta):
  160. """Metaclass for NoReturn"""
  161. def __new__(cls, name, bases, namespace, _root=False):
  162. return super().__new__(cls, name, bases, namespace, _root=_root)
  163. def __instancecheck__(self, obj):
  164. raise TypeError("NoReturn cannot be used with isinstance().")
  165. def __subclasscheck__(self, cls):
  166. raise TypeError("NoReturn cannot be used with issubclass().")
  167. class NoReturn(typing.Final, metaclass=_NoReturnMeta, _root=True):
  168. """Special type indicating functions that never return.
  169. Example::
  170. from typing import NoReturn
  171. def stop() -> NoReturn:
  172. raise Exception('no way')
  173. This type is invalid in other positions, e.g., ``List[NoReturn]``
  174. will fail in static type checkers.
  175. """
  176. __slots__ = ()
  177. # Some unconstrained type variables. These are used by the container types.
  178. # (These are not for export.)
  179. T = typing.TypeVar('T') # Any type.
  180. KT = typing.TypeVar('KT') # Key type.
  181. VT = typing.TypeVar('VT') # Value type.
  182. T_co = typing.TypeVar('T_co', covariant=True) # Any type covariant containers.
  183. V_co = typing.TypeVar('V_co', covariant=True) # Any type covariant containers.
  184. VT_co = typing.TypeVar('VT_co', covariant=True) # Value type covariant containers.
  185. T_contra = typing.TypeVar('T_contra', contravariant=True) # Ditto contravariant.
  186. if hasattr(typing, 'ClassVar'):
  187. ClassVar = typing.ClassVar
  188. elif hasattr(typing, '_FinalTypingBase'):
  189. class _ClassVar(typing._FinalTypingBase, _root=True):
  190. """Special type construct to mark class variables.
  191. An annotation wrapped in ClassVar indicates that a given
  192. attribute is intended to be used as a class variable and
  193. should not be set on instances of that class. Usage::
  194. class Starship:
  195. stats: ClassVar[Dict[str, int]] = {} # class variable
  196. damage: int = 10 # instance variable
  197. ClassVar accepts only types and cannot be further subscribed.
  198. Note that ClassVar is not a class itself, and should not
  199. be used with isinstance() or issubclass().
  200. """
  201. __slots__ = ('__type__',)
  202. def __init__(self, tp=None, **kwds):
  203. self.__type__ = tp
  204. def __getitem__(self, item):
  205. cls = type(self)
  206. if self.__type__ is None:
  207. return cls(typing._type_check(item,
  208. '{} accepts only single type.'.format(cls.__name__[1:])),
  209. _root=True)
  210. raise TypeError('{} cannot be further subscripted'
  211. .format(cls.__name__[1:]))
  212. def _eval_type(self, globalns, localns):
  213. new_tp = typing._eval_type(self.__type__, globalns, localns)
  214. if new_tp == self.__type__:
  215. return self
  216. return type(self)(new_tp, _root=True)
  217. def __repr__(self):
  218. r = super().__repr__()
  219. if self.__type__ is not None:
  220. r += '[{}]'.format(typing._type_repr(self.__type__))
  221. return r
  222. def __hash__(self):
  223. return hash((type(self).__name__, self.__type__))
  224. def __eq__(self, other):
  225. if not isinstance(other, _ClassVar):
  226. return NotImplemented
  227. if self.__type__ is not None:
  228. return self.__type__ == other.__type__
  229. return self is other
  230. ClassVar = _ClassVar(_root=True)
  231. else:
  232. class _ClassVarMeta(typing.TypingMeta):
  233. """Metaclass for ClassVar"""
  234. def __new__(cls, name, bases, namespace, tp=None, _root=False):
  235. self = super().__new__(cls, name, bases, namespace, _root=_root)
  236. if tp is not None:
  237. self.__type__ = tp
  238. return self
  239. def __instancecheck__(self, obj):
  240. raise TypeError("ClassVar cannot be used with isinstance().")
  241. def __subclasscheck__(self, cls):
  242. raise TypeError("ClassVar cannot be used with issubclass().")
  243. def __getitem__(self, item):
  244. cls = type(self)
  245. if self.__type__ is not None:
  246. raise TypeError('{} cannot be further subscripted'
  247. .format(cls.__name__[1:]))
  248. param = typing._type_check(
  249. item,
  250. '{} accepts only single type.'.format(cls.__name__[1:]))
  251. return cls(self.__name__, self.__bases__,
  252. dict(self.__dict__), tp=param, _root=True)
  253. def _eval_type(self, globalns, localns):
  254. new_tp = typing._eval_type(self.__type__, globalns, localns)
  255. if new_tp == self.__type__:
  256. return self
  257. return type(self)(self.__name__, self.__bases__,
  258. dict(self.__dict__), tp=self.__type__,
  259. _root=True)
  260. def __repr__(self):
  261. r = super().__repr__()
  262. if self.__type__ is not None:
  263. r += '[{}]'.format(typing._type_repr(self.__type__))
  264. return r
  265. def __hash__(self):
  266. return hash((type(self).__name__, self.__type__))
  267. def __eq__(self, other):
  268. if not isinstance(other, ClassVar):
  269. return NotImplemented
  270. if self.__type__ is not None:
  271. return self.__type__ == other.__type__
  272. return self is other
  273. class ClassVar(typing.Final, metaclass=_ClassVarMeta, _root=True):
  274. """Special type construct to mark class variables.
  275. An annotation wrapped in ClassVar indicates that a given
  276. attribute is intended to be used as a class variable and
  277. should not be set on instances of that class. Usage::
  278. class Starship:
  279. stats: ClassVar[Dict[str, int]] = {} # class variable
  280. damage: int = 10 # instance variable
  281. ClassVar accepts only types and cannot be further subscribed.
  282. Note that ClassVar is not a class itself, and should not
  283. be used with isinstance() or issubclass().
  284. """
  285. __type__ = None
  286. # On older versions of typing there is an internal class named "Final".
  287. if hasattr(typing, 'Final') and sys.version_info[:2] >= (3, 7):
  288. Final = typing.Final
  289. elif sys.version_info[:2] >= (3, 7):
  290. class _FinalForm(typing._SpecialForm, _root=True):
  291. def __repr__(self):
  292. return 'typing_extensions.' + self._name
  293. def __getitem__(self, parameters):
  294. item = typing._type_check(parameters,
  295. '{} accepts only single type'.format(self._name))
  296. return _GenericAlias(self, (item,))
  297. Final = _FinalForm('Final',
  298. doc="""A special typing construct to indicate that a name
  299. cannot be re-assigned or overridden in a subclass.
  300. For example:
  301. MAX_SIZE: Final = 9000
  302. MAX_SIZE += 1 # Error reported by type checker
  303. class Connection:
  304. TIMEOUT: Final[int] = 10
  305. class FastConnector(Connection):
  306. TIMEOUT = 1 # Error reported by type checker
  307. There is no runtime checking of these properties.""")
  308. elif hasattr(typing, '_FinalTypingBase'):
  309. class _Final(typing._FinalTypingBase, _root=True):
  310. """A special typing construct to indicate that a name
  311. cannot be re-assigned or overridden in a subclass.
  312. For example:
  313. MAX_SIZE: Final = 9000
  314. MAX_SIZE += 1 # Error reported by type checker
  315. class Connection:
  316. TIMEOUT: Final[int] = 10
  317. class FastConnector(Connection):
  318. TIMEOUT = 1 # Error reported by type checker
  319. There is no runtime checking of these properties.
  320. """
  321. __slots__ = ('__type__',)
  322. def __init__(self, tp=None, **kwds):
  323. self.__type__ = tp
  324. def __getitem__(self, item):
  325. cls = type(self)
  326. if self.__type__ is None:
  327. return cls(typing._type_check(item,
  328. '{} accepts only single type.'.format(cls.__name__[1:])),
  329. _root=True)
  330. raise TypeError('{} cannot be further subscripted'
  331. .format(cls.__name__[1:]))
  332. def _eval_type(self, globalns, localns):
  333. new_tp = typing._eval_type(self.__type__, globalns, localns)
  334. if new_tp == self.__type__:
  335. return self
  336. return type(self)(new_tp, _root=True)
  337. def __repr__(self):
  338. r = super().__repr__()
  339. if self.__type__ is not None:
  340. r += '[{}]'.format(typing._type_repr(self.__type__))
  341. return r
  342. def __hash__(self):
  343. return hash((type(self).__name__, self.__type__))
  344. def __eq__(self, other):
  345. if not isinstance(other, _Final):
  346. return NotImplemented
  347. if self.__type__ is not None:
  348. return self.__type__ == other.__type__
  349. return self is other
  350. Final = _Final(_root=True)
  351. else:
  352. class _FinalMeta(typing.TypingMeta):
  353. """Metaclass for Final"""
  354. def __new__(cls, name, bases, namespace, tp=None, _root=False):
  355. self = super().__new__(cls, name, bases, namespace, _root=_root)
  356. if tp is not None:
  357. self.__type__ = tp
  358. return self
  359. def __instancecheck__(self, obj):
  360. raise TypeError("Final cannot be used with isinstance().")
  361. def __subclasscheck__(self, cls):
  362. raise TypeError("Final cannot be used with issubclass().")
  363. def __getitem__(self, item):
  364. cls = type(self)
  365. if self.__type__ is not None:
  366. raise TypeError('{} cannot be further subscripted'
  367. .format(cls.__name__[1:]))
  368. param = typing._type_check(
  369. item,
  370. '{} accepts only single type.'.format(cls.__name__[1:]))
  371. return cls(self.__name__, self.__bases__,
  372. dict(self.__dict__), tp=param, _root=True)
  373. def _eval_type(self, globalns, localns):
  374. new_tp = typing._eval_type(self.__type__, globalns, localns)
  375. if new_tp == self.__type__:
  376. return self
  377. return type(self)(self.__name__, self.__bases__,
  378. dict(self.__dict__), tp=self.__type__,
  379. _root=True)
  380. def __repr__(self):
  381. r = super().__repr__()
  382. if self.__type__ is not None:
  383. r += '[{}]'.format(typing._type_repr(self.__type__))
  384. return r
  385. def __hash__(self):
  386. return hash((type(self).__name__, self.__type__))
  387. def __eq__(self, other):
  388. if not isinstance(other, Final):
  389. return NotImplemented
  390. if self.__type__ is not None:
  391. return self.__type__ == other.__type__
  392. return self is other
  393. class Final(typing.Final, metaclass=_FinalMeta, _root=True):
  394. """A special typing construct to indicate that a name
  395. cannot be re-assigned or overridden in a subclass.
  396. For example:
  397. MAX_SIZE: Final = 9000
  398. MAX_SIZE += 1 # Error reported by type checker
  399. class Connection:
  400. TIMEOUT: Final[int] = 10
  401. class FastConnector(Connection):
  402. TIMEOUT = 1 # Error reported by type checker
  403. There is no runtime checking of these properties.
  404. """
  405. __type__ = None
  406. if hasattr(typing, 'final'):
  407. final = typing.final
  408. else:
  409. def final(f):
  410. """This decorator can be used to indicate to type checkers that
  411. the decorated method cannot be overridden, and decorated class
  412. cannot be subclassed. For example:
  413. class Base:
  414. @final
  415. def done(self) -> None:
  416. ...
  417. class Sub(Base):
  418. def done(self) -> None: # Error reported by type checker
  419. ...
  420. @final
  421. class Leaf:
  422. ...
  423. class Other(Leaf): # Error reported by type checker
  424. ...
  425. There is no runtime checking of these properties.
  426. """
  427. return f
  428. def IntVar(name):
  429. return TypeVar(name)
  430. if hasattr(typing, 'Literal'):
  431. Literal = typing.Literal
  432. elif sys.version_info[:2] >= (3, 7):
  433. class _LiteralForm(typing._SpecialForm, _root=True):
  434. def __repr__(self):
  435. return 'typing_extensions.' + self._name
  436. def __getitem__(self, parameters):
  437. return _GenericAlias(self, parameters)
  438. Literal = _LiteralForm('Literal',
  439. doc="""A type that can be used to indicate to type checkers
  440. that the corresponding value has a value literally equivalent
  441. to the provided parameter. For example:
  442. var: Literal[4] = 4
  443. The type checker understands that 'var' is literally equal to
  444. the value 4 and no other value.
  445. Literal[...] cannot be subclassed. There is no runtime
  446. checking verifying that the parameter is actually a value
  447. instead of a type.""")
  448. elif hasattr(typing, '_FinalTypingBase'):
  449. class _Literal(typing._FinalTypingBase, _root=True):
  450. """A type that can be used to indicate to type checkers that the
  451. corresponding value has a value literally equivalent to the
  452. provided parameter. For example:
  453. var: Literal[4] = 4
  454. The type checker understands that 'var' is literally equal to the
  455. value 4 and no other value.
  456. Literal[...] cannot be subclassed. There is no runtime checking
  457. verifying that the parameter is actually a value instead of a type.
  458. """
  459. __slots__ = ('__values__',)
  460. def __init__(self, values=None, **kwds):
  461. self.__values__ = values
  462. def __getitem__(self, values):
  463. cls = type(self)
  464. if self.__values__ is None:
  465. if not isinstance(values, tuple):
  466. values = (values,)
  467. return cls(values, _root=True)
  468. raise TypeError('{} cannot be further subscripted'
  469. .format(cls.__name__[1:]))
  470. def _eval_type(self, globalns, localns):
  471. return self
  472. def __repr__(self):
  473. r = super().__repr__()
  474. if self.__values__ is not None:
  475. r += '[{}]'.format(', '.join(map(typing._type_repr, self.__values__)))
  476. return r
  477. def __hash__(self):
  478. return hash((type(self).__name__, self.__values__))
  479. def __eq__(self, other):
  480. if not isinstance(other, _Literal):
  481. return NotImplemented
  482. if self.__values__ is not None:
  483. return self.__values__ == other.__values__
  484. return self is other
  485. Literal = _Literal(_root=True)
  486. else:
  487. class _LiteralMeta(typing.TypingMeta):
  488. """Metaclass for Literal"""
  489. def __new__(cls, name, bases, namespace, values=None, _root=False):
  490. self = super().__new__(cls, name, bases, namespace, _root=_root)
  491. if values is not None:
  492. self.__values__ = values
  493. return self
  494. def __instancecheck__(self, obj):
  495. raise TypeError("Literal cannot be used with isinstance().")
  496. def __subclasscheck__(self, cls):
  497. raise TypeError("Literal cannot be used with issubclass().")
  498. def __getitem__(self, item):
  499. cls = type(self)
  500. if self.__values__ is not None:
  501. raise TypeError('{} cannot be further subscripted'
  502. .format(cls.__name__[1:]))
  503. if not isinstance(item, tuple):
  504. item = (item,)
  505. return cls(self.__name__, self.__bases__,
  506. dict(self.__dict__), values=item, _root=True)
  507. def _eval_type(self, globalns, localns):
  508. return self
  509. def __repr__(self):
  510. r = super().__repr__()
  511. if self.__values__ is not None:
  512. r += '[{}]'.format(', '.join(map(typing._type_repr, self.__values__)))
  513. return r
  514. def __hash__(self):
  515. return hash((type(self).__name__, self.__values__))
  516. def __eq__(self, other):
  517. if not isinstance(other, Literal):
  518. return NotImplemented
  519. if self.__values__ is not None:
  520. return self.__values__ == other.__values__
  521. return self is other
  522. class Literal(typing.Final, metaclass=_LiteralMeta, _root=True):
  523. """A type that can be used to indicate to type checkers that the
  524. corresponding value has a value literally equivalent to the
  525. provided parameter. For example:
  526. var: Literal[4] = 4
  527. The type checker understands that 'var' is literally equal to the
  528. value 4 and no other value.
  529. Literal[...] cannot be subclassed. There is no runtime checking
  530. verifying that the parameter is actually a value instead of a type.
  531. """
  532. __values__ = None
  533. def _overload_dummy(*args, **kwds):
  534. """Helper for @overload to raise when called."""
  535. raise NotImplementedError(
  536. "You should not call an overloaded function. "
  537. "A series of @overload-decorated functions "
  538. "outside a stub module should always be followed "
  539. "by an implementation that is not @overload-ed.")
  540. def overload(func):
  541. """Decorator for overloaded functions/methods.
  542. In a stub file, place two or more stub definitions for the same
  543. function in a row, each decorated with @overload. For example:
  544. @overload
  545. def utf8(value: None) -> None: ...
  546. @overload
  547. def utf8(value: bytes) -> bytes: ...
  548. @overload
  549. def utf8(value: str) -> bytes: ...
  550. In a non-stub file (i.e. a regular .py file), do the same but
  551. follow it with an implementation. The implementation should *not*
  552. be decorated with @overload. For example:
  553. @overload
  554. def utf8(value: None) -> None: ...
  555. @overload
  556. def utf8(value: bytes) -> bytes: ...
  557. @overload
  558. def utf8(value: str) -> bytes: ...
  559. def utf8(value):
  560. # implementation goes here
  561. """
  562. return _overload_dummy
  563. # This is not a real generic class. Don't use outside annotations.
  564. if hasattr(typing, 'Type'):
  565. Type = typing.Type
  566. else:
  567. # Internal type variable used for Type[].
  568. CT_co = typing.TypeVar('CT_co', covariant=True, bound=type)
  569. class Type(typing.Generic[CT_co], extra=type):
  570. """A special construct usable to annotate class objects.
  571. For example, suppose we have the following classes::
  572. class User: ... # Abstract base for User classes
  573. class BasicUser(User): ...
  574. class ProUser(User): ...
  575. class TeamUser(User): ...
  576. And a function that takes a class argument that's a subclass of
  577. User and returns an instance of the corresponding class::
  578. U = TypeVar('U', bound=User)
  579. def new_user(user_class: Type[U]) -> U:
  580. user = user_class()
  581. # (Here we could write the user object to a database)
  582. return user
  583. joe = new_user(BasicUser)
  584. At this point the type checker knows that joe has type BasicUser.
  585. """
  586. __slots__ = ()
  587. # Various ABCs mimicking those in collections.abc.
  588. # A few are simply re-exported for completeness.
  589. def _define_guard(type_name):
  590. """
  591. Returns True if the given type isn't defined in typing but
  592. is defined in collections_abc.
  593. Adds the type to __all__ if the collection is found in either
  594. typing or collection_abc.
  595. """
  596. if hasattr(typing, type_name):
  597. __all__.append(type_name)
  598. globals()[type_name] = getattr(typing, type_name)
  599. return False
  600. elif hasattr(collections_abc, type_name):
  601. __all__.append(type_name)
  602. return True
  603. else:
  604. return False
  605. class _ExtensionsGenericMeta(GenericMeta):
  606. def __subclasscheck__(self, subclass):
  607. """This mimics a more modern GenericMeta.__subclasscheck__() logic
  608. (that does not have problems with recursion) to work around interactions
  609. between collections, typing, and typing_extensions on older
  610. versions of Python, see https://github.com/python/typing/issues/501.
  611. """
  612. if sys.version_info[:3] >= (3, 5, 3) or sys.version_info[:3] < (3, 5, 0):
  613. if self.__origin__ is not None:
  614. if sys._getframe(1).f_globals['__name__'] not in ['abc', 'functools']:
  615. raise TypeError("Parameterized generics cannot be used with class "
  616. "or instance checks")
  617. return False
  618. if not self.__extra__:
  619. return super().__subclasscheck__(subclass)
  620. res = self.__extra__.__subclasshook__(subclass)
  621. if res is not NotImplemented:
  622. return res
  623. if self.__extra__ in subclass.__mro__:
  624. return True
  625. for scls in self.__extra__.__subclasses__():
  626. if isinstance(scls, GenericMeta):
  627. continue
  628. if issubclass(subclass, scls):
  629. return True
  630. return False
  631. if _define_guard('Awaitable'):
  632. class Awaitable(typing.Generic[T_co], metaclass=_ExtensionsGenericMeta,
  633. extra=collections_abc.Awaitable):
  634. __slots__ = ()
  635. if _define_guard('Coroutine'):
  636. class Coroutine(Awaitable[V_co], typing.Generic[T_co, T_contra, V_co],
  637. metaclass=_ExtensionsGenericMeta,
  638. extra=collections_abc.Coroutine):
  639. __slots__ = ()
  640. if _define_guard('AsyncIterable'):
  641. class AsyncIterable(typing.Generic[T_co],
  642. metaclass=_ExtensionsGenericMeta,
  643. extra=collections_abc.AsyncIterable):
  644. __slots__ = ()
  645. if _define_guard('AsyncIterator'):
  646. class AsyncIterator(AsyncIterable[T_co],
  647. metaclass=_ExtensionsGenericMeta,
  648. extra=collections_abc.AsyncIterator):
  649. __slots__ = ()
  650. if hasattr(typing, 'Deque'):
  651. Deque = typing.Deque
  652. elif _geqv_defined:
  653. class Deque(collections.deque, typing.MutableSequence[T],
  654. metaclass=_ExtensionsGenericMeta,
  655. extra=collections.deque):
  656. __slots__ = ()
  657. def __new__(cls, *args, **kwds):
  658. if _geqv(cls, Deque):
  659. return collections.deque(*args, **kwds)
  660. return _generic_new(collections.deque, cls, *args, **kwds)
  661. else:
  662. class Deque(collections.deque, typing.MutableSequence[T],
  663. metaclass=_ExtensionsGenericMeta,
  664. extra=collections.deque):
  665. __slots__ = ()
  666. def __new__(cls, *args, **kwds):
  667. if cls._gorg is Deque:
  668. return collections.deque(*args, **kwds)
  669. return _generic_new(collections.deque, cls, *args, **kwds)
  670. if hasattr(typing, 'ContextManager'):
  671. ContextManager = typing.ContextManager
  672. elif hasattr(contextlib, 'AbstractContextManager'):
  673. class ContextManager(typing.Generic[T_co],
  674. metaclass=_ExtensionsGenericMeta,
  675. extra=contextlib.AbstractContextManager):
  676. __slots__ = ()
  677. else:
  678. class ContextManager(typing.Generic[T_co]):
  679. __slots__ = ()
  680. def __enter__(self):
  681. return self
  682. @abc.abstractmethod
  683. def __exit__(self, exc_type, exc_value, traceback):
  684. return None
  685. @classmethod
  686. def __subclasshook__(cls, C):
  687. if cls is ContextManager:
  688. # In Python 3.6+, it is possible to set a method to None to
  689. # explicitly indicate that the class does not implement an ABC
  690. # (https://bugs.python.org/issue25958), but we do not support
  691. # that pattern here because this fallback class is only used
  692. # in Python 3.5 and earlier.
  693. if (any("__enter__" in B.__dict__ for B in C.__mro__) and
  694. any("__exit__" in B.__dict__ for B in C.__mro__)):
  695. return True
  696. return NotImplemented
  697. if hasattr(typing, 'AsyncContextManager'):
  698. AsyncContextManager = typing.AsyncContextManager
  699. __all__.append('AsyncContextManager')
  700. elif hasattr(contextlib, 'AbstractAsyncContextManager'):
  701. class AsyncContextManager(typing.Generic[T_co],
  702. metaclass=_ExtensionsGenericMeta,
  703. extra=contextlib.AbstractAsyncContextManager):
  704. __slots__ = ()
  705. __all__.append('AsyncContextManager')
  706. elif sys.version_info[:2] >= (3, 5):
  707. exec("""
  708. class AsyncContextManager(typing.Generic[T_co]):
  709. __slots__ = ()
  710. async def __aenter__(self):
  711. return self
  712. @abc.abstractmethod
  713. async def __aexit__(self, exc_type, exc_value, traceback):
  714. return None
  715. @classmethod
  716. def __subclasshook__(cls, C):
  717. if cls is AsyncContextManager:
  718. return _check_methods_in_mro(C, "__aenter__", "__aexit__")
  719. return NotImplemented
  720. __all__.append('AsyncContextManager')
  721. """)
  722. if hasattr(typing, 'DefaultDict'):
  723. DefaultDict = typing.DefaultDict
  724. elif _geqv_defined:
  725. class DefaultDict(collections.defaultdict, typing.MutableMapping[KT, VT],
  726. metaclass=_ExtensionsGenericMeta,
  727. extra=collections.defaultdict):
  728. __slots__ = ()
  729. def __new__(cls, *args, **kwds):
  730. if _geqv(cls, DefaultDict):
  731. return collections.defaultdict(*args, **kwds)
  732. return _generic_new(collections.defaultdict, cls, *args, **kwds)
  733. else:
  734. class DefaultDict(collections.defaultdict, typing.MutableMapping[KT, VT],
  735. metaclass=_ExtensionsGenericMeta,
  736. extra=collections.defaultdict):
  737. __slots__ = ()
  738. def __new__(cls, *args, **kwds):
  739. if cls._gorg is DefaultDict:
  740. return collections.defaultdict(*args, **kwds)
  741. return _generic_new(collections.defaultdict, cls, *args, **kwds)
  742. if hasattr(typing, 'Counter'):
  743. Counter = typing.Counter
  744. elif (3, 5, 0) <= sys.version_info[:3] <= (3, 5, 1):
  745. assert _geqv_defined
  746. _TInt = typing.TypeVar('_TInt')
  747. class _CounterMeta(typing.GenericMeta):
  748. """Metaclass for Counter"""
  749. def __getitem__(self, item):
  750. return super().__getitem__((item, int))
  751. class Counter(collections.Counter,
  752. typing.Dict[T, int],
  753. metaclass=_CounterMeta,
  754. extra=collections.Counter):
  755. __slots__ = ()
  756. def __new__(cls, *args, **kwds):
  757. if _geqv(cls, Counter):
  758. return collections.Counter(*args, **kwds)
  759. return _generic_new(collections.Counter, cls, *args, **kwds)
  760. elif _geqv_defined:
  761. class Counter(collections.Counter,
  762. typing.Dict[T, int],
  763. metaclass=_ExtensionsGenericMeta, extra=collections.Counter):
  764. __slots__ = ()
  765. def __new__(cls, *args, **kwds):
  766. if _geqv(cls, Counter):
  767. return collections.Counter(*args, **kwds)
  768. return _generic_new(collections.Counter, cls, *args, **kwds)
  769. else:
  770. class Counter(collections.Counter,
  771. typing.Dict[T, int],
  772. metaclass=_ExtensionsGenericMeta, extra=collections.Counter):
  773. __slots__ = ()
  774. def __new__(cls, *args, **kwds):
  775. if cls._gorg is Counter:
  776. return collections.Counter(*args, **kwds)
  777. return _generic_new(collections.Counter, cls, *args, **kwds)
  778. if hasattr(typing, 'ChainMap'):
  779. ChainMap = typing.ChainMap
  780. __all__.append('ChainMap')
  781. elif hasattr(collections, 'ChainMap'):
  782. # ChainMap only exists in 3.3+
  783. if _geqv_defined:
  784. class ChainMap(collections.ChainMap, typing.MutableMapping[KT, VT],
  785. metaclass=_ExtensionsGenericMeta,
  786. extra=collections.ChainMap):
  787. __slots__ = ()
  788. def __new__(cls, *args, **kwds):
  789. if _geqv(cls, ChainMap):
  790. return collections.ChainMap(*args, **kwds)
  791. return _generic_new(collections.ChainMap, cls, *args, **kwds)
  792. else:
  793. class ChainMap(collections.ChainMap, typing.MutableMapping[KT, VT],
  794. metaclass=_ExtensionsGenericMeta,
  795. extra=collections.ChainMap):
  796. __slots__ = ()
  797. def __new__(cls, *args, **kwds):
  798. if cls._gorg is ChainMap:
  799. return collections.ChainMap(*args, **kwds)
  800. return _generic_new(collections.ChainMap, cls, *args, **kwds)
  801. __all__.append('ChainMap')
  802. if _define_guard('AsyncGenerator'):
  803. class AsyncGenerator(AsyncIterator[T_co], typing.Generic[T_co, T_contra],
  804. metaclass=_ExtensionsGenericMeta,
  805. extra=collections_abc.AsyncGenerator):
  806. __slots__ = ()
  807. if hasattr(typing, 'NewType'):
  808. NewType = typing.NewType
  809. else:
  810. def NewType(name, tp):
  811. """NewType creates simple unique types with almost zero
  812. runtime overhead. NewType(name, tp) is considered a subtype of tp
  813. by static type checkers. At runtime, NewType(name, tp) returns
  814. a dummy function that simply returns its argument. Usage::
  815. UserId = NewType('UserId', int)
  816. def name_by_id(user_id: UserId) -> str:
  817. ...
  818. UserId('user') # Fails type check
  819. name_by_id(42) # Fails type check
  820. name_by_id(UserId(42)) # OK
  821. num = UserId(5) + 1 # type: int
  822. """
  823. def new_type(x):
  824. return x
  825. new_type.__name__ = name
  826. new_type.__supertype__ = tp
  827. return new_type
  828. if hasattr(typing, 'Text'):
  829. Text = typing.Text
  830. else:
  831. Text = str
  832. if hasattr(typing, 'TYPE_CHECKING'):
  833. TYPE_CHECKING = typing.TYPE_CHECKING
  834. else:
  835. # Constant that's True when type checking, but False here.
  836. TYPE_CHECKING = False
  837. def _gorg(cls):
  838. """This function exists for compatibility with old typing versions."""
  839. assert isinstance(cls, GenericMeta)
  840. if hasattr(cls, '_gorg'):
  841. return cls._gorg
  842. while cls.__origin__ is not None:
  843. cls = cls.__origin__
  844. return cls
  845. if OLD_GENERICS:
  846. def _next_in_mro(cls): # noqa
  847. """This function exists for compatibility with old typing versions."""
  848. next_in_mro = object
  849. for i, c in enumerate(cls.__mro__[:-1]):
  850. if isinstance(c, GenericMeta) and _gorg(c) is Generic:
  851. next_in_mro = cls.__mro__[i + 1]
  852. return next_in_mro
  853. _PROTO_WHITELIST = ['Callable', 'Awaitable',
  854. 'Iterable', 'Iterator', 'AsyncIterable', 'AsyncIterator',
  855. 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
  856. 'ContextManager', 'AsyncContextManager']
  857. def _get_protocol_attrs(cls):
  858. attrs = set()
  859. for base in cls.__mro__[:-1]: # without object
  860. if base.__name__ in ('Protocol', 'Generic'):
  861. continue
  862. annotations = getattr(base, '__annotations__', {})
  863. for attr in list(base.__dict__.keys()) + list(annotations.keys()):
  864. if (not attr.startswith('_abc_') and attr not in (
  865. '__abstractmethods__', '__annotations__', '__weakref__',
  866. '_is_protocol', '_is_runtime_protocol', '__dict__',
  867. '__args__', '__slots__',
  868. '__next_in_mro__', '__parameters__', '__origin__',
  869. '__orig_bases__', '__extra__', '__tree_hash__',
  870. '__doc__', '__subclasshook__', '__init__', '__new__',
  871. '__module__', '_MutableMapping__marker', '_gorg')):
  872. attrs.add(attr)
  873. return attrs
  874. def _is_callable_members_only(cls):
  875. return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls))
  876. if hasattr(typing, 'Protocol'):
  877. Protocol = typing.Protocol
  878. elif HAVE_PROTOCOLS and not PEP_560:
  879. class _ProtocolMeta(GenericMeta):
  880. """Internal metaclass for Protocol.
  881. This exists so Protocol classes can be generic without deriving
  882. from Generic.
  883. """
  884. if not OLD_GENERICS:
  885. def __new__(cls, name, bases, namespace,
  886. tvars=None, args=None, origin=None, extra=None, orig_bases=None):
  887. # This is just a version copied from GenericMeta.__new__ that
  888. # includes "Protocol" special treatment. (Comments removed for brevity.)
  889. assert extra is None # Protocols should not have extra
  890. if tvars is not None:
  891. assert origin is not None
  892. assert all(isinstance(t, TypeVar) for t in tvars), tvars
  893. else:
  894. tvars = _type_vars(bases)
  895. gvars = None
  896. for base in bases:
  897. if base is Generic:
  898. raise TypeError("Cannot inherit from plain Generic")
  899. if (isinstance(base, GenericMeta) and
  900. base.__origin__ in (Generic, Protocol)):
  901. if gvars is not None:
  902. raise TypeError(
  903. "Cannot inherit from Generic[...] or"
  904. " Protocol[...] multiple times.")
  905. gvars = base.__parameters__
  906. if gvars is None:
  907. gvars = tvars
  908. else:
  909. tvarset = set(tvars)
  910. gvarset = set(gvars)
  911. if not tvarset <= gvarset:
  912. raise TypeError(
  913. "Some type variables (%s) "
  914. "are not listed in %s[%s]" %
  915. (", ".join(str(t) for t in tvars if t not in gvarset),
  916. "Generic" if any(b.__origin__ is Generic
  917. for b in bases) else "Protocol",
  918. ", ".join(str(g) for g in gvars)))
  919. tvars = gvars
  920. initial_bases = bases
  921. if (extra is not None and type(extra) is abc.ABCMeta and
  922. extra not in bases):
  923. bases = (extra,) + bases
  924. bases = tuple(_gorg(b) if isinstance(b, GenericMeta) else b
  925. for b in bases)
  926. if any(isinstance(b, GenericMeta) and b is not Generic for b in bases):
  927. bases = tuple(b for b in bases if b is not Generic)
  928. namespace.update({'__origin__': origin, '__extra__': extra})
  929. self = super(GenericMeta, cls).__new__(cls, name, bases, namespace,
  930. _root=True)
  931. super(GenericMeta, self).__setattr__('_gorg',
  932. self if not origin else
  933. _gorg(origin))
  934. self.__parameters__ = tvars
  935. self.__args__ = tuple(... if a is _TypingEllipsis else
  936. () if a is _TypingEmpty else
  937. a for a in args) if args else None
  938. self.__next_in_mro__ = _next_in_mro(self)
  939. if orig_bases is None:
  940. self.__orig_bases__ = initial_bases
  941. elif origin is not None:
  942. self._abc_registry = origin._abc_registry
  943. self._abc_cache = origin._abc_cache
  944. if hasattr(self, '_subs_tree'):
  945. self.__tree_hash__ = (hash(self._subs_tree()) if origin else
  946. super(GenericMeta, self).__hash__())
  947. return self
  948. def __init__(cls, *args, **kwargs):
  949. super().__init__(*args, **kwargs)
  950. if not cls.__dict__.get('_is_protocol', None):
  951. cls._is_protocol = any(b is Protocol or
  952. isinstance(b, _ProtocolMeta) and
  953. b.__origin__ is Protocol
  954. for b in cls.__bases__)
  955. if cls._is_protocol:
  956. for base in cls.__mro__[1:]:
  957. if not (base in (object, Generic) or
  958. base.__module__ == 'collections.abc' and
  959. base.__name__ in _PROTO_WHITELIST or
  960. isinstance(base, TypingMeta) and base._is_protocol or
  961. isinstance(base, GenericMeta) and
  962. base.__origin__ is Generic):
  963. raise TypeError('Protocols can only inherit from other'
  964. ' protocols, got %r' % base)
  965. def _no_init(self, *args, **kwargs):
  966. if type(self)._is_protocol:
  967. raise TypeError('Protocols cannot be instantiated')
  968. cls.__init__ = _no_init
  969. def _proto_hook(other):
  970. if not cls.__dict__.get('_is_protocol', None):
  971. return NotImplemented
  972. if not isinstance(other, type):
  973. # Same error as for issubclass(1, int)
  974. raise TypeError('issubclass() arg 1 must be a class')
  975. for attr in _get_protocol_attrs(cls):
  976. for base in other.__mro__:
  977. if attr in base.__dict__:
  978. if base.__dict__[attr] is None:
  979. return NotImplemented
  980. break
  981. annotations = getattr(base, '__annotations__', {})
  982. if (isinstance(annotations, typing.Mapping) and
  983. attr in annotations and
  984. isinstance(other, _ProtocolMeta) and
  985. other._is_protocol):
  986. break
  987. else:
  988. return NotImplemented
  989. return True
  990. if '__subclasshook__' not in cls.__dict__:
  991. cls.__subclasshook__ = _proto_hook
  992. def __instancecheck__(self, instance):
  993. # We need this method for situations where attributes are
  994. # assigned in __init__.
  995. if ((not getattr(self, '_is_protocol', False) or
  996. _is_callable_members_only(self)) and
  997. issubclass(instance.__class__, self)):
  998. return True
  999. if self._is_protocol:
  1000. if all(hasattr(instance, attr) and
  1001. (not callable(getattr(self, attr, None)) or
  1002. getattr(instance, attr) is not None)
  1003. for attr in _get_protocol_attrs(self)):
  1004. return True
  1005. return super(GenericMeta, self).__instancecheck__(instance)
  1006. def __subclasscheck__(self, cls):
  1007. if self.__origin__ is not None:
  1008. if sys._getframe(1).f_globals['__name__'] not in ['abc', 'functools']:
  1009. raise TypeError("Parameterized generics cannot be used with class "
  1010. "or instance checks")
  1011. return False
  1012. if (self.__dict__.get('_is_protocol', None) and
  1013. not self.__dict__.get('_is_runtime_protocol', None)):
  1014. if sys._getframe(1).f_globals['__name__'] in ['abc',
  1015. 'functools',
  1016. 'typing']:
  1017. return False
  1018. raise TypeError("Instance and class checks can only be used with"
  1019. " @runtime protocols")
  1020. if (self.__dict__.get('_is_runtime_protocol', None) and
  1021. not _is_callable_members_only(self)):
  1022. if sys._getframe(1).f_globals['__name__'] in ['abc',
  1023. 'functools',
  1024. 'typing']:
  1025. return super(GenericMeta, self).__subclasscheck__(cls)
  1026. raise TypeError("Protocols with non-method members"
  1027. " don't support issubclass()")
  1028. return super(GenericMeta, self).__subclasscheck__(cls)
  1029. if not OLD_GENERICS:
  1030. @_tp_cache
  1031. def __getitem__(self, params):
  1032. # We also need to copy this from GenericMeta.__getitem__ to get
  1033. # special treatment of "Protocol". (Comments removed for brevity.)
  1034. if not isinstance(params, tuple):
  1035. params = (params,)
  1036. if not params and _gorg(self) is not Tuple:
  1037. raise TypeError(
  1038. "Parameter list to %s[...] cannot be empty" % self.__qualname__)
  1039. msg = "Parameters to generic types must be types."
  1040. params = tuple(_type_check(p, msg) for p in params)
  1041. if self in (Generic, Protocol):
  1042. if not all(isinstance(p, TypeVar) for p in params):
  1043. raise TypeError(
  1044. "Parameters to %r[...] must all be type variables" % self)
  1045. if len(set(params)) != len(params):
  1046. raise TypeError(
  1047. "Parameters to %r[...] must all be unique" % self)
  1048. tvars = params
  1049. args = params
  1050. elif self in (Tuple, Callable):
  1051. tvars = _type_vars(params)
  1052. args = params
  1053. elif self.__origin__ in (Generic, Protocol):
  1054. raise TypeError("Cannot subscript already-subscripted %s" %
  1055. repr(self))
  1056. else:
  1057. _check_generic(self, params)
  1058. tvars = _type_vars(params)
  1059. args = params
  1060. prepend = (self,) if self.__origin__ is None else ()
  1061. return self.__class__(self.__name__,
  1062. prepend + self.__bases__,
  1063. _no_slots_copy(self.__dict__),
  1064. tvars=tvars,
  1065. args=args,
  1066. origin=self,
  1067. extra=self.__extra__,
  1068. orig_bases=self.__orig_bases__)
  1069. class Protocol(metaclass=_ProtocolMeta):
  1070. """Base class for protocol classes. Protocol classes are defined as::
  1071. class Proto(Protocol):
  1072. def meth(self) -> int:
  1073. ...
  1074. Such classes are primarily used with static type checkers that recognize
  1075. structural subtyping (static duck-typing), for example::
  1076. class C:
  1077. def meth(self) -> int:
  1078. return 0
  1079. def func(x: Proto) -> int:
  1080. return x.meth()
  1081. func(C()) # Passes static type check
  1082. See PEP 544 for details. Protocol classes decorated with
  1083. @typing_extensions.runtime act as simple-minded runtime protocol that checks
  1084. only the presence of given attributes, ignoring their type signatures.
  1085. Protocol classes can be generic, they are defined as::
  1086. class GenProto({bases}):
  1087. def meth(self) -> T:
  1088. ...
  1089. """
  1090. __slots__ = ()
  1091. _is_protocol = True
  1092. def __new__(cls, *args, **kwds):
  1093. if _gorg(cls) is Protocol:
  1094. raise TypeError("Type Protocol cannot be instantiated; "
  1095. "it can be used only as a base class")
  1096. if OLD_GENERICS:
  1097. return _generic_new(_next_in_mro(cls), cls, *args, **kwds)
  1098. return _generic_new(cls.__next_in_mro__, cls, *args, **kwds)
  1099. if Protocol.__doc__ is not None:
  1100. Protocol.__doc__ = Protocol.__doc__.format(bases="Protocol, Generic[T]" if
  1101. OLD_GENERICS else "Protocol[T]")
  1102. elif PEP_560:
  1103. from typing import _type_check, _GenericAlias, _collect_type_vars # noqa
  1104. class _ProtocolMeta(abc.ABCMeta):
  1105. # This metaclass is a bit unfortunate and exists only because of the lack
  1106. # of __instancehook__.
  1107. def __instancecheck__(cls, instance):
  1108. # We need this method for situations where attributes are
  1109. # assigned in __init__.
  1110. if ((not getattr(cls, '_is_protocol', False) or
  1111. _is_callable_members_only(cls)) and
  1112. issubclass(instance.__class__, cls)):
  1113. return True
  1114. if cls._is_protocol:
  1115. if all(hasattr(instance, attr) and
  1116. (not callable(getattr(cls, attr, None)) or
  1117. getattr(instance, attr) is not None)
  1118. for attr in _get_protocol_attrs(cls)):
  1119. return True
  1120. return super().__instancecheck__(instance)
  1121. class Protocol(metaclass=_ProtocolMeta):
  1122. # There is quite a lot of overlapping code with typing.Generic.
  1123. # Unfortunately it is hard to avoid this while these live in two different
  1124. # modules. The duplicated code will be removed when Protocol is moved to typing.
  1125. """Base class for protocol classes. Protocol classes are defined as::
  1126. class Proto(Protocol):
  1127. def meth(self) -> int:
  1128. ...
  1129. Such classes are primarily used with static type checkers that recognize
  1130. structural subtyping (static duck-typing), for example::
  1131. class C:
  1132. def meth(self) -> int:
  1133. return 0
  1134. def func(x: Proto) -> int:
  1135. return x.meth()
  1136. func(C()) # Passes static type check
  1137. See PEP 544 for details. Protocol classes decorated with
  1138. @typing_extensions.runtime act as simple-minded runtime protocol that checks
  1139. only the presence of given attributes, ignoring their type signatures.
  1140. Protocol classes can be generic, they are defined as::
  1141. class GenProto(Protocol[T]):
  1142. def meth(self) -> T:
  1143. ...
  1144. """
  1145. __slots__ = ()
  1146. _is_protocol = True
  1147. def __new__(cls, *args, **kwds):
  1148. if cls is Protocol:
  1149. raise TypeError("Type Protocol cannot be instantiated; "
  1150. "it can only be used as a base class")
  1151. return super().__new__(cls)
  1152. @_tp_cache
  1153. def __class_getitem__(cls, params):
  1154. if not isinstance(params, tuple):
  1155. params = (params,)
  1156. if not params and cls is not Tuple:
  1157. raise TypeError(
  1158. "Parameter list to {}[...] cannot be empty".format(cls.__qualname__))
  1159. msg = "Parameters to generic types must be types."
  1160. params = tuple(_type_check(p, msg) for p in params)
  1161. if cls is Protocol:
  1162. # Generic can only be subscripted with unique type variables.
  1163. if not all(isinstance(p, TypeVar) for p in params):
  1164. i = 0
  1165. while isinstance(params[i], TypeVar):
  1166. i += 1
  1167. raise TypeError(
  1168. "Parameters to Protocol[...] must all be type variables."
  1169. " Parameter {} is {}".format(i + 1, params[i]))
  1170. if len(set(params)) != len(params):
  1171. raise TypeError(
  1172. "Parameters to Protocol[...] must all be unique")
  1173. else:
  1174. # Subscripting a regular Generic subclass.
  1175. _check_generic(cls, params)
  1176. return _GenericAlias(cls, params)
  1177. def __init_subclass__(cls, *args, **kwargs):
  1178. tvars = []
  1179. if '__orig_bases__' in cls.__dict__:
  1180. error = Generic in cls.__orig_bases__
  1181. else:
  1182. error = Generic in cls.__bases__
  1183. if error:
  1184. raise TypeError("Cannot inherit from plain Generic")
  1185. if '__orig_bases__' in cls.__dict__:
  1186. tvars = _collect_type_vars(cls.__orig_bases__)
  1187. # Look for Generic[T1, ..., Tn] or Protocol[T1, ..., Tn].
  1188. # If found, tvars must be a subset of it.
  1189. # If not found, tvars is it.
  1190. # Also check for and reject plain Generic,
  1191. # and reject multiple Generic[...] and/or Protocol[...].
  1192. gvars = None
  1193. for base in cls.__orig_bases__:
  1194. if (isinstance(base, _GenericAlias) and
  1195. base.__origin__ in (Generic, Protocol)):
  1196. # for error messages
  1197. the_base = 'Generic' if base.__origin__ is Generic else 'Protocol'
  1198. if gvars is not None:
  1199. raise TypeError(
  1200. "Cannot inherit from Generic[...]"
  1201. " and/or Protocol[...] multiple types.")
  1202. gvars = base.__parameters__
  1203. if gvars is None:
  1204. gvars = tvars
  1205. else:
  1206. tvarset = set(tvars)
  1207. gvarset = set(gvars)
  1208. if not tvarset <= gvarset:
  1209. s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
  1210. s_args = ', '.join(str(g) for g in gvars)
  1211. raise TypeError("Some type variables ({}) are"
  1212. " not listed in {}[{}]".format(s_vars,
  1213. the_base, s_args))
  1214. tvars = gvars
  1215. cls.__parameters__ = tuple(tvars)
  1216. # Determine if this is a protocol or a concrete subclass.
  1217. if not cls.__dict__.get('_is_protocol', None):
  1218. cls._is_protocol = any(b is Protocol for b in cls.__bases__)
  1219. # Set (or override) the protocol subclass hook.
  1220. def _proto_hook(other):
  1221. if not cls.__dict__.get('_is_protocol', None):
  1222. return NotImplemented
  1223. if not getattr(cls, '_is_runtime_protocol', False):
  1224. if sys._getframe(2).f_globals['__name__'] in ['abc', 'functools']:
  1225. return NotImplemented
  1226. raise TypeError("Instance and class checks can only be used with"
  1227. " @runtime protocols")
  1228. if not _is_callable_members_only(cls):
  1229. if sys._getframe(2).f_globals['__name__'] in ['abc', 'functools']:
  1230. return NotImplemented
  1231. raise TypeError("Protocols with non-method members"
  1232. " don't support issubclass()")
  1233. if not isinstance(other, type):
  1234. # Same error as for issubclass(1, int)
  1235. raise TypeError('issubclass() arg 1 must be a class')
  1236. for attr in _get_protocol_attrs(cls):
  1237. for base in other.__mro__:
  1238. if attr in base.__dict__:
  1239. if base.__dict__[attr] is None:
  1240. return NotImplemented
  1241. break
  1242. annotations = getattr(base, '__annotations__', {})
  1243. if (isinstance(annotations, typing.Mapping) and
  1244. attr in annotations and
  1245. isinstance(other, _ProtocolMeta) and
  1246. other._is_protocol):
  1247. break
  1248. else:
  1249. return NotImplemented
  1250. return True
  1251. if '__subclasshook__' not in cls.__dict__:
  1252. cls.__subclasshook__ = _proto_hook
  1253. # We have nothing more to do for non-protocols.
  1254. if not cls._is_protocol:
  1255. return
  1256. # Check consistency of bases.
  1257. for base in cls.__bases__:
  1258. if not (base in (object, Generic) or
  1259. base.__module__ == 'collections.abc' and
  1260. base.__name__ in _PROTO_WHITELIST or
  1261. isinstance(base, _ProtocolMeta) and base._is_protocol):
  1262. raise TypeError('Protocols can only inherit from other'
  1263. ' protocols, got %r' % base)
  1264. def _no_init(self, *args, **kwargs):
  1265. if type(self)._is_protocol:
  1266. raise TypeError('Protocols cannot be instantiated')
  1267. cls.__init__ = _no_init
  1268. if hasattr(typing, 'runtime_checkable'):
  1269. runtime_checkable = typing.runtime_checkable
  1270. elif HAVE_PROTOCOLS:
  1271. def runtime_checkable(cls):
  1272. """Mark a protocol class as a runtime protocol, so that it
  1273. can be used with isinstance() and issubclass(). Raise TypeError
  1274. if applied to a non-protocol class.
  1275. This allows a simple-minded structural check very similar to the
  1276. one-offs in collections.abc such as Hashable.
  1277. """
  1278. if not isinstance(cls, _ProtocolMeta) or not cls._is_protocol:
  1279. raise TypeError('@runtime_checkable can be only applied to protocol classes,'
  1280. ' got %r' % cls)
  1281. cls._is_runtime_protocol = True
  1282. return cls
  1283. if HAVE_PROTOCOLS:
  1284. # Exists for backwards compatibility.
  1285. runtime = runtime_checkable
  1286. if hasattr(typing, 'TypedDict'):
  1287. TypedDict = typing.TypedDict
  1288. else:
  1289. def _check_fails(cls, other):
  1290. try:
  1291. if sys._getframe(1).f_globals['__name__'] not in ['abc',
  1292. 'functools',
  1293. 'typing']:
  1294. # Typed dicts are only for static structural subtyping.
  1295. raise TypeError('TypedDict does not support instance and class checks')
  1296. except (AttributeError, ValueError):
  1297. pass
  1298. return False
  1299. def _dict_new(cls, *args, **kwargs):
  1300. return dict(*args, **kwargs)
  1301. def _typeddict_new(cls, _typename, _fields=None, **kwargs):
  1302. total = kwargs.pop('total', True)
  1303. if _fields is None:
  1304. _fields = kwargs
  1305. elif kwargs:
  1306. raise TypeError("TypedDict takes either a dict or keyword arguments,"
  1307. " but not both")
  1308. ns = {'__annotations__': dict(_fields), '__total__': total}
  1309. try:
  1310. # Setting correct module is necessary to make typed dict classes pickleable.
  1311. ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
  1312. except (AttributeError, ValueError):
  1313. pass
  1314. return _TypedDictMeta(_typename, (), ns)
  1315. class _TypedDictMeta(type):
  1316. def __new__(cls, name, bases, ns, total=True):
  1317. # Create new typed dict class object.
  1318. # This method is called directly when TypedDict is subclassed,
  1319. # or via _typeddict_new when TypedDict is instantiated. This way
  1320. # TypedDict supports all three syntaxes described in its docstring.
  1321. # Subclasses and instances of TypedDict return actual dictionaries
  1322. # via _dict_new.
  1323. ns['__new__'] = _typeddict_new if name == 'TypedDict' else _dict_new
  1324. tp_dict = super(_TypedDictMeta, cls).__new__(cls, name, (dict,), ns)
  1325. anns = ns.get('__annotations__', {})
  1326. msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
  1327. anns = {n: typing._type_check(tp, msg) for n, tp in anns.items()}
  1328. for base in bases:
  1329. anns.update(base.__dict__.get('__annotations__', {}))
  1330. tp_dict.__annotations__ = anns
  1331. if not hasattr(tp_dict, '__total__'):
  1332. tp_dict.__total__ = total
  1333. return tp_dict
  1334. __instancecheck__ = __subclasscheck__ = _check_fails
  1335. TypedDict = _TypedDictMeta('TypedDict', (dict,), {})
  1336. TypedDict.__module__ = __name__
  1337. TypedDict.__doc__ = \
  1338. """A simple typed name space. At runtime it is equivalent to a plain dict.
  1339. TypedDict creates a dictionary type that expects all of its
  1340. instances to have a certain set of keys, with each key
  1341. associated with a value of a consistent type. This expectation
  1342. is not checked at runtime but is only enforced by type checkers.
  1343. Usage::
  1344. class Point2D(TypedDict):
  1345. x: int
  1346. y: int
  1347. label: str
  1348. a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
  1349. b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
  1350. assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
  1351. The type info could be accessed via Point2D.__annotations__. TypedDict
  1352. supports two additional equivalent forms::
  1353. Point2D = TypedDict('Point2D', x=int, y=int, label=str)
  1354. Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
  1355. The class syntax is only supported in Python 3.6+, while two other
  1356. syntax forms work for Python 2.7 and 3.2+
  1357. """
  1358. if PEP_560:
  1359. class _AnnotatedAlias(typing._GenericAlias, _root=True):
  1360. """Runtime representation of an annotated type.
  1361. At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't'
  1362. with extra annotations. The alias behaves like a normal typing alias,
  1363. instantiating is the same as instantiating the underlying type, binding
  1364. it to types is also the same.
  1365. """
  1366. def __init__(self, origin, metadata):
  1367. if isinstance(origin, _AnnotatedAlias):
  1368. metadata = origin.__metadata__ + metadata
  1369. origin = origin.__origin__
  1370. super().__init__(origin, origin)
  1371. self.__metadata__ = metadata
  1372. def copy_with(self, params):
  1373. assert len(params) == 1
  1374. new_type = params[0]
  1375. return _AnnotatedAlias(new_type, self.__metadata__)
  1376. def __repr__(self):
  1377. return "typing_extensions.Annotated[{}, {}]".format(
  1378. typing._type_repr(self.__origin__),
  1379. ", ".join(repr(a) for a in self.__metadata__)
  1380. )
  1381. def __reduce__(self):
  1382. return operator.getitem, (
  1383. Annotated, (self.__origin__,) + self.__metadata__
  1384. )
  1385. def __eq__(self, other):
  1386. if not isinstance(other, _AnnotatedAlias):
  1387. return NotImplemented
  1388. if self.__origin__ != other.__origin__:
  1389. return False
  1390. return self.__metadata__ == other.__metadata__
  1391. def __hash__(self):
  1392. return hash((self.__origin__, self.__metadata__))
  1393. class Annotated:
  1394. """Add context specific metadata to a type.
  1395. Example: Annotated[int, runtime_check.Unsigned] indicates to the
  1396. hypothetical runtime_check module that this type is an unsigned int.
  1397. Every other consumer of this type can ignore this metadata and treat
  1398. this type as int.
  1399. The first argument to Annotated must be a valid type (and will be in
  1400. the __origin__ field), the remaining arguments are kept as a tuple in
  1401. the __extra__ field.
  1402. Details:
  1403. - It's an error to call `Annotated` with less than two arguments.
  1404. - Nested Annotated are flattened::
  1405. Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
  1406. - Instantiating an annotated type is equivalent to instantiating the
  1407. underlying type::
  1408. Annotated[C, Ann1](5) == C(5)
  1409. - Annotated can be used as a generic type alias::
  1410. Optimized = Annotated[T, runtime.Optimize()]
  1411. Optimized[int] == Annotated[int, runtime.Optimize()]
  1412. OptimizedList = Annotated[List[T], runtime.Optimize()]
  1413. OptimizedList[int] == Annotated[List[int], runtime.Optimize()]
  1414. """
  1415. __slots__ = ()
  1416. def __new__(cls, *args, **kwargs):
  1417. raise TypeError("Type Annotated cannot be instantiated.")
  1418. @_tp_cache
  1419. def __class_getitem__(cls, params):
  1420. if not isinstance(params, tuple) or len(params) < 2:
  1421. raise TypeError("Annotated[...] should be used "
  1422. "with at least two arguments (a type and an "
  1423. "annotation).")
  1424. msg = "Annotated[t, ...]: t must be a type."
  1425. origin = typing._type_check(params[0], msg)
  1426. metadata = tuple(params[1:])
  1427. return _AnnotatedAlias(origin, metadata)
  1428. def __init_subclass__(cls, *args, **kwargs):
  1429. raise TypeError(
  1430. "Cannot subclass {}.Annotated".format(cls.__module__)
  1431. )
  1432. def _strip_annotations(t):
  1433. """Strips the annotations from a given type.
  1434. """
  1435. if isinstance(t, _AnnotatedAlias):
  1436. return _strip_annotations(t.__origin__)
  1437. if isinstance(t, typing._GenericAlias):
  1438. stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
  1439. if stripped_args == t.__args__:
  1440. return t
  1441. res = t.copy_with(stripped_args)
  1442. res._special = t._special
  1443. return res
  1444. return t
  1445. def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
  1446. """Return type hints for an object.
  1447. This is often the same as obj.__annotations__, but it handles
  1448. forward references encoded as string literals, adds Optional[t] if a
  1449. default value equal to None is set and recursively replaces all
  1450. 'Annotated[T, ...]' with 'T' (unless 'include_extras=True').
  1451. The argument may be a module, class, method, or function. The annotations
  1452. are returned as a dictionary. For classes, annotations include also
  1453. inherited members.
  1454. TypeError is raised if the argument is not of a type that can contain
  1455. annotations, and an empty dictionary is returned if no annotations are
  1456. present.
  1457. BEWARE -- the behavior of globalns and localns is counterintuitive
  1458. (unless you are familiar with how eval() and exec() work). The
  1459. search order is locals first, then globals.
  1460. - If no dict arguments are passed, an attempt is made to use the
  1461. globals from obj (or the respective module's globals for classes),
  1462. and these are also used as the locals. If the object does not appear
  1463. to have globals, an empty dictionary is used.
  1464. - If one dict argument is passed, it is used for both globals and
  1465. locals.
  1466. - If two dict arguments are passed, they specify globals and
  1467. locals, respectively.
  1468. """
  1469. hint = typing.get_type_hints(obj, globalns=globalns, localns=localns)
  1470. if include_extras:
  1471. return hint
  1472. return {k: _strip_annotations(t) for k, t in hint.items()}
  1473. elif HAVE_ANNOTATED:
  1474. def _is_dunder(name):
  1475. """Returns True if name is a __dunder_variable_name__."""
  1476. return len(name) > 4 and name.startswith('__') and name.endswith('__')
  1477. # Prior to Python 3.7 types did not have `copy_with`. A lot of the equality
  1478. # checks, argument expansion etc. are done on the _subs_tre. As a result we
  1479. # can't provide a get_type_hints function that strips out annotations.
  1480. class AnnotatedMeta(typing.GenericMeta):
  1481. """Metaclass for Annotated"""
  1482. def __new__(cls, name, bases, namespace, **kwargs):
  1483. if any(b is not object for b in bases):
  1484. raise TypeError("Cannot subclass " + str(Annotated))
  1485. return super().__new__(cls, name, bases, namespace, **kwargs)
  1486. @property
  1487. def __metadata__(self):
  1488. return self._subs_tree()[2]
  1489. def _tree_repr(self, tree):
  1490. cls, origin, metadata = tree
  1491. if not isinstance(origin, tuple):
  1492. tp_repr = typing._type_repr(origin)
  1493. else:
  1494. tp_repr = origin[0]._tree_repr(origin)
  1495. metadata_reprs = ", ".join(repr(arg) for arg in metadata)
  1496. return '%s[%s, %s]' % (cls, tp_repr, metadata_reprs)
  1497. def _subs_tree(self, tvars=None, args=None): # noqa
  1498. if self is Annotated:
  1499. return Annotated
  1500. res = super()._subs_tree(tvars=tvars, args=args)
  1501. # Flatten nested Annotated
  1502. if isinstance(res[1], tuple) and res[1][0] is Annotated:
  1503. sub_tp = res[1][1]
  1504. sub_annot = res[1][2]
  1505. return (Annotated, sub_tp, sub_annot + res[2])
  1506. return res
  1507. def _get_cons(self):
  1508. """Return the class used to create instance of this type."""
  1509. if self.__origin__ is None:
  1510. raise TypeError("Cannot get the underlying type of a "
  1511. "non-specialized Annotated type.")
  1512. tree = self._subs_tree()
  1513. while isinstance(tree, tuple) and tree[0] is Annotated:
  1514. tree = tree[1]
  1515. if isinstance(tree, tuple):
  1516. return tree[0]
  1517. else:
  1518. return tree
  1519. @_tp_cache
  1520. def __getitem__(self, params):
  1521. if not isinstance(params, tuple):
  1522. params = (params,)
  1523. if self.__origin__ is not None: # specializing an instantiated type
  1524. return super().__getitem__(params)
  1525. elif not isinstance(params, tuple) or len(params) < 2:
  1526. raise TypeError("Annotated[...] should be instantiated "
  1527. "with at least two arguments (a type and an "
  1528. "annotation).")
  1529. else:
  1530. msg = "Annotated[t, ...]: t must be a type."
  1531. tp = typing._type_check(params[0], msg)
  1532. metadata = tuple(params[1:])
  1533. return self.__class__(
  1534. self.__name__,
  1535. self.__bases__,
  1536. _no_slots_copy(self.__dict__),
  1537. tvars=_type_vars((tp,)),
  1538. # Metadata is a tuple so it won't be touched by _replace_args et al.
  1539. args=(tp, metadata),
  1540. origin=self,
  1541. )
  1542. def __call__(self, *args, **kwargs):
  1543. cons = self._get_cons()
  1544. result = cons(*args, **kwargs)
  1545. try:
  1546. result.__orig_class__ = self
  1547. except AttributeError:
  1548. pass
  1549. return result
  1550. def __getattr__(self, attr):
  1551. # For simplicity we just don't relay all dunder names
  1552. if self.__origin__ is not None and not _is_dunder(attr):
  1553. return getattr(self._get_cons(), attr)
  1554. raise AttributeError(attr)
  1555. def __setattr__(self, attr, value):
  1556. if _is_dunder(attr) or attr.startswith('_abc_'):
  1557. super().__setattr__(attr, value)
  1558. elif self.__origin__ is None:
  1559. raise AttributeError(attr)
  1560. else:
  1561. setattr(self._get_cons(), attr, value)
  1562. def __instancecheck__(self, obj):
  1563. raise TypeError("Annotated cannot be used with isinstance().")
  1564. def __subclasscheck__(self, cls):
  1565. raise TypeError("Annotated cannot be used with issubclass().")
  1566. class Annotated(metaclass=AnnotatedMeta):
  1567. """Add context specific metadata to a type.
  1568. Example: Annotated[int, runtime_check.Unsigned] indicates to the
  1569. hypothetical runtime_check module that this type is an unsigned int.
  1570. Every other consumer of this type can ignore this metadata and treat
  1571. this type as int.
  1572. The first argument to Annotated must be a valid type, the remaining
  1573. arguments are kept as a tuple in the __metadata__ field.
  1574. Details:
  1575. - It's an error to call `Annotated` with less than two arguments.
  1576. - Nested Annotated are flattened::
  1577. Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
  1578. - Instantiating an annotated type is equivalent to instantiating the
  1579. underlying type::
  1580. Annotated[C, Ann1](5) == C(5)
  1581. - Annotated can be used as a generic type alias::
  1582. Optimized = Annotated[T, runtime.Optimize()]
  1583. Optimized[int] == Annotated[int, runtime.Optimize()]
  1584. OptimizedList = Annotated[List[T], runtime.Optimize()]
  1585. OptimizedList[int] == Annotated[List[int], runtime.Optimize()]
  1586. """