mesa icon indicating copy to clipboard operation
mesa copied to clipboard

Make `ContinuousSpace` generic over its agent type

Open bronsonhill opened this issue 2 months ago • 1 comments

What's the problem this feature will solve? #2885 made Agent, AgentSet and Model generic, so Model[MyAgent].agents is typed as AgentSet[MyAgent]. experimental.continuous_space.ContinuousSpace has no type parameter, so space.agents returns a bare AgentSet and the element type is lost. The same happens with get_agents_in_radius and get_k_nearest_agents. Users have to annotate or cast at every call site, or subclass the space and re-annotate each method.

Describe the solution you'd like Add a PEP 695 type parameter, as #2885 did:

class ContinuousSpace[A: ContinuousSpaceAgent]:
    @property
    def agents(self) -> AgentSet[A]: ...

and thread A through the methods that return agents. Nothing changes at runtime, and an unparameterized ContinuousSpace still resolves to Any, so existing code keeps type-checking.

Are you planning to open a PR for this? If the issue is approved, I'd like to.

Additional context Mesa 3.5.1. The discrete cell spaces could get the same treatment, but I haven't checked how they're typed.

bronsonhill avatar Sep 25 '26 04:09 bronsonhill

Fixes #3879 Opened PR #3900 to implement PEP 695 generic typing (ContinuousSpace[A]) for ContinuousSpace, resolving generic agent type preservation across spatial lookup methods.

suhailmalik-tech avatar Sep 28 '26 15:09 suhailmalik-tech