Skip to content
Open
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
21 changes: 21 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)],
Expand Down Expand Up @@ -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"),
[
Expand Down
4 changes: 3 additions & 1 deletion uvicorn/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions uvicorn/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
Loading