Repository navigation
Raise SpaceException when Grid.select_random_empty_cell finds a full grid - #4032
Merged
Merged
Conversation
…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.
quaquel
approved these changes
Nov 4, 2026
Author
|
Thanks for the review and merge, @quaquel! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Pre-PR Checklist
Summary
Grid.select_random_empty_cell()on a full grid now raisesSpaceException, likeNetworkandVoronoiGriddo since #4026.Bug / Issue
Fixes #4031.
Gridoverridesselect_random_empty_celland wrapped the empty-choiceIndexErrorin aValueError, whileDiscreteSpace.select_random_empty_cellraisesSpaceException. The same call on a full space raised different exception types depending on the space class.Implementation
mesa/discrete_space/grid.py: raiseSpaceException(stillfrom e), with the messageDiscreteSpaceuses, 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 caughtValueError, since this changes the exception type.Testing
test_infinite_loop_on_full_gridto expectSpaceExceptionand the class name in the message. It fails onmainwith the oldValueError.pytest tests -W error -W default::PendingDeprecationWarning: 1019 passed (tests/examples/test_examples_viz.pyskipped locally, it needs a Playwright browser).ruff checkandruff format --checkpass on the changed files.Additional Notes
SpaceExceptiondoes not subclassValueError, soexcept ValueErroraround this call no longer catches it; the migration guide entry covers that. If you'd rather keepexcept ValueErrorworking, I can switch to an exception that inherits from both, likeTableMissingException.