Repository navigation
Make CellCollection.select() raise on invalid float at_most - #3860
Merged
quaquel merged 1 commit intoSep 15, 2026
Merged
Conversation
CellCollection.select() had the same fraction check that mesa#3853 fixed in AbstractAgentSet.select(): `at_most <= 1.0 and isinstance(at_most, float)`. With at_most=float("nan") the branch is skipped and the generator's `count >= at_most` never becomes true, so every cell is selected. A float above 1.0 such as 1.5 is used as a raw count (2 cells), and negative floats or 0.0 silently select nothing. Validate the (0.0, 1.0] range on the float path and raise ValueError otherwise, matching AgentSet.select().
quaquel
approved these changes
Sep 15, 2026
|
Performance benchmarks:
|
1 task done
rishiiicreates
added a commit
to rishiiicreates/mesa
that referenced
this pull request
Oct 3, 2026
Following mesa#3853 and mesa#3860, negative integers (which previously silently returned an empty set) and booleans (which were treated as 0 or 1 because isinstance(bool, int) is True) were not validated. Now AbstractAgentSet.select() and CellCollection.select(): - Raise TypeError if at_most is a boolean or not int/float. - Raise ValueError if at_most is an integer < 0. Fixes mesa#3867
EwoutH
pushed a commit
that referenced
this pull request
Oct 28, 2026
CellCollection.select() had the same fraction check that #3853 fixed in AbstractAgentSet.select(): `at_most <= 1.0 and isinstance(at_most, float)`. With at_most=float("nan") the branch is skipped and the generator's `count >= at_most` never becomes true, so every cell is selected. A float above 1.0 such as 1.5 is used as a raw count (2 cells), and negative floats or 0.0 silently select nothing. Validate the (0.0, 1.0] range on the float path and raise ValueError otherwise, matching AgentSet.select(). (cherry picked from commit 730b7fd)
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
CellCollection.select()still has the fraction check that #3853 just fixed inAbstractAgentSet.select(), so invalid float values forat_mostare silently accepted on cell collections:nanselects every cell,1.5selects two cells, and-0.5or0.0select none.Bug / Issue
Same root cause as #3852, in
mesa/discrete_space/cell_collection.py:nan <= 1.0is False, so NaN skips the branch and reaches the generator, wherecount >= at_mostis never true and the loop never stops. A float above 1.0 also skips the branch and ends up used as a raw count, soat_most=1.5gives 2 cells rather than an error. Negative floats and0.0go through the branch and floor to a value that selects nothing.On a collection of 10 cells at current main I get
nan -> 10,1.5 -> 2,-0.5 -> 0,0.0 -> 0.Implementation
I mirrored the check from #3853: on the float path (excluding the
infdefault),at_mostmust be in(0.0, 1.0], otherwiseValueErroris raised. Integers and valid fractions are unchanged, and the docstring gets aRaises:entry.Testing
I added the same four cases as #3853 (
nan,0.0,-0.5,1.5) to the existingtest_cell_collection. With only the test applied it fails withDID NOT RAISE ValueError; with the fix it passes. After the fix,1.0 -> 10,0.5 -> 5and3 -> 3still behave as before. The wholetests/discrete_spacedirectory passes (100 tests), andruff checkandruff format --checkare clean on both files.Additional Notes
This makes
CellCollection.select()andAgentSet.select()consistent again. I didn't find any caller inside the repo that passes a float above 1.0 toCellCollection.select().Found by a defect-hunting pipeline I build and run (Dev-next-gen), using Claude Code with Anthropic's Claude Opus 5.