Skip to content

Commit

Permalink
Fix issue #1831: check if positions values are tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
maskarb committed Oct 7, 2023
1 parent d888a8c commit f813875
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
7 changes: 3 additions & 4 deletions mesa/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
22 changes: 22 additions & 0 deletions tests/test_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,28 @@ def move_agent(self):
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
Expand Down

0 comments on commit f813875

Please sign in to comment.