Skip to content

Commit bdb686d

Browse files
committed
style: apply ruff format to add_coordinator.py and test_add_command.py
1 parent d9ddc29 commit bdb686d

2 files changed

Lines changed: 49 additions & 35 deletions

File tree

openkb/add_coordinator.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,7 @@ def _failure_target(details: dict) -> str:
8888

8989
def run_add_mutation(kb_dir: Path, plan: AddMutationPlan) -> bool:
9090
if not kb_ingest_lock_held(kb_dir / ".openkb"):
91-
raise RuntimeError(
92-
"run_add_mutation requires the caller to hold kb_ingest_lock"
93-
)
91+
raise RuntimeError("run_add_mutation requires the caller to hold kb_ingest_lock")
9492
snapshot = None
9593
try:
9694
snapshot = snapshot_paths(
@@ -109,9 +107,7 @@ def run_add_mutation(kb_dir: Path, plan: AddMutationPlan) -> bool:
109107
# than committing more docs on top of dirty state that the next
110108
# recovery would roll back over.
111109
raise DirtyRollbackError(plan.operation, dirty_journal)
112-
click.echo(
113-
f" [ERROR] {plan.operation} failed{_failure_target(plan.details)}: {exc}"
114-
)
110+
click.echo(f" [ERROR] {plan.operation} failed{_failure_target(plan.details)}: {exc}")
115111
logger.debug("%s mutation failed:", plan.operation, exc_info=True)
116112
return False
117113
except BaseException:

tests/test_add_command.py

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,13 @@ def test_add_single_file_uses_add_mutation_coordinator(self, tmp_path):
114114
doc_name="coordinated",
115115
)
116116

117-
with patch("openkb.cli.convert_document", return_value=result), \
118-
patch("openkb.cli.publish_staged_tree"), \
119-
patch("openkb.cli.asyncio.run"), \
120-
patch("openkb.cli._setup_llm_key"), \
121-
patch("openkb.add_coordinator.run_add_mutation", return_value=True) as mock_run:
117+
with (
118+
patch("openkb.cli.convert_document", return_value=result),
119+
patch("openkb.cli.publish_staged_tree"),
120+
patch("openkb.cli.asyncio.run"),
121+
patch("openkb.cli._setup_llm_key"),
122+
patch("openkb.add_coordinator.run_add_mutation", return_value=True) as mock_run,
123+
):
122124
assert add_single_file(doc, kb_dir) == "added"
123125

124126
plan = mock_run.call_args.args[1]
@@ -248,8 +250,10 @@ def test_add_directory_stops_after_dirty_rollback(self, tmp_path):
248250
)
249251

250252
runner = CliRunner()
251-
with patch("openkb.cli.add_single_file", side_effect=dirty_error) as mock_add, \
252-
patch("openkb.cli._find_kb_dir", return_value=kb_dir):
253+
with (
254+
patch("openkb.cli.add_single_file", side_effect=dirty_error) as mock_add,
255+
patch("openkb.cli._find_kb_dir", return_value=kb_dir),
256+
):
253257
with pytest.raises(DirtyRollbackError) as exc_info:
254258
runner.invoke(cli, ["add", str(docs_dir)], catch_exceptions=False)
255259

@@ -524,9 +528,11 @@ def test_cloud_import_uses_add_mutation_coordinator(self, tmp_path):
524528
kb_dir = self._setup_kb(tmp_path)
525529
cloud = self._cloud_data(doc_name="Cloud-Paper")
526530

527-
with patch("openkb.cli.prepare_cloud_import", return_value=cloud), \
528-
patch("openkb.add_coordinator.run_add_mutation", return_value=True) as mock_run, \
529-
patch("openkb.cli._setup_llm_key"):
531+
with (
532+
patch("openkb.cli.prepare_cloud_import", return_value=cloud),
533+
patch("openkb.add_coordinator.run_add_mutation", return_value=True) as mock_run,
534+
patch("openkb.cli._setup_llm_key"),
535+
):
530536
assert import_from_pageindex_cloud("cloud-1", kb_dir) == "added"
531537

532538
plan = mock_run.call_args.args[1]
@@ -567,10 +573,12 @@ def race_before_lock(openkb_dir):
567573
with real_kb_ingest_lock(openkb_dir):
568574
yield
569575

570-
with patch("openkb.cli.prepare_cloud_import", return_value=cloud), \
571-
patch("openkb.cli.kb_ingest_lock", side_effect=race_before_lock), \
572-
patch("openkb.cli.compile_long_doc", return_value=None), \
573-
patch("openkb.cli._setup_llm_key"):
576+
with (
577+
patch("openkb.cli.prepare_cloud_import", return_value=cloud),
578+
patch("openkb.cli.kb_ingest_lock", side_effect=race_before_lock),
579+
patch("openkb.cli.compile_long_doc", return_value=None),
580+
patch("openkb.cli._setup_llm_key"),
581+
):
574582
assert import_from_pageindex_cloud("cloud-1", kb_dir) == "added"
575583

576584
registry = HashRegistry(kb_dir / ".openkb" / "hashes.json")
@@ -613,11 +621,13 @@ def register_same_doc_before_second_lock(openkb_dir):
613621
with real_kb_ingest_lock(openkb_dir):
614622
yield
615623

616-
with patch("openkb.cli.prepare_cloud_import", return_value=cloud) as mock_prepare, \
617-
patch("openkb.cli.kb_ingest_lock", side_effect=register_same_doc_before_second_lock), \
618-
patch("openkb.cli.compile_long_doc") as mock_compile, \
619-
patch("openkb.add_coordinator.run_add_mutation") as mock_run, \
620-
patch("openkb.cli._setup_llm_key"):
624+
with (
625+
patch("openkb.cli.prepare_cloud_import", return_value=cloud) as mock_prepare,
626+
patch("openkb.cli.kb_ingest_lock", side_effect=register_same_doc_before_second_lock),
627+
patch("openkb.cli.compile_long_doc") as mock_compile,
628+
patch("openkb.add_coordinator.run_add_mutation") as mock_run,
629+
patch("openkb.cli._setup_llm_key"),
630+
):
621631
assert import_from_pageindex_cloud("cloud-1", kb_dir) == "skipped"
622632

623633
assert lock_calls["count"] == 2
@@ -639,9 +649,11 @@ def test_cloud_import_propagates_dirty_rollback(self, tmp_path):
639649
kb_dir / ".openkb" / "journal" / "retained.json",
640650
)
641651

642-
with patch("openkb.cli.prepare_cloud_import", return_value=cloud), \
643-
patch("openkb.add_coordinator.run_add_mutation", side_effect=dirty_error), \
644-
patch("openkb.cli._setup_llm_key"):
652+
with (
653+
patch("openkb.cli.prepare_cloud_import", return_value=cloud),
654+
patch("openkb.add_coordinator.run_add_mutation", side_effect=dirty_error),
655+
patch("openkb.cli._setup_llm_key"),
656+
):
645657
with pytest.raises(DirtyRollbackError) as exc_info:
646658
import_from_pageindex_cloud("cloud-1", kb_dir)
647659

@@ -658,10 +670,14 @@ def test_cloud_import_failure_message_names_real_cause(self, tmp_path, capsys):
658670
kb_dir = self._setup_kb(tmp_path)
659671
cloud = self._cloud_data(doc_name="Cloud-Paper")
660672

661-
with patch("openkb.cli.prepare_cloud_import", return_value=cloud), \
662-
patch("openkb.cli.resolve_doc_name_from_key",
663-
side_effect=RuntimeError("name resolution blew up")), \
664-
patch("openkb.cli._setup_llm_key"):
673+
with (
674+
patch("openkb.cli.prepare_cloud_import", return_value=cloud),
675+
patch(
676+
"openkb.cli.resolve_doc_name_from_key",
677+
side_effect=RuntimeError("name resolution blew up"),
678+
),
679+
patch("openkb.cli._setup_llm_key"),
680+
):
665681
assert import_from_pageindex_cloud("cloud-1", kb_dir) == "failed"
666682

667683
out = capsys.readouterr().out
@@ -678,9 +694,11 @@ def test_cloud_import_keyboard_interrupt_rolls_back_artifacts(self, tmp_path):
678694
doc_name = "Cloud-Paper"
679695
cloud = self._cloud_data(doc_name=doc_name)
680696

681-
with patch("openkb.cli.prepare_cloud_import", return_value=cloud), \
682-
patch("openkb.cli.compile_long_doc", side_effect=KeyboardInterrupt()), \
683-
patch("openkb.cli._setup_llm_key"):
697+
with (
698+
patch("openkb.cli.prepare_cloud_import", return_value=cloud),
699+
patch("openkb.cli.compile_long_doc", side_effect=KeyboardInterrupt()),
700+
patch("openkb.cli._setup_llm_key"),
701+
):
684702
with pytest.raises(KeyboardInterrupt):
685703
import_from_pageindex_cloud("cloud-1", kb_dir)
686704

0 commit comments

Comments
 (0)