Skip to content

Missing Agent type hints #2716

@EwoutH

Description

@EwoutH

This issue is split of from feedback on the Mesa 3 JOSS paper.

Summary

Mesa uses type hints in its codebase, which is excellent for documentation and static type checking. However, there are gaps in how agent types are handled, which cause typing errors when running code in a strongly typed environment.

Problems:

  1. model.agents returns an AgentSet[Agent] but doesn't maintain the specific agent subclass type information
  2. When accessing self.model within an agent, the agent doesn't know about the model's specific type
  3. Type checkers can't properly validate attribute access on agents retrieved from collections like model.agents

Reproducible examples:

Example 1: Type error when accessing agent attributes through model.agents

From the introductory tutorial in "Agents Exchange"/"Running your first model" section:

def step(self):
    if self.wealth > 0:
        other_agent = self.random.choice(self.model.agents)
        other_agent.wealth += 1  # Type error: 'Agent' has no attribute 'wealth'
        self.wealth -= 1

Example 2: Type error when accessing model attributes from agents

class MyAgent(Agent):
    def __init__(self, unique_id, model):
        super().__init__(model)
        
    def move(self):
        self.model.grid.move_agent(self, new_pos)  # Type error: 'Model' has no attribute 'grid'

Additional context

This would improve the experience for users working in typed environments or using IDEs with type checking capabilities like PyCharm, VSCode with Pylance, or running mypy on their code.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions