Skip to content

Commit 45c648f

Browse files
authored
[chore]: Fix add_row_from_dict() typing issues (#3739)
Drop unused index-based mapping from `add_row_from_dict`. This simplifies its interface and fixes the `"str" is not the same as "str | int"` typing issue for callers that do not explicitly annotate `data` as `dict[str | int, Any]`.
1 parent 10b076e commit 45c648f

File tree

7 files changed

+18
-26
lines changed

7 files changed

+18
-26
lines changed

src/dstack/_internal/cli/commands/export.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import argparse
2-
from typing import Any, Union
32

43
from rich.table import Table
54

@@ -148,7 +147,7 @@ def print_exports_table(exports: list[Export]):
148147
)
149148
importers = ", ".join([i.project_name for i in export.imports]) if export.imports else "-"
150149

151-
row: dict[Union[str, int], Any] = {
150+
row = {
152151
"NAME": export.name,
153152
"FLEETS": fleets,
154153
"IMPORTERS": importers,

src/dstack/_internal/cli/commands/import_.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import argparse
2-
from typing import Any, Union
32

43
from rich.table import Table
54

@@ -44,7 +43,7 @@ def print_imports_table(imports: list[Import]):
4443
else "-"
4544
)
4645

47-
row: dict[Union[str, int], Any] = {
46+
row = {
4847
"NAME": name,
4948
"FLEETS": fleets,
5049
}

src/dstack/_internal/cli/commands/metrics.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import argparse
22
import time
3-
from typing import Any, Dict, List, Optional, Union
3+
from typing import Any, List, Optional
44

55
from rich.live import Live
66
from rich.table import Table
@@ -79,7 +79,7 @@ def _get_metrics_table(run: Run, metrics: List[JobMetrics]) -> Table:
7979
table.add_column("MEMORY")
8080
table.add_column("GPU")
8181

82-
run_row: Dict[Union[str, int], Any] = {"NAME": run.name, "STATUS": run.status.value}
82+
run_row = {"NAME": run.name, "STATUS": run.status.value}
8383
if len(run._run.jobs) != 1:
8484
add_row_from_dict(table, run_row)
8585

@@ -117,7 +117,7 @@ def _get_metrics_table(run: Run, metrics: List[JobMetrics]) -> Table:
117117
)
118118
gpu_metrics += f" util={gpu_util_percent}%"
119119

120-
job_row: Dict[Union[str, int], Any] = {
120+
job_row = {
121121
"NAME": f" replica={job.job_spec.replica_num} job={job.job_spec.job_num}",
122122
"STATUS": job.job_submissions[-1].status.value,
123123
"CPU": cpu_usage or "-",

src/dstack/_internal/cli/commands/project.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import argparse
22
import sys
3-
from typing import Any, Optional, Union
3+
from typing import Optional
44

55
import questionary
66
from requests import HTTPError
@@ -191,7 +191,7 @@ def _list(self, args: argparse.Namespace):
191191
for project_config in config_manager.list_project_configs():
192192
project_name = project_config.name
193193
is_default = project_name == default_project.name if default_project else False
194-
row: dict[Union[str, int], Any] = {
194+
row = {
195195
"PROJECT": project_name,
196196
"URL": project_config.url,
197197
"DEFAULT": "✓" if is_default else "",

src/dstack/_internal/cli/utils/common.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
from datetime import datetime, timezone
33
from pathlib import Path
4-
from typing import Any, Dict, Optional, Union
4+
from typing import Any, Optional
55

66
import requests
77
from rich.console import Console
@@ -110,17 +110,11 @@ def confirm_ask(prompt, **kwargs) -> bool:
110110
raise SystemExit(1)
111111

112112

113-
def add_row_from_dict(table: Table, data: Dict[Union[str, int], Any], **kwargs):
114-
"""Maps dict keys to a table columns. `data` key is a column name or index. Missing keys are ignored."""
113+
def add_row_from_dict(table: Table, data: dict[str, Any], **kwargs):
114+
"""Maps dict keys to table columns. `data` key is the column name. Missing keys are ignored."""
115115
row = []
116-
for i, col in enumerate(table.columns):
117-
# TODO(egor-s): clear header style
118-
if col.header in data:
119-
row.append(data[col.header])
120-
elif i in data:
121-
row.append(data[i])
122-
else:
123-
row.append("")
116+
for col in table.columns:
117+
row.append(data.get(str(col.header), ""))
124118
table.add_row(*row, **kwargs)
125119

126120

src/dstack/_internal/cli/utils/fleet.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict, List, Optional, Union
1+
from typing import Any, List, Optional
22

33
from rich.table import Table
44

@@ -75,7 +75,7 @@ def get_fleets_table(
7575
if verbose and config.placement and config.placement.value == "cluster":
7676
nodes = f"{nodes} (cluster)"
7777

78-
fleet_row: Dict[Union[str, int], Any] = {
78+
fleet_row = {
7979
"NAME": name,
8080
"NODES": nodes,
8181
"RESOURCES": resources,
@@ -116,7 +116,7 @@ def get_fleets_table(
116116
)
117117
instance_price = _format_price(instance.price)
118118

119-
instance_row: Dict[Union[str, int], Any] = {
119+
instance_row = {
120120
"NAME": f" instance={instance.instance_num}",
121121
"NODES": "",
122122
"RESOURCES": _format_instance_resources(instance),

src/dstack/_internal/cli/utils/run.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import shutil
2-
from typing import Any, Dict, List, Optional, Union
2+
from typing import Any, Dict, List, Optional
33

44
from rich.markup import escape
55
from rich.table import Table
@@ -398,7 +398,7 @@ def get_runs_table(
398398
group_name = group.name
399399
group_name_to_index[group_name] = idx
400400

401-
run_row: Dict[Union[str, int], Any] = {
401+
run_row = {
402402
"NAME": _format_run_name(run, show_deployment_num),
403403
"SUBMITTED": format_date(run.submitted_at),
404404
"STATUS": _format_run_status(run),
@@ -431,7 +431,7 @@ def get_job_sort_key(job: Job) -> tuple:
431431
if group_name_to_index:
432432
group_index = group_name_to_index.get(job.job_spec.replica_group)
433433

434-
job_row: Dict[Union[str, int], Any] = {
434+
job_row = {
435435
"NAME": _format_job_name(
436436
job,
437437
latest_job_submission,

0 commit comments

Comments
 (0)