replace AttributeDict with a dataclass#3138
Conversation
|
Performance benchmarks:
|
|
Quick API check: Are any of these used by downstream users or part of the public API?
If any are user-facing, I think we need to document that properly. |
Its in experimental and we don''t even use it in our own examples at the moment. So I would not worry about it too much.
Not sure what you mean, can you elaborate?
Yes, I merged that PR after opening this one, so I'll fix that |
EwoutH
left a comment
There was a problem hiding this comment.
Thanks for working on this.
I didn't review it in-depth, but overal looks good, and it's the experimental space, which is meant to move fast.
Summary
Replace the generic
AttributeDicthelper class with a dedicated frozen dataclassMessagefor improved type safety and developer experience when working with Mesa signals.Bug / Issue
The current signal implementation uses
AttributeDict, a generic dictionary with attribute-like access. This has several limitations:Part of #3131 (addresses item 1)
Implementation
Replaced
AttributeDictwith a frozen dataclassMessagein the mesa_signals module:Messagedataclass insignals_util.pywith explicit fields:name,old,new,owner,signal_type,additional_kwargsmesa_signal.pyto acceptstr | SignalType | Allwhere appropriatesignal.type→signal.signal_type(more explicit, avoids Python keyword)additional_kwargsdict rather than spread into the signal objectLocal benchmarking shows no measurable performance difference.
Testing
All existing tests pass with updated field names. Type checking and IDE autocomplete now work correctly for signal handlers:
Additional Notes
This is a breaking change for experimental code:
signal.type→signal.signal_typesignal.additional_kwargsdictSince
mesa_signalsis experimental, breaking changes are allowed per Mesa's policy.