add stub for contextvars.pyi (#1968)

Part of #1965.
This commit is contained in:
Jelle Zijlstra
2018-03-21 15:16:14 -07:00
committed by GitHub
parent 89cbd47344
commit bfe0b06953

View File

@@ -0,0 +1,27 @@
from typing import Any, Callable, ClassVar, Generic, Mapping, TypeVar, Union
_T = TypeVar('_T')
class ContextVar(Generic[_T]):
def __init__(self, name: str, *, default: _T = ...) -> None: ...
@property
def name(self) -> str: ...
def get(self, default: _T = ...) -> _T: ...
def set(self, value: _T) -> Token[_T]: ...
def reset(self, token: Token[_T]) -> None: ...
class Token(Generic[_T]):
@property
def var(self) -> ContextVar[_T]: ...
@property
def old_value(self) -> Any: ... # returns either _T or MISSING, but that's hard to express
MISSING: ClassVar[object]
def copy_context() -> Context: ...
# It doesn't make sense to make this generic, because for most Contexts each ContextVar will have
# a different value.
class Context(Mapping[ContextVar[Any], Any]):
def __init__(self) -> None: ...
def run(self, callable: Callable[..., _T], *args: Any, **kwargs: Any) -> _T: ...
def copy(self) -> Context: ...