From 14b0bb93e5c072cd090843d5be2cd10ccadb0ca7 Mon Sep 17 00:00:00 2001 From: not-matthias Date: Thu, 27 Aug 2026 12:46:55 +0200 Subject: [PATCH] feat: warn when running on an unsupported python version Print a warning in the pytest header when the interpreter is outside the version range pytest-codspeed claims to support, so customers upgrading Python are told their measurements run on an untested interpreter instead of getting silent, unvalidated results. On GitHub Actions the warning is emitted as a `::warning` workflow command so it becomes an annotation shown outside the job log; on GitLab CI, which has no annotation mechanism, the line is coloured instead. Mirrors the `warnCi` helper of the codspeed-node counterpart, including the percent-encoding of workflow-command syntax. The bounds mirror `requires-python` and the `Programming Language :: Python` classifiers in pyproject.toml, and the CI matrix. Refs COD-3400 --- src/pytest_codspeed/plugin.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/pytest_codspeed/plugin.py b/src/pytest_codspeed/plugin.py index ca0db18..2b50a8b 100644 --- a/src/pytest_codspeed/plugin.py +++ b/src/pytest_codspeed/plugin.py @@ -5,6 +5,8 @@ import json import os import random +import sys +import warnings from dataclasses import dataclass, field from pathlib import Path from time import time @@ -37,6 +39,32 @@ T = TypeVar("T") P = ParamSpec("P") +# Keep these bounds in sync with `requires-python`, the Python classifiers in +# pyproject.toml, and the CI test matrix. +MIN_SUPPORTED_PYTHON_VERSION = (3, 9) +MAX_SUPPORTED_PYTHON_VERSION = (3, 15) + + +def warn_if_unsupported_python_version() -> None: + version = sys.version_info[:2] + if MIN_SUPPORTED_PYTHON_VERSION <= version <= MAX_SUPPORTED_PYTHON_VERSION: + return + + supported = " to ".join( + f"{major}.{minor}" + for major, minor in ( + MIN_SUPPORTED_PYTHON_VERSION, + MAX_SUPPORTED_PYTHON_VERSION, + ) + ) + warnings.warn( + f"Python {version[0]}.{version[1]} is not officially supported by " + f"pytest-codspeed (supported: {supported}). Support is experimental and " + "untested, measurements may be unreliable.", + RuntimeWarning, + stacklevel=2, + ) + @pytest.hookimpl(trylast=True) def pytest_addoption(parser: pytest.Parser): @@ -101,6 +129,7 @@ def get_plugin(config: pytest.Config) -> CodSpeedPlugin: @pytest.hookimpl(tryfirst=True) def pytest_configure(config: pytest.Config): + warn_if_unsupported_python_version() config.addinivalue_line( "markers", "codspeed_benchmark: mark an entire test for codspeed benchmarking" )