From c4aa48a62604705e9d01e31f30073608c5e0d0c4 Mon Sep 17 00:00:00 2001 From: Max Muoto Date: Sat, 10 May 2025 16:33:33 -0500 Subject: [PATCH] Add context manager for `contextvars.Token` (3.14) (#13997) --- stdlib/@tests/stubtest_allowlists/py314.txt | 4 ---- stdlib/_contextvars.pyi | 8 +++++++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/stdlib/@tests/stubtest_allowlists/py314.txt b/stdlib/@tests/stubtest_allowlists/py314.txt index bb6b5ecf7..9f79c767b 100644 --- a/stdlib/@tests/stubtest_allowlists/py314.txt +++ b/stdlib/@tests/stubtest_allowlists/py314.txt @@ -6,8 +6,6 @@ _asyncio.all_tasks _asyncio.future_add_to_awaited_by _asyncio.future_discard_from_awaited_by _compression -_contextvars.Token.__enter__ -_contextvars.Token.__exit__ _ctypes.POINTER _ctypes.byref _ctypes.pointer @@ -92,8 +90,6 @@ concurrent.futures.thread._worker configparser.__all__ configparser.InvalidWriteError configparser.UnnamedSectionDisabledError -contextvars.Token.__enter__ -contextvars.Token.__exit__ ctypes.POINTER ctypes.byref ctypes.memoryview_at diff --git a/stdlib/_contextvars.pyi b/stdlib/_contextvars.pyi index 33df799a7..e2e2e4df9 100644 --- a/stdlib/_contextvars.pyi +++ b/stdlib/_contextvars.pyi @@ -1,5 +1,6 @@ +import sys from collections.abc import Callable, Iterator, Mapping -from types import GenericAlias +from types import GenericAlias, TracebackType from typing import Any, ClassVar, Generic, TypeVar, final, overload from typing_extensions import ParamSpec, Self @@ -35,6 +36,11 @@ class Token(Generic[_T]): MISSING: ClassVar[object] __hash__: ClassVar[None] # type: ignore[assignment] def __class_getitem__(cls, item: Any, /) -> GenericAlias: ... + if sys.version_info >= (3, 14): + def __enter__(self) -> Self: ... + def __exit__( + self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None + ) -> None: ... def copy_context() -> Context: ...