Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions stdlib/3.7/contextvars.pyi
Original file line number Diff line number Diff line change
@@ -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: ...
Copy link
Member

@1st1 1st1 Mar 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default is not a required parameter. It's not typing.Optional either, as it is either of type _T or not passed at all (None is not a valid value).

A couple of examples:

# valid:
int_var: ContextVar[int] = ContextVar(default=1)
int_var: ContextVar[int] = ContextVar()

# invalid:
int_var: ContextVar[int] = ContextVar(default=None)

and

# valid:
int_var: ContextVar[Optional[int]] = ContextVar(default=1)
int_var: ContextVar[Optional[int]] = ContextVar(default=None)
int_var: ContextVar[Optional[int]] = ContextVar()

Here's a pure Python implementaion of PEP 567 / ContextVar: https://github.com/MagicStack/contextvars/blob/master/contextvars/__init__.py#L111

I'm not sure how to express this in terms of typing. Maybe

class ContextVar(Generic[_T]):
    def __init__(self, name: str, *, default: _T=...) -> None: ...

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can write *, default: _T = .... Sorry for missing this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jelle: If = ... doesn't work, you can use an overload (once without, once with default).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NP!

@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: ...