Make get_app name optional (#2053)

This commit is contained in:
Adam Hopkins
2021-03-11 16:27:56 +02:00
committed by GitHub
parent 400f54c7ec
commit d76925cf35
2 changed files with 63 additions and 7 deletions

View File

@@ -1182,10 +1182,21 @@ class Sanic(BaseSanic):
cls._app_registry[name] = app
@classmethod
def get_app(cls, name: str, *, force_create: bool = False) -> "Sanic":
def get_app(
cls, name: Optional[str] = None, *, force_create: bool = False
) -> "Sanic":
"""
Retrieve an instantiated Sanic instance
"""
if name is None:
if len(cls._app_registry) > 1:
raise SanicException(
'Multiple Sanic apps found, use Sanic.get_app("app_name")'
)
elif len(cls._app_registry) == 0:
raise SanicException(f"No Sanic apps have been registered.")
else:
return list(cls._app_registry.values())[0]
try:
return cls._app_registry[name]
except KeyError: