Repository: projectmesa/mesa
Pull request #1832 (2023-10-07 18:45:32)

check if positions values are tuples
#1831 

Add extra check to the `accept_tuple_argument` wrapper. If `positions` is a tuple of tuples of length 2, then the `iter_cell_list_contents` raises a TypeError.

Example, given a cell_list of `((0, 98), (1, 99))`, this will unpack here `x, y in cell_list` as `x = (0, 98) and y = (1, 99)`.

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/projectmesa/mesa/pull/1832?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa) Report
All modified lines are covered by tests :white_check_mark:
> Comparison is base [(`d888a8c`)](https://app.codecov.io/gh/projectmesa/mesa/commit/d888a8cab397286c8fb3b496f1af8e79bb1b844d?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa) 81.38% compared to head [(`f813875`)](https://app.codecov.io/gh/projectmesa/mesa/pull/1832?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa) 81.38%.

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main    #1832   +/-   ##
=======================================
  Coverage   81.38%   81.38%           
=======================================
  Files          15       15           
  Lines         881      881           
  Branches      186      164   -22     
=======================================
  Hits          717      717           
  Misses        141      141           
  Partials       23       23           
```


| [Files](https://app.codecov.io/gh/projectmesa/mesa/pull/1832?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa) | Coverage Δ | |
|---|---|---|
| [mesa/space.py](https://app.codecov.io/gh/projectmesa/mesa/pull/1832?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa#diff-bWVzYS9zcGFjZS5weQ==) | `90.88% <100.00%> (ø)` | |


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/projectmesa/mesa/pull/1832?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa).


Patch:
diff --git a/mesa/space.py b/mesa/space.py
index 286f63c8..6ed7c096 100644
--- a/mesa/space.py
+++ b/mesa/space.py
@@ -68,10 +68,9 @@ def accept_tuple_argument(wrapped_function: F) -> F:
     single-item list rather than forcing user to do it."""
 
     def wrapper(grid_instance, positions) -> Any:
-        if isinstance(positions, tuple) and len(positions) == 2:
-            return wrapped_function(grid_instance, [positions])
-        else:
-            return wrapped_function(grid_instance, positions)
+        if len(positions) == 2 and not isinstance(positions[0], tuple):
+            positions = [positions]
+        return wrapped_function(grid_instance, positions)
 
     return cast(F, wrapper)
 


Test patch:
diff --git a/tests/test_space.py b/tests/test_space.py
index 8691dc45..f8f2cc94 100644
--- a/tests/test_space.py
+++ b/tests/test_space.py
@@ -327,6 +327,28 @@ class TestSingleGrid(unittest.TestCase):
         assert self.space[initial_pos[0]][initial_pos[1]] is None
         assert self.space[final_pos[0]][final_pos[1]] == _agent
 
+    def test_iter_cell_list_contents(self):
+        """
+        Test neighborhood retrieval
+        """
+        cell_list_1 = list(self.space.iter_cell_list_contents(TEST_AGENTS_GRID[0]))
+        assert len(cell_list_1) == 1
+
+        cell_list_2 = list(
+            self.space.iter_cell_list_contents(
+                (TEST_AGENTS_GRID[0], TEST_AGENTS_GRID[1])
+            )
+        )
+        assert len(cell_list_2) == 2
+
+        cell_list_3 = list(self.space.iter_cell_list_contents(tuple(TEST_AGENTS_GRID)))
+        assert len(cell_list_3) == 3
+
+        cell_list_4 = list(
+            self.space.iter_cell_list_contents((TEST_AGENTS_GRID[0], (0, 0)))
+        )
+        assert len(cell_list_4) == 1
+
 
 class TestSingleNetworkGrid(unittest.TestCase):
     GRAPH_SIZE = 10