mesa icon indicating copy to clipboard operation
mesa copied to clipboard

Make AbstractAgentSet.select() and CellCollection.select() raise on negative integer or boolean at_most

Open Dilraj07 opened this issue 2 months ago • 0 comments

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 < 0 should raise ValueError("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 ..."), matching AbstractAgentSet.select_random().

I have a fix with unit tests ready for both modules and can open a PR.

Dilraj07 avatar Sep 18 '26 14:09 Dilraj07