Skip to content

Commit cb64077

Browse files
authored
chore(lint): enforce Python type annotations, unused args, and mypy strictness (#1665)
* chore(lint): enforce Python type annotations, unused args, and mypy strictness Enable `ANN001`, `ANN201`, `ANN202`, `ARG`, and `SLF001` in `pyproject.toml` (keeping `per-file-ignores` scoped to test files, storage migrations, `examples/**` private stub access, framework callback hooks, and uniform CLI step callbacks) and add explicit parameter and return annotations across prod scripts while tightening mock lambda parameter names in `examples/`. Also enable `warn_unreachable = true`, `strict_equality = true`, and `enable_error_code = ["ignore-without-code"]` in `[tool.mypy]`, fix three unreachable branches, narrow one Optional assertion in `github_utils_test.py`, and specify explicit error codes on the eight `# type: ignore` suppressions in `xmc_entities_validator_test.py`. Assumptions: - Production Python functions require explicit parameter and return type annotations while test modules and generated migrations retain their existing exemptions. TAG=agy CONV=ba8411ef-c093-4f45-950a-b3bc1899268c * chore(lint): address PR #1665 review feedback on typing and ARG001 Replace overly broad `Any` annotations and file-level `ARG001` suppression raised during review of PR #1665: - Type `examples/chart_artifact_job/launch.py` dashboard, chart, artifact, and ESS wrapper signatures concretely via `if TYPE_CHECKING:` imports (`xdash._protos.messages_pb2`, `ExperimentStateGRpcApi`) and `artifact_pb2.Artifact`. - Remove the file-wide `ARG001` ignore on `tools/dev_env/deploy_dev_env.py` in `pyproject.toml` and prefix unused parameters in the 8 `step_*` callbacks with `_` so positional callers and tests remain intact without masking unused parameters in the rest of the file. - Narrow `tools/pr_review/update_pr_description.py:update_pr_description` return type from `Any` to `dict[str, Any]`. - Narrow `tools/release/dispatch_and_watch.py:_extract_page_url` parameter type from `Any` to `object` while keeping the `isinstance(..., dict)` guard reachable under `warn_unreachable`, with a unit test for non-dict inputs. Comment link: #1665 (comment) Comment link: #1665 (comment) Comment link: #1665 (comment) Comment link: #1665 (comment) TAG=agy CONV=ba8411ef-c093-4f45-950a-b3bc1899268c
1 parent f16344b commit cb64077

35 files changed

Lines changed: 145 additions & 95 deletions

File tree

‎cmd/ui/build_ui_oci.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def get_sha256(filepath: str) -> str:
5151
return h.hexdigest()
5252

5353

54-
def copy_flat(src_dir: str, dest_dir: str):
54+
def copy_flat(src_dir: str, dest_dir: str) -> None:
5555
"""Copies UI source files resolving symlinks into flat physical files.
5656
5757
Bazel sandboxes and monorepos often rely heavily on symlinks. When building
@@ -106,7 +106,7 @@ def copy_flat(src_dir: str, dest_dir: str):
106106
raise
107107

108108

109-
def main():
109+
def main() -> None:
110110
"""Main entrypoint for building UI container image.
111111
112112
Strategy:

‎cmd/ui/run_ui_tests.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def missing_dependencies_error(ui_dir: str) -> str | None:
3737
)
3838

3939

40-
def main():
40+
def main() -> None:
4141
"""Runs Angular UI unit tests in physical directory."""
4242
# Resolve the real physical directory of this script on the host system
4343
real_script_path = os.path.realpath(__file__)

‎examples/chart_artifact_job/launch.py‎

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import urllib.error
3939
import urllib.request
4040
from collections.abc import Sequence
41+
from typing import TYPE_CHECKING
4142

4243
import dotenv
4344
from absl import app
@@ -67,8 +68,14 @@
6768
work_unit_pb2,
6869
)
6970

71+
if TYPE_CHECKING:
72+
# Type-only imports resolved via xdash._protos and xmanager_cloud;
73+
# `from __future__ import annotations` keeps them out of the runtime path.
74+
from xdash._protos import messages_pb2 as dashboard_messages_pb2
75+
from xmanager_cloud.experiment_state_server import experiment_state_api
76+
7077
# Skip git push snapshot during local test launch
71-
snapshot_code.push_repo_state = lambda *args, **kwargs: type("Dummy", (), {"commit_url": None})()
78+
snapshot_code.push_repo_state = lambda *_args, **_kwargs: type("Dummy", (), {"commit_url": None})()
7279

7380
_DEFAULT_GATEWAY_URL = "http://ingestion-gateway-service.xmc.svc.cluster.local:8080/api/v1/metrics"
7481
_DEFAULT_TOTAL_STEPS = "60"
@@ -149,7 +156,7 @@ def _resolve_image_path() -> str:
149156
return f"us-docker.pkg.dev/{_require_env('PROJECT_ID')}/xmc/chart-artifact-job:latest"
150157

151158

152-
def build_training_chart(xid: int, wid: int):
159+
def build_training_chart(xid: int, wid: int) -> dashboard_messages_pb2.Chart:
153160
"""Builds the Chart plotting the metrics the worker writes to ClickHouse.
154161
155162
The chart is created inline with the dashboard, so the server renames it, generates
@@ -271,7 +278,10 @@ def dashboard_id_for(xid: int, wid: int) -> str:
271278

272279

273280
def _publish_dashboard_over_rest(
274-
dashboard_endpoint: str, dashboard, dashboard_id: str, chart
281+
dashboard_endpoint: str,
282+
dashboard: dashboard_messages_pb2.Dashboard,
283+
dashboard_id: str,
284+
chart: dashboard_messages_pb2.Chart,
275285
) -> None:
276286
"""Creates the Dashboard and its Chart through the transcoded REST route."""
277287
print(f"--> Publishing Dashboard '{dashboard.title}' to HTTP REST {dashboard_endpoint}...")
@@ -297,7 +307,10 @@ def _publish_dashboard_over_rest(
297307

298308

299309
def _publish_dashboard_over_grpc(
300-
dashboard_endpoint: str, dashboard, dashboard_id: str, chart
310+
dashboard_endpoint: str,
311+
dashboard: dashboard_messages_pb2.Dashboard,
312+
dashboard_id: str,
313+
chart: dashboard_messages_pb2.Chart,
301314
) -> None:
302315
"""Creates the Dashboard and its Chart through the Dashboard Service gRPC port."""
303316
print(f"--> Connecting to Dashboard Service gRPC at {dashboard_endpoint}...")
@@ -331,7 +344,11 @@ def create_experiment_dashboard(dashboard_endpoint: str, xid: int, wid: int) ->
331344
_publish_dashboard_over_grpc(dashboard_endpoint, dashboard, dashboard_id, chart)
332345

333346

334-
def create_chart_artifact(stub, work_unit_name: str, dashboard_url: str):
347+
def create_chart_artifact(
348+
stub: experiment_state_api.ExperimentStateGRpcApi,
349+
work_unit_name: str,
350+
dashboard_url: str,
351+
) -> artifact_pb2.Artifact:
335352
"""Attaches the ChartArtifact to the work unit.
336353
337354
Only `title` and `metric_names` may be supplied: the server rejects client-set
@@ -358,7 +375,11 @@ def create_chart_artifact(stub, work_unit_name: str, dashboard_url: str):
358375
return stub.create_artifact(request)
359376

360377

361-
def create_dashboard_link_artifact(stub, work_unit_name: str, dashboard_url: str):
378+
def create_dashboard_link_artifact(
379+
stub: experiment_state_api.ExperimentStateGRpcApi,
380+
work_unit_name: str,
381+
dashboard_url: str,
382+
) -> artifact_pb2.Artifact:
362383
"""Attaches a URL artifact linking the work unit to its plotted dashboard.
363384
364385
A chart artifact renders as its metric list and provisioned chart resource — the
@@ -413,7 +434,8 @@ def main(argv: Sequence[str]) -> None:
413434
experiment_title=_EXPERIMENT_TITLE,
414435
# No `acls`: XMC makes you (the creator) the owner. See docs/authentication.md.
415436
) as experiment:
416-
stub = experiment._experiment_state_stub # pylint: disable=protected-access
437+
# pylint: disable=protected-access
438+
stub: experiment_state_api.ExperimentStateGRpcApi = experiment._experiment_state_stub
417439
xid = int(experiment.resource_name.split("/")[-1])
418440
print(f"\n[1/4] Created Experiment successfully: {experiment.resource_name} (XID: {xid})")
419441

‎examples/clickhouse_metrics_job/launch.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
)
4747

4848
# Skip git push snapshot during local test launch
49-
snapshot_code.push_repo_state = lambda *args, **kwargs: type("Dummy", (), {"commit_url": None})()
49+
snapshot_code.push_repo_state = lambda *_args, **_kwargs: type("Dummy", (), {"commit_url": None})()
5050

5151

5252
def _get_env_bool(key: str, default: bool = True) -> bool:

‎examples/init_container_job/launch.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
)
3232

3333
# Skip git push snapshot during local test launch
34-
snapshot_code.push_repo_state = lambda *args, **kwargs: type("Dummy", (), {"commit_url": None})()
34+
snapshot_code.push_repo_state = lambda *_args, **_kwargs: type("Dummy", (), {"commit_url": None})()
3535

3636

3737
def main(argv: Sequence[str]) -> None:

‎examples/managed_xprof_artifact_job/launch.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
)
3232

3333
# Bypass remote git repository check for standalone launch execution
34-
snapshot_code.push_repo_state = lambda *args, **kwargs: type("Dummy", (), {"commit_url": None})()
34+
snapshot_code.push_repo_state = lambda *_args, **_kwargs: type("Dummy", (), {"commit_url": None})()
3535

3636

3737
def _build_work_unit(

‎examples/managed_xprof_artifact_job/worker.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import os
1919
import time
20+
from collections.abc import Sequence
2021

2122
import jax
2223
import jax.numpy as jnp
@@ -83,7 +84,7 @@ def _run_workload(
8384
time.sleep(1)
8485

8586

86-
def main(argv):
87+
def main(argv: Sequence[str]) -> None:
8788
if len(argv) > 1:
8889
raise app.UsageError("Too many command-line arguments.")
8990

‎examples/metrics_dashboard_job/launch.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
from xdash._protos import messages_pb2 as dashboard_messages_pb2
5656

5757
# Skip git push snapshot during local test launch
58-
snapshot_code.push_repo_state = lambda *args, **kwargs: type("Dummy", (), {"commit_url": None})()
58+
snapshot_code.push_repo_state = lambda *_args, **_kwargs: type("Dummy", (), {"commit_url": None})()
5959

6060
DEFAULT_GATEWAY_URL = "http://ingestion-gateway-service.xmc.svc.cluster.local:8080/api/v1/metrics"
6161

‎examples/metrics_job/launch.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
)
3232

3333
# Skip git push snapshot during local test launch
34-
snapshot_code.push_repo_state = lambda *args, **kwargs: type("Dummy", (), {"commit_url": None})()
34+
snapshot_code.push_repo_state = lambda *_args, **_kwargs: type("Dummy", (), {"commit_url": None})()
3535

3636

3737
def main(argv: Sequence[str]) -> None:

‎examples/multipod_job/launch.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
)
4040

4141
# Skip git push snapshot during local test launch so SSH passphrase is not prompted
42-
snapshot_code.push_repo_state = lambda *args, **kwargs: type("Dummy", (), {"commit_url": None})()
42+
snapshot_code.push_repo_state = lambda *_args, **_kwargs: type("Dummy", (), {"commit_url": None})()
4343

4444

4545
def main(argv: Sequence[str]) -> None:

0 commit comments

Comments
 (0)