Skip to content

Commit a7ddd55

Browse files
authored
Add initial typing to config module (#1074)
1 parent cc494e3 commit a7ddd55

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

tiled/config.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from datetime import timedelta
1212
from functools import cache
1313
from pathlib import Path
14+
from typing import Any, Optional, Union
1415

1516
import jsonschema
1617

@@ -36,7 +37,9 @@ def schema():
3637
return yaml.safe_load(file)
3738

3839

39-
def construct_build_app_kwargs(config, *, source_filepath=None):
40+
def construct_build_app_kwargs(
41+
config, *, source_filepath: Union[Path, str, None] = None
42+
):
4043
"""
4144
Given parsed configuration, construct arguments for build_app(...).
4245
@@ -193,8 +196,8 @@ def construct_build_app_kwargs(config, *, source_filepath=None):
193196
}
194197

195198

196-
def merge(configs):
197-
merged = {"trees": []}
199+
def merge(configs: dict[Path, dict[str, Any]]) -> dict[str, Any]:
200+
merged: dict[str, Any] = {"trees": []}
198201

199202
# These variables are used to produce error messages that point
200203
# to the relevant config file(s).
@@ -304,7 +307,7 @@ def merge(configs):
304307
return merged
305308

306309

307-
def parse_configs(config_path):
310+
def parse_configs(config_path: Union[str, Path]) -> dict[str, Any]:
308311
"""
309312
Parse configuration file or directory of configuration files.
310313
@@ -318,7 +321,9 @@ def parse_configs(config_path):
318321
filepaths = [config_path]
319322
elif config_path.is_dir():
320323
filepaths = [
321-
fn for fn in config_path.iterdir() if fn.suffix in (".yml", ".yaml")
324+
fn
325+
for fn in config_path.iterdir()
326+
if fn.suffix in (".yml", ".yaml") and fn.is_file()
322327
]
323328
elif not config_path.exists():
324329
raise ValueError(f"The config path {config_path!s} doesn't exist.")
@@ -328,7 +333,7 @@ def parse_configs(config_path):
328333
f"The config path {config_path!s} exists but is not a file or directory."
329334
)
330335

331-
parsed_configs = {}
336+
parsed_configs: dict[Path, dict[str, Any]] = {}
332337
# The sorting here is just to make the order of the results deterministic.
333338
# There is *not* any sorting-based precedence applied.
334339
for filepath in sorted(filepaths):

tiled/utils.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import warnings
1717
from collections import namedtuple
1818
from pathlib import Path
19-
from typing import Any, Callable, Optional
19+
from typing import Any, Callable, Iterator, Optional, TextIO, TypeVar, Union
2020
from urllib.parse import urlparse, urlunparse
2121

2222
import anyio
@@ -32,6 +32,9 @@
3232
)
3333

3434

35+
T = TypeVar("T")
36+
37+
3538
class ListView(collections.abc.Sequence):
3639
"An immutable view of a list."
3740

@@ -465,7 +468,7 @@ def modules_available(*module_names):
465468
return False
466469

467470

468-
def parse(file):
471+
def parse(file: TextIO) -> dict[Any, Any]:
469472
"""
470473
Given a config file, parse it.
471474
@@ -477,7 +480,7 @@ def parse(file):
477480
return expand_environment_variables(content)
478481

479482

480-
def expand_environment_variables(config):
483+
def expand_environment_variables(config: T) -> T:
481484
"""Expand environment variables in a nested config dictionary
482485
483486
VENDORED FROM dask.config.
@@ -500,7 +503,8 @@ def expand_environment_variables(config):
500503
{'x': [1, 2, 'my-username']}
501504
"""
502505
if isinstance(config, collections.abc.Mapping):
503-
return {k: expand_environment_variables(v) for k, v in config.items()}
506+
# ignore type checks as type might change but it's still a mapping
507+
return {k: expand_environment_variables(v) for k, v in config.items()} # type: ignore
504508
elif isinstance(config, str):
505509
return os.path.expandvars(config)
506510
elif isinstance(config, (list, tuple, builtins.set)):
@@ -510,7 +514,7 @@ def expand_environment_variables(config):
510514

511515

512516
@contextlib.contextmanager
513-
def prepend_to_sys_path(*paths):
517+
def prepend_to_sys_path(*paths: Union[str, Path]) -> Iterator[None]:
514518
"Temporarily prepend items to sys.path."
515519

516520
for item in reversed(paths):

0 commit comments

Comments
 (0)