Repository navigation
Make CellCollection.select_random_cell empty-safe with a default parameter - #3725
Merged
Merged
Conversation
|
Performance benchmarks:
|
quaquel
approved these changes
May 31, 2026
Member
|
This makes sense to me and looks good. Thanks for the PR. |
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.
What
CellCollection.select_random_cell()callsself.random.choice(self.cells)directly, which raises a bare
IndexErrorwith no context when the collectionis empty.
The sibling method
select_random_agent()already handles this gracefully via adefaultparameter (returning the default when empty, or raisingLookupErrorotherwise). This PR brings
select_random_cell()in line with that existing APIso the two methods behave consistently.
Change
select_random_cell(default=RAISES): returnsdefaultwhen the collection isempty, or raises
LookupErrorwith a clear message if no default is given.Non-empty behavior is unchanged.
This mirrors the existing
select_random_agentsignature and semantics exactly —no new patterns introduced.
Test
Added
test_select_random_cell_empty_safe, matching the existingtest_select_random_agent_empty_safe:LookupErroron an empty collectiondefaultwhen emptyAll 88 tests in
tests/discrete_space/test_discrete_space.pypass;ruffclean.