Skip to content

Commit

Permalink
chore: remove deprecated code (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxb2 authored Jul 20, 2023
1 parent a79245a commit e363a80
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 152 deletions.
71 changes: 65 additions & 6 deletions docs/examples/pyproject.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -85,3 +86,61 @@ things nothing others
$ python my_tool.py --config other.toml
something else entirely
```

<!--- Test the combinator
```python title="my_tool.py"
from typing import Any, Dict
from typing_extensions import Annotated
import typer
from typer_config import conf_callback_factory
from typer_config.loaders import toml_loader
from typer_config.decorators import use_config
### You can define the same loader using the loader_transformer combinator:
#
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)
app = typer.Typer()
@app.command()
@use_config(pyproject_callback)
def main(
arg1: str,
opt1: Annotated[str, typer.Option()],
opt2: Annotated[str, typer.Option()] = "hello",
):
typer.echo(f"{opt1} {opt2} {arg1}")
if __name__ == "__main__":
app()
```
```bash
$ ls .
my_tool.py
other.toml
pyproject.toml
$ python my_tool.py
things nothing stuff
$ python my_tool.py others
things nothing others
$ python my_tool.py --config other.toml
something else entirely
```
--->
43 changes: 1 addition & 42 deletions tests/test_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
)
Expand Down Expand Up @@ -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."""

Expand Down
7 changes: 1 addition & 6 deletions typer_config/__typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"""
Expand All @@ -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."""

Expand Down
98 changes: 0 additions & 98 deletions typer_config/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit e363a80

Please sign in to comment.