Repository: projectmesa/mesa
Pull request #1509 (2022-11-06 09:50:05)

fix: position_agent: Enforce type of x and y

codecov[bot]: # [Codecov](https://codecov.io/gh/projectmesa/mesa/pull/1509?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa) Report
Base: **91.32**% // Head: **91.20**% // Decreases project coverage by **`-0.12%`** :warning:
> Coverage data is based on head [(`e064f42`)](https://codecov.io/gh/projectmesa/mesa/pull/1509?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa) compared to base [(`c7ec7b4`)](https://codecov.io/gh/projectmesa/mesa/commit/c7ec7b477b34e17b7fe41d610a9d95d0be65af38?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.

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


```diff
@@            Coverage Diff             @@
##             main    #1509      +/-   ##
==========================================
- Coverage   91.32%   91.20%   -0.13%     
==========================================
  Files          15       15              
  Lines        1303     1307       +4     
  Branches      223      225       +2     
==========================================
+ Hits         1190     1192       +2     
- Misses         80       81       +1     
- Partials       33       34       +1     
```


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

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/1509?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).

Tortar: It seems to me that `position_agent` should be deprecater because everything can be done just by `place_agent` and `move_to_empty`, to me it doesn't make much sense to have also `position_agent`  
rht: > It seems to me that `position_agent` should be deprecated because everything can be done just by `place_agent` and `move_to_empty`, to me it doesn't make much sense to have also `position_agent`

Yes, I wanted to say this but forgot to send the message.
tpike3: LGTM... glad we Kaunta identified, thanks for fixing @rht  

Patch:
diff --git a/examples/schelling/model.py b/examples/schelling/model.py
index 52cfafa2..aadc9700 100644
--- a/examples/schelling/model.py
+++ b/examples/schelling/model.py
@@ -70,7 +70,7 @@ class Schelling(mesa.Model):
                     agent_type = 0
 
                 agent = SchellingAgent((x, y), self, agent_type)
-                self.grid.position_agent(agent, (x, y))
+                self.grid.position_agent(agent, x, y)
                 self.schedule.add(agent)
 
         self.running = True
diff --git a/mesa/space.py b/mesa/space.py
index c0081f74..538423f1 100644
--- a/mesa/space.py
+++ b/mesa/space.py
@@ -504,6 +504,17 @@ class SingleGrid(Grid):
         If x or y are positive, they are used.
         Use 'swap_pos()' to swap agents positions.
         """
+        if not (isinstance(x, int) or x == "random"):
+            raise Exception(
+                "x must be an integer or a string 'random'."
+                f" Actual type: {type(x)}. Actual value: {x}."
+            )
+        if not (isinstance(y, int) or y == "random"):
+            raise Exception(
+                "y must be an integer or a string 'random'."
+                f" Actual type: {type(y)}. Actual value: {y}."
+            )
+
         if x == "random" or y == "random":
             if len(self.empties) == 0:
                 raise Exception("ERROR: Grid full")


Test patch:
diff --git a/tests/test_grid.py b/tests/test_grid.py
index 245a5c35..7a706198 100644
--- a/tests/test_grid.py
+++ b/tests/test_grid.py
@@ -262,6 +262,25 @@ class TestSingleGrid(unittest.TestCase):
                 self.grid.place_agent(a, (x, y))
         self.num_agents = len(self.agents)
 
+    @patch.object(MockAgent, "model", create=True)
+    def test_position_agent(self, mock_model):
+        a = MockAgent(100, None)
+        with self.assertRaises(Exception) as exc_info:
+            self.grid.position_agent(a, (1, 1))
+        expected = (
+            "x must be an integer or a string 'random'."
+            " Actual type: <class 'tuple'>. Actual value: (1, 1)."
+        )
+        assert str(exc_info.exception) == expected
+        with self.assertRaises(Exception) as exc_info:
+            self.grid.position_agent(a, "(1, 1)")
+        expected = (
+            "x must be an integer or a string 'random'."
+            " Actual type: <class 'str'>. Actual value: (1, 1)."
+        )
+        assert str(exc_info.exception) == expected
+        self.grid.position_agent(a, "random")
+
     @patch.object(MockAgent, "model", create=True)
     def test_enforcement(self, mock_model):
         """