mesa / mesa/mesa

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

Open
#3,867 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
3.9k
Forks
1.3k
Avg merge
2d 12h
Merged PRs (30d)
20

Description

### 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
```python
# 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.

Contributor guide

Open the contributing guide

Research direction

Start with AbstractAgentSet.select() and CellCollection.select(), comparing their validation behavior with select_random(). Add unit tests for negative integers, booleans, and non-numeric values, then verify the methods raise the specified ValueError or TypeError before iteration.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.