Allow path to be relativ to configuration file by making it absolute in FileLoader
Consider a CLI application for Python Project that lint files (i.e. mylinter
).
I want this linter to exclude some files, for example (Minimal working example):
# mylinter.py
from pathlib import Path
import click
import typed_settings as ts
@ts.settings
class LintSettings:
exclude: tuple[Path, ...] = ts.option(
factory=tuple, help="Exclude the given paths from linting"
)
config_file = ts.find("pyproject.toml")
@click.command()
@ts.click_options(
LintSettings,
loaders=ts.default_loaders(
appname="mylinter",
config_files=[config_file],
config_file_section=f"tool.mylinter",
),
)
def my_cli(settings: LintSettings) -> None:
print(f"Paths to be exclude")
print(f"{config_file=}")
for path in settings.exclude:
print(path.absolute())
if __name__ == "__main__":
my_cli()
Now given the configuration file:
# pyproject.toml
[tool.mylinter]
exclude = [
'tests/lib_lint.py'
]
Will lead to the following results
# Executing from project root works as expected
$ carli@computer ~/mylint python -m mylint
Paths to be exclude
config_file=PosixPath('/home/carli/mylint/pyproject.toml')
/home/carli/Work/mylint/teststests/lib_lint.py
# Now cd to test
$ carli@computer ~/mylint cd tests
# settings loaded from config file give a wrong result
$ carli@computer ~/mylint/tests cpython -m mylint
Paths to be exclude
config_file=PosixPath('/home/carli/mylint/pyproject.toml')
/home/carli/mylint/tests/tests/lib_lint.py
# using the cli argument giving the correct result
$ carli@computer ~/mylint/tests python -m mylint --exclude test_lib.py
Paths to be exclude
config_file=PosixPath('/home/carli/mylint/pyproject.toml')
/home/carli/mylint/tests/test_lib.py
I would expect the paths loaded from the settings to be relative to the loaded file. It is otherwise hard to impossible to figure out the correct path when handling cli options together with loading files.