11from collections .abc import Generator
22from contextlib import contextmanager
3- from typing import Annotated , Any , Callable , Generic , Literal , Optional , Protocol , TypeVar , Union
3+ from typing import (
4+ Annotated ,
5+ Any ,
6+ Callable ,
7+ Generic ,
8+ Literal ,
9+ Optional ,
10+ Protocol ,
11+ TypeVar ,
12+ Union ,
13+ cast ,
14+ )
415
516import yaml
617from kubernetes .client import CoreV1Api , V1Status
920 # XXX: This function is missing in the stubs package
1021 new_client_from_config_dict , # pyright: ignore[reportAttributeAccessIssue]
1122)
12-
13- # XXX: The watch module is missing in the stubs package
14- from kubernetes .watch import Watch # pyright: ignore[reportMissingImports]
23+ from kubernetes .watch import Watch
1524from pydantic import Field
1625from typing_extensions import ParamSpec , TypedDict
1726from urllib3 .exceptions import HTTPError
@@ -157,7 +166,8 @@ def watch_events(
157166 method : Callable [P , ObjectList [T ]], * args : P .args , ** kwargs : P .kwargs
158167) -> Generator [Generator [tuple [str , T ], None , None ], None , None ]:
159168 watch = Watch ()
160- gen = _watch_events_gen (watch .stream (method , * args , ** kwargs ))
169+ inner_gen = cast (Generator [_EventDict [T ], None , None ], watch .stream (method , * args , ** kwargs ))
170+ gen = _watch_events_gen (inner_gen )
161171 try :
162172 yield gen
163173 finally :
@@ -182,8 +192,11 @@ class _ErrorEventDict(TypedDict):
182192 object : V1Status
183193
184194
195+ _EventDict = Union [_StateEventDict [T ], _BookmarkEventDict [T ], _ErrorEventDict ]
196+
197+
185198def _watch_events_gen (
186- gen : Generator [Union [ _StateEventDict [ T ], _BookmarkEventDict [ T ], _ErrorEventDict ], None , None ],
199+ gen : Generator [_EventDict [ T ], None , None ],
187200) -> Generator [tuple [str , T ], None , None ]:
188201 try :
189202 for event in gen :
0 commit comments