You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After merging PR #143, we will have Beartype on every method, but nothing automatically verifies that each concrete class still honors the abstract class’s type hints.
A small drift—e.g. changing an add(...) signature—can silently break downstream code or introduce subtle variance bugs that only fail at runtime.
💡 Proposed Solution
Add a new metaclass (e.g. InterfaceMeta) in a util module (say mesa_frames.utils.interface_meta) that, in its __init__, walks every ABC base and uses Beartype’s is_subhint to compare each abstract method’s type hints against the override in the subclass—raising a TypeError at class‐definition time on mismatch.
Switch our ABCs (like AgentContainer) to use metaclass=InterfaceMeta instead of plain ABCMeta. Any drop-in mismatch in AgentSetPolars (or any other subclass) will now fail immediately when Python imports the module, giving a clear error message.
Example in mesa-frames
# mesa_frames/abstract/agents.pyfromabcimportabstractmethodfromtypingimportget_type_hintsfromtyping_extensionsimportSelffrommesa_frames.types_importDataFrameInputfromcollections.abcimportCollection
…
classAgentSetDF(AgentContainer, DataFrameMixin):
@abstractmethoddefadd(
self,
agents: DataFrameInput,
inplace: bool=True,
) ->Self:
"""Add agents to the container."""
…
Here, the metaclass check would ensure that the concrete add’s agents: hint (pl.DataFrame | Sequence[Any] | dict[str,Any]) is a valid superhint of the abstract DataFrameInput alias (dict[str, Any] | Sequence[Sequence] | pl.DataFrame), and would error out at class‐definition time if someone ever drifted one side out of alignment.
🔄 Alternatives Considered
Decorator (opt-in per class), but easy to forget.
CI test to compare signatures, but that only catches errors later in testing.
🤔 Problem Description
After merging PR #143, we will have Beartype on every method, but nothing automatically verifies that each concrete class still honors the abstract class’s type hints.
A small drift—e.g. changing an
add(...)
signature—can silently break downstream code or introduce subtle variance bugs that only fail at runtime.💡 Proposed Solution
InterfaceMeta
) in a util module (saymesa_frames.utils.interface_meta
) that, in its__init__
, walks everyABC
base and uses Beartype’sis_subhint
to compare each abstract method’s type hints against the override in the subclass—raising aTypeError
at class‐definition time on mismatch.AgentContainer
) to usemetaclass=InterfaceMeta
instead of plainABCMeta
. Any drop-in mismatch inAgentSetPolars
(or any other subclass) will now fail immediately when Python imports the module, giving a clear error message.Example in mesa-frames
Here, the metaclass check would ensure that the concrete
add
’sagents:
hint (pl.DataFrame | Sequence[Any] | dict[str,Any]
) is a valid superhint of the abstractDataFrameInput
alias (dict[str, Any] | Sequence[Sequence] | pl.DataFrame
), and would error out at class‐definition time if someone ever drifted one side out of alignment.🔄 Alternatives Considered
➕ Additional Context
mesa_frames/abstract/agents.py
mesa_frames/concrete/agentset.py
The text was updated successfully, but these errors were encountered: