Skip to content

Commit 82c1d93

Browse files
author
Jonas Dedden
committed
General code quality improvements for the controller part of this repository.
Make sure exported symbols are correctly set in __init__.py with __all__. Renamed pytest fixture for creating a k8s namespace from ns to namespace since ns is a bit overloaded, especially in test_controller.py Introduce typehints in controller.py and test_controller.py to massively improve developer experience (previously, one just really had to guess what functions are for and which types are expected) Removed unused function parameters Made function headers (i.e. parameter [type] defintion) compatible with what `kopf.on.[create/change/...]' expects Introduce mypy (and missing stubs of external packages if available) Introduce mypy ignores for (still) untyped parts of the codebase for now
1 parent 77b9043 commit 82c1d93

File tree

7 files changed

+480
-237
lines changed

7 files changed

+480
-237
lines changed

dask_kubernetes/conftest.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
import sys
77
import tempfile
88
import uuid
9+
from typing import Final, Iterator
910

1011
import pytest
1112
from kopf.testing import KopfRunner
1213
from pytest_kind.cluster import KindCluster
1314

14-
DIR = pathlib.Path(__file__).parent.absolute()
15+
DIR: Final[pathlib.Path] = pathlib.Path(__file__).parent.absolute()
1516

1617

1718
def check_dependency(dependency):
@@ -26,24 +27,24 @@ def check_dependency(dependency):
2627
check_dependency("kubectl")
2728
check_dependency("docker")
2829

29-
DISABLE_LOGGERS = ["httpcore", "httpx"]
30+
DISABLE_LOGGERS: Final[list[str]] = ["httpcore", "httpx"]
3031

3132

32-
def pytest_configure():
33+
def pytest_configure() -> None:
3334
for logger_name in DISABLE_LOGGERS:
3435
logger = logging.getLogger(logger_name)
3536
logger.disabled = True
3637

3738

3839
@pytest.fixture()
39-
def kopf_runner(k8s_cluster, ns):
40+
def kopf_runner(k8s_cluster: KindCluster, namespace: str) -> KopfRunner:
4041
yield KopfRunner(
41-
["run", "-m", "dask_kubernetes.operator", "--verbose", "--namespace", ns]
42+
["run", "-m", "dask_kubernetes.operator", "--verbose", "--namespace", namespace]
4243
)
4344

4445

4546
@pytest.fixture(scope="session")
46-
def docker_image():
47+
def docker_image() -> str:
4748
image_name = "dask-kubernetes:dev"
4849
python_version = f"{sys.version_info.major}.{sys.version_info.minor}"
4950
subprocess.run(
@@ -62,7 +63,9 @@ def docker_image():
6263

6364

6465
@pytest.fixture(scope="session")
65-
def k8s_cluster(request, docker_image):
66+
def k8s_cluster(
67+
request: pytest.FixtureRequest, docker_image: str
68+
) -> Iterator[KindCluster]:
6669
image = None
6770
if version := os.environ.get("KUBERNETES_VERSION"):
6871
image = f"kindest/node:v{version}"
@@ -81,7 +84,7 @@ def k8s_cluster(request, docker_image):
8184

8285

8386
@pytest.fixture(scope="session", autouse=True)
84-
def install_istio(k8s_cluster):
87+
def install_istio(k8s_cluster: KindCluster) -> None:
8588
if bool(os.environ.get("TEST_ISTIO", False)):
8689
check_dependency("istioctl")
8790
subprocess.run(
@@ -93,15 +96,15 @@ def install_istio(k8s_cluster):
9396

9497

9598
@pytest.fixture(autouse=True)
96-
def ns(k8s_cluster):
99+
def namespace(k8s_cluster: KindCluster) -> Iterator[str]:
97100
ns = "dask-k8s-pytest-" + uuid.uuid4().hex[:10]
98101
k8s_cluster.kubectl("create", "ns", ns)
99102
yield ns
100103
k8s_cluster.kubectl("delete", "ns", ns, "--wait=false", "--ignore-not-found=true")
101104

102105

103106
@pytest.fixture(scope="session", autouse=True)
104-
def install_gateway(k8s_cluster):
107+
def install_gateway(k8s_cluster: KindCluster) -> Iterator[None]:
105108
if bool(os.environ.get("TEST_DASK_GATEWAY", False)):
106109
check_dependency("helm")
107110
# To ensure the operator can coexist with Gateway
@@ -137,11 +140,11 @@ def install_gateway(k8s_cluster):
137140

138141

139142
@pytest.fixture(scope="session", autouse=True)
140-
def customresources(k8s_cluster):
143+
def customresources(k8s_cluster: KindCluster) -> Iterator[None]:
141144
temp_dir = tempfile.TemporaryDirectory()
142145
crd_path = os.path.join(DIR, "operator", "customresources")
143146

144-
def run_generate(crd_path, patch_path, temp_path):
147+
def run_generate(crd_path: str, patch_path: str, temp_path: str) -> None:
145148
subprocess.run(
146149
["k8s-crd-resolver", "-r", "-j", patch_path, crd_path, temp_path],
147150
check=True,
@@ -162,5 +165,5 @@ def run_generate(crd_path, patch_path, temp_path):
162165

163166

164167
@pytest.fixture
165-
def anyio_backend():
168+
def anyio_backend() -> str:
166169
return "asyncio"

dask_kubernetes/operator/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@
55
make_worker_spec,
66
discover,
77
)
8+
9+
__all__ = [
10+
"KubeCluster",
11+
"make_cluster_spec",
12+
"make_scheduler_spec",
13+
"make_worker_spec",
14+
"discover",
15+
]

0 commit comments

Comments
 (0)