Repository navigation
Make select() raise on invalid float at_most instead of silently ignoring it - #3853
Merged
quaquel merged 1 commit intoSep 15, 2026
Merged
Conversation
…ring it
at_most <= 1.0 and isinstance(at_most, float) skips the fraction branch
whenever the comparison is False, which is also what happens for NaN
(all NaN comparisons are False). So at_most=float('nan') fell through
untouched and was later compared as count >= at_most in the generator,
which is always False for NaN, so the loop never breaks and every
agent gets selected instead of none. Negative floats and 0.0 had the
same silent-fallthrough problem, just landing on 0 selected instead of
unlimited.
Now the float path validates the (0.0, 1.0] range up front and raises
ValueError otherwise, matching how select_random already validates its
own n parameter.
|
Performance benchmarks:
|
Member
|
It's a very implausible bug that is fixed here but it might crop up in rare cases. Fix seems fine. |
quaquel
approved these changes
Sep 15, 2026
1 task done
quaquel
pushed a commit
that referenced
this pull request
Sep 15, 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().
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
…ring it (#3853) at_most <= 1.0 and isinstance(at_most, float) skips the fraction branch whenever the comparison is False, which is also what happens for NaN (all NaN comparisons are False). So at_most=float('nan') fell through untouched and was later compared as count >= at_most in the generator, which is always False for NaN, so the loop never breaks and every agent gets selected instead of none. Negative floats and 0.0 had the same silent-fallthrough problem, just landing on 0 selected instead of unlimited. Now the float path validates the (0.0, 1.0] range up front and raises ValueError otherwise, matching how select_random already validates its own n parameter. (cherry picked from commit c267146)
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.
Fixes #3852
at_most <= 1.0 and isinstance(at_most, float)skips the fraction branch whenever the comparison is False, which is also what NaN does (every NaN comparison is False). Soat_most=float('nan')fell through untouched, and the generator'scount >= at_mostcheck is then always False for NaN, so the loop never breaks and every agent gets selected instead of none. Negative floats and0.0had the same fallthrough problem, just landing on 0 selected instead of unlimited.Fix validates the
(0.0, 1.0]range up front and raisesValueErrorfor anything outside it, matching howselect_random'snparameter already validates the same range on its own float path.Verified against the pre-fix commit:
at_most=float('nan')returned 10/10 agents before the fix, raisesValueErrorafter. Ran the fulltest_agentset.py, 39 passed. Added 4 cases (nan, 0.0, -0.5, 1.5) to the existingtest_agentsettest. ruff check + format clean.