Skip to content

Commit

Permalink
make typing behavior of AgentSet.get explicit (projectmesa#2293)
Browse files Browse the repository at this point in the history
* make typing behavior of AgentSet.get explicit

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
quaquel and pre-commit-ci[bot] authored Sep 13, 2024
1 parent eaf7457 commit ead9cd1
Showing 1 changed file with 19 additions and 3 deletions.
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

0 comments on commit ead9cd1

Please sign in to comment.