From 7a922d2343e4fc885b6e79802e098600c20733e9 Mon Sep 17 00:00:00 2001 From: Farzad Khoobbakht Date: Mon, 14 Apr 2025 19:04:45 +0330 Subject: [PATCH 1/3] feat: Add lifespan support to FastAPI app creation Add optional lifespan parameter to create_fastapi_app function to support setup and teardown operations in the FastAPI application lifecycle. This enables proper resource management and initialization when creating FastAPI applications. --- archipy/helpers/utils/app_utils.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/archipy/helpers/utils/app_utils.py b/archipy/helpers/utils/app_utils.py index 334cb491..371b4a6f 100644 --- a/archipy/helpers/utils/app_utils.py +++ b/archipy/helpers/utils/app_utils.py @@ -3,7 +3,7 @@ import logging from collections.abc import Awaitable, Callable from http import HTTPStatus -from typing import Any, cast +from typing import Any, cast, AsyncContextManager from fastapi import FastAPI, Request, Response from fastapi.exceptions import RequestValidationError @@ -229,12 +229,14 @@ def create_fastapi_app( config: BaseConfig | None = None, *, configure_exception_handlers: bool = True, + lifespan: Callable[..., AsyncContextManager] | None = None, ) -> FastAPI: """Creates and configures a FastAPI application. Args: config (BaseConfig | None): Optional custom configuration. If not provided, uses global config. configure_exception_handlers (bool): Whether to configure exception handlers. + lifespan (Callable[..., AsyncContextManager] | None): Optional lifespan context manager for the app. Returns: FastAPI: The configured FastAPI application instance. @@ -253,6 +255,7 @@ def create_fastapi_app( docs_url=config.FASTAPI.DOCS_URL, redocs_url=config.FASTAPI.RE_DOCS_URL, responses=cast(dict[int | str, Any], common_responses), + lifespan=lifespan, ) FastAPIUtils.setup_sentry(config) From 3291d9f7cdf60ec017263514e9b187b892d29f40 Mon Sep 17 00:00:00 2001 From: Mohammadreza Khoobbakht Date: Tue, 15 Apr 2025 12:19:29 +0330 Subject: [PATCH 2/3] fix: replace AsyncContextManager with AbstractAsyncContextManager to fix UP035 lint error --- archipy/helpers/utils/app_utils.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/archipy/helpers/utils/app_utils.py b/archipy/helpers/utils/app_utils.py index 371b4a6f..3a48dae1 100644 --- a/archipy/helpers/utils/app_utils.py +++ b/archipy/helpers/utils/app_utils.py @@ -2,8 +2,9 @@ import logging from collections.abc import Awaitable, Callable +from contextlib import AbstractAsyncContextManager from http import HTTPStatus -from typing import Any, cast, AsyncContextManager +from typing import Any, cast from fastapi import FastAPI, Request, Response from fastapi.exceptions import RequestValidationError @@ -225,18 +226,18 @@ class AppUtils: @classmethod def create_fastapi_app( - cls, - config: BaseConfig | None = None, - *, - configure_exception_handlers: bool = True, - lifespan: Callable[..., AsyncContextManager] | None = None, + cls, + config: BaseConfig | None = None, + *, + configure_exception_handlers: bool = True, + lifespan: Callable[..., AbstractAsyncContextManager] | None = None, ) -> FastAPI: """Creates and configures a FastAPI application. Args: config (BaseConfig | None): Optional custom configuration. If not provided, uses global config. configure_exception_handlers (bool): Whether to configure exception handlers. - lifespan (Callable[..., AsyncContextManager] | None): Optional lifespan context manager for the app. + lifespan: Callable[..., AbstractAsyncContextManager] | None = None, Returns: FastAPI: The configured FastAPI application instance. From a0fdfce118c8cbb95156a31c398036de21a818db Mon Sep 17 00:00:00 2001 From: Mohammadreza Khoobbakht Date: Tue, 15 Apr 2025 12:24:50 +0330 Subject: [PATCH 3/3] fix: replace AsyncContextManager with AbstractAsyncContextManager to fix UP035 lint error --- archipy/helpers/utils/app_utils.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/archipy/helpers/utils/app_utils.py b/archipy/helpers/utils/app_utils.py index 3a48dae1..656c50d1 100644 --- a/archipy/helpers/utils/app_utils.py +++ b/archipy/helpers/utils/app_utils.py @@ -226,11 +226,11 @@ class AppUtils: @classmethod def create_fastapi_app( - cls, - config: BaseConfig | None = None, - *, - configure_exception_handlers: bool = True, - lifespan: Callable[..., AbstractAsyncContextManager] | None = None, + cls, + config: BaseConfig | None = None, + *, + configure_exception_handlers: bool = True, + lifespan: Callable[..., AbstractAsyncContextManager] | None = None, ) -> FastAPI: """Creates and configures a FastAPI application.