Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug where only literal error had position info #9826

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading