diff --git a/CHANGES.md b/CHANGES.md index b4585d8..1f600ee 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -5,11 +5,13 @@ reverse chronological order. Releases follow [semantic versioning](https://semve and all releases are available on [Anaconda.org](https://anaconda.org/conda-forge/pytask-environment). -## 0.2.x - 2023-xx-xx +## 0.3.0 - 2023-02-11 - {pull}`17` adds ruff and refurb to pre-commits and fixes the banner. +- {pull}`21` adds dependabot to update Github actions. +- {pull}`22` makes the package compatible with pytask v0.3. -## 0.2.0 - 2022-16-04 +## 0.2.0 - 2022-04-16 - {pull}`15` aligns pytask-environment with pytask v0.2.0. diff --git a/environment.yml b/environment.yml index 3e762a5..6f68675 100644 --- a/environment.yml +++ b/environment.yml @@ -5,7 +5,7 @@ channels: - nodefaults dependencies: - - python >=3.7 + - python >=3.7,<3.11 - pip - setuptools_scm - toml diff --git a/src/pytask_environment/config.py b/src/pytask_environment/config.py index 85d517e..31b90bc 100644 --- a/src/pytask_environment/config.py +++ b/src/pytask_environment/config.py @@ -2,7 +2,6 @@ from __future__ import annotations from typing import Any -from typing import Callable import click from pytask import hookimpl @@ -15,68 +14,15 @@ def pytask_extend_command_line_interface(cli: click.Group) -> None: click.Option( ["--update-environment"], is_flag=True, - default=None, + default=False, help="Update the information on the environment stored in the database.", ) ) @hookimpl -def pytask_parse_config( - config: dict[str, Any], - config_from_file: dict[str, Any], - config_from_cli: dict[str, Any], -) -> None: +def pytask_parse_config(config: dict[str, Any]) -> None: """Parse the configuration.""" - config["check_python_version"] = _get_first_non_none_value( - config_from_file, - key="check_python_version", - default=True, - callback=_convert_truthy_or_falsy_to_bool, - ) - - config["check_environment"] = _get_first_non_none_value( - config_from_file, - key="check_environment", - default=True, - callback=_convert_truthy_or_falsy_to_bool, - ) - - config["update_environment"] = _get_first_non_none_value( - config_from_cli, - key="update_environment", - default=False, - callback=_convert_truthy_or_falsy_to_bool, - ) - - -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. - - """ - callback = (lambda x: x) if callback is None else callback - processed_values = (callback(config.get(key)) for config in configs) - return next((value for value in processed_values if value is not None), default) - - -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 + config["check_python_version"] = config.get("check_python_version", True) + config["check_environment"] = config.get("check_environment", True) + config["update_environment"] = config.get("update_environment", False) diff --git a/src/pytask_environment/database.py b/src/pytask_environment/database.py index 8cbdd88..2328aa6 100644 --- a/src/pytask_environment/database.py +++ b/src/pytask_environment/database.py @@ -2,7 +2,13 @@ from __future__ import annotations from pony import orm -from pytask import db + + +# Can be removed with pytask v0.4. +try: + from pytask import db +except ImportError: + from _pytask.database_utils import db class Environment(db.Entity): diff --git a/tests/test_logging.py b/tests/test_logging.py index a65afd4..b8a5aef 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -6,17 +6,22 @@ import pytest from pony import orm from pytask import cli -from pytask import db from pytask import ExitCode from pytask_environment.database import Environment +# Can be removed with pytask v0.4. +try: + from pytask import db +except ImportError: + from _pytask.database_utils import db + @pytest.mark.end_to_end def test_existence_of_python_executable_in_db(tmp_path, runner): """Test that the Python executable is stored in the database.""" task_path = tmp_path.joinpath("task_dummy.py") task_path.write_text(textwrap.dedent("def task_dummy(): pass")) - tmp_path.joinpath("pytask.ini").write_text("[pytask]") + tmp_path.joinpath("pyproject.toml").write_text("[tool.pytask.ini_options]") result = runner.invoke(cli, [tmp_path.as_posix()]) @@ -34,13 +39,7 @@ def test_existence_of_python_executable_in_db(tmp_path, runner): @pytest.mark.end_to_end -@pytest.mark.parametrize( - "config_file, content", - [("pytask.ini", "[pytask]"), ("pyproject.toml", "[tool.pytask.ini_options]")], -) -def test_flow_when_python_version_has_changed( - monkeypatch, tmp_path, runner, config_file, content -): +def test_flow_when_python_version_has_changed(monkeypatch, tmp_path, runner): """Test the whole use-case. 1. Run a simple task to cache the Python version and path. @@ -57,7 +56,7 @@ def test_flow_when_python_version_has_changed( "[MSC v.1916 64 bit (AMD64)]" ) - tmp_path.joinpath(config_file).write_text(content) + tmp_path.joinpath("pyproject.toml").write_text("[tool.pytask.ini_options]") source = "def task_dummy(): pass" task_path = tmp_path.joinpath("task_dummy.py") task_path.write_text(textwrap.dedent(source)) @@ -98,22 +97,17 @@ def test_flow_when_python_version_has_changed( @pytest.mark.end_to_end -@pytest.mark.parametrize( - "config_file, content", - [ - ("pytask.ini", "[pytask]\ncheck_python_version = {}"), - ("pyproject.toml", "[tool.pytask.ini_options]\ncheck_python_version = {}"), - ], -) @pytest.mark.parametrize("check_python_version, expected", [("true", 1), ("false", 0)]) def test_python_version_changed( - monkeypatch, tmp_path, runner, config_file, content, check_python_version, expected + monkeypatch, tmp_path, runner, check_python_version, expected ): fake_version = ( "2.7.8 | packaged by conda-forge | (default, Jul 31 2020, 01:53:57) " "[MSC v.1916 64 bit (AMD64)]" ) - tmp_path.joinpath(config_file).write_text(content.format(check_python_version)) + tmp_path.joinpath("pyproject.toml").write_text( + f"[tool.pytask.ini_options]\ncheck_python_version = {check_python_version}" + ) source = "def task_dummy(): pass" task_path = tmp_path.joinpath("task_dummy.py") task_path.write_text(textwrap.dedent(source)) @@ -136,18 +130,13 @@ def test_python_version_changed( @pytest.mark.end_to_end -@pytest.mark.parametrize( - "config_file, content", - [ - ("pytask.ini", "[pytask]\ncheck_environment = {}"), - ("pyproject.toml", "[tool.pytask.ini_options]\ncheck_environment = {}"), - ], -) @pytest.mark.parametrize("check_python_version, expected", [("true", 1), ("false", 0)]) def test_environment_changed( - monkeypatch, tmp_path, runner, config_file, content, check_python_version, expected + monkeypatch, tmp_path, runner, check_python_version, expected ): - tmp_path.joinpath(config_file).write_text(content.format(check_python_version)) + tmp_path.joinpath("pyproject.toml").write_text( + f"[tool.pytask.ini_options]\ncheck_environment = {check_python_version}" + ) source = "def task_dummy(): pass" task_path = tmp_path.joinpath("task_dummy.py") task_path.write_text(textwrap.dedent(source))