diff --git a/tests/test_config.py b/tests/test_config.py index fdbf275a42..773be2113e 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,4 +1,5 @@ import json +import logging import socket from copy import deepcopy @@ -100,7 +101,7 @@ def test_app_unimportable_other(caplog): ) -def test_app_factory(): +def test_app_factory(caplog): def create_app(): return asgi_app @@ -108,10 +109,15 @@ def create_app(): config.load() assert config.loaded_app is asgi_app - # Flag missing. - config = Config(app=create_app) - with pytest.raises(SystemExit): + # Flag not passed. In this case, successfully load the app, but issue a warning + # to indicate that an explicit flag is preferred. + caplog.clear() + config = Config(app=create_app, proxy_headers=False) + with caplog.at_level(logging.WARNING): config.load() + assert config.loaded_app is asgi_app + assert len(caplog.records) == 1 + assert "--factory" in caplog.records[0].message # App not a no-arguments callable. config = Config(app=asgi_app, factory=True) diff --git a/uvicorn/config.py b/uvicorn/config.py index 62fe3c5d0f..5a4845d87d 100644 --- a/uvicorn/config.py +++ b/uvicorn/config.py @@ -310,18 +310,18 @@ def load(self): logger.error("Error loading ASGI app. %s" % exc) sys.exit(1) - if self.factory: - try: - self.loaded_app = self.loaded_app() - except TypeError as exc: + try: + self.loaded_app = self.loaded_app() + except TypeError as exc: + if self.factory: logger.error("Error loading ASGI app factory: %s", exc) sys.exit(1) - elif not inspect.signature(self.loaded_app).parameters: - logger.error( - "APP seems to be an application factory. " - "Run uvicorn with the --factory flag." - ) - sys.exit(1) + else: + if not self.factory: + logger.warning( + "ASGI factory detected. Using it, " + "but please consider setting the --factory flag explicitly." + ) if self.interface == "auto": if inspect.isclass(self.loaded_app):