Skip to content

Commit bea71b0

Browse files
authored
Make the package compatible with pytask v0.3." (#22)
1 parent ca807db commit bea71b0

File tree

5 files changed

+34
-91
lines changed

5 files changed

+34
-91
lines changed

CHANGES.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ reverse chronological order. Releases follow [semantic versioning](https://semve
55
and all releases are available on
66
[Anaconda.org](https://anaconda.org/conda-forge/pytask-environment).
77

8-
## 0.2.x - 2023-xx-xx
8+
## 0.3.0 - 2023-02-11
99

1010
- {pull}`17` adds ruff and refurb to pre-commits and fixes the banner.
11+
- {pull}`21` adds dependabot to update Github actions.
12+
- {pull}`22` makes the package compatible with pytask v0.3.
1113

12-
## 0.2.0 - 2022-16-04
14+
## 0.2.0 - 2022-04-16
1315

1416
- {pull}`15` aligns pytask-environment with pytask v0.2.0.
1517

environment.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ channels:
55
- nodefaults
66

77
dependencies:
8-
- python >=3.7
8+
- python >=3.7,<3.11
99
- pip
1010
- setuptools_scm
1111
- toml

src/pytask_environment/config.py

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from __future__ import annotations
33

44
from typing import Any
5-
from typing import Callable
65

76
import click
87
from pytask import hookimpl
@@ -15,68 +14,15 @@ def pytask_extend_command_line_interface(cli: click.Group) -> None:
1514
click.Option(
1615
["--update-environment"],
1716
is_flag=True,
18-
default=None,
17+
default=False,
1918
help="Update the information on the environment stored in the database.",
2019
)
2120
)
2221

2322

2423
@hookimpl
25-
def pytask_parse_config(
26-
config: dict[str, Any],
27-
config_from_file: dict[str, Any],
28-
config_from_cli: dict[str, Any],
29-
) -> None:
24+
def pytask_parse_config(config: dict[str, Any]) -> None:
3025
"""Parse the configuration."""
31-
config["check_python_version"] = _get_first_non_none_value(
32-
config_from_file,
33-
key="check_python_version",
34-
default=True,
35-
callback=_convert_truthy_or_falsy_to_bool,
36-
)
37-
38-
config["check_environment"] = _get_first_non_none_value(
39-
config_from_file,
40-
key="check_environment",
41-
default=True,
42-
callback=_convert_truthy_or_falsy_to_bool,
43-
)
44-
45-
config["update_environment"] = _get_first_non_none_value(
46-
config_from_cli,
47-
key="update_environment",
48-
default=False,
49-
callback=_convert_truthy_or_falsy_to_bool,
50-
)
51-
52-
53-
def _get_first_non_none_value(
54-
*configs: dict[str, Any],
55-
key: str,
56-
default: Any | None = None,
57-
callback: Callable[..., Any] | None = None,
58-
) -> Any:
59-
"""Get the first non-None value for a key from a list of dictionaries.
60-
61-
This function allows to prioritize information from many configurations by changing
62-
the order of the inputs while also providing a default.
63-
64-
"""
65-
callback = (lambda x: x) if callback is None else callback
66-
processed_values = (callback(config.get(key)) for config in configs)
67-
return next((value for value in processed_values if value is not None), default)
68-
69-
70-
def _convert_truthy_or_falsy_to_bool(x: bool | str | None) -> bool:
71-
"""Convert truthy or falsy value in .ini to Python boolean."""
72-
if x in (True, "True", "true", "1"):
73-
out = True
74-
elif x in (False, "False", "false", "0"):
75-
out = False
76-
elif x in (None, "None", "none"):
77-
out = None
78-
else:
79-
raise ValueError(
80-
f"Input {x!r} is neither truthy (True, true, 1) or falsy (False, false, 0)."
81-
)
82-
return out
26+
config["check_python_version"] = config.get("check_python_version", True)
27+
config["check_environment"] = config.get("check_environment", True)
28+
config["update_environment"] = config.get("update_environment", False)

src/pytask_environment/database.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
from __future__ import annotations
33

44
from pony import orm
5-
from pytask import db
5+
6+
7+
# Can be removed with pytask v0.4.
8+
try:
9+
from pytask import db
10+
except ImportError:
11+
from _pytask.database_utils import db
612

713

814
class Environment(db.Entity):

tests/test_logging.py

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,22 @@
66
import pytest
77
from pony import orm
88
from pytask import cli
9-
from pytask import db
109
from pytask import ExitCode
1110
from pytask_environment.database import Environment
1211

12+
# Can be removed with pytask v0.4.
13+
try:
14+
from pytask import db
15+
except ImportError:
16+
from _pytask.database_utils import db
17+
1318

1419
@pytest.mark.end_to_end
1520
def test_existence_of_python_executable_in_db(tmp_path, runner):
1621
"""Test that the Python executable is stored in the database."""
1722
task_path = tmp_path.joinpath("task_dummy.py")
1823
task_path.write_text(textwrap.dedent("def task_dummy(): pass"))
19-
tmp_path.joinpath("pytask.ini").write_text("[pytask]")
24+
tmp_path.joinpath("pyproject.toml").write_text("[tool.pytask.ini_options]")
2025

2126
result = runner.invoke(cli, [tmp_path.as_posix()])
2227

@@ -34,13 +39,7 @@ def test_existence_of_python_executable_in_db(tmp_path, runner):
3439

3540

3641
@pytest.mark.end_to_end
37-
@pytest.mark.parametrize(
38-
"config_file, content",
39-
[("pytask.ini", "[pytask]"), ("pyproject.toml", "[tool.pytask.ini_options]")],
40-
)
41-
def test_flow_when_python_version_has_changed(
42-
monkeypatch, tmp_path, runner, config_file, content
43-
):
42+
def test_flow_when_python_version_has_changed(monkeypatch, tmp_path, runner):
4443
"""Test the whole use-case.
4544
4645
1. Run a simple task to cache the Python version and path.
@@ -57,7 +56,7 @@ def test_flow_when_python_version_has_changed(
5756
"[MSC v.1916 64 bit (AMD64)]"
5857
)
5958

60-
tmp_path.joinpath(config_file).write_text(content)
59+
tmp_path.joinpath("pyproject.toml").write_text("[tool.pytask.ini_options]")
6160
source = "def task_dummy(): pass"
6261
task_path = tmp_path.joinpath("task_dummy.py")
6362
task_path.write_text(textwrap.dedent(source))
@@ -98,22 +97,17 @@ def test_flow_when_python_version_has_changed(
9897

9998

10099
@pytest.mark.end_to_end
101-
@pytest.mark.parametrize(
102-
"config_file, content",
103-
[
104-
("pytask.ini", "[pytask]\ncheck_python_version = {}"),
105-
("pyproject.toml", "[tool.pytask.ini_options]\ncheck_python_version = {}"),
106-
],
107-
)
108100
@pytest.mark.parametrize("check_python_version, expected", [("true", 1), ("false", 0)])
109101
def test_python_version_changed(
110-
monkeypatch, tmp_path, runner, config_file, content, check_python_version, expected
102+
monkeypatch, tmp_path, runner, check_python_version, expected
111103
):
112104
fake_version = (
113105
"2.7.8 | packaged by conda-forge | (default, Jul 31 2020, 01:53:57) "
114106
"[MSC v.1916 64 bit (AMD64)]"
115107
)
116-
tmp_path.joinpath(config_file).write_text(content.format(check_python_version))
108+
tmp_path.joinpath("pyproject.toml").write_text(
109+
f"[tool.pytask.ini_options]\ncheck_python_version = {check_python_version}"
110+
)
117111
source = "def task_dummy(): pass"
118112
task_path = tmp_path.joinpath("task_dummy.py")
119113
task_path.write_text(textwrap.dedent(source))
@@ -136,18 +130,13 @@ def test_python_version_changed(
136130

137131

138132
@pytest.mark.end_to_end
139-
@pytest.mark.parametrize(
140-
"config_file, content",
141-
[
142-
("pytask.ini", "[pytask]\ncheck_environment = {}"),
143-
("pyproject.toml", "[tool.pytask.ini_options]\ncheck_environment = {}"),
144-
],
145-
)
146133
@pytest.mark.parametrize("check_python_version, expected", [("true", 1), ("false", 0)])
147134
def test_environment_changed(
148-
monkeypatch, tmp_path, runner, config_file, content, check_python_version, expected
135+
monkeypatch, tmp_path, runner, check_python_version, expected
149136
):
150-
tmp_path.joinpath(config_file).write_text(content.format(check_python_version))
137+
tmp_path.joinpath("pyproject.toml").write_text(
138+
f"[tool.pytask.ini_options]\ncheck_environment = {check_python_version}"
139+
)
151140
source = "def task_dummy(): pass"
152141
task_path = tmp_path.joinpath("task_dummy.py")
153142
task_path.write_text(textwrap.dedent(source))

0 commit comments

Comments
 (0)