Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check if positions values are tuples #1832

Merged
merged 1 commit into from
Oct 8, 2023
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
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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes more sense, because the previous code mistakenly detects ((1, 2), (2, 3)) as [positions].

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