Skip to content

Commit 86e1e03

Browse files
authored
Fix Kubernetes backend utils.py typing (#3889)
See: lexdene/kubernetes-stubs#11
1 parent 117195d commit 86e1e03

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ dev = [
152152
"testcontainers>=4.9.2",
153153
"pytest-xdist>=3.6.1",
154154
"pyinstrument>=5.0.0",
155-
"kubernetes-stubs-elephant-fork",
155+
"kubernetes-stubs-elephant-fork>=35.0.0.post3",
156156
# docs
157157
"dstack[server]; python_version >= '3.11'",
158158
"dstack-plugin-server; python_version >= '3.11'",

src/dstack/_internal/core/backends/kubernetes/utils.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
from collections.abc import Generator
22
from 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

516
import yaml
617
from kubernetes.client import CoreV1Api, V1Status
@@ -9,9 +20,7 @@
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
1524
from pydantic import Field
1625
from typing_extensions import ParamSpec, TypedDict
1726
from 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+
185198
def _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

Comments
 (0)