Fix NoneType position error in Hotelling’s Law SpaceDrawer #283
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes a TypeError in the Hotelling’s Law example that caused the visualization to crash whenever an agent didn’t have a valid grid position.
Bug / Issue
In examples/hotelling_law/app.py, the SpaceDrawer component assumes that agent.pos is always a tuple like (x, y).
However, starting with Mesa 3.2, agents can temporarily have pos = None when they're not placed on the grid.
When the code tried to access pos[0], it resulted in:
TypeError: 'NoneType' object is not subscriptable
Fix
I updated SpaceDrawer to safely handle agents whose positions are None:
Added a simple if agent.pos is None: continue in the loop that builds the cell contents for store agents.
Added the same check in the loop that draws the scatter plot.
With these guards, agents without positions are skipped during rendering, preventing the crash.
Testing
Manual testing: Confirmed that solara run app.py now runs without errors and the visualization loads correctly.
Automated testing: Ran pytest test_examples.py locally. All 15 tests passed, so the underlying model behavior remains unchanged.
Additional Notes
This update only adjusts the visualization logic to match Mesa 3.2+ behavior regarding pos=None.
No changes were made to the simulation itself.