Skip to content
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: 6 additions & 3 deletions src/formula/formula.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ def __call__(
pass
elif values is not None and len(variables) == 1:
variables_to_values = {variables.pop(): str(values)}
elif values is None:
raise ValueError(
f"Missing values for variables: {variables}"
)
else:
raise ValueError(
"The value of the 'values' parameter is not a dict!"
" Its type is %s A dictionary is expected with values for"
" the following variables: %s" % (type(values), str(variables))
f"Expected a Mapping for 'values' (got "
f"{type(values).__name__}); variables to provide: {variables}"
)

for key in variables_to_values:
Expand Down
34 changes: 34 additions & 0 deletions tests/test_solver_missing_values_message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Regression: clear error message when values are missing or the wrong type.

Before, calling solver() (values=None) on a multi-variable formula raised
ValueError with "The value of the 'values' parameter is not a dict! Its
type is <class 'NoneType'>" — misleading, because the actual problem is
that the caller didn't pass any values at all.
"""

import pytest

from formula import Solver


def test_missing_values_dict_for_multi_variable_formula():
solver = Solver("x + y")
with pytest.raises(ValueError, match="Missing values for variables"):
solver()


def test_wrong_type_for_values_on_multi_variable_formula():
solver = Solver("x + y")
with pytest.raises(ValueError, match="Expected a Mapping"):
solver(123)


def test_wrong_type_error_names_the_actual_type():
solver = Solver("x + y")
with pytest.raises(ValueError, match="int"):
solver(42)


def test_proper_values_still_work():
solver = Solver("x + y")
assert solver({"x": "1", "y": "2"}) == "3"
Loading