From 80a0a75f5fc72ab43abbdc4dae48eff32d53fe2a Mon Sep 17 00:00:00 2001 From: stevenjackson121 Date: Tue, 14 Aug 2018 11:05:02 -0400 Subject: [PATCH] Add stubs for `contextvars` backport (3.5 and 3.6) (#2378) * Add stubs for contextvars backport (3.5 and 3.6) * Add contextvars to tests/check_consistent.py --- tests/check_consistent.py | 1 + third_party/3.5/contextvars.pyi | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 third_party/3.5/contextvars.pyi diff --git a/tests/check_consistent.py b/tests/check_consistent.py index b1dda90c4..08e9bd5a1 100755 --- a/tests/check_consistent.py +++ b/tests/check_consistent.py @@ -24,6 +24,7 @@ consistent_files = [ {'stdlib/3/concurrent/futures/process.pyi', 'third_party/2/concurrent/futures/process.pyi'}, {'stdlib/3.7/dataclasses.pyi', 'third_party/3/dataclasses.pyi'}, {'stdlib/3/pathlib.pyi', 'third_party/2/pathlib2.pyi'}, + {'stdlib/3.7/contextvars.pyi', 'third_party/3.5/contextvars.pyi'}, ] def main(): diff --git a/third_party/3.5/contextvars.pyi b/third_party/3.5/contextvars.pyi new file mode 100644 index 000000000..ab2ae9e5f --- /dev/null +++ b/third_party/3.5/contextvars.pyi @@ -0,0 +1,30 @@ +from typing import Any, Callable, ClassVar, Generic, Iterator, 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: ... + def __getitem__(self, key: ContextVar[Any]) -> Any: ... + def __iter__(self) -> Iterator[ContextVar[Any]]: ... + def __len__(self) -> int: ...