From 0c3527b8b278f3f3470f2fc9af2e77186021ebf7 Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Mon, 12 Dec 2022 12:20:55 +0200 Subject: [PATCH] Cleanup typing --- sanic/application/ext.py | 2 +- sanic/mixins/startup.py | 3 ++- tests/test_app.py | 10 +++++----- tests/test_config.py | 18 +++++++++++++++++- tests/test_logging.py | 19 +++++++++---------- 5 files changed, 34 insertions(+), 18 deletions(-) diff --git a/sanic/application/ext.py b/sanic/application/ext.py index 405d0a7f..54206c24 100644 --- a/sanic/application/ext.py +++ b/sanic/application/ext.py @@ -33,7 +33,7 @@ def setup_ext(app: Sanic, *, fail: bool = False, **kwargs): return if not getattr(app, "_ext", None): - Ext: Extend = getattr(sanic_ext, "Extend") + Ext = getattr(sanic_ext, "Extend") app._ext = Ext(app, **kwargs) return app.ext diff --git a/sanic/mixins/startup.py b/sanic/mixins/startup.py index 43c1aaac..4be6c181 100644 --- a/sanic/mixins/startup.py +++ b/sanic/mixins/startup.py @@ -41,6 +41,7 @@ from sanic.application.motd import MOTD from sanic.application.state import ApplicationServerInfo, Mode, ServerStage from sanic.base.meta import SanicMeta from sanic.compat import OS_IS_WINDOWS, is_atty +from sanic.constants import RestartOrder from sanic.exceptions import ServerKilled from sanic.helpers import Default from sanic.http.constants import HTTP @@ -814,7 +815,7 @@ class StartupMixin(metaclass=SanicMeta): cls._get_context(), (monitor_pub, monitor_sub), worker_state, - primary.config.RESTART_ORDER, + cast(RestartOrder, primary.config.RESTART_ORDER), ) if cls.should_auto_reload(): reload_dirs: Set[Path] = primary.state.reload_dirs.union( diff --git a/tests/test_app.py b/tests/test_app.py index b6cf98e3..f5eec8e2 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -349,11 +349,11 @@ def test_get_app_does_not_exist(): with pytest.raises( SanicException, match="Sanic app name 'does-not-exist' not found.\n" - "App instantiation must occur outside " - "if __name__ == '__main__' " - "block or by using an AppLoader.\nSee " - "https://sanic.dev/en/guide/deployment/app-loader.html" - " for more details." + "App instantiation must occur outside " + "if __name__ == '__main__' " + "block or by using an AppLoader.\nSee " + "https://sanic.dev/en/guide/deployment/app-loader.html" + " for more details.", ): Sanic.get_app("does-not-exist") diff --git a/tests/test_config.py b/tests/test_config.py index a702bc88..9b2ab719 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -14,7 +14,7 @@ from pytest import MonkeyPatch from sanic import Sanic from sanic.config import DEFAULT_CONFIG, Config -from sanic.constants import LocalCertCreator +from sanic.constants import LocalCertCreator, RestartOrder from sanic.exceptions import PyFileError @@ -436,3 +436,19 @@ def test_convert_local_cert_creator(passed, expected): app = Sanic("Test") assert app.config.LOCAL_CERT_CREATOR is expected del os.environ["SANIC_LOCAL_CERT_CREATOR"] + + +@pytest.mark.parametrize( + "passed,expected", + ( + ("shutdown_first", RestartOrder.SHUTDOWN_FIRST), + ("startup_first", RestartOrder.STARTUP_FIRST), + ("SHUTDOWN_FIRST", RestartOrder.SHUTDOWN_FIRST), + ("STARTUP_FIRST", RestartOrder.STARTUP_FIRST), + ), +) +def test_convert_restart_order(passed, expected): + os.environ["SANIC_RESTART_ORDER"] = passed + app = Sanic("Test") + assert app.config.RESTART_ORDER is expected + del os.environ["SANIC_RESTART_ORDER"] diff --git a/tests/test_logging.py b/tests/test_logging.py index 23a7d9a3..3a7ba4f5 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -10,8 +10,7 @@ import pytest import sanic from sanic import Sanic -from sanic.log import Colors -from sanic.log import LOGGING_CONFIG_DEFAULTS, logger +from sanic.log import LOGGING_CONFIG_DEFAULTS, Colors, logger from sanic.response import text @@ -254,11 +253,11 @@ def test_verbosity(app, caplog, app_verbosity, log_verbosity, exists): def test_colors_enum_format(): - assert f'{Colors.END}' == Colors.END.value - assert f'{Colors.BOLD}' == Colors.BOLD.value - assert f'{Colors.BLUE}' == Colors.BLUE.value - assert f'{Colors.GREEN}' == Colors.GREEN.value - assert f'{Colors.PURPLE}' == Colors.PURPLE.value - assert f'{Colors.RED}' == Colors.RED.value - assert f'{Colors.SANIC}' == Colors.SANIC.value - assert f'{Colors.YELLOW}' == Colors.YELLOW.value + assert f"{Colors.END}" == Colors.END.value + assert f"{Colors.BOLD}" == Colors.BOLD.value + assert f"{Colors.BLUE}" == Colors.BLUE.value + assert f"{Colors.GREEN}" == Colors.GREEN.value + assert f"{Colors.PURPLE}" == Colors.PURPLE.value + assert f"{Colors.RED}" == Colors.RED.value + assert f"{Colors.SANIC}" == Colors.SANIC.value + assert f"{Colors.YELLOW}" == Colors.YELLOW.value