Skip to content

Add static dependency injection to @computed_property for @emit support#3462

Merged
quaquel merged 7 commits intomesa:mainfrom
codebreaker32:static_deps
Mar 11, 2026
Merged

Add static dependency injection to @computed_property for @emit support#3462
quaquel merged 7 commits intomesa:mainfrom
codebreaker32:static_deps

Conversation

@codebreaker32
Copy link
Copy Markdown
Collaborator

@codebreaker32 codebreaker32 commented Mar 6, 2026

Summary

This PR adds static dependency injection to the @computed_property decorator via a new dependencies keyword argument. This allows derived states to manually subscribe to specific signals, explicitly supporting events generated by the @emit decorator which are otherwise invisible to the reactive system.

Motive

See #3227

Implementation

  • Decorator Expansion: Updated @computed_property to accept a dependencies kwarg, which takes a list of tuples containing the target dependencies name and the specific signal types (or pass nothing for ALL sentinel).
  • State Initialization: Modified ComputedState.__init__ to parse the dependencies list upon creation.
  • Lifecycle Wiring: For each dependency provided, the ComputedState actively registers itself with the target using owner.observe(obs_name, signal_type, self._set_dirty). This bypasses the need for __get__ auto-discovery and directly injects the cache invalidation trigger into the emitter's internal subscribers dictionary.

Usage Examples

from mesa.experimental.mesa_signals import HasEmitters, computed_property, emit, ModelSignals

class MyModel(mesa.Model):
    def __init__(self):
        super().__init__()
        self.agent_count = 0

    @emit("model", ModelSignals.AGENT_ADDED)
    def add_agent(self):
        self.agent_count += 1

    # Statically bridging the built-in signal to the computed property
    @computed_property(dependencies=[("model", ModelSignals.AGENT_ADDED)])
    def population_status(self):
        return "Populated" if self.agent_count > 0 else "Empty"

@codebreaker32 codebreaker32 changed the title Add static dependency injection to @computed_property for @emit support Add static dependency injection to @computed_property for @emit support Mar 6, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 6, 2026

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.2% [-0.5%, +0.0%] 🔵 +0.4% [+0.3%, +0.5%]
BoltzmannWealth large 🔵 -0.0% [-0.3%, +0.3%] 🔵 +1.8% [+1.1%, +2.9%]
Schelling small 🔵 +1.5% [+1.3%, +1.8%] 🔵 +0.4% [+0.2%, +0.5%]
Schelling large 🔵 +1.9% [+1.4%, +2.3%] 🔵 +0.6% [-0.3%, +1.4%]
WolfSheep small 🔵 -1.5% [-1.9%, -1.1%] 🔵 +0.2% [+0.1%, +0.4%]
WolfSheep large 🔵 -0.6% [-1.6%, +0.3%] 🔵 +0.0% [-0.8%, +1.0%]
SugarscapeG1mt small 🔵 -0.4% [-0.9%, +0.2%] 🔵 +1.0% [+0.8%, +1.2%]
SugarscapeG1mt large 🔵 -0.3% [-0.9%, +0.3%] 🔵 -0.2% [-0.6%, +0.2%]
BoidFlockers small 🔵 -2.0% [-2.2%, -1.8%] 🔵 +0.5% [+0.3%, +0.6%]
BoidFlockers large 🔵 -0.8% [-1.1%, -0.4%] 🔵 +0.2% [-0.0%, +0.5%]

@quaquel quaquel merged commit 46bd7e7 into mesa:main Mar 11, 2026
13 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants