From 8e7a3aac09e421f1cc07d0c09864ed2c6f3354d9 Mon Sep 17 00:00:00 2001 From: Artur Shiriev Date: Wed, 7 Oct 2026 19:12:37 +0300 Subject: [PATCH] fix: annotate context managers with Generator for ty 0.0.85 --- lite_bootstrap/bootstrappers/fastapi_bootstrapper.py | 3 ++- lite_bootstrap/bootstrappers/fastmcp_bootstrapper.py | 3 ++- lite_bootstrap/exceptions.py | 6 +++--- tests/conftest.py | 5 +++-- tests/test_fastapi_bootstrap.py | 5 +++-- tests/test_litestar_bootstrap.py | 3 ++- 6 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lite_bootstrap/bootstrappers/fastapi_bootstrapper.py b/lite_bootstrap/bootstrappers/fastapi_bootstrapper.py index d99de2e..177ea8b 100644 --- a/lite_bootstrap/bootstrappers/fastapi_bootstrapper.py +++ b/lite_bootstrap/bootstrappers/fastapi_bootstrapper.py @@ -2,6 +2,7 @@ import dataclasses import time import typing +from collections.abc import AsyncGenerator from lite_bootstrap import import_checker from lite_bootstrap.bootstrappers.base import BaseBootstrapper @@ -264,7 +265,7 @@ class FastAPIBootstrapper(BaseBootstrapper["fastapi.FastAPI"]): not_ready_message = "fastapi is not installed" @contextlib.asynccontextmanager - async def lifespan_manager(self, _: "fastapi.FastAPI") -> typing.AsyncIterator[dict[str, typing.Any]]: + async def lifespan_manager(self, _: "fastapi.FastAPI") -> AsyncGenerator[dict[str, typing.Any]]: try: yield {} finally: diff --git a/lite_bootstrap/bootstrappers/fastmcp_bootstrapper.py b/lite_bootstrap/bootstrappers/fastmcp_bootstrapper.py index 21fe3b3..cee2550 100644 --- a/lite_bootstrap/bootstrappers/fastmcp_bootstrapper.py +++ b/lite_bootstrap/bootstrappers/fastmcp_bootstrapper.py @@ -2,6 +2,7 @@ import dataclasses import time import typing +from collections.abc import AsyncGenerator from lite_bootstrap import import_checker from lite_bootstrap.bootstrappers.base import BaseBootstrapper @@ -42,7 +43,7 @@ def __init__(self, teardown: typing.Callable[[], None]) -> None: self._teardown = teardown @contextlib.asynccontextmanager - async def lifespan(self) -> typing.AsyncIterator[None]: + async def lifespan(self) -> AsyncGenerator[None]: try: yield finally: diff --git a/lite_bootstrap/exceptions.py b/lite_bootstrap/exceptions.py index b400547..c90ab62 100644 --- a/lite_bootstrap/exceptions.py +++ b/lite_bootstrap/exceptions.py @@ -1,6 +1,6 @@ import contextlib import logging -import typing +from collections.abc import Generator class LiteBootstrapError(RuntimeError): @@ -44,7 +44,7 @@ def __init__(self, logger: logging.Logger | None = None) -> None: self._logger = logger @contextlib.contextmanager - def capture(self, name: str) -> typing.Iterator[None]: + def capture(self, name: str) -> Generator[None]: """Record an ``Exception`` raised by one teardown step under ``name`` and continue.""" try: yield @@ -55,7 +55,7 @@ def capture(self, name: str) -> typing.Iterator[None]: @contextlib.contextmanager -def collect_teardown_errors(logger: logging.Logger | None = None) -> typing.Iterator[TeardownErrorCollector]: +def collect_teardown_errors(logger: logging.Logger | None = None) -> Generator[TeardownErrorCollector]: """Collect the failures of individual teardown steps and raise them together. Raises :class:`TeardownError` on leaving the block if any ``capture()`` recorded diff --git a/tests/conftest.py b/tests/conftest.py index 16eb4bb..762947e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,6 +2,7 @@ import sys import typing import warnings +from collections.abc import Generator from importlib import reload import opentelemetry.metrics._internal @@ -89,7 +90,7 @@ def warning_source_files(caught: typing.Iterable[warnings.WarningMessage], categ @contextlib.contextmanager -def emulate_package_missing(package_name: str) -> typing.Iterator[None]: +def emulate_package_missing(package_name: str) -> Generator[None]: old_module = sys.modules[package_name] sys.modules[package_name] = None # ty: ignore[invalid-assignment] reload(import_checker) @@ -103,7 +104,7 @@ def emulate_package_missing(package_name: str) -> typing.Iterator[None]: @contextlib.contextmanager def emulate_package_missing_with_module_reload( package_name: str, module_names: typing.Iterable[str] -) -> typing.Iterator[None]: +) -> Generator[None]: # Reload listed modules under emulate_package_missing so their # `if import_checker.is_X_installed: import X` blocks re-evaluate against # the patched flag. `importlib.reload` preserves existing module globals, diff --git a/tests/test_fastapi_bootstrap.py b/tests/test_fastapi_bootstrap.py index 97db9de..329b480 100644 --- a/tests/test_fastapi_bootstrap.py +++ b/tests/test_fastapi_bootstrap.py @@ -5,6 +5,7 @@ import typing import uuid import warnings +from collections.abc import Generator from unittest.mock import patch import fastapi @@ -290,7 +291,7 @@ def access_logger() -> typing.Iterator[RecordingAccessLogger]: @contextlib.contextmanager -def _bootstrapped(config: FastAPIConfig) -> typing.Iterator["fastapi.FastAPI"]: +def _bootstrapped(config: FastAPIConfig) -> Generator["fastapi.FastAPI"]: bootstrapper = FastAPIBootstrapper(bootstrap_config=config) try: yield bootstrapper.bootstrap() @@ -299,7 +300,7 @@ def _bootstrapped(config: FastAPIConfig) -> typing.Iterator["fastapi.FastAPI"]: @contextlib.contextmanager -def _bootstrapped_with_route(config: FastAPIConfig) -> typing.Iterator["fastapi.FastAPI"]: +def _bootstrapped_with_route(config: FastAPIConfig) -> Generator["fastapi.FastAPI"]: with _bootstrapped(config) as application: @application.post("/items/{item_id}") diff --git a/tests/test_litestar_bootstrap.py b/tests/test_litestar_bootstrap.py index 73aa3aa..de7955f 100644 --- a/tests/test_litestar_bootstrap.py +++ b/tests/test_litestar_bootstrap.py @@ -8,6 +8,7 @@ import typing import warnings import weakref +from collections.abc import Generator from unittest.mock import AsyncMock, patch import litestar @@ -307,7 +308,7 @@ def emit(self, record: logging.LogRecord) -> None: @contextlib.contextmanager -def _recorded_litestar_logs() -> typing.Iterator[list[str]]: +def _recorded_litestar_logs() -> Generator[list[str]]: """Record Litestar's rendered log lines. Enter this inside the TestClient context: Litestar's StructLoggingConfig runs dictConfig at