Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

133 rindas
5.1 KiB

  1. # Type hints copied from the typeshed project under the Apache License 2.0
  2. # https://github.com/python/typeshed/blob/64c85cdd449ccaff90b546676220c9ecfa6e697f/LICENSE
  3. import sys
  4. from types import TracebackType
  5. from typing import (
  6. IO,
  7. Any,
  8. AsyncContextManager,
  9. AsyncIterator,
  10. Awaitable,
  11. Callable,
  12. ContextManager,
  13. Iterator,
  14. Optional,
  15. Type,
  16. TypeVar,
  17. overload,
  18. )
  19. from typing_extensions import ParamSpec, Protocol
  20. # contextlib2 API adaptation notes:
  21. # * the various 'if True:' guards replace sys.version checks in the original
  22. # typeshed file (those APIs are available on all supported versions)
  23. # * deliberately omitted APIs are listed in `dev/mypy.allowlist`
  24. # (e.g. deprecated experimental APIs that never graduated to the stdlib)
  25. AbstractContextManager = ContextManager
  26. if True:
  27. AbstractAsyncContextManager = AsyncContextManager
  28. _T = TypeVar("_T")
  29. _T_co = TypeVar("_T_co", covariant=True)
  30. _T_io = TypeVar("_T_io", bound=Optional[IO[str]])
  31. _F = TypeVar("_F", bound=Callable[..., Any])
  32. _P = ParamSpec("_P")
  33. _ExitFunc = Callable[[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]], bool]
  34. _CM_EF = TypeVar("_CM_EF", ContextManager[Any], _ExitFunc)
  35. class _GeneratorContextManager(ContextManager[_T_co]):
  36. def __call__(self, func: _F) -> _F: ...
  37. # type ignore to deal with incomplete ParamSpec support in mypy
  38. def contextmanager(func: Callable[_P, Iterator[_T]]) -> Callable[_P, _GeneratorContextManager[_T]]: ... # type: ignore
  39. if True:
  40. def asynccontextmanager(func: Callable[_P, AsyncIterator[_T]]) -> Callable[_P, AsyncContextManager[_T]]: ... # type: ignore
  41. class _SupportsClose(Protocol):
  42. def close(self) -> object: ...
  43. _SupportsCloseT = TypeVar("_SupportsCloseT", bound=_SupportsClose)
  44. class closing(ContextManager[_SupportsCloseT]):
  45. def __init__(self, thing: _SupportsCloseT) -> None: ...
  46. if True:
  47. class _SupportsAclose(Protocol):
  48. async def aclose(self) -> object: ...
  49. _SupportsAcloseT = TypeVar("_SupportsAcloseT", bound=_SupportsAclose)
  50. class aclosing(AsyncContextManager[_SupportsAcloseT]):
  51. def __init__(self, thing: _SupportsAcloseT) -> None: ...
  52. _AF = TypeVar("_AF", bound=Callable[..., Awaitable[Any]])
  53. class AsyncContextDecorator:
  54. def __call__(self, func: _AF) -> _AF: ...
  55. class suppress(ContextManager[None]):
  56. def __init__(self, *exceptions: Type[BaseException]) -> None: ...
  57. def __exit__(
  58. self, exctype: Optional[Type[BaseException]], excinst: Optional[BaseException], exctb: Optional[TracebackType]
  59. ) -> bool: ...
  60. class redirect_stdout(ContextManager[_T_io]):
  61. def __init__(self, new_target: _T_io) -> None: ...
  62. class redirect_stderr(ContextManager[_T_io]):
  63. def __init__(self, new_target: _T_io) -> None: ...
  64. class ContextDecorator:
  65. def __call__(self, func: _F) -> _F: ...
  66. _U = TypeVar("_U", bound=ExitStack)
  67. class ExitStack(ContextManager[ExitStack]):
  68. def __init__(self) -> None: ...
  69. def enter_context(self, cm: ContextManager[_T]) -> _T: ...
  70. def push(self, exit: _CM_EF) -> _CM_EF: ...
  71. def callback(self, callback: Callable[..., Any], *args: Any, **kwds: Any) -> Callable[..., Any]: ...
  72. def pop_all(self: _U) -> _U: ...
  73. def close(self) -> None: ...
  74. def __enter__(self: _U) -> _U: ...
  75. def __exit__(
  76. self,
  77. __exc_type: Optional[Type[BaseException]],
  78. __exc_value: Optional[BaseException],
  79. __traceback: Optional[TracebackType],
  80. ) -> bool: ...
  81. if True:
  82. _S = TypeVar("_S", bound=AsyncExitStack)
  83. _ExitCoroFunc = Callable[[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]], Awaitable[bool]]
  84. _CallbackCoroFunc = Callable[..., Awaitable[Any]]
  85. _ACM_EF = TypeVar("_ACM_EF", AsyncContextManager[Any], _ExitCoroFunc)
  86. class AsyncExitStack(AsyncContextManager[AsyncExitStack]):
  87. def __init__(self) -> None: ...
  88. def enter_context(self, cm: ContextManager[_T]) -> _T: ...
  89. def enter_async_context(self, cm: AsyncContextManager[_T]) -> Awaitable[_T]: ...
  90. def push(self, exit: _CM_EF) -> _CM_EF: ...
  91. def push_async_exit(self, exit: _ACM_EF) -> _ACM_EF: ...
  92. def callback(self, callback: Callable[..., Any], *args: Any, **kwds: Any) -> Callable[..., Any]: ...
  93. def push_async_callback(self, callback: _CallbackCoroFunc, *args: Any, **kwds: Any) -> _CallbackCoroFunc: ...
  94. def pop_all(self: _S) -> _S: ...
  95. def aclose(self) -> Awaitable[None]: ...
  96. def __aenter__(self: _S) -> Awaitable[_S]: ...
  97. def __aexit__(
  98. self,
  99. __exc_type: Optional[Type[BaseException]],
  100. __exc_value: Optional[BaseException],
  101. __traceback: Optional[TracebackType],
  102. ) -> Awaitable[bool]: ...
  103. if True:
  104. class nullcontext(AbstractContextManager[_T]):
  105. enter_result: _T
  106. @overload
  107. def __init__(self: nullcontext[None], enter_result: None = ...) -> None: ...
  108. @overload
  109. def __init__(self: nullcontext[_T], enter_result: _T) -> None: ...
  110. def __enter__(self) -> _T: ...
  111. def __exit__(self, *exctype: Any) -> bool: ...