From 29319a382cf79e9dcaa71efabb5c06fbfb838bf5 Mon Sep 17 00:00:00 2001 From: Ewout ter Hoeven <15776622+EwoutH@users.noreply.github.com> Date: Tue, 7 Oct 2025 20:40:17 +0200 Subject: [PATCH 1/4] 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. --- .pre-commit-config.yaml | 2 +- docs/tutorials/0_first_model.ipynb | 2 +- pyproject.toml | 7 +++---- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 50b6d4a3714..03f7191b741 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,7 +17,7 @@ repos: rev: v3.20.0 hooks: - id: pyupgrade - args: [--py311-plus] + args: [--py312-plus] - repo: https://github.com/pre-commit/pre-commit-hooks rev: v6.0.0 # Use the ref you want to point at hooks: diff --git a/docs/tutorials/0_first_model.ipynb b/docs/tutorials/0_first_model.ipynb index cad5cbf217e..5848d1f9dbc 100644 --- a/docs/tutorials/0_first_model.ipynb +++ b/docs/tutorials/0_first_model.ipynb @@ -53,7 +53,7 @@ "source": [ "### Tutorial Setup\n", "\n", - "Create and activate a [virtual environment](http://docs.python-guide.org/en/latest/dev/virtualenvs/). *Python version 3.11 or higher is required*.\n", + "Create and activate a [virtual environment](http://docs.python-guide.org/en/latest/dev/virtualenvs/). *Python version 3.12 or higher is required*.\n", "\n", "Install Mesa:\n", "\n", diff --git a/pyproject.toml b/pyproject.toml index e5e8a74ae30..159302d2910 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ build-backend = "hatchling.build" name = "Mesa" description = "Agent-based modeling (ABM) in Python" license = { text = "Apache 2.0" } -requires-python = ">=3.11" +requires-python = ">=3.12" authors = [ { name = "Project Mesa Team", email = "maintainers@projectmesa.dev" }, ] @@ -25,7 +25,6 @@ classifiers = [ "Topic :: Scientific/Engineering :: Artificial Intelligence", "Intended Audience :: Science/Research", "Programming Language :: Python :: 3 :: Only", - "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", "Programming Language :: Python :: 3.13", "License :: OSI Approved :: Apache Software License", @@ -98,9 +97,9 @@ path = "mesa/__init__.py" [tool.ruff] # See https://github.com/charliermarsh/ruff#rules for error code definitions. -# Hardcode to Python 3.11. +# Hardcode to Python 3.12. # Reminder to update mesa-examples if the value below is changed. -target-version = "py311" +target-version = "py312" extend-exclude = ["build"] [tool.ruff.lint] From 3c80a55bd1e74350a6735be0002cc7386d685a52 Mon Sep 17 00:00:00 2001 From: Ewout ter Hoeven <15776622+EwoutH@users.noreply.github.com> Date: Tue, 7 Oct 2025 20:51:55 +0200 Subject: [PATCH 2/4] 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. --- mesa/discrete_space/cell_collection.py | 4 ++-- mesa/discrete_space/discrete_space.py | 4 ++-- mesa/space.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/mesa/discrete_space/cell_collection.py b/mesa/discrete_space/cell_collection.py index 834168b48a3..049fb0eed4d 100644 --- a/mesa/discrete_space/cell_collection.py +++ b/mesa/discrete_space/cell_collection.py @@ -20,7 +20,7 @@ from collections.abc import Callable, Iterable, Mapping from functools import cached_property from random import Random -from typing import TYPE_CHECKING, Generic, TypeVar +from typing import TYPE_CHECKING, TypeVar if TYPE_CHECKING: from mesa.discrete_space.cell import Cell @@ -29,7 +29,7 @@ T = TypeVar("T", bound="Cell") -class CellCollection(Generic[T]): +class CellCollection[T: Cell]: """An immutable collection of cells. Attributes: diff --git a/mesa/discrete_space/discrete_space.py b/mesa/discrete_space/discrete_space.py index f480ff15fbe..770a82938ac 100644 --- a/mesa/discrete_space/discrete_space.py +++ b/mesa/discrete_space/discrete_space.py @@ -18,7 +18,7 @@ import warnings from functools import cached_property from random import Random -from typing import Generic, TypeVar +from typing import TypeVar from mesa.agent import AgentSet from mesa.discrete_space.cell import Cell @@ -27,7 +27,7 @@ T = TypeVar("T", bound=Cell) -class DiscreteSpace(Generic[T]): +class DiscreteSpace[T: Cell]: """Base class for all discrete spaces. Attributes: diff --git a/mesa/space.py b/mesa/space.py index d366db30337..c9cd04f4223 100644 --- a/mesa/space.py +++ b/mesa/space.py @@ -61,7 +61,7 @@ F = TypeVar("F", bound=Callable[..., Any]) -def accept_tuple_argument(wrapped_function: F) -> F: +def accept_tuple_argument[F: Callable[..., Any]](wrapped_function: F) -> F: """Decorator to allow grid methods that take a list of (x, y) coord tuples to also handle a single position. Tuples are wrapped in a single-item list rather than forcing user to do it. From 24cf991ae3c3e70df0da77ea931f4f8cf1c8f095 Mon Sep 17 00:00:00 2001 From: Ewout ter Hoeven <15776622+EwoutH@users.noreply.github.com> Date: Tue, 7 Oct 2025 20:59:16 +0200 Subject: [PATCH 3/4] 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. --- mesa/discrete_space/grid.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mesa/discrete_space/grid.py b/mesa/discrete_space/grid.py index 5a99033f91f..7b0c5f5a586 100644 --- a/mesa/discrete_space/grid.py +++ b/mesa/discrete_space/grid.py @@ -17,7 +17,7 @@ from collections.abc import Sequence from itertools import product from random import Random -from typing import Any, Generic, TypeVar +from typing import Any, TypeVar from mesa.discrete_space import Cell, DiscreteSpace from mesa.discrete_space.property_layer import ( @@ -56,7 +56,7 @@ def unpickle_gridcell(parent, fields): return instance -class Grid(DiscreteSpace[T], Generic[T], HasPropertyLayers): +class Grid(DiscreteSpace[T], HasPropertyLayers): """Base class for all grid classes. Attributes: From c4f5b77e5c40d77dd13c3b3360d28afe564b59e1 Mon Sep 17 00:00:00 2001 From: Ewout ter Hoeven <15776622+EwoutH@users.noreply.github.com> Date: Tue, 7 Oct 2025 21:06:10 +0200 Subject: [PATCH 4/4] CI: Remove Python 3.11 run --- .github/workflows/build_lint.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/build_lint.yml b/.github/workflows/build_lint.yml index 79007d8b47f..4a8889eb8ca 100644 --- a/.github/workflows/build_lint.yml +++ b/.github/workflows/build_lint.yml @@ -35,8 +35,6 @@ jobs: include: - os: ubuntu python-version: "3.12" - - os: ubuntu - python-version: "3.11" # Disabled for now. See https://github.com/projectmesa/mesa/issues/1253 #- os: ubuntu # python-version: 'pypy-3.8'