diff --git a/docs/examples/pyproject.md b/docs/examples/pyproject.md index b9a24f4..695a845 100644 --- a/docs/examples/pyproject.md +++ b/docs/examples/pyproject.md @@ -40,13 +40,14 @@ def pyproject_loader(param_value: str) -> Dict[str, Any]: return conf -### You can define the same loader using some provided combinators: +### You can define the same loader using the loader_transformer combinator: # -# from typer_config.loaders import default_value_loader, subpath_loader, toml_loader -# -# pyproject_loader = subpath_loader( -# default_value_loader(toml_loader, lambda: "pyproject.toml"), -# ["tool", "my_tool", "parameters"], +# from typer_config.loaders import loader_transformer + +# pyproject_loader = loader_transformer( +# toml_loader, +# param_transformer=lambda param: param or "pyproject.toml", +# config_transformer=lambda config: config["tool"]["my_tool"]["parameters"], # ) pyproject_callback = conf_callback_factory(pyproject_loader) @@ -85,3 +86,61 @@ things nothing others $ python my_tool.py --config other.toml something else entirely ``` + + \ No newline at end of file diff --git a/tests/test_example.py b/tests/test_example.py index d44e579..a3e9fc8 100644 --- a/tests/test_example.py +++ b/tests/test_example.py @@ -63,9 +63,7 @@ def main( # Have to make one dynamically because of the required INI section INI_CALLBACK = typer_config.conf_callback_factory( typer_config.loaders.loader_transformer( - typer_config.loaders.subpath_loader( - typer_config.loaders.ini_loader, ["simple_app"] - ), + lambda config: typer_config.loaders.ini_loader(config)["simple_app"], loader_conditional=lambda param_value: param_value, ) ) @@ -169,45 +167,6 @@ def test_simple_example_decorated(simple_app_decorated, confs): assert "No such file" in result.stdout, f"Wrong error message for {conf}" -def test_pyproject_example_deprecated(simple_app): - """Test pyproject example (deprecated loader combinators).""" - - from typer_config.loaders import default_value_loader, subpath_loader, toml_loader - - pyproject_loader = subpath_loader( - default_value_loader(toml_loader, lambda: str(HERE.joinpath("pyproject.toml"))), - ["tool", "my_tool", "parameters"], - ) - - pyproject_callback = typer_config.conf_callback_factory(pyproject_loader) - - _app = simple_app(pyproject_callback) - - result = RUNNER.invoke(_app, ["--help"]) - - assert result.exit_code == 0, f"{result.stdout}" - - result = RUNNER.invoke(_app) - - assert result.exit_code == 0, f"{result.stdout}" - assert result.stdout.strip() == "things nothing stuff" - - result = RUNNER.invoke(_app, ["others"]) - - assert result.exit_code == 0, f"{result.stdout}" - assert result.stdout.strip() == "things nothing others" - - result = RUNNER.invoke(_app, ["--opt1", "people"]) - - assert result.exit_code == 0, f"{result.stdout}" - assert result.stdout.strip() == "people nothing stuff" - - result = RUNNER.invoke(_app, ["--config", str(HERE.joinpath("other.toml"))]) - - assert result.exit_code == 0, f"{result.stdout}" - assert result.stdout.strip() == "something else entirely" - - def test_pyproject_example(simple_app): """Test pyproject example.""" diff --git a/typer_config/__typing.py b/typer_config/__typing.py index 6158a60..096ba00 100644 --- a/typer_config/__typing.py +++ b/typer_config/__typing.py @@ -2,7 +2,7 @@ import sys from pathlib import Path -from typing import Any, Callable, Dict, Iterable, Union +from typing import Any, Callable, Dict, Union from typer import CallbackParam, Context @@ -22,8 +22,6 @@ ConfigDict: TypeAlias = Dict[TyperParameterName, Any] """Configuration dictionary to be applied to the click context default map.""" -ConfigDictAccessorPath: TypeAlias = Iterable[str] -"""Configuration dictionary accessor path.""" FilePath: TypeAlias = Union[Path, str] """File path""" @@ -48,9 +46,6 @@ ] """Typer config parameter callback function.""" -NoArgCallable: TypeAlias = Callable[[], Any] -"""No argument callable.""" - ConfigDumper: TypeAlias = Callable[[ConfigDict, FilePath], None] """Configuration dumper function.""" diff --git a/typer_config/loaders.py b/typer_config/loaders.py index 5ab0441..0d4a74e 100644 --- a/typer_config/loaders.py +++ b/typer_config/loaders.py @@ -5,16 +5,13 @@ import json from configparser import ConfigParser from typing import Optional -from warnings import warn from .__optional_imports import try_import from .__typing import ( ConfigDict, - ConfigDictAccessorPath, ConfigDictTransformer, ConfigLoader, ConfigLoaderConditional, - NoArgCallable, TyperParameterValue, TyperParameterValueTransformer, ) @@ -91,101 +88,6 @@ def _loader(param_value: TyperParameterValue) -> ConfigDict: return _loader -def subpath_loader( - loader: ConfigLoader, dictpath: ConfigDictAccessorPath -) -> ConfigLoader: - """Modify a loader to return a subpath of the dictionary from file. - - Warns: - DeprecationWarning: This function is deprecated. Please use - typer_config.loaders.loader_transformer instead. - - Examples: - The following example reads the values from the `my_app` section in - a YAML file structured like this: - ```yaml - tools: - my_app: - ... # use these values - others: # ignore - stuff: # ignore - ``` - - ```py - my_loader = subpath_loader(yaml_loader, ["tools", "my_app"]) - ``` - - Args: - loader (ConfigLoader): loader to modify - dictpath (ConfigDictAccessorPath): path to the section of dictionary - - Returns: - ConfigLoader: sub dictionary loader - """ - - warn( # noqa: B028 - "typer_config.loaders.subpath_loader is deprecated. " - "Please use typer_config.loaders.loader_transformer instead.", - DeprecationWarning, - ) - - def _loader(param_value: str) -> ConfigDict: - # get original ConfigDict - conf: ConfigDict = loader(param_value) - - # get subpath of dictionary - for path in dictpath: - conf = conf.get(path, {}) - return conf - - return _loader - - -def default_value_loader( - loader: ConfigLoader, value_getter: NoArgCallable -) -> ConfigLoader: - """Modify a loader to use a default value if the passed value is false-ish. - - Warns: - DeprecationWarning: This function is deprecated. Please use - typer_config.loaders.loader_transformer instead. - - Examples: - The following example lets a user specify a config file, but will load - the `pyproject.toml` if they don't. - - ```py - pyproject_loader = default_value_loader(toml_loader, lambda: "pyproject.toml") - ``` - - Args: - loader (ConfigLoader): loader to modify - value_getter (NoArgCallable): function that returns default value - - - - Returns: - ConfigLoader: modified loader - """ - - warn( # noqa: B028 - "typer_config.loaders.default_value_loader is deprecated. " - "Please use typer_config.loaders.loader_transformer instead.", - DeprecationWarning, - ) - - def _loader(param_value: str) -> ConfigDict: - # parameter value was not specified by user - if not param_value: - param_value = value_getter() - - conf: ConfigDict = loader(param_value) - - return conf - - return _loader - - def yaml_loader(param_value: TyperParameterValue) -> ConfigDict: """YAML file loader.