Skip to content

Commit 38d662e

Browse files
committed
mypy: Use "|" operator instead of Union/Optional
See https://peps.python.org/pep-0604/.
1 parent 991a017 commit 38d662e

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

mesa/agent.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
Core Objects: Agent
55
66
"""
7+
# Mypy; for the `|` operator purpose
8+
# Remove this __future__ import once the oldest supported Python is 3.10
9+
from __future__ import annotations
10+
711
# mypy
8-
from typing import Optional, TYPE_CHECKING
12+
from typing import TYPE_CHECKING
913
from random import Random
1014

1115
if TYPE_CHECKING:
@@ -27,7 +31,7 @@ def __init__(self, unique_id: int, model: "Model") -> None:
2731
"""
2832
self.unique_id = unique_id
2933
self.model = model
30-
self.pos: Optional[Position] = None
34+
self.pos: Position | None = None
3135

3236
def step(self) -> None:
3337
"""A single step of the agent."""

mesa/model.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
Core Objects: Model
55
66
"""
7+
# Mypy; for the `|` operator purpose
8+
# Remove this __future__ import once the oldest supported Python is 3.10
9+
from __future__ import annotations
10+
711
import random
812

913
from mesa.datacollection import DataCollector
1014

1115
# mypy
12-
from typing import Any, Optional
16+
from typing import Any
1317

1418

1519
class Model:
@@ -52,7 +56,7 @@ def next_id(self) -> int:
5256
self.current_id += 1
5357
return self.current_id
5458

55-
def reset_randomizer(self, seed: Optional[int] = None) -> None:
59+
def reset_randomizer(self, seed: int | None = None) -> None:
5660
"""Reset the model random number generator.
5761
5862
Args:

mesa/space.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
# good reason to use one-character variable names for x and y.
1414
# pylint: disable=invalid-name
1515

16+
# Mypy; for the `|` operator purpose
17+
# Remove this __future__ import once the oldest supported Python is 3.10
18+
from __future__ import annotations
19+
1620
import itertools
1721
import math
1822
from warnings import warn
@@ -26,7 +30,6 @@
2630
Iterable,
2731
Iterator,
2832
List,
29-
Optional,
3033
Set,
3134
Sequence,
3235
Tuple,
@@ -48,7 +51,7 @@
4851

4952
Position = Union[Coordinate, FloatCoordinate, NetworkCoordinate]
5053

51-
GridContent = Optional[Agent]
54+
GridContent = Union[Agent, None]
5255
MultiGridContent = List[Agent]
5356

5457
F = TypeVar("F", bound=Callable[..., Any])
@@ -128,8 +131,8 @@ def __getitem__(self, index: int) -> List[GridContent]:
128131

129132
@overload
130133
def __getitem__(
131-
self, index: Tuple[Union[int, slice], Union[int, slice]]
132-
) -> Union[GridContent, List[GridContent]]:
134+
self, index: Tuple[int | slice, int | slice]
135+
) -> GridContent | List[GridContent]:
133136
...
134137

135138
@overload
@@ -138,12 +141,8 @@ def __getitem__(self, index: Sequence[Coordinate]) -> List[GridContent]:
138141

139142
def __getitem__(
140143
self,
141-
index: Union[
142-
int,
143-
Sequence[Coordinate],
144-
Tuple[Union[int, slice], Union[int, slice]],
145-
],
146-
) -> Union[GridContent, List[GridContent]]:
144+
index: int | Sequence[Coordinate] | Tuple[int | slice, int | slice],
145+
) -> GridContent | List[GridContent]:
147146
"""Access contents from the grid."""
148147

149148
if isinstance(index, int):
@@ -436,7 +435,7 @@ def is_cell_empty(self, pos: Coordinate) -> bool:
436435
return self.grid[x][y] == self.default_val()
437436

438437
def move_to_empty(
439-
self, agent: Agent, cutoff: float = 0.998, num_agents: Optional[int] = None
438+
self, agent: Agent, cutoff: float = 0.998, num_agents: int | None = None
440439
) -> None:
441440
"""Moves agent to a random empty cell, vacating agent's old cell."""
442441
if len(self.empties) == 0:
@@ -478,7 +477,7 @@ def move_to_empty(
478477
self._place_agent(agent, new_pos)
479478
agent.pos = new_pos
480479

481-
def find_empty(self) -> Optional[Coordinate]:
480+
def find_empty(self) -> Coordinate | None:
482481
"""Pick a random empty cell."""
483482
import random
484483

@@ -509,7 +508,7 @@ class SingleGrid(Grid):
509508
empties: Set[Coordinate] = set()
510509

511510
def position_agent(
512-
self, agent: Agent, x: Union[int, str] = "random", y: Union[int, str] = "random"
511+
self, agent: Agent, x: int | str = "random", y: int | str = "random"
513512
) -> None:
514513
"""Position an agent on the grid.
515514
This is used when first placing agents! Use 'move_to_empty()'
@@ -781,7 +780,7 @@ def __init__(
781780
self.size = np.array((self.width, self.height))
782781
self.torus = torus
783782

784-
self._agent_points: Optional[npt.NDArray[FloatCoordinate]] = None
783+
self._agent_points: npt.NDArray[FloatCoordinate] | None = None
785784
self._index_to_agent: Dict[int, Agent] = {}
786785
self._agent_to_index: Dict[Agent, int] = {}
787786

mesa/time.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@
2121
model has taken.
2222
"""
2323

24+
# Mypy; for the `|` operator purpose
25+
# Remove this __future__ import once the oldest supported Python is 3.10
26+
from __future__ import annotations
27+
2428
from collections import OrderedDict, defaultdict
2529

2630
# mypy
27-
from typing import Dict, Iterator, List, Optional, Union, Type
31+
from typing import Dict, Iterator, List, Type, Union
2832
from mesa.agent import Agent
2933
from mesa.model import Model
3034

@@ -163,7 +167,7 @@ class StagedActivation(BaseScheduler):
163167
def __init__(
164168
self,
165169
model: Model,
166-
stage_list: Optional[List[str]] = None,
170+
stage_list: List[str] | None = None,
167171
shuffle: bool = False,
168172
shuffle_between_stages: bool = False,
169173
) -> None:

0 commit comments

Comments
 (0)