Skip to content

Commit 327a0f6

Browse files
authored
Refactorings related to ExitCode. (#188)
1 parent 13aac48 commit 327a0f6

28 files changed

+98
-70
lines changed

docs/source/changes.rst

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ all releases are available on `PyPI <https://pypi.org/project/pytask>`_ and
1111
------------------
1212

1313
- :gh:`186` enhance live displays by deactivating auto-refresh among other things.
14+
- :gh:`188` refactors some code related to :class:`_pytask.enums.ExitCode`.
1415

1516

1617
0.1.4 - 2022-01-04

src/_pytask/build.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
import click
88
from _pytask.config import hookimpl
99
from _pytask.console import console
10-
from _pytask.enums import ExitCode
1110
from _pytask.exceptions import CollectionError
1211
from _pytask.exceptions import ConfigurationError
1312
from _pytask.exceptions import ExecutionError
1413
from _pytask.exceptions import ResolvingDependenciesError
14+
from _pytask.outcomes import ExitCode
1515
from _pytask.pluginmanager import get_plugin_manager
1616
from _pytask.session import Session
1717

src/_pytask/clean.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
from _pytask.config import hookimpl
2121
from _pytask.config import IGNORED_TEMPORARY_FILES_AND_FOLDERS
2222
from _pytask.console import console
23-
from _pytask.enums import ExitCode
2423
from _pytask.exceptions import CollectionError
2524
from _pytask.nodes import MetaTask
25+
from _pytask.outcomes import ExitCode
2626
from _pytask.path import find_common_ancestor
2727
from _pytask.path import relative_to
2828
from _pytask.pluginmanager import get_plugin_manager

src/_pytask/collect_command.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
from _pytask.console import format_task_id
1616
from _pytask.console import PYTHON_ICON
1717
from _pytask.console import TASK_ICON
18-
from _pytask.enums import ExitCode
1918
from _pytask.exceptions import CollectionError
2019
from _pytask.exceptions import ConfigurationError
2120
from _pytask.exceptions import ResolvingDependenciesError
2221
from _pytask.mark import select_by_keyword
2322
from _pytask.mark import select_by_mark
23+
from _pytask.outcomes import ExitCode
2424
from _pytask.path import find_common_ancestor
2525
from _pytask.path import relative_to
2626
from _pytask.pluginmanager import get_plugin_manager

src/_pytask/enums.py

-20
This file was deleted.

src/_pytask/graph.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
from _pytask.config import hookimpl
1414
from _pytask.console import console
1515
from _pytask.dag import descending_tasks
16-
from _pytask.enums import ExitCode
1716
from _pytask.exceptions import CollectionError
1817
from _pytask.exceptions import ConfigurationError
1918
from _pytask.exceptions import ResolvingDependenciesError
19+
from _pytask.outcomes import ExitCode
2020
from _pytask.pluginmanager import get_plugin_manager
2121
from _pytask.session import Session
2222
from _pytask.shared import get_first_non_none_value

src/_pytask/live.py

+6
Original file line numberDiff line numberDiff line change
@@ -265,20 +265,24 @@ class LiveCollection:
265265

266266
@hookimpl(hookwrapper=True)
267267
def pytask_collect(self) -> Generator[None, None, None]:
268+
"""Start the status of the cllection."""
268269
self._live_manager.start()
269270
yield
270271

271272
@hookimpl
272273
def pytask_collect_file_log(self, reports: List[CollectionReport]) -> None:
274+
"""Update the status after a file is collected."""
273275
self._update_statistics(reports)
274276
self._update_status()
275277

276278
@hookimpl(hookwrapper=True)
277279
def pytask_collect_log(self) -> Generator[None, None, None]:
280+
"""Stop the live display when all tasks have been collected."""
278281
self._live_manager.stop(transient=True)
279282
yield
280283

281284
def _update_statistics(self, reports: List[CollectionReport]) -> None:
285+
"""Update the statistics on collected tasks and errors."""
282286
if reports is None:
283287
reports = []
284288
for report in reports:
@@ -288,10 +292,12 @@ def _update_statistics(self, reports: List[CollectionReport]) -> None:
288292
self._n_errors += 1
289293

290294
def _update_status(self) -> None:
295+
"""Update the status."""
291296
status = self._generate_status()
292297
self._live_manager.update(status)
293298

294299
def _generate_status(self) -> Status:
300+
"""Generate the status."""
295301
msg = f"Collected {self._n_collected_tasks} tasks."
296302
if self._n_errors > 0:
297303
msg += f" {self._n_errors} errors."

src/_pytask/logging.py

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def pytask_parse_config(
5656
config_from_file: Dict[str, Any],
5757
config_from_cli: Dict[str, Any],
5858
) -> None:
59+
"""Parse configuration."""
5960
config["show_locals"] = get_first_non_none_value(
6061
config_from_cli,
6162
config_from_file,
@@ -138,6 +139,7 @@ def pytask_log_session_footer(
138139

139140

140141
def _format_duration(duration: float) -> str:
142+
"""Format the duration."""
141143
duration_tuples = _humanize_time(duration, "seconds", short_label=False)
142144

143145
# Remove seconds if the execution lasted days or hours.

src/_pytask/mark/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from _pytask.config import hookimpl
1212
from _pytask.console import console
1313
from _pytask.dag import task_and_preceding_tasks
14-
from _pytask.enums import ExitCode
1514
from _pytask.exceptions import ConfigurationError
1615
from _pytask.mark.expression import Expression
1716
from _pytask.mark.expression import ParseError
@@ -20,6 +19,7 @@
2019
from _pytask.mark.structures import MarkDecorator
2120
from _pytask.mark.structures import MarkGenerator
2221
from _pytask.nodes import MetaTask
22+
from _pytask.outcomes import ExitCode
2323
from _pytask.pluginmanager import get_plugin_manager
2424
from _pytask.session import Session
2525
from _pytask.shared import convert_truthy_or_falsy_to_bool

src/_pytask/outcomes.py

+23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""This module contains code related to outcomes."""
22
from enum import auto
33
from enum import Enum
4+
from enum import IntEnum
45
from typing import Dict
56
from typing import Optional
67
from typing import Sequence
@@ -99,6 +100,7 @@ class TaskOutcome(Enum):
99100

100101
@property
101102
def symbol(self) -> str:
103+
"""The symbol of an outcome."""
102104
symbols = {
103105
TaskOutcome.SUCCESS: ".",
104106
TaskOutcome.PERSISTENCE: "p",
@@ -112,6 +114,7 @@ def symbol(self) -> str:
112114

113115
@property
114116
def description(self) -> str:
117+
"""A description of an outcome used in the summary panel."""
115118
descriptions = {
116119
TaskOutcome.SUCCESS: "Succeeded",
117120
TaskOutcome.PERSISTENCE: "Persisted",
@@ -125,6 +128,7 @@ def description(self) -> str:
125128

126129
@property
127130
def style(self) -> str:
131+
"""Return the style of an outcome."""
128132
styles = {
129133
TaskOutcome.SUCCESS: "success",
130134
TaskOutcome.PERSISTENCE: "success",
@@ -138,6 +142,7 @@ def style(self) -> str:
138142

139143
@property
140144
def style_textonly(self) -> str:
145+
"""Return the style of an outcome when only the text is colored."""
141146
styles_textonly = {
142147
TaskOutcome.SUCCESS: "success.textonly",
143148
TaskOutcome.PERSISTENCE: "success.textonly",
@@ -169,6 +174,24 @@ def count_outcomes(
169174
}
170175

171176

177+
class ExitCode(IntEnum):
178+
"""Exit codes for pytask."""
179+
180+
OK = 0
181+
"""Tasks were executed successfully."""
182+
183+
FAILED = 1
184+
"""Failed while executing tasks."""
185+
186+
CONFIGURATION_FAILED = 2
187+
188+
COLLECTION_FAILED = 3
189+
"""Failed while collecting tasks."""
190+
191+
RESOLVING_DEPENDENCIES_FAILED = 4
192+
"""Failed while resolving dependencies."""
193+
194+
172195
class PytaskOutcome(Exception):
173196
"""Base outcome of a task."""
174197

src/_pytask/profile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
from _pytask.console import console
2121
from _pytask.console import format_task_id
2222
from _pytask.database import db
23-
from _pytask.enums import ExitCode
2423
from _pytask.exceptions import CollectionError
2524
from _pytask.exceptions import ConfigurationError
2625
from _pytask.nodes import FilePathNode
2726
from _pytask.nodes import MetaTask
27+
from _pytask.outcomes import ExitCode
2828
from _pytask.outcomes import TaskOutcome
2929
from _pytask.pluginmanager import get_plugin_manager
3030
from _pytask.report import ExecutionReport

src/_pytask/session.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import attr
88
import networkx as nx
9-
from _pytask.enums import ExitCode
9+
from _pytask.outcomes import ExitCode
1010

1111

1212
# Location was moved from pluggy v0.13.1 to v1.0.0.

src/pytask/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""This module contains the main namespace for pytask."""
12
from _pytask import __version__
23
from _pytask.build import main
34
from _pytask.cli import cli

tests/test_build.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import textwrap
22

33
import pytest
4+
from _pytask.outcomes import ExitCode
45
from pytask import cli
56

67

@@ -13,13 +14,13 @@ def task_raises():
1314
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
1415

1516
result = runner.invoke(cli, [tmp_path.as_posix()])
16-
assert result.exit_code == 1
17+
assert result.exit_code == ExitCode.FAILED
1718

1819

1920
@pytest.mark.end_to_end
2021
def test_configuration_failed(runner, tmp_path):
2122
result = runner.invoke(cli, [tmp_path.joinpath("non_existent_path").as_posix()])
22-
assert result.exit_code == 2
23+
assert result.exit_code == ExitCode.CONFIGURATION_FAILED
2324

2425

2526
@pytest.mark.end_to_end
@@ -30,7 +31,7 @@ def test_collection_failed(runner, tmp_path):
3031
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
3132

3233
result = runner.invoke(cli, [tmp_path.as_posix()])
33-
assert result.exit_code == 3
34+
assert result.exit_code == ExitCode.COLLECTION_FAILED
3435

3536

3637
@pytest.mark.end_to_end
@@ -51,4 +52,4 @@ def task_passes_2():
5152
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
5253

5354
result = runner.invoke(cli, [tmp_path.as_posix()])
54-
assert result.exit_code == 4
55+
assert result.exit_code == ExitCode.RESOLVING_DEPENDENCIES_FAILED

tests/test_capture.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from _pytask.capture import CaptureManager
1818
from _pytask.capture import CaptureResult
1919
from _pytask.capture import MultiCapture
20+
from _pytask.outcomes import ExitCode
2021
from pytask import cli
2122

2223

@@ -78,7 +79,7 @@ def task_show_capture():
7879
cmd_arg = "-s" if show_capture == "s" else f"--show-capture={show_capture}"
7980
result = runner.invoke(cli, [tmp_path.as_posix(), cmd_arg])
8081

81-
assert result.exit_code == 1
82+
assert result.exit_code == ExitCode.FAILED
8283

8384
if show_capture in ["no", "s"]:
8485
assert "Captured" not in result.output
@@ -197,7 +198,7 @@ def task_unicode():
197198
result = runner.invoke(cli, [tmp_path.as_posix(), f"--capture={method}"])
198199

199200
assert "1 Succeeded" in result.output
200-
assert result.exit_code == 0
201+
assert result.exit_code == ExitCode.OK
201202

202203

203204
@pytest.mark.end_to_end
@@ -214,7 +215,7 @@ def task_unicode():
214215
result = runner.invoke(cli, [tmp_path.as_posix(), f"--capture={method}"])
215216

216217
assert "1 Succeeded" in result.output
217-
assert result.exit_code == 0
218+
assert result.exit_code == ExitCode.OK
218219

219220

220221
@pytest.mark.end_to_end
@@ -739,7 +740,7 @@ def task_stdin():
739740
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
740741

741742
result = runner.invoke(cli, [tmp_path.as_posix(), "--capture=fd"])
742-
assert result.exit_code == 0
743+
assert result.exit_code == ExitCode.OK
743744
assert "3 Succeeded" in result.output
744745

745746
def test_fdcapture_invalid_fd_with_fd_reuse(self, tmp_path):

tests/test_clean.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import textwrap
22

33
import pytest
4+
from _pytask.outcomes import ExitCode
45
from pytask import cli
56

67

@@ -153,7 +154,7 @@ def test_configuration_failed(runner, tmp_path):
153154
result = runner.invoke(
154155
cli, ["clean", tmp_path.joinpath("non_existent_path").as_posix()]
155156
)
156-
assert result.exit_code == 2
157+
assert result.exit_code == ExitCode.CONFIGURATION_FAILED
157158

158159

159160
@pytest.mark.end_to_end
@@ -164,4 +165,4 @@ def test_collection_failed(runner, tmp_path):
164165
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
165166

166167
result = runner.invoke(cli, ["clean", tmp_path.as_posix()])
167-
assert result.exit_code == 3
168+
assert result.exit_code == ExitCode.COLLECTION_FAILED

tests/test_collect.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from _pytask.nodes import create_task_name
1111
from _pytask.nodes import PythonFunctionTask
1212
from _pytask.outcomes import CollectionOutcome
13+
from _pytask.outcomes import ExitCode
1314
from _pytask.session import Session
1415
from pytask import cli
1516
from pytask import main
@@ -78,7 +79,7 @@ def task_1(depends_on, produces):
7879

7980
result = runner.invoke(cli, [tmp_path.as_posix()])
8081

81-
assert result.exit_code == 0
82+
assert result.exit_code == ExitCode.OK
8283
assert tmp_path.joinpath("out_0.txt").read_text() == "in root"
8384
assert tmp_path.joinpath("out_1.txt").read_text() == "in sub"
8485

tests/test_database.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55
from _pytask.database import create_database
66
from _pytask.database import State
7+
from _pytask.outcomes import ExitCode
78
from pony import orm
89
from pytask import cli
910

@@ -27,7 +28,7 @@ def task_write(produces):
2728
os.chdir(tmp_path)
2829
result = runner.invoke(cli)
2930

30-
assert result.exit_code == 0
31+
assert result.exit_code == ExitCode.OK
3132

3233
with orm.db_session:
3334

tests/test_debugging.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import pytest
88
from _pytask.debugging import _pdbcls_callback
9+
from _pytask.outcomes import ExitCode
910
from pytask import cli
1011

1112
try:
@@ -442,7 +443,7 @@ def helper():
442443
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
443444

444445
result = runner.invoke(cli, [tmp_path.as_posix(), "--show-locals"])
445-
assert result.exit_code == 1
446+
assert result.exit_code == ExitCode.FAILED
446447

447448
captured = result.output
448449
assert " locals " in captured

0 commit comments

Comments
 (0)