2 from typing import Any, Callable, ClassVar, Generic, Iterator, Mapping, TypeVar, overload
4 if sys.version_info >= (3, 9):
5 from types import GenericAlias
10 class ContextVar(Generic[_T]):
11 def __init__(self, name: str, *, default: _T = ...) -> None: ...
13 def name(self) -> str: ...
15 def get(self) -> _T: ...
17 def get(self, default: _D | _T) -> _D | _T: ...
18 def set(self, value: _T) -> Token[_T]: ...
19 def reset(self, token: Token[_T]) -> None: ...
20 if sys.version_info >= (3, 9):
21 def __class_getitem__(cls, item: Any) -> GenericAlias: ...
23 class Token(Generic[_T]):
25 def var(self) -> ContextVar[_T]: ...
27 def old_value(self) -> Any: ... # returns either _T or MISSING, but that's hard to express
28 MISSING: ClassVar[object]
29 if sys.version_info >= (3, 9):
30 def __class_getitem__(cls, item: Any) -> GenericAlias: ...
32 def copy_context() -> Context: ...
34 # It doesn't make sense to make this generic, because for most Contexts each ContextVar will have
36 class Context(Mapping[ContextVar[Any], Any]):
37 def __init__(self) -> None: ...
39 def get(self, __key: ContextVar[Any]) -> Any | None: ...
41 def get(self, __key: ContextVar[Any], __default: Any | None) -> Any: ...
42 def run(self, callable: Callable[..., _T], *args: Any, **kwargs: Any) -> _T: ...
43 def copy(self) -> Context: ...
44 def __getitem__(self, key: ContextVar[Any]) -> Any: ...
45 def __iter__(self) -> Iterator[ContextVar[Any]]: ...
46 def __len__(self) -> int: ...