Skip to content

Commit

Permalink
improve message for missing keys
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetretto committed Oct 9, 2024
1 parent df85692 commit fbcb674
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
18 changes: 16 additions & 2 deletions src/qtoolkit/io/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import abc
import difflib
import shlex
from dataclasses import fields
from pathlib import Path
Expand Down Expand Up @@ -82,9 +83,22 @@ def generate_header(self, options: dict | QResources | None) -> str:

# check that all the options are present in the template
keys = set(options.keys())
extra = keys.difference(template.get_identifiers())
all_identifiers = template.get_identifiers()
extra = keys.difference(all_identifiers)
if extra:
msg = f"The following keys are not present in the template: {', '.join(sorted(extra))}"
close_matches = {}
for extra_val in extra:
m = difflib.get_close_matches(extra_val, all_identifiers, n=1)
if m:
close_matches[extra_val] = m[0]
msg = (
f"The following keys are not present in the template: {', '.join(sorted(extra))}. "
f"Check the template in {type(self).__module__}.{type(self).__qualname__}.header_template"
)
if close_matches:
msg += "Possible replacements:"
for extra_val, match in close_matches.items():
msg += f" {match} instead of {extra_val}."
raise ValueError(msg)

unclean_header = template.safe_substitute(options)
Expand Down
14 changes: 13 additions & 1 deletion tests/io/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def test_generate_header(self, scheduler):

with pytest.raises(
ValueError,
match=r"The following keys are not present in the template: tata, titi",
match=r"The following keys are not present in the template: tata, titi. Check the template in .*MyScheduler.header_template",
):
res = QResources(
nodes=4,
Expand All @@ -155,6 +155,18 @@ def test_generate_header(self, scheduler):
)
scheduler.generate_header(res)

with pytest.raises(
ValueError,
match=r"The following keys are not present in the template: option32, processes-per-node. "
r"Check the template in .*MyScheduler.header_template.*option3 instead of option32. processes_per_node instead of processes-per-node",
):
res = QResources(
nodes=4,
processes_per_node=16,
scheduler_kwargs={"option32": "xxx", "processes-per-node": "yyy"},
)
scheduler.generate_header(res)

def test_generate_ids_list(self, scheduler):
ids_list = scheduler.generate_ids_list(
[QJob(job_id=4), QJob(job_id="job_id_abc1"), 215, "job12345"]
Expand Down

0 comments on commit fbcb674

Please sign in to comment.