Repository navigation
Add regression test for retained empty agents_by_type bucket - #3809
Merged
Merged
Conversation
|
Performance benchmarks:
|
dietcoke-17
approved these changes
Aug 19, 2026
dietcoke-17
left a comment
Contributor
There was a problem hiding this comment.
LGTM
One things that looked good to me is that :--
- list(model.agents_by_type[Prey]) before removing avoids mutating the
set mid-iteration — mirrors the pattern Model.remove_all_agents()
already uses internally.
Contributor
Author
|
Thanks for the review. Yes, the copy-before-iterate is deliberate so the test |
quaquel
approved these changes
Aug 26, 2026
1 task done
EwoutH
pushed a commit
that referenced
this pull request
Oct 28, 2026
(cherry picked from commit 19eff50)
tpike3
added a commit
that referenced
this pull request
Nov 10, 2026
… agent (#3970) * Answer the class lookup from the dict key, not from a live agent extract_class looked a class up by name and then read the class back off an arbitrary instance: agent_type = agent_type_names.get(new_agent_class) if agent_type is None: return None return type(next(iter(agents_by_type[agent_type]))) agents_by_type is keyed by the agent class itself, and Model.register_agent keys it by exact type(agent), so type(instance) is always the key the instance was found under. The round trip answered nothing, but it did make the result depend on the population: dissolving the last group of a name leaves an empty bucket behind, so the name lookup still succeeded while there was no instance left to take the class from, and next(iter(...)) raised StopIteration. team = membership.create("Team", [a, b]) membership.dissolve(team) membership.create("Team", [a, b]) # StopIteration Returning the key fixes the crash and makes the lookup independent of whether any instance is alive, which is what the docstring promises. The empty bucket is not the bug. #3809 added a regression test locking that behaviour in, because WolfSheep indexes agents_by_type[Wolf] every step and expects an empty set rather than a KeyError once that population dies out. Pruning the bucket would break that, so the fix belongs here. @shipitdev diagnosed this in #3850 and reached the same one-line change; that PR was closed as a stale draft after the meta-agents rewrite, without review of the approach. Adds three tests: the reported dissolve-then-recreate path, an invariant that the lookup gives the same class with and without live instances, and the unknown-name case so the None early return stays covered. The first two fail on main with StopIteration. Fixes #3849 * Describe extract_class and its test generically in their docstrings Both docstrings were written as an account of the bug this PR fixes. Per PEP 257 they should describe what the function and the test do, so rewrite them that way: extract_class gets a summary line plus Args and Returns in the package's Google style, and the test's docstring states the behaviour it asserts. No behaviour change. Co-Authored-By: Claude Opus 5 <[email protected]> --------- Co-authored-by: Claude Opus 5 <[email protected]> Co-authored-by: Tom Pike <[email protected]>
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.
When the last agent of a given type is removed, its per-type AgentSet is kept
in agents_by_type as an empty set, and the type stays listed in agent_types.
This behavior was not covered by any test: test_agent_remove only checks that
model.agents is empty after remove_all_agents, and nothing asserted what happens
to the per-type buckets once a type goes extinct.
The behavior is relied on in practice. WolfSheep, for example, indexes
agents_by_type[Wolf] and agents_by_type[Sheep] every step and expects an empty
set rather than a KeyError once one of those populations dies out. Locking it
down with a test prevents a well-meaning change (for instance, deleting the
bucket to "clean up") from silently breaking those models.
The new test creates two agent types, removes all agents of one type, and
asserts the type is still in agent_types, that agents_by_type returns an empty
set for it, and that the other type's set is untouched. No production code is
changed.
Verified locally: full test suite passes (712 tests, examples and visualization
components included), ruff check and ruff format both clean.