DataCollector does not handle callable class instances as model_reporters
Describe the bug
When a callable class instance is passed as a model_reporter to DataCollector, calling collect(model) raises a RuntimeError.
The reporter is expected to receive the model as an argument, but the current implementation invokes callable class instances without arguments during validation.
This appears inconsistent with the handling of other model-aware reporters, such as functions and functools.partial objects.
Expected behavior
Callable class instances implementing __call__(self, model) should be invoked with the model argument, just like other model-aware reporters.
The collected value should be recorded successfully without raising an exception.
To Reproduce
from mesa import Agent, Model
from mesa.datacollection import DataCollector
class AgentCountReporter:
def __call__(self, model):
return len(model.agents)
class SimpleModel(Model):
def __init__(self):
super().__init__()
Agent(self)
self.datacollector = DataCollector(
model_reporters={
"agent_count": AgentCountReporter()
}
)
model = SimpleModel()
model.datacollector.collect(model)
assert model.datacollector.model_vars["agent_count"] == [1]
Actual result:
Calling model.datacollector.collect(model) raises:
RuntimeError: Method reporter 'agent_count' failed validation:
AgentCountReporter.__call__() missing 1 required positional argument: 'model'
The reporter works when invoked directly:
AgentCountReporter()(model) # Returns 1
Additional context
The relevant implementation is in mesa/datacollection.py, specifically _validate_model_reporter() and the model reporter dispatch logic in collect().
The current implementation handles types.LambdaType and functools.partial by passing the model argument. Other callable objects fall through to a branch that invokes them without arguments, apparently assuming they are zero-argument bound methods.
I also noticed that agent reporters are invoked with the agent argument, so callable objects work in that context.
Callable class instances could be useful for encapsulating reusable or stateful model-level metric calculations.
I haven't established whether their exclusion from model_reporters is intentional. I'd appreciate clarification on whether this behaviour is within the intended API scope.
I'd be interested in contributing a fix and regression test if this is considered a bug.