Skip to content

Commit f4ae953

Browse files
authored
Drop support for Python 3.9, require Python >= 3.10 (#2132)
* Drop support for Python 3.9, require Python >= 3.10 Starting development for Mesa 3.0 and following SPEC 0 we can drop Python 3.9 support and require Python 3.10 or higher for future development. This allows adopting more modern Python features and simplifies the testing and CI configurations. See the Python 3.10 release notes: https://docs.python.org/3/whatsnew/3.10.html The 2.3.x release series will keep supporting Python 3.9. * Update type hinting for Python 3.10 `X | Y` can now be used for type annotations, including making a variable optional with `X | None` * intro_tutorial.ipynb: Require Python 3.10 * typing: Replace Union with pipe (|) We can do this now in Python 3.10!
1 parent 1c0b8d5 commit f4ae953

File tree

17 files changed

+34
-40
lines changed

17 files changed

+34
-40
lines changed

.github/workflows/build_lint.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ jobs:
3737
python-version: "3.11"
3838
- os: ubuntu
3939
python-version: "3.10"
40-
- os: ubuntu
41-
python-version: "3.9"
4240
# Disabled for now. See https://github.com/projectmesa/mesa/issues/1253
4341
#- os: ubuntu
4442
# python-version: 'pypy-3.8'

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ repos:
1717
rev: v3.15.2
1818
hooks:
1919
- id: pyupgrade
20-
args: [--py38-plus]
20+
args: [--py310-plus]
2121
- repo: https://github.com/pre-commit/pre-commit-hooks
2222
rev: v4.5.0 # Use the ref you want to point at
2323
hooks:

docs/tutorials/intro_tutorial.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"source": [
6161
"### Tutorial Setup\n",
6262
"\n",
63-
"Create and activate a [virtual environment](http://docs.python-guide.org/en/latest/dev/virtualenvs/). *Python version 3.9 or higher is required*.\n",
63+
"Create and activate a [virtual environment](http://docs.python-guide.org/en/latest/dev/virtualenvs/). *Python version 3.10 or higher is required*.\n",
6464
"\n",
6565
"Install Mesa:\n",
6666
"\n",

mesa/agent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
import warnings
1515
import weakref
1616
from collections import defaultdict
17-
from collections.abc import Iterable, Iterator, MutableSet, Sequence
17+
from collections.abc import Callable, Iterable, Iterator, MutableSet, Sequence
1818
from random import Random
1919

2020
# mypy
21-
from typing import TYPE_CHECKING, Any, Callable
21+
from typing import TYPE_CHECKING, Any
2222

2323
if TYPE_CHECKING:
2424
# We ensure that these are not imported during runtime to prevent cyclic

mesa/batchrunner.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from collections.abc import Iterable, Mapping
33
from functools import partial
44
from multiprocessing import Pool
5-
from typing import Any, Optional, Union
5+
from typing import Any
66

77
from tqdm.auto import tqdm
88

@@ -11,9 +11,9 @@
1111

1212
def batch_run(
1313
model_cls: type[Model],
14-
parameters: Mapping[str, Union[Any, Iterable[Any]]],
14+
parameters: Mapping[str, Any | Iterable[Any]],
1515
# We still retain the Optional[int] because users may set it to None (i.e. use all CPUs)
16-
number_processes: Optional[int] = 1,
16+
number_processes: int | None = 1,
1717
iterations: int = 1,
1818
data_collection_period: int = -1,
1919
max_steps: int = 1000,
@@ -76,7 +76,7 @@ def batch_run(
7676

7777

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

mesa/datacollection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def collect(self, model):
196196
if self.model_reporters:
197197
for var, reporter in self.model_reporters.items():
198198
# Check if lambda or partial function
199-
if isinstance(reporter, (types.LambdaType, partial)):
199+
if isinstance(reporter, types.LambdaType | partial):
200200
self.model_vars[var].append(reporter(model))
201201
# Check if model attribute
202202
elif isinstance(reporter, str):

mesa/experimental/cell_space/cell_collection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from __future__ import annotations
22

33
import itertools
4-
from collections.abc import Iterable, Mapping
4+
from collections.abc import Callable, Iterable, Mapping
55
from functools import cached_property
66
from random import Random
7-
from typing import TYPE_CHECKING, Callable, Generic, TypeVar
7+
from typing import TYPE_CHECKING, Generic, TypeVar
88

99
if TYPE_CHECKING:
1010
from mesa.experimental.cell_space.cell import Cell

mesa/experimental/cell_space/grid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def _validate_parameters(self):
6060
raise ValueError("Dimensions must be a list of positive integers.")
6161
if not isinstance(self.torus, bool):
6262
raise ValueError("Torus must be a boolean.")
63-
if self.capacity is not None and not isinstance(self.capacity, (float, int)):
63+
if self.capacity is not None and not isinstance(self.capacity, float | int):
6464
raise ValueError("Capacity must be a number or None.")
6565

6666
def select_random_empty_cell(self) -> T:

mesa/experimental/cell_space/network.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from random import Random
2-
from typing import Any, Optional
2+
from typing import Any
33

44
from mesa.experimental.cell_space.cell import Cell
55
from mesa.experimental.cell_space.discrete_space import DiscreteSpace
@@ -11,8 +11,8 @@ class Network(DiscreteSpace):
1111
def __init__(
1212
self,
1313
G: Any, # noqa: N803
14-
capacity: Optional[int] = None,
15-
random: Optional[Random] = None,
14+
capacity: int | None = None,
15+
random: Random | None = None,
1616
cell_klass: type[Cell] = Cell,
1717
) -> None:
1818
"""A Networked grid

mesa/experimental/components/altair.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import contextlib
2-
from typing import Optional
32

43
import solara
54

@@ -8,7 +7,7 @@
87

98

109
@solara.component
11-
def SpaceAltair(model, agent_portrayal, dependencies: Optional[list[any]] = None):
10+
def SpaceAltair(model, agent_portrayal, dependencies: list[any] | None = None):
1211
space = getattr(model, "grid", None)
1312
if space is None:
1413
# Sometimes the space is defined as model.space instead of model.grid

0 commit comments

Comments
 (0)