11import queue
2+ import sys
23from collections .abc import Callable , Iterable , Mapping , Set as AbstractSet
34from threading import Lock , Semaphore , Thread
45from types import GenericAlias
5- from typing import Any , Generic , TypeVar , overload
6- from typing_extensions import TypeVarTuple , Unpack
6+ from typing import Any , Generic , Protocol , TypeVar , overload , type_check_only
7+ from typing_extensions import Self , TypeAlias , TypeVarTuple , Unpack
78from weakref import ref
89
910from ._base import BrokenExecutor , Executor , Future
@@ -18,25 +19,71 @@ def _python_exit() -> None: ...
1819
1920_S = TypeVar ("_S" )
2021
21- class _WorkItem (Generic [_S ]):
22- future : Future [_S ]
23- fn : Callable [..., _S ]
24- args : Iterable [Any ]
25- kwargs : Mapping [str , Any ]
26- def __init__ (self , future : Future [_S ], fn : Callable [..., _S ], args : Iterable [Any ], kwargs : Mapping [str , Any ]) -> None : ...
27- def run (self ) -> None : ...
28- def __class_getitem__ (cls , item : Any , / ) -> GenericAlias : ...
29-
30- def _worker (
31- executor_reference : ref [Any ],
32- work_queue : queue .SimpleQueue [Any ],
33- initializer : Callable [[Unpack [_Ts ]], object ],
34- initargs : tuple [Unpack [_Ts ]],
35- ) -> None : ...
22+ _Task : TypeAlias = tuple [Callable [..., Any ], tuple [Any , ...], dict [str , Any ]]
23+
24+ _C = TypeVar ("_C" , bound = Callable [..., object ])
25+ _KT = TypeVar ("_KT" , bound = str )
26+ _VT = TypeVar ("_VT" )
27+
28+ @type_check_only
29+ class _ResolveTaskFunc (Protocol ):
30+ def __call__ (
31+ self , func : _C , args : tuple [Unpack [_Ts ]], kwargs : dict [_KT , _VT ]
32+ ) -> tuple [_C , tuple [Unpack [_Ts ]], dict [_KT , _VT ]]: ...
33+
34+ if sys .version_info >= (3 , 14 ):
35+ class WorkerContext :
36+ @overload
37+ @classmethod
38+ def prepare (
39+ cls , initializer : Callable [[Unpack [_Ts ]], object ], initargs : tuple [Unpack [_Ts ]]
40+ ) -> tuple [Callable [[], Self ], _ResolveTaskFunc ]: ...
41+ @overload
42+ @classmethod
43+ def prepare (
44+ cls , initializer : Callable [[], object ], initargs : tuple [()]
45+ ) -> tuple [Callable [[], Self ], _ResolveTaskFunc ]: ...
46+ @overload
47+ def __init__ (self , initializer : Callable [[Unpack [_Ts ]], object ], initargs : tuple [Unpack [_Ts ]]) -> None : ...
48+ @overload
49+ def __init__ (self , initializer : Callable [[], object ], initargs : tuple [()]) -> None : ...
50+ def initialize (self ) -> None : ...
51+ def finalize (self ) -> None : ...
52+ def run (self , task : _Task ) -> None : ...
53+
54+ if sys .version_info >= (3 , 14 ):
55+ class _WorkItem (Generic [_S ]):
56+ future : Future [Any ]
57+ task : _Task
58+ def __init__ (self , future : Future [Any ], task : _Task ) -> None : ...
59+ def run (self , ctx : WorkerContext ) -> None : ...
60+ def __class_getitem__ (cls , item : Any , / ) -> GenericAlias : ...
61+
62+ def _worker (executor_reference : ref [Any ], ctx : WorkerContext , work_queue : queue .SimpleQueue [Any ]) -> None : ...
63+
64+ else :
65+ class _WorkItem (Generic [_S ]):
66+ future : Future [_S ]
67+ fn : Callable [..., _S ]
68+ args : Iterable [Any ]
69+ kwargs : Mapping [str , Any ]
70+ def __init__ (self , future : Future [_S ], fn : Callable [..., _S ], args : Iterable [Any ], kwargs : Mapping [str , Any ]) -> None : ...
71+ def run (self ) -> None : ...
72+ def __class_getitem__ (cls , item : Any , / ) -> GenericAlias : ...
73+
74+ def _worker (
75+ executor_reference : ref [Any ],
76+ work_queue : queue .SimpleQueue [Any ],
77+ initializer : Callable [[Unpack [_Ts ]], object ],
78+ initargs : tuple [Unpack [_Ts ]],
79+ ) -> None : ...
3680
3781class BrokenThreadPool (BrokenExecutor ): ...
3882
3983class ThreadPoolExecutor (Executor ):
84+ if sys .version_info >= (3 , 14 ):
85+ BROKEN : type [BrokenThreadPool ]
86+
4087 _max_workers : int
4188 _idle_semaphore : Semaphore
4289 _threads : AbstractSet [Thread ]
@@ -47,6 +94,19 @@ class ThreadPoolExecutor(Executor):
4794 _initializer : Callable [..., None ] | None
4895 _initargs : tuple [Any , ...]
4996 _work_queue : queue .SimpleQueue [_WorkItem [Any ]]
97+
98+ if sys .version_info >= (3 , 14 ):
99+ @overload
100+ @classmethod
101+ def prepare_context (
102+ cls , initializer : Callable [[], object ], initargs : tuple [()]
103+ ) -> tuple [Callable [[], Self ], _ResolveTaskFunc ]: ...
104+ @overload
105+ @classmethod
106+ def prepare_context (
107+ cls , initializer : Callable [[Unpack [_Ts ]], object ], initargs : tuple [Unpack [_Ts ]]
108+ ) -> tuple [Callable [[], Self ], _ResolveTaskFunc ]: ...
109+
50110 @overload
51111 def __init__ (
52112 self ,
0 commit comments