Skip to content

Raise SpaceException when Grid.select_random_empty_cell finds a full grid - #4032

Merged
quaquel merged 2 commits into
mesa:mainfrom
arshil42:fix/grid-full-space-exception
Nov 4, 2026
Merged

quaquel merged 2 commits into
mesa:mainfrom
arshil42:fix/grid-full-space-exception

Conversation

@arshil42

@arshil42 arshil42 commented Nov 4, 2026

Copy link
Copy Markdown

Pre-PR Checklist

  • This PR is a bug fix, not a new feature or enhancement.

Summary

Grid.select_random_empty_cell() on a full grid now raises SpaceException, like Network and VoronoiGrid do since #4026.

Bug / Issue

Fixes #4031.

Grid overrides select_random_empty_cell and wrapped the empty-choice IndexError in a ValueError, while DiscreteSpace.select_random_empty_cell raises SpaceException. The same call on a full space raised different exception types depending on the space class.

Implementation

  • mesa/discrete_space/grid.py: raise SpaceException (still from e), with the message DiscreteSpace uses, naming the class: OrthogonalMooreGrid is completely full. No empty cells available. Cannot select a random empty cell.
  • docs/migration_guide.md: Mesa 4.0.0 entry for code that caught ValueError, since this changes the exception type.

Testing

  • Updated test_infinite_loop_on_full_grid to expect SpaceException and the class name in the message. It fails on main with the old ValueError.
  • pytest tests -W error -W default::PendingDeprecationWarning: 1019 passed (tests/examples/test_examples_viz.py skipped locally, it needs a Playwright browser).
  • ruff check and ruff format --check pass on the changed files.

Additional Notes

SpaceException does not subclass ValueError, so except ValueError around this call no longer catches it; the migration guide entry covers that. If you'd rather keep except ValueError working, I can switch to an exception that inherits from both, like TableMissingException.

…grid

Grid overrides select_random_empty_cell and still raised ValueError on a
full grid, while DiscreteSpace (Network, VoronoiGrid) raises
SpaceException since mesa#4026. Raise SpaceException from Grid too, with the
same message, and add a Mesa 4.0 migration guide entry.
@github-actions github-actions Bot added docs Release notes label testing Release notes label labels Nov 4, 2026
@quaquel quaquel added enhancement Release notes label and removed testing Release notes label labels Nov 4, 2026
@quaquel
quaquel merged commit 013d2bb into mesa:main Nov 4, 2026
18 checks passed
@github-actions github-actions Bot added the testing Release notes label label Nov 4, 2026
@arshil42

arshil42 commented Nov 4, 2026

Copy link
Copy Markdown
Author

Thanks for the review and merge, @quaquel!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Release notes label enhancement Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Align Grid.select_random_empty_cell exception with DiscreteSpace (SpaceException)

2 participants