Skip to content

Commit ae329df

Browse files
committed
Restore duration plugin and ty config
1 parent bbf88f2 commit ae329df

4 files changed

Lines changed: 47 additions & 21 deletions

File tree

justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ test-cov *FLAGS:
1212

1313
# Run type checking
1414
typing:
15-
uv run --group typing --group test --isolated ty check src/ tests/
15+
uv run --group typing --group test --isolated ty check
1616

1717
# Run linting
1818
lint:

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ include = [
179179
unused-ignore-comment = "ignore"
180180

181181
[tool.ty.src]
182+
include = ["src", "tests"]
182183
exclude = ["src/_pytask/_hashlib.py"]
183184

184185
[tool.ty.terminal]

src/_pytask/profile.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ def pytask_extend_command_line_interface(cli: click.Group) -> None:
5454
@hookimpl
5555
def pytask_post_parse(config: dict[str, Any]) -> None:
5656
"""Register the export option."""
57-
config["pm"].register(ProfilePlugin(RuntimeState.from_root(config["root"])))
57+
runtime_state = RuntimeState.from_root(config["root"])
58+
config["pm"].register(ProfilePlugin(runtime_state))
59+
config["pm"].register(DurationNameSpace(runtime_state))
5860
config["pm"].register(ExportNameSpace)
5961
config["pm"].register(FileSizeNameSpace)
6062

@@ -86,15 +88,6 @@ def pytask_execute_task_process_report(
8688
if report.outcome == TaskOutcome.SUCCESS and duration is not None:
8789
self.runtime_state.update_task(task, *duration)
8890

89-
@hookimpl
90-
def pytask_profile_add_info_on_task(
91-
self, session: Session, tasks: list[PTask], profile: dict[str, dict[str, Any]]
92-
) -> None:
93-
"""Add the runtime for tasks to the profile."""
94-
_ = session
95-
for name, duration in self._collect_runtimes(tasks).items():
96-
profile[name]["Duration (in s)"] = round(duration, 2)
97-
9891
@hookimpl
9992
def pytask_unconfigure(self, session: Session) -> None:
10093
"""Flush runtime information on normal build exits."""
@@ -104,13 +97,23 @@ def pytask_unconfigure(self, session: Session) -> None:
10497
return
10598
self.runtime_state.flush()
10699

107-
def _collect_runtimes(self, tasks: list[PTask]) -> dict[str, float]:
108-
"""Collect runtimes."""
109-
return {
110-
task.name: duration
111-
for task in tasks
112-
if (duration := self.runtime_state.get_duration(task)) is not None
113-
}
100+
101+
class DurationNameSpace:
102+
"""A namespace for adding durations to the profile."""
103+
104+
def __init__(self, runtime_state: RuntimeState) -> None:
105+
self.runtime_state = runtime_state
106+
107+
@hookimpl
108+
def pytask_profile_add_info_on_task(
109+
self, session: Session, tasks: list[PTask], profile: dict[str, dict[str, Any]]
110+
) -> None:
111+
"""Add the runtime for tasks to the profile."""
112+
_ = session
113+
for task in tasks:
114+
duration = self.runtime_state.get_duration(task)
115+
if duration is not None:
116+
profile[task.name]["Duration (in s)"] = round(duration, 2)
114117

115118

116119
@click.command(cls=ColoredCommand)

tests/test_runtime_store.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
11
from __future__ import annotations
22

3-
from types import SimpleNamespace
3+
from typing import Any
44

55
import pytest
66

77
from _pytask.runtime_store import RuntimeState
88

99

10+
class DummyTask:
11+
def __init__(self, name: str) -> None:
12+
self.name = name
13+
self.depends_on = {}
14+
self.produces = {}
15+
self.function = lambda: None
16+
self.markers = []
17+
self.report_sections = []
18+
self.attributes = {}
19+
20+
@property
21+
def signature(self) -> str:
22+
return self.name
23+
24+
def state(self) -> str | None:
25+
return None
26+
27+
def execute(self, **kwargs: Any) -> Any:
28+
_ = kwargs
29+
return None
30+
31+
1032
def test_runtime_state_recovers_from_journal(tmp_path):
1133
tmp_path.joinpath(".pytask").mkdir()
12-
task = SimpleNamespace(name="task_example")
34+
task = DummyTask(name="task_example")
1335

1436
state = RuntimeState.from_root(tmp_path)
1537
state.update_task(task, 1.0, 3.0)
@@ -20,7 +42,7 @@ def test_runtime_state_recovers_from_journal(tmp_path):
2042

2143
def test_runtime_state_flushes_journal(tmp_path):
2244
tmp_path.joinpath(".pytask").mkdir()
23-
task = SimpleNamespace(name="task_example")
45+
task = DummyTask(name="task_example")
2446

2547
state = RuntimeState.from_root(tmp_path)
2648
state.update_task(task, 2.0, 5.5)

0 commit comments

Comments
 (0)