Skip to content

Make FixedAgent handle being unplaced as agreed in #3748 - #3986

Merged
quaquel merged 1 commit into
mesa:mainfrom
AshNicolus:fix/fixedagent-unplaced
Oct 26, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
AshNicolus:fix/fixedagent-unplaced

Conversation

@AshNicolus

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

  • This PR is a bug fix, not a new feature or enhancement.

Summary

A FixedAgent that hasn't been placed in a cell yet crashed with an unclear AttributeError in two places. This makes it follow the rules agreed with @quaquel in #3748: assigning None before placement is fine, and removing an unplaced agent is an error, raised clearly and before anything is changed.

Bug / Issue

Fixes #3748. The behaviour below is from the discussion there (1, 2).

A fixed agent gets its cell once, after it's created, so it starts out unplaced:

stateDiagram-v2
    direction LR
    [*] --> Unplaced: FixedAgent(model)
    Unplaced --> Unplaced: cell = None (no-op)
    Unplaced --> Placed: cell = some_cell
    Placed --> Removed: remove()
    Unplaced --> Unplaced: remove() raises ValueError, agent untouched
    Placed --> Placed: cell = None or another cell raises ValueError
Loading

What changes:

main this PR
unplaced, agent.cell = None AttributeError no-op, stays unplaced
unplaced, remove() deregisters from the model, then AttributeError ValueError with a clear message, agent untouched
placed, cell = None or another cell ValueError unchanged
placed, remove() works unchanged

The second row was the worst one: the agent was already gone from model.agents by the time the error came up, so it ended up half removed.

Implementation

Two guard clauses in mesa/discrete_space/cell_agent.py, so the normal path is unchanged:

  • FixedCell.cell setter: return early when assigning None to an unplaced agent.
  • FixedAgent.remove(): check for a cell first and raise ValueError before calling super().remove().

The new error reads:

ValueError: Cannot remove FixedAgent 1: it is not in a cell. A fixed agent has to be placed in a cell before it can be removed.

I went with ValueError to match the existing "Cannot move agent in FixedCell" error. Happy to switch both to a SpaceException if you'd prefer that.

As agreed, model.remove_all_agents() still raises when the model has an unplaced fixed agent, now with this message instead of the AttributeError.

Testing

Four tests in tests/discrete_space/test_discrete_space.py:

  • assigning None while unplaced is a no-op, and the agent can still be placed afterwards
  • remove() on an unplaced agent raises and the agent is still in model.agents
  • a second remove() after a successful one raises
  • assigning None after placement still raises (unchanged behaviour, kept as a guard)

The first three fail on main and pass here. The full suite passes with pytest tests/ -W error (937 passed), and pre-commit is clean. I also ran the mesa-examples suite against this branch: 5 example files use FixedAgent and all 25 tests pass, so nothing there relies on removing an unplaced fixed agent.

A FixedAgent can be assigned a cell once, after it is created, so being
unplaced is a valid state until then. Two cases didn't handle it:

- agent.cell = None while unplaced raised AttributeError. It is now a
  no-op that leaves the agent unplaced.
- remove() on an unplaced agent deregistered it from the model first and
  then raised AttributeError, leaving it half removed. It now raises a
  clear ValueError before touching anything.

Assigning None or another cell after placement still raises, as before.

Fixes mesa#3748
@quaquel quaquel added the bug Release notes label label Oct 26, 2026
@quaquel
quaquel merged commit 7f69078 into mesa:main Oct 26, 2026
16 of 17 checks passed
EwoutH pushed a commit that referenced this pull request Oct 28, 2026
A FixedAgent can be assigned a cell once, after it is created, so being
unplaced is a valid state until then. Two cases didn't handle it:

- agent.cell = None while unplaced raised AttributeError. It is now a
  no-op that leaves the agent unplaced.
- remove() on an unplaced agent deregistered it from the model first and
  then raised AttributeError, leaving it half removed. It now raises a
  clear ValueError before touching anything.

Assigning None or another cell after placement still raises, as before.

Fixes #3748

(cherry picked from commit 7f69078)
@EwoutH EwoutH mentioned this pull request Nov 15, 2026
53 of 65 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FixedAgent.remove() raises AttributeError when the agent has no cell

2 participants