Skip to content

check whether model reporter is a partial function#3220

Merged
quaquel merged 1 commit intomesa:mainfrom
wang-boyu:fix/model-reporter-partial-function
Jan 29, 2026
Merged

check whether model reporter is a partial function#3220
quaquel merged 1 commit intomesa:mainfrom
wang-boyu:fix/model-reporter-partial-function

Conversation

@wang-boyu
Copy link
Copy Markdown
Member

@wang-boyu wang-boyu commented Jan 29, 2026

fixes #1871

If we use a partial function as a data collector, for example

from functools import partial


def count_agents(model, multiplier):
    return len(model.agents) * multiplier

dc = DataCollector(
    model_reporters={"AgentsTimesTwo": partial(count_agents, multiplier=2)}
)
dc.collect(model)

then we will get a RuntimeError:

# Type 2: Method of class/instance (bound methods are callable)
if callable(reporter) and not isinstance(reporter, types.LambdaType):
    try:
>        reporter()  # Call without args for bound methods
          ^^^^^^^^^^
E               TypeError: TestPartialReporterValidation.test_partial_model_reporter.<locals>.count_agents() missing 1 required positional argument: 'model'

mesa/datacollection.py:172: TypeError

A similar problem was previously fixed in #1872 by checking whether the reporter is a partial function:

# Check if lambda or partial function
if isinstance(reporter, (types.LambdaType, partial)):
    self.model_vars[var].append(reporter(model))

However in #2605 where we extracted a separate function to validate reporters, this check was not performed:

# Type 1: Lambda function
if isinstance(reporter, types.LambdaType):
    ...

This PR essentially added this check into the model reporter validation function:

# Type 1: Lambda function or partial
if isinstance(reporter, (types.LambdaType, partial)):
    ...

and added a test case for when a model reporter is a partial function.

@wang-boyu wang-boyu added the bug Release notes label label Jan 29, 2026
@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🟢 -5.8% [-6.1%, -5.6%] 🔵 -1.3% [-1.5%, -1.0%]
BoltzmannWealth large 🔵 -1.9% [-2.5%, -1.4%] 🔵 -2.3% [-2.7%, -1.8%]
Schelling small 🔵 -1.3% [-1.5%, -1.0%] 🔵 +0.0% [-0.1%, +0.2%]
Schelling large 🔵 -0.6% [-1.2%, +0.0%] 🔵 +2.4% [+1.4%, +3.2%]
WolfSheep small 🔵 -1.8% [-2.1%, -1.5%] 🔵 +0.6% [+0.4%, +0.7%]
WolfSheep large 🔵 -0.6% [-0.8%, -0.2%] 🔵 +0.4% [+0.0%, +0.7%]
BoidFlockers small 🔵 +1.0% [+0.8%, +1.3%] 🔵 +0.1% [-0.0%, +0.3%]
BoidFlockers large 🔵 +1.5% [+1.1%, +1.9%] 🔵 -0.2% [-0.3%, -0.1%]

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Jan 29, 2026

Can you ensure the starting post is independently readable instead of only a link? Otherwise, this looks fine to me.

@wang-boyu
Copy link
Copy Markdown
Member Author

Sorry about the trouble. I've edited the description.

@quaquel quaquel merged commit 0944797 into mesa:main Jan 29, 2026
16 checks passed
@wang-boyu wang-boyu deleted the fix/model-reporter-partial-function branch January 29, 2026 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TyperError when model reporter is a partial function

2 participants