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

type annotations: Replace Union[a, b] with a | b, Optional[a] with a | None #1341 #2003

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions benchmarks/global_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import time
import timeit

from configurations import configurations

# making sure we use this version of mesa and not one
# also installed in site_packages or so.
sys.path.insert(0, os.path.abspath(".."))

from configurations import configurations # noqa: E402


# Generic function to initialize and run a model
def run_model(model_class, seed, parameters):
Expand Down
10 changes: 5 additions & 5 deletions mesa/batchrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from collections.abc import Iterable, Mapping
from functools import partial
from multiprocessing import Pool
from typing import Any, Optional, Union
from typing import Any, Optional

from tqdm.auto import tqdm

Expand All @@ -11,7 +11,7 @@

def batch_run(
model_cls: type[Model],
parameters: Mapping[str, Union[Any, Iterable[Any]]],
parameters: Mapping[str, 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,
iterations: int = 1,
Expand All @@ -25,7 +25,7 @@ def batch_run(
----------
model_cls : Type[Model]
The model class to batch-run
parameters : Mapping[str, Union[Any, Iterable[Any]]],
parameters : Mapping[str, Any | Iterable[Any]],
Dictionary with model parameters over which to run the model. You can either pass single values or iterables.
number_processes : int, optional
Number of processes used, by default 1. Set this to None if you want to use all CPUs.
Expand Down Expand Up @@ -76,13 +76,13 @@ def batch_run(


def _make_model_kwargs(
parameters: Mapping[str, Union[Any, Iterable[Any]]],
parameters: Mapping[str, Any | Iterable[Any]],
) -> list[dict[str, Any]]:
"""Create model kwargs from parameters dictionary.

Parameters
----------
parameters : Mapping[str, Union[Any, Iterable[Any]]]
parameters : Mapping[str, [Any | Iterable[Any]]
Single or multiple values for each model parameter name

Returns
Expand Down
4 changes: 1 addition & 3 deletions mesa/experimental/components/matplotlib.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

import networkx as nx
import solara
from matplotlib.figure import Figure
Expand All @@ -9,7 +7,7 @@


@solara.component
def SpaceMatplotlib(model, agent_portrayal, dependencies: Optional[list[any]] = None):
def SpaceMatplotlib(model, agent_portrayal, dependencies: list[any] | None = None):
space_fig = Figure()
space_ax = space_fig.subplots()
space = getattr(model, "grid", None)
Expand Down
10 changes: 5 additions & 5 deletions mesa/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import warnings
from collections.abc import Iterable, Iterator, Sequence
from numbers import Real
from typing import Any, Callable, TypeVar, Union, cast, overload
from typing import Any, Callable, TypeVar, cast, overload
from warnings import warn

with contextlib.suppress(ImportError):
Expand All @@ -42,12 +42,12 @@

Coordinate = tuple[int, int]
# used in ContinuousSpace
FloatCoordinate = Union[tuple[float, float], npt.NDArray[float]]
FloatCoordinate = tuple[float, float] | npt.NDArray[float]
NetworkCoordinate = int

Position = Union[Coordinate, FloatCoordinate, NetworkCoordinate]
Position = Coordinate | FloatCoordinate | NetworkCoordinate

GridContent = Union[Agent, None]
GridContent = Agent | None
MultiGridContent = list[Agent]

F = TypeVar("F", bound=Callable[..., Any])
Expand Down Expand Up @@ -905,7 +905,7 @@ def select_cells(
return_list (bool, optional): If True, return a list of coordinates, otherwise return a mask.

Returns:
Union[list[Coordinate], np.ndarray]: Coordinates where conditions are satisfied or the combined mask.
list[Coordinate] | np.ndarray: Coordinates where conditions are satisfied or the combined mask.
"""
# Initialize the combined mask
combined_mask = np.ones((self.width, self.height), dtype=bool)
Expand Down
9 changes: 1 addition & 8 deletions mesa/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,18 @@
model has taken.
"""

# Mypy; for the `|` operator purpose
# Remove this __future__ import once the oldest supported Python is 3.10
from __future__ import annotations

import heapq
import warnings
import weakref
from collections import defaultdict
from collections.abc import Iterable

# mypy
from typing import Union

from mesa.agent import Agent, AgentSet
from mesa.model import Model

# BaseScheduler has a self.time of int, while
# StagedActivation has a self.time of float
TimeT = Union[float, int]
TimeT = float | int


class BaseScheduler:
Expand Down
Loading