From 06f57d58a2b9ee5315d50577ae077422b1888928 Mon Sep 17 00:00:00 2001 From: Ivan Ergunov Date: Mon, 11 May 2026 14:56:04 +0200 Subject: [PATCH 1/2] fix: clearer ValueError when Solver values are missing or wrong type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Calling solver() with no arguments on a multi-variable formula raised 'The value of the \"values\" parameter is not a dict! Its type is ' — which incorrectly framed the problem as a type mismatch when the caller had actually just forgotten to pass anything. Split the two cases: - values is None and the formula has variables → "Missing values for variables: {...}" - values is not None but not a Mapping → "Expected a Mapping for 'values' (got ); variables to provide: {...}" Regression test in tests/test_solver_missing_values_message.py. See ai/improvements_2026-05-09.md item #7. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/formula/formula.py | 9 ++++-- tests/test_solver_missing_values_message.py | 36 +++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 tests/test_solver_missing_values_message.py diff --git a/src/formula/formula.py b/src/formula/formula.py index 884943f8..cd3614ab 100644 --- a/src/formula/formula.py +++ b/src/formula/formula.py @@ -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: diff --git a/tests/test_solver_missing_values_message.py b/tests/test_solver_missing_values_message.py new file mode 100644 index 00000000..3c97acb7 --- /dev/null +++ b/tests/test_solver_missing_values_message.py @@ -0,0 +1,36 @@ +"""Regression: clear error message when values are missing or the wrong type. + +See ai/improvements_2026-05-09.md item #7. + +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 " — 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" From 558838825df6e567acffbfb80218039c74b8657b Mon Sep 17 00:00:00 2001 From: "Ivan (don_vanchos) Ergunov" Date: Sat, 16 May 2026 23:42:16 +0200 Subject: [PATCH 2/2] Update test_solver_missing_values_message.py --- tests/test_solver_missing_values_message.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_solver_missing_values_message.py b/tests/test_solver_missing_values_message.py index 3c97acb7..b3bb36a8 100644 --- a/tests/test_solver_missing_values_message.py +++ b/tests/test_solver_missing_values_message.py @@ -1,7 +1,5 @@ """Regression: clear error message when values are missing or the wrong type. -See ai/improvements_2026-05-09.md item #7. - 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 " — misleading, because the actual problem is