From a3aab3ae0ebdd9c751df8d1d6dad2e18072193f2 Mon Sep 17 00:00:00 2001 From: antonrh Date: Sun, 30 Aug 2026 10:05:58 +0300 Subject: [PATCH] Keep a renamed anyio helper from breaking every test run --- .github/workflows/ci.yml | 23 +++++++++++++++++++++++ anydi/ext/pytest_plugin.py | 14 +++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f859b78..e16f780 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -84,6 +84,29 @@ jobs: - name: Run tests run: uv run --no-sync pytest tests + upstream: + name: Run unit tests on anyio main + runs-on: ubuntu-latest + # The pytest plugin uses two helpers `anyio` keeps out of its public API, + # and it loads in every test run of everyone who installs `anydi`. + continue-on-error: true + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Install uv + uses: astral-sh/setup-uv@v5 + with: + python-version: '3.12' + enable-cache: true + - name: Install dependencies + run: uv sync --all-extras --dev + - name: Take anyio from main + run: uv pip install --upgrade "anyio @ git+https://github.com/agronholm/anyio" + - name: Run tests + run: | + uv run --no-sync pytest tests + uv run --no-sync pytest tests/ext/test_pytest_plugin.py -p anydi + benchmarks: name: Run benchmarks runs-on: ubuntu-latest diff --git a/anydi/ext/pytest_plugin.py b/anydi/ext/pytest_plugin.py index 05ca600..5744e5d 100644 --- a/anydi/ext/pytest_plugin.py +++ b/anydi/ext/pytest_plugin.py @@ -7,7 +7,6 @@ from typing import TYPE_CHECKING, Annotated, Any, cast, get_args, get_origin import pytest -from anyio.pytest_plugin import extract_backend_and_options, get_runner from typing_extensions import get_annotations from anydi import ( @@ -121,6 +120,19 @@ async def _resolve() -> None: "Failed to resolve '%s' for %s", name, request.node.nodeid ) + # `anyio` keeps these out of its public API, so a version that renames them + # breaks async injection only, not every test run that imports this plugin. + try: + from anyio.pytest_plugin import extract_backend_and_options, get_runner + except ImportError: # pragma: no cover - a version that moved them + pytest.fail( + "Injecting dependencies into async tests needs " # ty: ignore[invalid-argument-type] + "`extract_backend_and_options` and `get_runner` from " + "`anyio.pytest_plugin`, which this version of `anyio` does not " + "have. Resolve the dependency in the test instead, or report it.", + pytrace=False, # ty: ignore[parameter-already-assigned] + ) + anyio_backend = request.getfixturevalue("anyio_backend") backend_name, backend_options = extract_backend_and_options(anyio_backend)