Skip to content

Commit 3e44287

Browse files
committed
Skip lockfile writes for unchanged tasks
1 parent d9dc5e6 commit 3e44287

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

src/_pytask/lockfile.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ def update_task(self, session: Session, task: PTask) -> None:
248248
entry = _build_task_entry(session, task, self.root)
249249
if entry is None:
250250
return
251+
existing = self._task_index.get(entry.id)
252+
if existing == entry:
253+
return
251254
self._task_index[entry.id] = entry
252255
self.lockfile = _Lockfile(
253256
lock_version=CURRENT_LOCKFILE_VERSION,

tests/test_lockfile.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66

7+
import _pytask.lockfile as lockfile_module
78
from _pytask.lockfile import LockfileError
89
from _pytask.lockfile import LockfileVersionError
910
from _pytask.lockfile import build_portable_node_id
@@ -127,3 +128,33 @@ def func_second(path):
127128
lockfile = read_lockfile(tmp_path / "pytask.lock")
128129
assert lockfile is not None
129130
assert {entry.id for entry in lockfile.task} == {"task_first"}
131+
132+
133+
def test_update_task_skips_write_when_unchanged(tmp_path, monkeypatch):
134+
def func(path):
135+
path.write_text("data")
136+
137+
task = TaskWithoutPath(
138+
name="task",
139+
function=func,
140+
produces={"path": PathNode(path=tmp_path / "out.txt")},
141+
)
142+
143+
session = build(tasks=[task], paths=tmp_path)
144+
assert session.exit_code == ExitCode.OK
145+
146+
lockfile_state = session.config["lockfile_state"]
147+
assert lockfile_state is not None
148+
149+
calls = {"count": 0}
150+
151+
original_write = lockfile_module.write_lockfile
152+
153+
def _counting_write(path, lockfile):
154+
calls["count"] += 1
155+
return original_write(path, lockfile)
156+
157+
monkeypatch.setattr(lockfile_module, "write_lockfile", _counting_write)
158+
lockfile_state.update_task(session, session.tasks[0])
159+
160+
assert calls["count"] == 0

0 commit comments

Comments
 (0)