8000 allow additional kwargs for fastapi by lesnik512 · Pull Request #35 · modern-python/lite-bootstrap · GitHub
[go: up one dir, main page]

Skip to content

allow additional kwargs for fastapi #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions lite_bootstrap/bootstrappers/fastapi_bootstrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,23 @@ class FastAPIConfig(
CorsConfig, HealthChecksConfig, LoggingConfig, OpentelemetryConfig, PrometheusConfig, SentryConfig, SwaggerConfig
):
application: "fastapi.FastAPI" = dataclasses.field(default=None) # type: ignore[assignment]
application_kwargs: dict[str, typing.Any] = dataclasses.field(default_factory=dict)
opentelemetry_excluded_urls: list[str] = dataclasses.field(default_factory=list)
prometheus_instrumentator_params: dict[str, typing.Any] = dataclasses.field(default_factory=dict)
prometheus_instrument_params: dict[str, typing.Any] = dataclasses.field(default_factory=dict)
prometheus_expose_params: dict[str, typing.Any] = dataclasses.field(default_factory=dict)

def __post_init__(self) -> None:
if not self.application:
object.__setattr__(self, "application", fastapi.FastAPI(docs_url=self.swagger_path))
object.__setattr__(
self, "application", fastapi.FastAPI(docs_url=self.swagger_path, **self.application_kwargs)
)
elif self.application_kwargs:
warnings.warn("application_kwargs must be used without application", stacklevel=2)

self.application.title = self.service_name
self.application.debug = self.service_debug
self.application.version = self.service_version


@dataclasses.dataclass(kw_only=True, slots=True, frozen=True)
Expand Down Expand Up @@ -182,9 +191,6 @@ async def lifespan_manager(self, _: "fastapi.FastAPI") -> typing.AsyncIterator[d

def __init__(self, bootstrap_config: FastAPIConfig) -> None:
super().__init__(bootstrap_config)
self.bootstrap_config.application.title = bootstrap_config.service_name
self.bootstrap_config.application.debug = bootstrap_config.service_debug
self.bootstrap_config.application.version = bootstrap_config.service_version

old_lifespan_manager = self.bootstrap_config.application.router.lifespan_context
self.bootstrap_config.application.router.lifespan_context = _merge_lifespan_context(
Expand Down
5 changes: 5 additions & 0 deletions tests/test_fastapi_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ def test_fastapi_bootstrapper_docs_url_differ(fastapi_config: FastAPIConfig) ->
bootstrapper.bootstrap()


def test_fastapi_bootstrapper_apps_and_kwargs_warning(fastapi_config: FastAPIConfig) -> None:
with pytest.warns(UserWarning, match="application_kwargs must be used without application"):
dataclasses.replace(fastapi_config, application=fastapi.FastAPI(), application_kwargs={"title": "some title"})


@pytest.mark.parametrize(
"package_name",
[
Expand Down
Loading
0