-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix #2716: missing agent type hints #2885
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
22303a0
b4ed50a
61f7e16
129f3c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -24,13 +24,11 @@ | |||||
| import numpy as np | ||||||
|
|
||||||
| if TYPE_CHECKING: | ||||||
| # We ensure that these are not imported during runtime to prevent cyclic | ||||||
| # dependency. | ||||||
| from mesa.model import Model | ||||||
| from mesa.space import Position | ||||||
|
|
||||||
|
|
||||||
| class Agent: | ||||||
| class Agent[M: Model]: | ||||||
| """Base class for a model agent in Mesa. | ||||||
|
|
||||||
| Attributes: | ||||||
|
|
@@ -48,7 +46,7 @@ class Agent: | |||||
| # so, unique_id is unique relative to a model, and counting starts from 1 | ||||||
| _ids = defaultdict(functools.partial(itertools.count, 1)) | ||||||
|
|
||||||
| def __init__(self, model: Model, *args, **kwargs) -> None: | ||||||
| def __init__(self, model: M, *args, **kwargs) -> None: | ||||||
| """Create a new agent. | ||||||
|
|
||||||
| Args: | ||||||
|
|
@@ -62,7 +60,9 @@ def __init__(self, model: Model, *args, **kwargs) -> None: | |||||
| """ | ||||||
| super().__init__(*args, **kwargs) | ||||||
|
|
||||||
| self.model: Model = model | ||||||
| # Preserve the more specific model type for static type checkers. | ||||||
| # At runtime this remains the Model instance passed in. | ||||||
|
||||||
| # Preserve the more specific model type for static type checkers. | |
| # At runtime this remains the Model instance passed in. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, I will make the neccessary changes
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,21 +12,24 @@ | |
| from collections.abc import Sequence | ||
|
|
||
| # mypy | ||
| from typing import Any | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| import numpy as np | ||
|
|
||
| from mesa.agent import Agent, AgentSet | ||
| from mesa.mesa_logging import create_module_logger, method_logger | ||
|
|
||
| if TYPE_CHECKING: | ||
| pass | ||
|
|
||
| SeedLike = int | np.integer | Sequence[int] | np.random.SeedSequence | ||
| RNGLike = np.random.Generator | np.random.BitGenerator | ||
|
|
||
|
|
||
| _mesa_logger = create_module_logger() | ||
|
|
||
|
|
||
| class Model: | ||
| class Model[A: Agent]: | ||
| """Base class for models in the Mesa ABM library. | ||
|
|
||
| This class serves as a foundational structure for creating agent-based models. | ||
|
|
@@ -107,11 +110,9 @@ def __init__( | |
| # setup agent registration data structures | ||
| self._agents = {} # the hard references to all agents in the model | ||
| self._agents_by_type: dict[ | ||
| type[Agent], AgentSet | ||
| ] = {} # a dict with an agentset for each class of agents | ||
| self._all_agents = AgentSet( | ||
| [], random=self.random | ||
| ) # an agenset with all agents | ||
|
Comment on lines
110
to
114
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please restore the original comments
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made the changes, please review the new push |
||
| type[A], AgentSet[A] | ||
| ] = {} # agentset per agent class | ||
| self._all_agents: AgentSet[A] = AgentSet([], random=self.random) | ||
|
|
||
| def _wrapped_step(self, *args: Any, **kwargs: Any) -> None: | ||
| """Automatically increments time and steps after calling the user's step method.""" | ||
|
|
@@ -122,7 +123,7 @@ def _wrapped_step(self, *args: Any, **kwargs: Any) -> None: | |
| self._user_step(*args, **kwargs) | ||
|
|
||
| @property | ||
| def agents(self) -> AgentSet: | ||
| def agents(self) -> AgentSet[A]: | ||
| """Provides an AgentSet of all agents in the model, combining agents from all types.""" | ||
| return self._all_agents | ||
|
|
||
|
|
@@ -140,11 +141,11 @@ def agent_types(self) -> list[type]: | |
| return list(self._agents_by_type.keys()) | ||
|
|
||
| @property | ||
| def agents_by_type(self) -> dict[type[Agent], AgentSet]: | ||
| def agents_by_type(self) -> dict[type[A], AgentSet[A]]: | ||
| """A dictionary where the keys are agent types and the values are the corresponding AgentSets.""" | ||
| return self._agents_by_type | ||
|
|
||
| def register_agent(self, agent): | ||
| def register_agent(self, agent: A): | ||
| """Register the agent with the model. | ||
|
|
||
| Args: | ||
|
|
@@ -174,7 +175,7 @@ def register_agent(self, agent): | |
| f"registered {agent.__class__.__name__} with agent_id {agent.unique_id}" | ||
| ) | ||
|
|
||
| def deregister_agent(self, agent): | ||
| def deregister_agent(self, agent: A): | ||
| """Deregister the agent with the model. | ||
|
|
||
| Args: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you check if we still need this? (honestly I don't know)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I went through the codebase, I think numpy is needed
Example usecase (Line 146)