11import typing
22
33import fastapi
4- import typing_extensions
54from fastapi .middleware .cors import CORSMiddleware
65from fastapi_offline_docs import enable_offline_docs
76from health_checks .fastapi_healthcheck import build_fastapi_health_check_router
2221from microbootstrap .settings import FastApiSettings
2322
2423
24+ ApplicationT = typing .TypeVar ("ApplicationT" , bound = fastapi .FastAPI )
25+
26+
2527class FastApiBootstrapper (
2628 ApplicationBootstrapper [FastApiSettings , fastapi .FastAPI , FastApiConfig ],
2729):
2830 application_config = FastApiConfig ()
2931 application_type = fastapi .FastAPI
3032
31- def bootstrap_before (self : typing_extensions . Self ) -> dict [str , typing .Any ]:
33+ def bootstrap_before (self ) -> dict [str , typing .Any ]:
3234 return {
3335 "debug" : self .settings .service_debug ,
3436 "on_shutdown" : [self .teardown ],
@@ -57,15 +59,15 @@ def bootstrap_before(self) -> dict[str, typing.Any]:
5759 "version" : self .instrument_config .service_version ,
5860 }
5961
60- def bootstrap_after (self , application : fastapi . FastAPI ) -> fastapi . FastAPI :
62+ def bootstrap_after (self , application : ApplicationT ) -> ApplicationT :
6163 if self .instrument_config .swagger_offline_docs :
6264 enable_offline_docs (application , static_files_handler = self .instrument_config .service_static_path )
6365 return application
6466
6567
6668@FastApiBootstrapper .use_instrument ()
6769class FastApiCorsInstrument (CorsInstrument ):
68- def bootstrap_after (self , application : fastapi . FastAPI ) -> fastapi . FastAPI :
70+ def bootstrap_after (self , application : ApplicationT ) -> ApplicationT :
6971 application .add_middleware (
7072 CORSMiddleware ,
7173 allow_origins = self .instrument_config .cors_allowed_origins ,
@@ -81,7 +83,7 @@ def bootstrap_after(self, application: fastapi.FastAPI) -> fastapi.FastAPI:
8183
8284@FastApiBootstrapper .use_instrument ()
8385class FastApiOpentelemetryInstrument (OpentelemetryInstrument ):
84- def bootstrap_after (self , application : fastapi . FastAPI ) -> fastapi . FastAPI :
86+ def bootstrap_after (self , application : ApplicationT ) -> ApplicationT :
8587 FastAPIInstrumentor .instrument_app (
8688 application ,
8789 tracer_provider = self .tracer_provider ,
@@ -92,16 +94,16 @@ def bootstrap_after(self, application: fastapi.FastAPI) -> fastapi.FastAPI:
9294
9395@FastApiBootstrapper .use_instrument ()
9496class FastApiLoggingInstrument (LoggingInstrument ):
95- def bootstrap_after (self , application : fastapi . FastAPI ) -> fastapi . FastAPI :
96- application .add_middleware (
97- build_fastapi_logging_middleware (self .instrument_config .logging_exclude_endpoints ),
97+ def bootstrap_after (self , application : ApplicationT ) -> ApplicationT :
98+ application .add_middleware ( # type: ignore[call-arg]
99+ build_fastapi_logging_middleware (self .instrument_config .logging_exclude_endpoints ), # type: ignore[arg-type]
98100 )
99101 return application
100102
101103
102104@FastApiBootstrapper .use_instrument ()
103105class FastApiPrometheusInstrument (PrometheusInstrument [FastApiPrometheusConfig ]):
104- def bootstrap_after (self , application : fastapi . FastAPI ) -> fastapi . FastAPI :
106+ def bootstrap_after (self , application : ApplicationT ) -> ApplicationT :
105107 Instrumentator (** self .instrument_config .prometheus_instrumentator_params ).instrument (
106108 application ,
107109 ** self .instrument_config .prometheus_instrument_params ,
@@ -120,7 +122,7 @@ def get_config_type(cls) -> type[FastApiPrometheusConfig]:
120122
121123@FastApiBootstrapper .use_instrument ()
122124class FastApiHealthChecksInstrument (HealthChecksInstrument ):
123- def bootstrap_after (self , application : fastapi . FastAPI ) -> fastapi . FastAPI :
125+ def bootstrap_after (self , application : ApplicationT ) -> ApplicationT :
124126 application .include_router (
125127 build_fastapi_health_check_router (
126128 health_check = self .health_check ,
0 commit comments