Skip to content

Commit e21a5b5

Browse files
authored
Drop Python 3.11, require Python 3.12+ and update to modern type parameter syntax (#2842)
* Drop Python 3.11, require Python 3.12+, test Python 3.14 This commit drops Python 3.11 support and require Python 3.12+ following SPEC 0. See the Python 3.12 release notes: https://docs.python.org/3/whatsnew/3.12.html The Mesa 3.3.x release series will keep supporting Python 3.11. * Upgrade to Python 3.12 and apply type parameter syntax With the minimum Python version now at 3.12, we can use the modern type parameter syntax (PEP 695) instead of the legacy Generic[T] pattern. This change improves code readability and aligns with current Python typing best practices. Changes: - CellCollection: Convert from Generic[T] to type parameter syntax with Cell bound - DiscreteSpace: Convert from Generic[T] to type parameter syntax with Cell bound - Grid: Remove redundant Generic[T] inheritance (already inherits from DiscreteSpace[T]) - accept_tuple_argument: Convert function type variable to use type parameter syntax - Remove unused Generic imports from typing module These changes are purely syntactic modernizations with no functional impact. The type parameter syntax is more concise and better integrated with Python's type system. Fixes ruff UP046 and UP047 violations. * Remove redundant Generic inheritance from Grid class The Grid class inherits from DiscreteSpace[T], which already provides generic type support. The additional Generic[T] inheritance is redundant and triggers a ruff UP046 violation. Changes: - Remove Generic[T] from Grid class inheritance - Remove unused Generic import from typing module - Keep TypeVar T definition as it's still needed for DiscreteSpace[T] parameterization Fixes ruff UP046 violation in grid.py. * CI: Remove Python 3.11 run
1 parent e1f9780 commit e21a5b5

File tree

8 files changed

+12
-15
lines changed

8 files changed

+12
-15
lines changed

.github/workflows/build_lint.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ jobs:
3535
include:
3636
- os: ubuntu
3737
python-version: "3.12"
38-
- os: ubuntu
39-
python-version: "3.11"
4038
# Disabled for now. See https://github.com/projectmesa/mesa/issues/1253
4139
#- os: ubuntu
4240
# 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.20.0
1818
hooks:
1919
- id: pyupgrade
20-
args: [--py311-plus]
20+
args: [--py312-plus]
2121
- repo: https://github.com/pre-commit/pre-commit-hooks
2222
rev: v6.0.0 # Use the ref you want to point at
2323
hooks:

docs/tutorials/0_first_model.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"source": [
5454
"### Tutorial Setup\n",
5555
"\n",
56-
"Create and activate a [virtual environment](http://docs.python-guide.org/en/latest/dev/virtualenvs/). *Python version 3.11 or higher is required*.\n",
56+
"Create and activate a [virtual environment](http://docs.python-guide.org/en/latest/dev/virtualenvs/). *Python version 3.12 or higher is required*.\n",
5757
"\n",
5858
"Install Mesa:\n",
5959
"\n",

mesa/discrete_space/cell_collection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from collections.abc import Callable, Iterable, Mapping
2121
from functools import cached_property
2222
from random import Random
23-
from typing import TYPE_CHECKING, Generic, TypeVar
23+
from typing import TYPE_CHECKING, TypeVar
2424

2525
if TYPE_CHECKING:
2626
from mesa.discrete_space.cell import Cell
@@ -29,7 +29,7 @@
2929
T = TypeVar("T", bound="Cell")
3030

3131

32-
class CellCollection(Generic[T]):
32+
class CellCollection[T: Cell]:
3333
"""An immutable collection of cells.
3434
3535
Attributes:

mesa/discrete_space/discrete_space.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import warnings
1919
from functools import cached_property
2020
from random import Random
21-
from typing import Generic, TypeVar
21+
from typing import TypeVar
2222

2323
from mesa.agent import AgentSet
2424
from mesa.discrete_space.cell import Cell
@@ -27,7 +27,7 @@
2727
T = TypeVar("T", bound=Cell)
2828

2929

30-
class DiscreteSpace(Generic[T]):
30+
class DiscreteSpace[T: Cell]:
3131
"""Base class for all discrete spaces.
3232
3333
Attributes:

mesa/discrete_space/grid.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from collections.abc import Sequence
1818
from itertools import product
1919
from random import Random
20-
from typing import Any, Generic, TypeVar
20+
from typing import Any, TypeVar
2121

2222
from mesa.discrete_space import Cell, DiscreteSpace
2323
from mesa.discrete_space.property_layer import (
@@ -56,7 +56,7 @@ def unpickle_gridcell(parent, fields):
5656
return instance
5757

5858

59-
class Grid(DiscreteSpace[T], Generic[T], HasPropertyLayers):
59+
class Grid(DiscreteSpace[T], HasPropertyLayers):
6060
"""Base class for all grid classes.
6161
6262
Attributes:

mesa/space.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
F = TypeVar("F", bound=Callable[..., Any])
6262

6363

64-
def accept_tuple_argument(wrapped_function: F) -> F:
64+
def accept_tuple_argument[F: Callable[..., Any]](wrapped_function: F) -> F:
6565
"""Decorator to allow grid methods that take a list of (x, y) coord tuples to also handle a single position.
6666
6767
Tuples are wrapped in a single-item list rather than forcing user to do it.

pyproject.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "hatchling.build"
66
name = "Mesa"
77
description = "Agent-based modeling (ABM) in Python"
88
license = { text = "Apache 2.0" }
9-
requires-python = ">=3.11"
9+
requires-python = ">=3.12"
1010
authors = [
1111
{ name = "Project Mesa Team", email = "[email protected]" },
1212
]
@@ -25,7 +25,6 @@ classifiers = [
2525
"Topic :: Scientific/Engineering :: Artificial Intelligence",
2626
"Intended Audience :: Science/Research",
2727
"Programming Language :: Python :: 3 :: Only",
28-
"Programming Language :: Python :: 3.11",
2928
"Programming Language :: Python :: 3.12",
3029
"Programming Language :: Python :: 3.13",
3130
"License :: OSI Approved :: Apache Software License",
@@ -98,9 +97,9 @@ path = "mesa/__init__.py"
9897

9998
[tool.ruff]
10099
# See https://github.com/charliermarsh/ruff#rules for error code definitions.
101-
# Hardcode to Python 3.11.
100+
# Hardcode to Python 3.12.
102101
# Reminder to update mesa-examples if the value below is changed.
103-
target-version = "py311"
102+
target-version = "py312"
104103
extend-exclude = ["build"]
105104

106105
[tool.ruff.lint]

0 commit comments

Comments
 (0)