-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
On 3.9 and lower, the contextvars types call themselves builtins.* which we can't and won't match. This improves naming fidelity for 3.10 and newer.
- Loading branch information
Showing
8 changed files
with
65 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import sys | ||
from collections.abc import Callable, Iterator, Mapping | ||
from typing import Any, ClassVar, Generic, TypeVar, final, overload | ||
from typing_extensions import ParamSpec | ||
|
||
if sys.version_info >= (3, 9): | ||
from types import GenericAlias | ||
|
||
_T = TypeVar("_T") | ||
_D = TypeVar("_D") | ||
_P = ParamSpec("_P") | ||
|
||
@final | ||
class ContextVar(Generic[_T]): | ||
@overload | ||
def __init__(self, name: str) -> None: ... | ||
@overload | ||
def __init__(self, name: str, *, default: _T) -> None: ... | ||
def __hash__(self) -> int: ... | ||
@property | ||
def name(self) -> str: ... | ||
@overload | ||
def get(self) -> _T: ... | ||
@overload | ||
def get(self, default: _T, /) -> _T: ... | ||
@overload | ||
def get(self, default: _D, /) -> _D | _T: ... | ||
def set(self, value: _T, /) -> Token[_T]: ... | ||
def reset(self, token: Token[_T], /) -> None: ... | ||
if sys.version_info >= (3, 9): | ||
def __class_getitem__(cls, item: Any, /) -> GenericAlias: ... | ||
|
||
@final | ||
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] | ||
if sys.version_info >= (3, 9): | ||
def __class_getitem__(cls, item: Any, /) -> GenericAlias: ... | ||
|
||
def copy_context() -> Context: ... | ||
|
||
# It doesn't make sense to make this generic, because for most Contexts each ContextVar will have | ||
# a different value. | ||
@final | ||
class Context(Mapping[ContextVar[Any], Any]): | ||
def __init__(self) -> None: ... | ||
@overload | ||
def get(self, key: ContextVar[_T], default: None = None, /) -> _T | None: ... | ||
@overload | ||
def get(self, key: ContextVar[_T], default: _T, /) -> _T: ... | ||
@overload | ||
def get(self, key: ContextVar[_T], default: _D, /) -> _T | _D: ... | ||
def run(self, callable: Callable[_P, _T], *args: _P.args, **kwargs: _P.kwargs) -> _T: ... | ||
def copy(self) -> Context: ... | ||
def __getitem__(self, key: ContextVar[_T], /) -> _T: ... | ||
def __iter__(self) -> Iterator[ContextVar[Any]]: ... | ||
def __len__(self) -> int: ... | ||
def __eq__(self, value: object, /) -> bool: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,3 @@ | ||
import sys | ||
from collections.abc import Callable, Iterator, Mapping | ||
from typing import Any, ClassVar, Generic, TypeVar, final, overload | ||
from typing_extensions import ParamSpec | ||
|
||
if sys.version_info >= (3, 9): | ||
from types import GenericAlias | ||
from _contextvars import Context as Context, ContextVar as ContextVar, Token as Token, copy_context as copy_context | ||
|
||
__all__ = ("Context", "ContextVar", "Token", "copy_context") | ||
|
||
_T = TypeVar("_T") | ||
_D = TypeVar("_D") | ||
_P = ParamSpec("_P") | ||
|
||
@final | ||
class ContextVar(Generic[_T]): | ||
@overload | ||
def __init__(self, name: str) -> None: ... | ||
@overload | ||
def __init__(self, name: str, *, default: _T) -> None: ... | ||
def __hash__(self) -> int: ... | ||
@property | ||
def name(self) -> str: ... | ||
@overload | ||
def get(self) -> _T: ... | ||
@overload | ||
def get(self, default: _T, /) -> _T: ... | ||
@overload | ||
def get(self, default: _D, /) -> _D | _T: ... | ||
def set(self, value: _T, /) -> Token[_T]: ... | ||
def reset(self, token: Token[_T], /) -> None: ... | ||
if sys.version_info >= (3, 9): | ||
def __class_getitem__(cls, item: Any, /) -> GenericAlias: ... | ||
|
||
@final | ||
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] | ||
if sys.version_info >= (3, 9): | ||
def __class_getitem__(cls, item: Any, /) -> GenericAlias: ... | ||
|
||
def copy_context() -> Context: ... | ||
|
||
# It doesn't make sense to make this generic, because for most Contexts each ContextVar will have | ||
# a different value. | ||
@final | ||
class Context(Mapping[ContextVar[Any], Any]): | ||
def __init__(self) -> None: ... | ||
@overload | ||
def get(self, key: ContextVar[_T], default: None = None, /) -> _T | None: ... | ||
@overload | ||
def get(self, key: ContextVar[_T], default: _T, /) -> _T: ... | ||
@overload | ||
def get(self, key: ContextVar[_T], default: _D, /) -> _T | _D: ... | ||
def run(self, callable: Callable[_P, _T], *args: _P.args, **kwargs: _P.kwargs) -> _T: ... | ||
def copy(self) -> Context: ... | ||
def __getitem__(self, key: ContextVar[_T], /) -> _T: ... | ||
def __iter__(self) -> Iterator[ContextVar[Any]]: ... | ||
def __len__(self) -> int: ... | ||
def __eq__(self, value: object, /) -> bool: ... |