Feat: Introduce dedicated exception hierarchy in mesa/errors.py #2992
Feat: Introduce dedicated exception hierarchy in mesa/errors.py #2992codebreaker32 wants to merge 7 commits intomesa:mainfrom
Conversation
|
Performance benchmarks:
|
|
Thanks for this. I still need to decide how to proceed with exceptions in MESA. I believe this is a topic that warrants further discussion. For example, there are a bunch of errors in MESA that might also justifiably be subclassed from default Python errors. I don't know what the recommended practice is in Python and would like to read upcommit on this before ting to any specific approach. |
|
I have opened a discussion on mesa exceptions in the mesa 4.0 discussion. I am closing this PR, but included a link to it there. Depending on how we decide to move forward, we might reopen this or least draw heavily from it. |
|
@codebreaker32, Hello. |
|
Good to know this, @Nithurshen. You’re welcome to take a stab at it, but I’d suggest first dropping your ideas on #2972 (comment) |
It's quite funny. But thank you for your support. |
|
I think it’s good to open a separate discussion thread for error types in Mesa. Then we can follow our development process https://github.com/mesa/mesa/blob/main/CONTRIBUTING.md#mesa-development-process |
|
My apologies if that came across wrong, @Nithurshen . I wasn't overlooking your previous work on this, I was referring specifically to @quaquel recent note in #3146 about pausing new PRs until we have a consensus. Since @EwoutH has suggested opening a separate discussion thread, that seems like the best place to finalize the direction. Would be great to see your proposal there :) |
|
I'd say there is consensus on the basic idea. Now it's more a matter of fleshing out the details and agreeing on a process. One option is to fully assess the current code and identify all places where a special mesa exception might be useful. The other option is to take a more piecemeal approach. I don't know yet which is better. Also, we might want to establish some basic guidelines for deciding which exception to use. |
Is it fine if we open a separate discussion thread like @EwoutH suggested? |
Of course |
This PR introduces a new module, mesa/errors.py, which defines a dedicated and structured exception hierarchy for the Mesa framework. It establishes a base MesaError class and categorizes specific exceptions for Models, Spaces, Scheduling, and Visualization.
Currently, the Mesa codebase relies heavily on generic Python exceptions (such as ValueError, AttributeError, and RuntimeError) to handle failure states.
This creates several issues:
Implementation
Created mesa/errors.py.
Defined a base class MesaError which automatically prepends the installed Mesa version to all error messages to assist with issue reporting.
Implemented semantic grouping for exceptions:
Usage Examples
Note: This PR only adds the definitions(to keep it simple for review). In upcoming PRs, I will be updating the codebase will be updated to usage patterns like this:
Future Work Plan: I will be opening multiple follow-up PRs to incrementally refactor the codebase to use these new errors.