13
13
# good reason to use one-character variable names for x and y.
14
14
# pylint: disable=invalid-name
15
15
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
+
16
20
import itertools
17
21
import math
18
22
from warnings import warn
26
30
Iterable ,
27
31
Iterator ,
28
32
List ,
29
- Optional ,
30
33
Set ,
31
34
Sequence ,
32
35
Tuple ,
48
51
49
52
Position = Union [Coordinate , FloatCoordinate , NetworkCoordinate ]
50
53
51
- GridContent = Optional [Agent ]
54
+ GridContent = Union [Agent , None ]
52
55
MultiGridContent = List [Agent ]
53
56
54
57
F = TypeVar ("F" , bound = Callable [..., Any ])
@@ -128,8 +131,8 @@ def __getitem__(self, index: int) -> List[GridContent]:
128
131
129
132
@overload
130
133
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 ]:
133
136
...
134
137
135
138
@overload
@@ -138,12 +141,8 @@ def __getitem__(self, index: Sequence[Coordinate]) -> List[GridContent]:
138
141
139
142
def __getitem__ (
140
143
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 ]:
147
146
"""Access contents from the grid."""
148
147
149
148
if isinstance (index , int ):
@@ -436,7 +435,7 @@ def is_cell_empty(self, pos: Coordinate) -> bool:
436
435
return self .grid [x ][y ] == self .default_val ()
437
436
438
437
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
440
439
) -> None :
441
440
"""Moves agent to a random empty cell, vacating agent's old cell."""
442
441
if len (self .empties ) == 0 :
@@ -478,7 +477,7 @@ def move_to_empty(
478
477
self ._place_agent (agent , new_pos )
479
478
agent .pos = new_pos
480
479
481
- def find_empty (self ) -> Optional [ Coordinate ] :
480
+ def find_empty (self ) -> Coordinate | None :
482
481
"""Pick a random empty cell."""
483
482
import random
484
483
@@ -509,7 +508,7 @@ class SingleGrid(Grid):
509
508
empties : Set [Coordinate ] = set ()
510
509
511
510
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"
513
512
) -> None :
514
513
"""Position an agent on the grid.
515
514
This is used when first placing agents! Use 'move_to_empty()'
@@ -781,7 +780,7 @@ def __init__(
781
780
self .size = np .array ((self .width , self .height ))
782
781
self .torus = torus
783
782
784
- self ._agent_points : Optional [ npt .NDArray [FloatCoordinate ]] = None
783
+ self ._agent_points : npt .NDArray [FloatCoordinate ] | None = None
785
784
self ._index_to_agent : Dict [int , Agent ] = {}
786
785
self ._agent_to_index : Dict [Agent , int ] = {}
787
786
0 commit comments