diff --git a/examples/sugarscape_ig/ss_polars/agents.py b/examples/sugarscape_ig/ss_polars/agents.py index a61e7343..a5e1a8b4 100644 --- a/examples/sugarscape_ig/ss_polars/agents.py +++ b/examples/sugarscape_ig/ss_polars/agents.py @@ -167,7 +167,8 @@ def get_best_moves(self, neighborhood: pl.DataFrame) -> pl.DataFrame: pl.DataFrame DataFrame with the best moves for each agent """ - raise NotImplementedError("Subclasses must implement this method") + if __debug__: # Only execute in non-optimized mode + raise NotImplementedError("Subclasses must implement this method") class AntPolarsLoopDF(AntPolarsBase): @@ -309,7 +310,8 @@ def _prepare_cells( return occupied_cells, free_cells, target_cells def _get_best_moves(self): - raise NotImplementedError("Subclasses must implement this method") + if __debug__: # Only execute in non-optimized mode + raise NotImplementedError("Subclasses must implement this method") class AntPolarsLoopNoVec(AntPolarsLoop): diff --git a/mesa_frames/abstract/agents.py b/mesa_frames/abstract/agents.py index e341c48c..806d4384 100644 --- a/mesa_frames/abstract/agents.py +++ b/mesa_frames/abstract/agents.py @@ -998,10 +998,11 @@ def __iadd__(self, other: DataFrame | Sequence[Any] | dict[str, Any]) -> Self: @abstractmethod def __getattr__(self, name: str) -> Any: - if name == "_agents": - raise RuntimeError( - "The _agents attribute is not set. You probably forgot to call super().__init__ in the __init__ method." - ) + if __debug__: # Only execute in non-optimized mode + if name == "_agents": + raise AttributeError( + "The _agents attribute is not set. You probably forgot to call super().__init__ in the __init__ method." + ) @overload def __getitem__(self, key: str | tuple[AgentMask, str]) -> Series | DataFrame: ... diff --git a/mesa_frames/concrete/agents.py b/mesa_frames/concrete/agents.py index a32c51cd..f68dc38c 100644 --- a/mesa_frames/concrete/agents.py +++ b/mesa_frames/concrete/agents.py @@ -438,10 +438,12 @@ def __add__(self, other: AgentSetDF | Iterable[AgentSetDF]) -> Self: return super().__add__(other) def __getattr__(self, name: str) -> dict[AgentSetDF, Any]: - if name.startswith("_"): # Avoids infinite recursion of private attributes - raise AttributeError( - f"'{self.__class__.__name__}' object has no attribute '{name}'" - ) + # Avoids infinite recursion of private attributes + if __debug__: # Only execute in non-optimized mode + if name.startswith("_"): + raise AttributeError( + f"'{self.__class__.__name__}' object has no attribute '{name}'" + ) return {agentset: getattr(agentset, name) for agentset in self._agentsets} @overload diff --git a/mesa_frames/concrete/model.py b/mesa_frames/concrete/model.py index bfec0157..5efcbcff 100644 --- a/mesa_frames/concrete/model.py +++ b/mesa_frames/concrete/model.py @@ -175,14 +175,17 @@ def agents(self) -> AgentsDF: try: return self._agents except AttributeError: - raise ValueError( - "You haven't called super().__init__() in your model. Make sure to call it in your __init__ method." - ) + if __debug__: # Only execute in non-optimized mode + raise RuntimeError( + "You haven't called super().__init__() in your model. Make sure to call it in your __init__ method." + ) @agents.setter def agents(self, agents: AgentsDF) -> None: - if not isinstance(agents, AgentsDF): - raise TypeError("agents must be an instance of AgentsDF") + if __debug__: # Only execute in non-optimized mode + if not isinstance(agents, AgentsDF): + raise TypeError("agents must be an instance of AgentsDF") + self._agents = agents @property