Skip to content

Feat: Introduce minimal Mesa-specific exception hierarchy in mesa/errors.py#3146

Closed
navneetkumaryadav207001 wants to merge 5 commits intomesa:mainfrom
navneetkumaryadav207001:main
Closed

Feat: Introduce minimal Mesa-specific exception hierarchy in mesa/errors.py#3146
navneetkumaryadav207001 wants to merge 5 commits intomesa:mainfrom
navneetkumaryadav207001:main

Conversation

@navneetkumaryadav207001
Copy link
Copy Markdown

Summary

This PR introduces a minimal, safe, and non-breaking Mesa-specific exception hierarchy.
It defines a base exception (MesaError) and three domain-specific subclasses:

  • ModelError – for errors related to model configuration, initialization, or execution
  • AgentError – for errors related to agent lifecycle or behavior
  • SpaceError – for errors related to spaces, grids, movement, or spatial constraints

This provides a foundation for future exception standardization and migration of existing generic exceptions.


Motivation


Key Features

  • Uses from __future__ import annotations to avoid circular import issues
  • Defines __all__ for a clean, explicit public API
  • Safely stores Mesa version using importlib.metadata without importing mesa
  • Empty domain-specific subclasses include pass for linter compliance
  • Non-breaking: does not change any existing behavior or messages
  • Ready for future migration of existing exceptions in Space, Model, or Agent modules

Example Usage

from mesa.errors import MesaError, SpaceError

# Raising a domain-specific exception
if not self.is_cell_empty(pos):
    raise SpaceError(f"Cell {pos} is already occupied")

# Catching all Mesa exceptions safely
try:
    model.step()
except MesaError as e:
    print(f"Mesa error occurred (v{e.mesa_version}): {e}")

@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +2.6% [+1.9%, +3.4%] 🔵 +0.7% [+0.5%, +0.8%]
BoltzmannWealth large 🔵 -0.1% [-0.5%, +0.3%] 🔵 -1.8% [-2.9%, -0.6%]
Schelling small 🔵 +0.9% [+0.7%, +1.1%] 🔵 +0.4% [+0.3%, +0.5%]
Schelling large 🔵 +1.0% [+0.6%, +1.3%] 🔵 +0.6% [-0.6%, +1.8%]
WolfSheep small 🔵 +0.5% [+0.3%, +0.8%] 🔵 +0.2% [+0.1%, +0.4%]
WolfSheep large 🔵 -0.7% [-1.7%, +0.1%] 🔵 -1.2% [-2.8%, +0.5%]
BoidFlockers small 🔵 -0.2% [-0.8%, +0.4%] 🔵 +1.4% [+1.2%, +1.6%]
BoidFlockers large 🔵 -0.7% [-1.2%, -0.2%] 🔵 +1.0% [+0.9%, +1.2%]

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Jan 16, 2026

As you can see, there is an ongoing discussion on this. We first need to agree on what exceptions we need inside MESA before opening any PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants