From cc9bb12ed239e619329db26f6f4266ad9b185a93 Mon Sep 17 00:00:00 2001 From: rht Date: Mon, 7 Aug 2023 07:31:46 -0400 Subject: [PATCH] Apply pyupgrade --py39-plus --- mesa/batchrunner.py | 19 +++++++++---------- mesa/space.py | 10 ++++------ 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/mesa/batchrunner.py b/mesa/batchrunner.py index c9c528470a3..6a6679c11a1 100644 --- a/mesa/batchrunner.py +++ b/mesa/batchrunner.py @@ -4,14 +4,13 @@ from typing import ( Any, Dict, - Iterable, List, - Mapping, Optional, Tuple, Type, Union, ) +from collections.abc import Iterable, Mapping from tqdm.auto import tqdm @@ -19,7 +18,7 @@ def batch_run( - model_cls: Type[Model], + model_cls: type[Model], parameters: Mapping[str, Union[Any, Iterable[Any]]], # We still retain the Optional[int] because users may set it to None (i.e. use all CPUs) number_processes: Optional[int] = 1, @@ -27,7 +26,7 @@ def batch_run( data_collection_period: int = -1, max_steps: int = 1000, display_progress: bool = True, -) -> List[Dict[str, Any]]: +) -> list[dict[str, Any]]: """Batch run a mesa model with a set of parameter values. Parameters @@ -67,7 +66,7 @@ def batch_run( data_collection_period=data_collection_period, ) - results: List[Dict[str, Any]] = [] + results: list[dict[str, Any]] = [] with tqdm(total=len(runs_list), disable=not display_progress) as pbar: if number_processes == 1: @@ -86,7 +85,7 @@ def batch_run( def _make_model_kwargs( parameters: Mapping[str, Union[Any, Iterable[Any]]] -) -> List[Dict[str, Any]]: +) -> list[dict[str, Any]]: """Create model kwargs from parameters dictionary. Parameters @@ -116,11 +115,11 @@ def _make_model_kwargs( def _model_run_func( - model_cls: Type[Model], - run: Tuple[int, int, Dict[str, Any]], + model_cls: type[Model], + run: tuple[int, int, dict[str, Any]], max_steps: int, data_collection_period: int, -) -> List[Dict[str, Any]]: +) -> list[dict[str, Any]]: """Run a single model run and collect model and agent data. Parameters @@ -185,7 +184,7 @@ def _model_run_func( def _collect_data( model: Model, step: int, -) -> Tuple[Dict[str, Any], List[Dict[str, Any]]]: +) -> tuple[dict[str, Any], list[dict[str, Any]]]: """Collect model and agent data from a model using mesas datacollector.""" dc = model.datacollector diff --git a/mesa/space.py b/mesa/space.py index eebcb3c1758..46a4ddb6f92 100644 --- a/mesa/space.py +++ b/mesa/space.py @@ -27,16 +27,14 @@ from typing import ( Any, Callable, - Iterable, - Iterator, List, - Sequence, Tuple, TypeVar, Union, cast, overload, ) +from collections.abc import Iterable, Iterator, Sequence from warnings import warn import networkx as nx @@ -49,15 +47,15 @@ # for better performance, we calculate the tuple to use in the is_integer function _types_integer = (int, np.integer) -Coordinate = Tuple[int, int] +Coordinate = tuple[int, int] # used in ContinuousSpace -FloatCoordinate = Union[Tuple[float, float], npt.NDArray[float]] +FloatCoordinate = Union[tuple[float, float], npt.NDArray[float]] NetworkCoordinate = int Position = Union[Coordinate, FloatCoordinate, NetworkCoordinate] GridContent = Union[Agent, None] -MultiGridContent = List[Agent] +MultiGridContent = list[Agent] F = TypeVar("F", bound=Callable[..., Any])