From 253e4e4cab171cfcd914b4683cb1a58378039df5 Mon Sep 17 00:00:00 2001 From: ShreyasN Date: Wed, 3 Dec 2025 00:46:33 +0530 Subject: [PATCH 1/7] solved the warning issues --- tests/test_solara_viz.py | 2 ++ tests/test_space_drawer.py | 15 ++++++++------- tests/test_space_renderer.py | 25 ++++++++++++++++++------- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/tests/test_solara_viz.py b/tests/test_solara_viz.py index 8408c871dd6..3de53a901ac 100644 --- a/tests/test_solara_viz.py +++ b/tests/test_solara_viz.py @@ -1,5 +1,6 @@ """Test Solara visualizations.""" +import random import re import unittest @@ -204,6 +205,7 @@ def drawer(model): voronoi_model = mesa.Model() voronoi_model.grid = mesa.discrete_space.VoronoiGrid( centroids_coordinates=[(0, 1), (0, 0), (1, 0)], + random=random.Random(42), ) solara.render( SolaraViz(voronoi_model, components=[make_mpl_space_component(agent_portrayal)]) diff --git a/tests/test_space_drawer.py b/tests/test_space_drawer.py index a6e29197049..02faf6a55bf 100644 --- a/tests/test_space_drawer.py +++ b/tests/test_space_drawer.py @@ -1,5 +1,6 @@ """Test space drawer classes for various grid types.""" +import random from unittest.mock import MagicMock, patch import altair as alt @@ -20,12 +21,12 @@ @pytest.fixture def orthogonal_grid(): # noqa: D103 - return OrthogonalMooreGrid([5, 5]) + return OrthogonalMooreGrid([5, 5], random=random.Random(42)) @pytest.fixture def hex_grid(): # noqa: D103 - return HexGrid([5, 5]) + return HexGrid([5, 5], random=random.Random(42)) @pytest.fixture @@ -38,13 +39,13 @@ def network_grid(): # noqa: D103 G = nx.Graph() # noqa: N806 G.add_nodes_from([0, 1, 2]) G.add_edges_from([(0, 1), (1, 2)]) - return Network(G) + return Network(G, random=random.Random(42)) @pytest.fixture def voronoi_space(): # noqa: D103 points = [(0, 0), (1, 0), (0, 1), (1, 1)] - return VoronoiGrid(points) + return VoronoiGrid(points, random=random.Random(42)) class TestOrthogonalSpaceDrawer: @@ -288,7 +289,7 @@ def test_get_clipped_segments(self, voronoi_space): # noqa: D102 class TestEdgeCases: # noqa: D101 def test_single_point_voronoi_grid(self): # noqa: D102 - single_point_voronoi = VoronoiGrid([(0.5, 0.5)]) + single_point_voronoi = VoronoiGrid([(0.5, 0.5)], random=random.Random(42)) drawer = VoronoiSpaceDrawer(single_point_voronoi) assert drawer.viz_xmin is not None assert drawer.viz_xmax is not None @@ -300,13 +301,13 @@ def test_continuous_space_zero_size(self): # noqa: D102 def test_network_empty_graph(self): # noqa: D102 empty_graph = nx.Graph() - network = Network(empty_graph) + network = Network(empty_graph, random=random.Random(42)) drawer = NetworkSpaceDrawer(network) assert len(drawer.pos) == 0 assert drawer.s_default == 1 # Default when no nodes def test_hex_grid_single_cell(self): # noqa: D102 - grid = HexGrid([1, 1]) + grid = HexGrid([1, 1], random=random.Random(42)) drawer = HexSpaceDrawer(grid) assert len(drawer.hexagons) == 1 assert drawer.s_default == (180 / 1) ** 2 diff --git a/tests/test_space_renderer.py b/tests/test_space_renderer.py index d74c6468bd8..f86463124f4 100644 --- a/tests/test_space_renderer.py +++ b/tests/test_space_renderer.py @@ -1,5 +1,6 @@ """Test cases for the SpaceRenderer class in Mesa.""" +import random import re from unittest.mock import MagicMock, patch @@ -39,7 +40,9 @@ class CustomModel(mesa.Model): def __init__(self, seed=None): # noqa: D107 super().__init__(seed=seed) - self.grid = mesa.discrete_space.OrthogonalMooreGrid([2, 2]) + self.grid = mesa.discrete_space.OrthogonalMooreGrid( + [2, 2], random=random.Random(42) + ) self.layer = PropertyLayer("test", [2, 2], default_value=0) self.grid.add_property_layer(self.layer) @@ -59,16 +62,22 @@ def test_backend_selection(): @pytest.mark.parametrize( "grid,expected_drawer", [ - (OrthogonalMooreGrid([2, 2]), OrthogonalSpaceDrawer), + ( + OrthogonalMooreGrid([2, 2], random=random.Random(42)), + OrthogonalSpaceDrawer, + ), (SingleGrid(width=2, height=2, torus=False), OrthogonalSpaceDrawer), (MultiGrid(width=2, height=2, torus=False), OrthogonalSpaceDrawer), - (HexGrid([2, 2]), HexSpaceDrawer), + (HexGrid([2, 2], random=random.Random(42)), HexSpaceDrawer), (HexSingleGrid(width=2, height=2, torus=False), HexSpaceDrawer), (HexMultiGrid(width=2, height=2, torus=False), HexSpaceDrawer), - (Network(G=MagicMock()), NetworkSpaceDrawer), + (Network(G=MagicMock(), random=random.Random(42)), NetworkSpaceDrawer), (NetworkGrid(g=MagicMock()), NetworkSpaceDrawer), (ContinuousSpace(x_max=2, y_max=2, torus=False), ContinuousSpaceDrawer), - (VoronoiGrid([[0, 0], [1, 1]]), VoronoiSpaceDrawer), + ( + VoronoiGrid([[0, 0], [1, 1]], random=random.Random(42)), + VoronoiSpaceDrawer, + ), ], ) def test_space_drawer_selection(grid, expected_drawer): @@ -91,14 +100,16 @@ def test_map_coordinates(): # same for orthogonal grids assert np.array_equal(mapped["loc"], arr) - with patch.object(model, "grid", new=HexGrid([2, 2])): + with patch.object(model, "grid", new=HexGrid([2, 2], random=random.Random(42))): sr = SpaceRenderer(model) mapped = sr._map_coordinates(args) assert not np.array_equal(mapped["loc"], arr) assert mapped["loc"].shape == arr.shape - with patch.object(model, "grid", new=Network(G=MagicMock())): + with patch.object( + model, "grid", new=Network(G=MagicMock(), random=random.Random(42)) + ): sr = SpaceRenderer(model) # Patch the space_drawer.pos to provide a mapping for the test sr.space_drawer.pos = {0: (0, 0), 1: (1, 1), 2: (2, 2), 3: (3, 3)} From 2b8f43b99733c025e0f74d49e82d0fbc11305549 Mon Sep 17 00:00:00 2001 From: ShreyasN Date: Wed, 3 Dec 2025 10:12:50 +0530 Subject: [PATCH 2/7] Fixed the warnings of files in issue 2.2 --- tests/test_components_matplotlib.py | 4 +++- tests/test_solara_viz.py | 2 +- tests/test_solara_viz_updated.py | 2 +- tests/test_space_renderer.py | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/test_components_matplotlib.py b/tests/test_components_matplotlib.py index 265b8f58257..1b2093a4bce 100644 --- a/tests/test_components_matplotlib.py +++ b/tests/test_components_matplotlib.py @@ -222,7 +222,9 @@ def test_draw_property_layers(): """Test drawing property layers.""" model = Model(seed=42) grid = SingleGrid(10, 10, torus=True) - grid.add_property_layer(PropertyLayer("test", grid.width, grid.height, 0)) + grid.add_property_layer( + PropertyLayer("test", grid.width, grid.height, 0, dtype=int) + ) _, ax = plt.subplots() draw_property_layers(grid, {"test": {"colormap": "viridis", "colorbar": True}}, ax) diff --git a/tests/test_solara_viz.py b/tests/test_solara_viz.py index 3de53a901ac..50313291139 100644 --- a/tests/test_solara_viz.py +++ b/tests/test_solara_viz.py @@ -118,7 +118,7 @@ class MockModel(mesa.Model): def __init__(self, seed=None): super().__init__(seed=seed) layer1 = PropertyLayer( - name="sugar", width=10, height=10, default_value=10.0 + name="sugar", width=10, height=10, default_value=10.0, dtype=float ) self.grid = MultiGrid( width=10, height=10, torus=True, property_layers=layer1 diff --git a/tests/test_solara_viz_updated.py b/tests/test_solara_viz_updated.py index 03db138fa2d..ebbe62bf5dc 100644 --- a/tests/test_solara_viz_updated.py +++ b/tests/test_solara_viz_updated.py @@ -132,7 +132,7 @@ class MockModel(mesa.Model): def __init__(self, seed=None): super().__init__(seed=seed) layer1 = PropertyLayer( - name="sugar", width=10, height=10, default_value=10.0 + name="sugar", width=10, height=10, default_value=10.0, dtype=float ) self.grid = MultiGrid( width=10, height=10, torus=True, property_layers=layer1 diff --git a/tests/test_space_renderer.py b/tests/test_space_renderer.py index f86463124f4..422dd8f5e07 100644 --- a/tests/test_space_renderer.py +++ b/tests/test_space_renderer.py @@ -43,7 +43,7 @@ def __init__(self, seed=None): # noqa: D107 self.grid = mesa.discrete_space.OrthogonalMooreGrid( [2, 2], random=random.Random(42) ) - self.layer = PropertyLayer("test", [2, 2], default_value=0) + self.layer = PropertyLayer("test", [2, 2], default_value=0, dtype=int) self.grid.add_property_layer(self.layer) From 3863507072c91b9c87c405daef4e2672c2dec63e Mon Sep 17 00:00:00 2001 From: ShreyasN Date: Thu, 4 Dec 2025 00:10:20 +0530 Subject: [PATCH 3/7] Fix Ignored Dict Keys in Agent Portrayal --- mesa/visualization/mpl_space_drawing.py | 2 +- tests/test_components_matplotlib.py | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/mesa/visualization/mpl_space_drawing.py b/mesa/visualization/mpl_space_drawing.py index fb3ea7b5b47..7dcdc396608 100644 --- a/mesa/visualization/mpl_space_drawing.py +++ b/mesa/visualization/mpl_space_drawing.py @@ -205,7 +205,7 @@ def draw_space( Returns the Axes object with the plot drawn onto it. ``agent_portrayal`` is called with an agent and should return a AgentPortrayalStyle. Valid fields in this object are "color", - "size", "marker", "zorder", alpha, linewidths, and edgecolors. Other field are ignored and will result in a user warning. + "size", "marker", "zorder", "alpha", "linewidths", and "edgecolors". Other field are ignored and will result in a user warning. """ if ax is None: diff --git a/tests/test_components_matplotlib.py b/tests/test_components_matplotlib.py index 1b2093a4bce..eb55953ffdb 100644 --- a/tests/test_components_matplotlib.py +++ b/tests/test_components_matplotlib.py @@ -37,8 +37,8 @@ def agent_portrayal(agent): """ return { - "s": 10, - "c": "tab:blue", + "size": 10, + "color": "tab:blue", "marker": "s" if (agent.unique_id % 2) == 0 else "o", } @@ -54,12 +54,12 @@ def my_portrayal(agent): """ return { - "s": 10, - "c": "tab:blue", + "size": 10, + "color": "tab:blue", "marker": "s" if (agent.unique_id % 2) == 0 else "o", "alpha": 0.5, "linewidths": 1, - "linecolors": "tab:orange", + "edgecolors": "tab:orange", } # draw space for hexgrid From 49f1b33f1ab5ed439988d387c52ae8100e2df9ba Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 3 Dec 2025 18:46:19 +0000 Subject: [PATCH 4/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_components_matplotlib.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_components_matplotlib.py b/tests/test_components_matplotlib.py index 7d6dc78fa55..eb55953ffdb 100644 --- a/tests/test_components_matplotlib.py +++ b/tests/test_components_matplotlib.py @@ -18,7 +18,6 @@ PropertyLayer, SingleGrid, ) -from mesa.visualization.components import AgentPortrayalStyle from mesa.visualization.mpl_space_drawing import ( draw_continuous_space, draw_hex_grid, From 564f750e3baf893ed53f0b21a275c02d7dd1addc Mon Sep 17 00:00:00 2001 From: ShreyasN Date: Thu, 4 Dec 2025 22:26:16 +0530 Subject: [PATCH 5/7] updated the tests to use new versions --- tests/test_components_matplotlib.py | 34 +++++++++++++---------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/tests/test_components_matplotlib.py b/tests/test_components_matplotlib.py index eb55953ffdb..0a4396ffad9 100644 --- a/tests/test_components_matplotlib.py +++ b/tests/test_components_matplotlib.py @@ -4,6 +4,7 @@ import networkx as nx from mesa import Agent, Model +from mesa.visualization import AgentPortrayalStyle from mesa.discrete_space import ( CellAgent, HexGrid, @@ -30,17 +31,11 @@ def agent_portrayal(agent): - """Simple portrayal of an agent. - - Args: - agent (Agent): The agent to portray - - """ - return { - "size": 10, - "color": "tab:blue", - "marker": "s" if (agent.unique_id % 2) == 0 else "o", - } + return AgentPortrayalStyle( + size=10, + color="tab:blue", + marker="s" if (agent.unique_id % 2) == 0 else "o", + ) def test_draw_space(): @@ -53,14 +48,15 @@ def my_portrayal(agent): agent (Agent): The agent to portray """ - return { - "size": 10, - "color": "tab:blue", - "marker": "s" if (agent.unique_id % 2) == 0 else "o", - "alpha": 0.5, - "linewidths": 1, - "edgecolors": "tab:orange", - } + return AgentPortrayalStyle( + size=10, + color="tab:blue", + marker="s" if (agent.unique_id % 2) == 0 else "o", + alpha=0.5, + linewidths=1, + edgecolors="tab:orange", + ) + # draw space for hexgrid model = Model(seed=42) From 604521cab6a7c256627363b887a52504628cb23e Mon Sep 17 00:00:00 2001 From: ShreyasN Date: Thu, 4 Dec 2025 22:38:36 +0530 Subject: [PATCH 6/7] fixed the visualization import issue --- tests/test_components_matplotlib.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_components_matplotlib.py b/tests/test_components_matplotlib.py index 0a4396ffad9..c2ac69434d7 100644 --- a/tests/test_components_matplotlib.py +++ b/tests/test_components_matplotlib.py @@ -4,7 +4,6 @@ import networkx as nx from mesa import Agent, Model -from mesa.visualization import AgentPortrayalStyle from mesa.discrete_space import ( CellAgent, HexGrid, @@ -19,6 +18,7 @@ PropertyLayer, SingleGrid, ) +from mesa.visualization.components import AgentPortrayalStyle from mesa.visualization.mpl_space_drawing import ( draw_continuous_space, draw_hex_grid, @@ -31,6 +31,7 @@ def agent_portrayal(agent): + """Return a simple AgentPortrayalStyle for testing matplotlib drawing.""" return AgentPortrayalStyle( size=10, color="tab:blue", @@ -57,7 +58,6 @@ def my_portrayal(agent): edgecolors="tab:orange", ) - # draw space for hexgrid model = Model(seed=42) grid = HexSingleGrid(10, 10, torus=True) From 536ded9d949b97d55099328e96a4628b798219de Mon Sep 17 00:00:00 2001 From: ShreyasN Date: Thu, 4 Dec 2025 22:50:03 +0530 Subject: [PATCH 7/7] tests: expect FutureWarning for simulator.time deprecation (match runtime) --- tests/test_devs.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_devs.py b/tests/test_devs.py index bf962c66ca7..7b01cfbd78a 100644 --- a/tests/test_devs.py +++ b/tests/test_devs.py @@ -135,7 +135,9 @@ def test_simulator_time_deprecation(): model = Model() simulator.setup(model) - with pytest.warns(DeprecationWarning, match="simulator.time is deprecated"): + # The runtime now emits a FutureWarning for this deprecation. + # Update test to match the current warning type. + with pytest.warns(FutureWarning, match="simulator.time is deprecated"): _ = simulator.time