Skip to content

Deprecate INI configuration. #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ releases are available on [Anaconda.org](https://anaconda.org/conda-forge/pytask

## 0.3.0 - 2022-12-xx

- {pull}`49` removes support for INI configurations.
- {pull}`50` removes the deprecation message and related code to the old API.

## 0.2.1 - 2022-04-19
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![image](https://img.shields.io/conda/vn/conda-forge/pytask-latex.svg)](https://anaconda.org/conda-forge/pytask-latex)
[![image](https://img.shields.io/conda/pn/conda-forge/pytask-latex.svg)](https://anaconda.org/conda-forge/pytask-latex)
[![PyPI - License](https://img.shields.io/pypi/l/pytask-latex)](https://pypi.org/project/pytask-latex)
[![image](https://img.shields.io/github/workflow/status/pytask-dev/pytask-latex/main/main)](https://github.com/pytask-dev/pytask-latex/actions?query=branch%3Amain)
[![image](https://img.shields.io/github/actions/workflow/status/pytask-dev/pytask-latex/main.yml?branch=main)](https://github.com/pytask-dev/pytask-latex/actions?query=branch%3Amain)
[![image](https://codecov.io/gh/pytask-dev/pytask-latex/branch/main/graph/badge.svg)](https://codecov.io/gh/pytask-dev/pytask-latex)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/pytask-dev/pytask-latex/main.svg)](https://results.pre-commit.ci/latest/github/pytask-dev/pytask-latex/main)
[![image](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
Expand Down
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dependencies:
- toml

# Package dependencies
- pytask >= 0.2
- pytask >= 0.3
- pytask-parallel >= 0.1
- latex-dependency-scanner >=0.1.1
- pybaum >=0.1.1
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ install_requires =
click
latex-dependency-scanner>=0.1.1
pybaum>=0.1.1
pytask>=0.2
pytask>=0.3
python_requires = >=3.7
include_package_data = True
package_dir = =src
Expand Down
53 changes: 2 additions & 51 deletions src/pytask_latex/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,12 @@
from __future__ import annotations

from typing import Any
from typing import Callable

from pytask import hookimpl


@hookimpl
def pytask_parse_config(config, config_from_file):
def pytask_parse_config(config: dict[str, Any]) -> None:
"""Register the latex marker in the configuration."""
config["markers"]["latex"] = "Tasks which compile LaTeX documents."
config["infer_latex_dependencies"] = _get_first_non_none_value(
config_from_file,
key="infer_latex_dependencies",
callback=_convert_truthy_or_falsy_to_bool,
default=True,
)


def _convert_truthy_or_falsy_to_bool(x: bool | str | None) -> bool:
"""Convert truthy or falsy value in .ini to Python boolean."""
if x in [True, "True", "true", "1"]:
out = True
elif x in [False, "False", "false", "0"]:
out = False
elif x in [None, "None", "none"]:
out = None
else:
raise ValueError(
f"Input {x!r} is neither truthy (True, true, 1) or falsy (False, false, 0)."
)
return out


def _get_first_non_none_value(
*configs: dict[str, Any],
key: str,
default: Any | None = None,
callback: Callable[..., Any] | None = None,
) -> Any:
"""Get the first non-None value for a key from a list of dictionaries.

This function allows to prioritize information from many configurations by changing
the order of the inputs while also providing a default.

Examples
--------
>>> _get_first_non_none_value({"a": None}, {"a": 1}, key="a")
1
>>> _get_first_non_none_value({"a": None}, {"a": None}, key="a", default="default")
'default'
>>> _get_first_non_none_value({}, {}, key="a", default="default")
'default'
>>> _get_first_non_none_value({"a": None}, {"a": "b"}, key="a")
'b'

"""
callback = (lambda x: x) if callback is None else callback # noqa: E731
processed_values = (callback(config.get(key)) for config in configs)
return next((value for value in processed_values if value is not None), default)
config["infer_latex_dependencies"] = config.get("infer_latex_dependencies", True)
27 changes: 0 additions & 27 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,10 @@
from __future__ import annotations

from contextlib import ExitStack as does_not_raise # noqa: N813

import pytest
from pytask import main
from pytask_latex.config import _convert_truthy_or_falsy_to_bool


@pytest.mark.end_to_end
def test_marker_is_configured(tmp_path):
session = main({"paths": tmp_path})
assert "latex" in session.config["markers"]


@pytest.mark.unit
@pytest.mark.parametrize(
"value, expectation, expected",
[
(True, does_not_raise(), True),
("True", does_not_raise(), True),
("true", does_not_raise(), True),
("1", does_not_raise(), True),
(False, does_not_raise(), False),
("False", does_not_raise(), False),
("false", does_not_raise(), False),
("0", does_not_raise(), False),
(None, does_not_raise(), None),
("None", does_not_raise(), None),
("none", does_not_raise(), None),
("asklds", pytest.raises(ValueError, match="Input"), None),
],
)
def test_convert_truthy_or_falsy_to_bool(value, expectation, expected):
with expectation:
result = _convert_truthy_or_falsy_to_bool(value)
assert result == expected
15 changes: 4 additions & 11 deletions tests/test_latex_dependency_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,7 @@
@skip_on_github_actions_with_win
@pytest.mark.end_to_end
@pytest.mark.parametrize("infer_dependencies", ["true", "false"])
@pytest.mark.parametrize(
"config_file, content",
[
("pytask.ini", "[pytask]\ninfer_latex_dependencies = {}"),
("pyproject.toml", "[tool.pytask.ini_options]\ninfer_latex_dependencies = {}"),
],
)
def test_infer_dependencies_from_task(
tmp_path, infer_dependencies, config_file, content
):
def test_infer_dependencies_from_task(tmp_path, infer_dependencies):
task_source = """
import pytask

Expand All @@ -41,7 +32,9 @@ def task_compile_document():
tmp_path.joinpath("document.tex").write_text(textwrap.dedent(latex_source))
tmp_path.joinpath("sub_document.tex").write_text("Lorem ipsum.")

tmp_path.joinpath(config_file).write_text(content.format(infer_dependencies))
tmp_path.joinpath("pyproject.toml").write_text(
f"[tool.pytask.ini_options]\ninfer_latex_dependencies = {infer_dependencies}"
)

session = main({"paths": tmp_path})
assert session.exit_code == ExitCode.OK
Expand Down