POC: Exception Hierarchy for Mesa 4.0#3197
Conversation
|
Performance benchmarks:
|
|
Thanks for this PR. I think its still a bit premature. I don't agree with Also |
|
Thanks for the update. I think this is moving in a good direction. What other generic exceptions should be replaced in mesa.discrete_space. |
|
Thanks. I have posted the detailed breakdown and rationale for these additions in the main Discussion thread. |
Sounds good to me. Where possible, consider using the |
Done |
|
@EwoutH, |
|
Just a kind request: please refrain from pinging maintainers. We get the updates and try to respond as soon as we can. Sometimes, we as maintainers might ping each other because we are aware of our respective expertise across different parts of the codebase. |
|
As discussed in the thread, |
|
I like the direction this is moving in. Can you next take a look at the experimental continouous space and see what custom exception needs there might be there and how those might be integrated cleanly into this hierarchy? |
|
If we establish Proposed HierarchyRationale for |
|
Do we need the distinction between ContinuousSpaceException and DiscreteSpaceException, or can we simplify this to just a generic SpaceException? I'll get back to you on the |
How about, as of now we implement all the exceptions we want under
Sure. |
|
Do we need a separate |
…nd DiscreteSpaceException
I don't think we will need separate exceptions. I have implemented a unified |
mesa/exceptions.py
Outdated
| super().__init__(f"Cell at coordinate {coordinate} is full.") | ||
|
|
||
|
|
||
| class AgentMissingException(SpaceException): |
There was a problem hiding this comment.
there are other places that this might be used (e.g., model.deregister_agent), so this might subclass mesaexception, rather than space exception.
|
@EwoutH, I am happy with how this is turning out and would be in favor of merging this as part of 4.0 while expanding the use of custom exceptions where appropriate. Would like to hear your thoughts as well before merging this. |
|
Sorry, missed it last week. But it looks good and clean, thanks both! |
Summary
This PR introduces a standardized exception hierarchy for Mesa 4.0, acting as a Proof of Concept (POC) for the architectural proposal discussed recently. It adds a new
mesa.errorsmodule containing a baseMesaErrorclass and refactorsSingleGridto use a specificCellNotEmptyErrorinstead of a genericException.Motive
Currently, Mesa relies heavily on generic Python exceptions (like
ExceptionorValueError) for simulation logic failures. This makes it difficult for users to programmatically handle simulation errors without risking the suppression of unrelated system errors (likeKeyboardInterruptorMemoryError).By introducing a dedicated hierarchy rooted in
MesaError, we allow users to write cleaner, safer error-handling code. This PR demonstrates this pattern by fixing the "Cell not empty" error inSingleGrid, which was previously raising a rawException.Implementation
mesa/exceptions.py.MesaException(Exception)as the root for all Mesa-specific exceptions.Additional Notes