mesa
mesa copied to clipboard
Make AbstractAgentSet.select() and CellCollection.select() raise on negative integer or boolean at_most
Describe the bug
Following PR #3853 and PR #3860 which added float fraction validation in select(), negative integers and boolean inputs are still not validated in AbstractAgentSet.select() and CellCollection.select().
To Reproduce
# 1. Negative integer silently returns empty set instead of raising ValueError
model.agents.select(at_most=-1) # returns 0 agents
# 2. Booleans are silently treated as 0 or 1 because isinstance(bool, int) is True
model.agents.select(at_most=False) # returns 0 agents
model.agents.select(at_most=True) # returns 1 agent
# 3. Non-numeric types crash late inside generator rather than failing eagerly
list(model.agents.select(at_most="5")) # TypeError: '>=' not supported between instances of 'int' and 'str'
(The same behavior reproduces on CellCollection.select()).
Expected Behavior
- Passing an integer
< 0should raiseValueError("Integer at_most must be non-negative, got ..."). - Passing a boolean or non-number should raise
TypeError("at_most must be an integer or float, got ..."), matchingAbstractAgentSet.select_random().
I have a fix with unit tests ready for both modules and can open a PR.