Skip to content

Commit

Permalink
Fix bug where only literal error had position info
Browse files Browse the repository at this point in the history
  • Loading branch information
frode-aarstad committed Jan 23, 2025
1 parent c1af334 commit 8529ab4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
9 changes: 4 additions & 5 deletions src/everest/config/everest_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,14 +754,13 @@ def load_file(config_file: str) -> "EverestConfig":
for e in error.errors(
include_context=True, include_input=True, include_url=False
):
if e["type"] == "literal_error":
if e["type"] in {"missing", "value_error"} or e["input"] is None:
exp.errors.append((e, None))
else:
for index, line in enumerate(file_content):
if (pos := line.find(e["input"])) != -1:
if (pos := line.find(str(e["input"]))) != -1:
exp.errors.append((e, (index + 1, pos + 1)))
break
else:
exp.errors.append((e, None))

raise exp from error

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion tests/everest/functional/test_main_everest_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def test_everest_main_lint_entry(cached_example):
f"""Loading config file <config_minimal.yml> failed with:
Found 1 validation error:
controls -> 0 -> initial_guess
line: 2, column: 18. controls -> 0 -> initial_guess
* Input should be a valid number, unable to parse string as a number {type_}
"""
)
Expand Down
22 changes: 19 additions & 3 deletions tests/everest/test_config_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -998,14 +998,30 @@ def test_load_file_with_errors(copy_math_func_test_data_to_tmp, capsys):
content = file.read()

with open("config_minimal_error.yml", "w", encoding="utf-8") as file:
file.write(content.replace("generic_control", "yolo_control"))
content = content.replace("generic_control", "yolo_control")
content = content.replace("max: 1.0", "max: not_a number")
pos = content.find("name: distance")
content = content[: pos + 14] + "\n invalid: invalid" + content[pos + 14 :]
file.write(content)

with pytest.raises(SystemExit):
parser = ArgumentParser(prog="test")
EverestConfig.load_file_with_argparser("config_minimal_error.yml", parser)

captured = capsys.readouterr()

assert "Found 1 validation error" in captured.err
assert "Found 3 validation error" in captured.err
assert "line: 3, column: 11" in captured.err
assert "Input should be 'well_control' or 'generic_control'" in captured.err
assert (
"Input should be 'well_control' or 'generic_control' (type=literal_error)"
in captured.err
)

assert "line: 5, column: 10" in captured.err
assert (
"Input should be a valid number, unable to parse string as a number (type=float_parsing)"
in captured.err
)

assert "line: 16, column: 5" in captured.err
assert "Extra inputs are not permitted (type=extra_forbidden)" in captured.err

0 comments on commit 8529ab4

Please sign in to comment.