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