diff --git a/tests/test_config.py b/tests/test_config.py index 0ba903e5e..5927182df 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -49,6 +49,14 @@ def wsgi_app(environ: Environ, start_response: StartResponse) -> None: pass # pragma: nocover +def asgi_app_factory(**kwargs): + async def app(scope: Scope, receive: ASGIReceiveCallable, send: ASGISendCallable) -> None: + pass # pragma: nocover + + app.factory_kwargs = kwargs # type: ignore + return app + + @pytest.mark.parametrize( "app, expected_should_reload", [(asgi_app, False), ("tests.test_config:asgi_app", True)], @@ -542,6 +550,19 @@ def test_warn_when_using_reload_and_workers(caplog: pytest.LogCaptureFixture) -> assert '"workers" flag is ignored when reloading is enabled.' in caplog.records[0].message +def test_config_factory_kwargs(): + config = Config( + app="tests.test_config:asgi_app_factory", + factory_kwargs={"alpha": 12, "beta": [3, 4, 5]}, + factory=True, + workers=4, + proxy_headers=False, + ) + config.load() + + assert config.loaded_app.factory_kwargs == {"alpha": 12, "beta": [3, 4, 5]} + + @pytest.mark.parametrize( ("loop_type", "expected_loop_factory"), [ diff --git a/uvicorn/config.py b/uvicorn/config.py index 017ae769f..7e24c8312 100644 --- a/uvicorn/config.py +++ b/uvicorn/config.py @@ -225,6 +225,7 @@ def __init__( headers: list[tuple[str, str]] | None = None, factory: bool = False, h11_max_incomplete_event_size: int | None = None, + factory_kwargs: dict[str, Any] | None = None, ): self.app = app self.host = host @@ -270,6 +271,7 @@ def __init__( self.encoded_headers: list[tuple[bytes, bytes]] = [] self.factory = factory self.h11_max_incomplete_event_size = h11_max_incomplete_event_size + self.factory_kwargs = factory_kwargs or {} self.loaded = False self.configure_logging() @@ -439,7 +441,7 @@ def load(self) -> None: sys.exit(1) try: - self.loaded_app = self.loaded_app() + self.loaded_app = self.loaded_app(**self.factory_kwargs) except TypeError as exc: if self.factory: logger.error("Error loading ASGI app factory: %s", exc) diff --git a/uvicorn/main.py b/uvicorn/main.py index 3c8398998..f8f293436 100644 --- a/uvicorn/main.py +++ b/uvicorn/main.py @@ -512,6 +512,7 @@ def run( app_dir: str | None = None, factory: bool = False, h11_max_incomplete_event_size: int | None = None, + factory_kwargs: dict[str, Any] | None = None, ) -> None: if app_dir is not None: sys.path.insert(0, app_dir) @@ -563,6 +564,7 @@ def run( use_colors=use_colors, factory=factory, h11_max_incomplete_event_size=h11_max_incomplete_event_size, + factory_kwargs=factory_kwargs, ) server = Server(config=config)