Skip to content

Commit

Permalink
Tweak detection of app factories (#914)
Browse files Browse the repository at this point in the history
  • Loading branch information
florimondmanca committed Dec 26, 2020
1 parent f8dd9fb commit afb2d56
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
14 changes: 10 additions & 4 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import logging
import socket
from copy import deepcopy

Expand Down Expand Up @@ -100,18 +101,23 @@ def test_app_unimportable_other(caplog):
)


def test_app_factory():
def test_app_factory(caplog):
def create_app():
return asgi_app

config = Config(app=create_app, factory=True, proxy_headers=False)
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)
Expand Down
20 changes: 10 additions & 10 deletions uvicorn/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 app factory detected. Using it, "
"but please consider setting the --factory flag explicitly."
)

if self.interface == "auto":
if inspect.isclass(self.loaded_app):
Expand Down

0 comments on commit afb2d56

Please sign in to comment.