Skip to content
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

make typing behavior of AgentSet.get explicit #2293

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions mesa/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from random import Random

# mypy
from typing import TYPE_CHECKING, Any, Literal
from typing import TYPE_CHECKING, Any, Literal, overload

if TYPE_CHECKING:
# We ensure that these are not imported during runtime to prevent cyclic
Expand Down Expand Up @@ -348,12 +348,28 @@ def agg(self, attribute: str, func: Callable) -> Any:
values = self.get(attribute)
return func(values)

@overload
def get(
self,
attr_names: str | list[str],
attr_names: str,
handle_missing: Literal["error", "default"] = "error",
default_value: Any = None,
) -> list[Any] | list[list[Any]]:
) -> list[Any]: ...

@overload
def get(
self,
attr_names: list[str],
handle_missing: Literal["error", "default"] = "error",
default_value: Any = None,
) -> list[list[Any]]: ...

def get(
self,
attr_names,
handle_missing="error",
default_value=None,
):
"""
Retrieve the specified attribute(s) from each agent in the AgentSet.

Expand Down
Loading