Skip to content

Commit 12ee098

Browse files
Validate inputs.json contents in mise tasks (#452)
1 parent 99ebd7d commit 12ee098

3 files changed

Lines changed: 66 additions & 18 deletions

File tree

mise-tasks/inputs/add.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import sys
1616
import tarfile
1717
import tempfile
18-
from typing import TYPE_CHECKING
18+
from typing import TYPE_CHECKING, Any, TypeIs
1919

2020
if TYPE_CHECKING:
2121
from collections.abc import Iterable, Iterator
@@ -33,6 +33,26 @@ def log_level(verbosity: int) -> int:
3333
return logging.DEBUG
3434

3535

36+
def is_str_list(val: Any) -> TypeIs[list[str]]: # noqa: ANN401
37+
if not isinstance(val, list):
38+
return False
39+
return all(isinstance(item, str) for item in val) # pyright: ignore[reportUnknownVariableType]
40+
41+
42+
def load_inputs_db(inputs_db_file: pathlib.Path) -> list[str]:
43+
if not inputs_db_file.exists():
44+
return []
45+
46+
with inputs_db_file.open("r") as f:
47+
data = json.load(f)
48+
49+
if not is_str_list(data):
50+
msg = f"Invalid inputs.json file: expected list of strings, got {data}"
51+
raise TypeError(msg)
52+
53+
return data
54+
55+
3656
def move_file_to_local_directory(
3757
file: pathlib.Path, repo_root: pathlib.Path, target_file_name: str
3858
) -> bool:
@@ -52,11 +72,7 @@ def move_file_to_local_directory(
5272
class FileRecords:
5373
def __init__(self, repo_root: pathlib.Path) -> None:
5474
self._inputs_db_file = repo_root / "inputs.json"
55-
if self._inputs_db_file.exists():
56-
with self._inputs_db_file.open("r") as f:
57-
self._inputs_db = json.load(f)
58-
else:
59-
self._inputs_db = []
75+
self._inputs_db = load_inputs_db(self._inputs_db_file)
6076

6177
def contains(self, target_file_name: str) -> bool:
6278
return target_file_name in self._inputs_db

mise-tasks/inputs/check.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import os
77
import pathlib
88
import sys
9-
from typing import TYPE_CHECKING
9+
from typing import TYPE_CHECKING, Any, TypeIs
1010

1111
if TYPE_CHECKING:
1212
from collections.abc import Iterator
@@ -24,14 +24,30 @@ def log_level(verbosity: int) -> int:
2424
return logging.DEBUG
2525

2626

27+
def is_str_list(val: Any) -> TypeIs[list[str]]: # noqa: ANN401
28+
if not isinstance(val, list):
29+
return False
30+
return all(isinstance(item, str) for item in val) # pyright: ignore[reportUnknownVariableType]
31+
32+
33+
def load_inputs_db(inputs_db_file: pathlib.Path) -> list[str]:
34+
if not inputs_db_file.exists():
35+
return []
36+
37+
with inputs_db_file.open("r") as f:
38+
data = json.load(f)
39+
40+
if not is_str_list(data):
41+
msg = f"Invalid inputs.json file: expected list of strings, got {data}"
42+
raise TypeError(msg)
43+
44+
return data
45+
46+
2747
class FileRecords:
2848
def __init__(self, repo_root: pathlib.Path) -> None:
2949
self._inputs_db_file = repo_root / "inputs.json"
30-
if self._inputs_db_file.exists():
31-
with self._inputs_db_file.open("r") as f:
32-
self._inputs_db = json.load(f)
33-
else:
34-
self._inputs_db = []
50+
self._inputs_db = load_inputs_db(self._inputs_db_file)
3551

3652
def iter(self) -> Iterator[str]:
3753
yield from self._inputs_db

mise-tasks/inputs/extract.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import sys
1010
import tarfile
1111
import tempfile
12-
from typing import TYPE_CHECKING
12+
from typing import TYPE_CHECKING, Any, TypeIs
1313

1414
if TYPE_CHECKING:
1515
from collections.abc import Iterable, Iterator
@@ -27,14 +27,30 @@ def log_level(verbosity: int) -> int:
2727
return logging.DEBUG
2828

2929

30+
def is_str_list(val: Any) -> TypeIs[list[str]]: # noqa: ANN401
31+
if not isinstance(val, list):
32+
return False
33+
return all(isinstance(item, str) for item in val) # pyright: ignore[reportUnknownVariableType]
34+
35+
36+
def load_inputs_db(inputs_db_file: pathlib.Path) -> list[str]:
37+
if not inputs_db_file.exists():
38+
return []
39+
40+
with inputs_db_file.open("r") as f:
41+
data = json.load(f)
42+
43+
if not is_str_list(data):
44+
msg = f"Invalid inputs.json file: expected list of strings, got {data}"
45+
raise TypeError(msg)
46+
47+
return data
48+
49+
3050
class FileRecords:
3151
def __init__(self, repo_root: pathlib.Path) -> None:
3252
self._inputs_db_file = repo_root / "inputs.json"
33-
if self._inputs_db_file.exists():
34-
with self._inputs_db_file.open("r") as f:
35-
self._inputs_db = json.load(f)
36-
else:
37-
self._inputs_db = []
53+
self._inputs_db = load_inputs_db(self._inputs_db_file)
3854

3955
def iter(self) -> Iterator[str]:
4056
yield from self._inputs_db

0 commit comments

Comments
 (0)