Skip to content

Commit 14b261e

Browse files
authored
Merge pull request #35 from modern-python/fastapi-kwargs
allow additional kwargs for fastapi
2 parents 3461df8 + 7a159c2 commit 14b261e

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

lite_bootstrap/bootstrappers/fastapi_bootstrapper.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,23 @@ class FastAPIConfig(
3939
CorsConfig, HealthChecksConfig, LoggingConfig, OpentelemetryConfig, PrometheusConfig, SentryConfig, SwaggerConfig
4040
):
4141
application: "fastapi.FastAPI" = dataclasses.field(default=None) # type: ignore[assignment]
42+
application_kwargs: dict[str, typing.Any] = dataclasses.field(default_factory=dict)
4243
opentelemetry_excluded_urls: list[str] = dataclasses.field(default_factory=list)
4344
prometheus_instrumentator_params: dict[str, typing.Any] = dataclasses.field(default_factory=dict)
4445
prometheus_instrument_params: dict[str, typing.Any] = dataclasses.field(default_factory=dict)
4546
prometheus_expose_params: dict[str, typing.Any] = dataclasses.field(default_factory=dict)
4647

4748
def __post_init__(self) -> None:
4849
if not self.application:
49-
object.__setattr__(self, "application", fastapi.FastAPI(docs_url=self.swagger_path))
50+
object.__setattr__(
51+
self, "application", fastapi.FastAPI(docs_url=self.swagger_path, **self.application_kwargs)
52+
)
53+
elif self.application_kwargs:
54+
warnings.warn("application_kwargs must be used without application", stacklevel=2)
55+
56+
self.application.title = self.service_name
57+
self.application.debug = self.service_debug
58+
self.application.version = self.service_version
5059

5160

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

183192
def __init__(self, bootstrap_config: FastAPIConfig) -> None:
184193
super().__init__(bootstrap_config)
185-
self.bootstrap_config.application.title = bootstrap_config.service_name
186-
self.bootstrap_config.application.debug = bootstrap_config.service_debug
187-
self.bootstrap_config.application.version = bootstrap_config.service_version
188194

189195
old_lifespan_manager = self.bootstrap_config.application.router.lifespan_context
190196
self.bootstrap_config.application.router.lifespan_context = _merge_lifespan_context(

tests/test_fastapi_bootstrap.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ def test_fastapi_bootstrapper_docs_url_differ(fastapi_config: FastAPIConfig) ->
7171
bootstrapper.bootstrap()
7272

7373

74+
def test_fastapi_bootstrapper_apps_and_kwargs_warning(fastapi_config: FastAPIConfig) -> None:
75+
with pytest.warns(UserWarning, match="application_kwargs must be used without application"):
76+
dataclasses.replace(fastapi_config, application=fastapi.FastAPI(), application_kwargs={"title": "some title"})
77+
78+
7479
@pytest.mark.parametrize(
7580
"package_name",
7681
[

0 commit comments

Comments
 (0)