Skip to content

Commit 8353641

Browse files
rhttpike3
authored andcommitted
space: Ensure get_neighborhood output & cache are immutable
1 parent cedbefb commit 8353641

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

mesa/space.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def __init__(self, width: int, height: int, torus: bool) -> None:
117117
self._empties_built = False
118118

119119
# Neighborhood Cache
120-
self._neighborhood_cache: dict[Any, list[Coordinate]] = {}
120+
self._neighborhood_cache: dict[Any, Sequence[Coordinate]] = {}
121121

122122
# Cutoff used inside self.move_to_empty. The parameters are fitted on Python
123123
# 3.11 and it was verified that they are roughly the same for 3.10. Refer to
@@ -233,7 +233,7 @@ def get_neighborhood(
233233
moore: bool,
234234
include_center: bool = False,
235235
radius: int = 1,
236-
) -> list[Coordinate]:
236+
) -> Sequence[Coordinate]:
237237
"""Return a list of cells that are in the neighborhood of a
238238
certain point.
239239
@@ -307,9 +307,9 @@ def get_neighborhood(
307307
if not include_center:
308308
neighborhood.pop(pos, None)
309309

310-
self._neighborhood_cache[cache_key] = list(neighborhood.keys())
310+
self._neighborhood_cache[cache_key] = tuple(neighborhood.keys())
311311

312-
return list(neighborhood.keys())
312+
return tuple(neighborhood.keys())
313313

314314
def iter_neighbors(
315315
self,
@@ -681,7 +681,7 @@ def get_neighborhood(
681681
else:
682682
coordinates.discard(pos)
683683

684-
neighborhood = sorted(coordinates)
684+
neighborhood = tuple(sorted(coordinates))
685685
self._neighborhood_cache[cache_key] = neighborhood
686686

687687
return neighborhood

0 commit comments

Comments
 (0)