Repository: projectmesa/mesa
Pull request #1524 (2022-11-12 13:20:29)

Raise exception when pos is out of bounds in Grid.get_neighborhood
By https://github.com/projectmesa/mesa/pull/1476#issuecomment-1301238002

codecov[bot]: # [Codecov](https://codecov.io/gh/projectmesa/mesa/pull/1524?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa) Report
Base: **91.31**% // Head: **91.33**% // Increases project coverage by **`+0.01%`** :tada:
> Coverage data is based on head [(`4943725`)](https://codecov.io/gh/projectmesa/mesa/pull/1524?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa) compared to base [(`78a6f9a`)](https://codecov.io/gh/projectmesa/mesa/commit/78a6f9a523362b8b51a0808824435b02ac764098?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa).
> Patch coverage: 100.00% of modified lines in pull request are covered.

> :exclamation: Current head 4943725 differs from pull request most recent head d8b0492. Consider uploading reports for the commit d8b0492 to get more accurate results

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


```diff
@@            Coverage Diff             @@
##             main    #1524      +/-   ##
==========================================
+ Coverage   91.31%   91.33%   +0.01%     
==========================================
  Files          15       15              
  Lines        1313     1315       +2     
  Branches      227      228       +1     
==========================================
+ Hits         1199     1201       +2     
  Misses         80       80              
  Partials       34       34              
```


| [Impacted Files](https://codecov.io/gh/projectmesa/mesa/pull/1524?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://codecov.io/gh/projectmesa/mesa/pull/1524/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa#diff-bWVzYS9zcGFjZS5weQ==) | `95.84% <100.00%> (+0.02%)` | :arrow_up: |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa)

</details>

[:umbrella: View full report at Codecov](https://codecov.io/gh/projectmesa/mesa/pull/1524?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa).

rht: Since this is a breaking change, we need to wait until we are aiming at Mesa 2.0.
Tortar: yes, no problem, thank you for the reminder!
rht: I'm about to merge this since we are going for 2.0, but I wasn't able to force push https://github.com/Tortar/mesa/tree/patch-16, unlike in other PRs. Not sure if maintainer edit is disabled, or if it is due to something else. Can you rebase your branch?
rht: It seems GH is buggy right now, since I can't even push to my own fork.
rht: Nvm, I force-pushed.

Patch:
diff --git a/mesa/space.py b/mesa/space.py
index 9dc64245..22ca214a 100644
--- a/mesa/space.py
+++ b/mesa/space.py
@@ -268,6 +268,9 @@ class _Grid:
         if neighborhood is not None:
             return neighborhood
 
+        if self.out_of_bounds(pos):
+            raise Exception("The `pos` tuple passed is out of bounds.")
+
         # We use a list instead of a dict for the neighborhood because it would
         # be easier to port the code to Cython or Numba (for performance
         # purpose), with minimal changes. To better understand how the
@@ -309,7 +312,7 @@ class _Grid:
 
                     neighborhood.append((nx, ny))
 
-        if not include_center and neighborhood:
+        if not include_center:
             neighborhood.remove(pos)
 
         self._neighborhood_cache[cache_key] = neighborhood


Test patch:
diff --git a/tests/test_grid.py b/tests/test_grid.py
index 7b4c590d..a24db60a 100644
--- a/tests/test_grid.py
+++ b/tests/test_grid.py
@@ -119,11 +119,8 @@ class TestSingleGrid(unittest.TestCase):
         neighborhood = self.grid.get_neighborhood((0, 0), moore=False)
         assert len(neighborhood) == 2
 
-        neighbors = self.grid.get_neighbors((4, 1), moore=False)
-        assert len(neighbors) == 0
-
-        neighbors = self.grid.get_neighbors((4, 1), moore=True)
-        assert len(neighbors) == 0
+        with self.assertRaises(Exception):
+            neighbors = self.grid.get_neighbors((4, 1), moore=False)
 
         neighbors = self.grid.get_neighbors((1, 1), moore=False, include_center=True)
         assert len(neighbors) == 3