From b98cf95380d194c7e141c415eb363edb2cf1d5de Mon Sep 17 00:00:00 2001 From: sahil-chhoker Date: Fri, 29 Nov 2024 21:45:12 +0530 Subject: [PATCH 1/2] added empty iterable checks and updated tests --- mesa/batchrunner.py | 15 +++++++++++++-- tests/test_batch_run.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/mesa/batchrunner.py b/mesa/batchrunner.py index bc35d1cfe65..f3b19bc5ecf 100644 --- a/mesa/batchrunner.py +++ b/mesa/batchrunner.py @@ -106,9 +106,16 @@ def _make_model_kwargs( Parameters ---------- parameters : Mapping[str, Union[Any, Iterable[Any]]] - Single or multiple values for each model parameter name + Single or multiple values for each model parameter name. - Returns: + Allowed values for each parameter: + - A single value (e.g., `32`, `"relu"`). + - A non-empty iterable (e.g., `[0.01, 0.1]`, `["relu", "sigmoid"]`). + + Not allowed: + - Empty lists or empty iterables (e.g., `[]`, `()`, etc.). These should be removed manually. + + Returns ------- List[Dict[str, Any]] A list of all kwargs combinations. @@ -118,6 +125,10 @@ def _make_model_kwargs( if isinstance(values, str): # The values is a single string, so we shouldn't iterate over it. all_values = [(param, values)] + elif isinstance(values, list | tuple | set) and len(values) == 0: + # If it's an empty iterable, raise an error + raise ValueError(f"Parameter '{param}' contains an empty iterable, which is not allowed.") + else: try: all_values = [(param, value) for value in values] diff --git a/tests/test_batch_run.py b/tests/test_batch_run.py index c60232b50e9..16046a15263 100644 --- a/tests/test_batch_run.py +++ b/tests/test_batch_run.py @@ -24,6 +24,34 @@ def test_make_model_kwargs(): # noqa: D103 assert _make_model_kwargs({"a": "value"}) == [{"a": "value"}] +def test_batch_run_with_params_with_empty_content(): + """Test handling of empty iterables in model kwargs.""" + + # If "a" is a single value and "b" is an empty list (should raise error for the empty list) + parameters_with_empty_list = { + "a": 3, + "b": [], + } + + try: + _make_model_kwargs(parameters_with_empty_list) + raise AssertionError("Expected ValueError for empty iterable but no error was raised.") + except ValueError as e: + assert "contains an empty iterable" in str(e) + + # If "a" is a iterable and "b" is an empty list (should still raise error) + parameters_with_empty_b = { + "a": [1, 2], + "b": [], + } + + try: + _make_model_kwargs(parameters_with_empty_b) + raise AssertionError("Expected ValueError for empty iterable but no error was raised.") + except ValueError as e: + assert "contains an empty iterable" in str(e) + + class MockAgent(Agent): """Minimalistic agent implementation for testing purposes.""" From c006fc36c726b530213471006d37c1496fe8c061 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 29 Nov 2024 16:18:06 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mesa/batchrunner.py | 8 +++++--- tests/test_batch_run.py | 9 ++++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/mesa/batchrunner.py b/mesa/batchrunner.py index f3b19bc5ecf..bcc4dffb164 100644 --- a/mesa/batchrunner.py +++ b/mesa/batchrunner.py @@ -115,7 +115,7 @@ def _make_model_kwargs( Not allowed: - Empty lists or empty iterables (e.g., `[]`, `()`, etc.). These should be removed manually. - Returns + Returns: ------- List[Dict[str, Any]] A list of all kwargs combinations. @@ -127,8 +127,10 @@ def _make_model_kwargs( all_values = [(param, values)] elif isinstance(values, list | tuple | set) and len(values) == 0: # If it's an empty iterable, raise an error - raise ValueError(f"Parameter '{param}' contains an empty iterable, which is not allowed.") - + raise ValueError( + f"Parameter '{param}' contains an empty iterable, which is not allowed." + ) + else: try: all_values = [(param, value) for value in values] diff --git a/tests/test_batch_run.py b/tests/test_batch_run.py index 16046a15263..aec13bc4ccc 100644 --- a/tests/test_batch_run.py +++ b/tests/test_batch_run.py @@ -26,7 +26,6 @@ def test_make_model_kwargs(): # noqa: D103 def test_batch_run_with_params_with_empty_content(): """Test handling of empty iterables in model kwargs.""" - # If "a" is a single value and "b" is an empty list (should raise error for the empty list) parameters_with_empty_list = { "a": 3, @@ -35,7 +34,9 @@ def test_batch_run_with_params_with_empty_content(): try: _make_model_kwargs(parameters_with_empty_list) - raise AssertionError("Expected ValueError for empty iterable but no error was raised.") + raise AssertionError( + "Expected ValueError for empty iterable but no error was raised." + ) except ValueError as e: assert "contains an empty iterable" in str(e) @@ -47,7 +48,9 @@ def test_batch_run_with_params_with_empty_content(): try: _make_model_kwargs(parameters_with_empty_b) - raise AssertionError("Expected ValueError for empty iterable but no error was raised.") + raise AssertionError( + "Expected ValueError for empty iterable but no error was raised." + ) except ValueError as e: assert "contains an empty iterable" in str(e)