From 801c5c94958e1a7710892c2d962e68adce0270a6 Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Sat, 28 Dec 2024 08:00:04 -0800 Subject: [PATCH] improve identity of threading module's Lock and RLock (#12966) --- stdlib/@tests/stubtest_allowlists/common.txt | 1 - stdlib/_thread.pyi | 40 +++++++++++++++----- stdlib/threading.pyi | 20 +++------- 3 files changed, 36 insertions(+), 25 deletions(-) diff --git a/stdlib/@tests/stubtest_allowlists/common.txt b/stdlib/@tests/stubtest_allowlists/common.txt index 496c62a34faf..d67417d484c8 100644 --- a/stdlib/@tests/stubtest_allowlists/common.txt +++ b/stdlib/@tests/stubtest_allowlists/common.txt @@ -31,7 +31,6 @@ turtledemo\..+ # TODO: Module members that exist at runtime, but are missing from stubs # ====================================================================== -_thread.RLock tkinter.Misc.config diff --git a/stdlib/_thread.pyi b/stdlib/_thread.pyi index f0b70ed2a0b0..378ac2423757 100644 --- a/stdlib/_thread.pyi +++ b/stdlib/_thread.pyi @@ -13,17 +13,11 @@ error = RuntimeError def _count() -> int: ... @final -class LockType: +class RLock: def acquire(self, blocking: bool = True, timeout: float = -1) -> bool: ... def release(self) -> None: ... - def locked(self) -> bool: ... - def acquire_lock(self, blocking: bool = True, timeout: float = -1) -> bool: ... - def release_lock(self) -> None: ... - def locked_lock(self) -> bool: ... - def __enter__(self) -> bool: ... - def __exit__( - self, type: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None - ) -> None: ... + __enter__ = acquire + def __exit__(self, t: type[BaseException] | None, v: BaseException | None, tb: TracebackType | None) -> None: ... if sys.version_info >= (3, 13): @final @@ -37,7 +31,33 @@ if sys.version_info >= (3, 13): def start_joinable_thread( function: Callable[[], object], handle: _ThreadHandle | None = None, daemon: bool = True ) -> _ThreadHandle: ... - lock = LockType + @final + class lock: + def acquire(self, blocking: bool = True, timeout: float = -1) -> bool: ... + def release(self) -> None: ... + def locked(self) -> bool: ... + def acquire_lock(self, blocking: bool = True, timeout: float = -1) -> bool: ... + def release_lock(self) -> None: ... + def locked_lock(self) -> bool: ... + def __enter__(self) -> bool: ... + def __exit__( + self, type: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None + ) -> None: ... + + LockType = lock +else: + @final + class LockType: + def acquire(self, blocking: bool = True, timeout: float = -1) -> bool: ... + def release(self) -> None: ... + def locked(self) -> bool: ... + def acquire_lock(self, blocking: bool = True, timeout: float = -1) -> bool: ... + def release_lock(self) -> None: ... + def locked_lock(self) -> bool: ... + def __enter__(self) -> bool: ... + def __exit__( + self, type: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None + ) -> None: ... @overload def start_new_thread(function: Callable[[Unpack[_Ts]], object], args: tuple[Unpack[_Ts]], /) -> int: ... diff --git a/stdlib/threading.pyi b/stdlib/threading.pyi index c441a04681e2..efeea69d0234 100644 --- a/stdlib/threading.pyi +++ b/stdlib/threading.pyi @@ -100,30 +100,22 @@ class Thread: class _DummyThread(Thread): def __init__(self) -> None: ... -@final -class Lock: - def __enter__(self) -> bool: ... - def __exit__( - self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None - ) -> None: ... - def acquire(self, blocking: bool = ..., timeout: float = ...) -> bool: ... - def release(self) -> None: ... - def locked(self) -> bool: ... - def acquire_lock(self, blocking: bool = ..., timeout: float = ...) -> bool: ... # undocumented - def release_lock(self) -> None: ... # undocumented - def locked_lock(self) -> bool: ... # undocumented +# This is actually the function _thread.allocate_lock for <= 3.12 +Lock = _thread.LockType +# Python implementation of RLock. @final class _RLock: + _count: int def acquire(self, blocking: bool = True, timeout: float = -1) -> bool: ... def release(self) -> None: ... __enter__ = acquire def __exit__(self, t: type[BaseException] | None, v: BaseException | None, tb: TracebackType | None) -> None: ... -RLock = _RLock +RLock = _thread.RLock # Actually a function at runtime. class Condition: - def __init__(self, lock: Lock | _RLock | None = None) -> None: ... + def __init__(self, lock: Lock | _RLock | RLock | None = None) -> None: ... def __enter__(self) -> bool: ... def __exit__( self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None