From c78b246b3fa4ab62d7a6b5f28cca0f0f17548cbb Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Sun, 19 Dec 2021 23:20:02 +0200 Subject: [PATCH] Move cast registry to instance variable --- sanic/config.py | 6 +++--- tests/test_config.py | 4 +--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/sanic/config.py b/sanic/config.py index 84d1d9bb93..462adb9b5b 100644 --- a/sanic/config.py +++ b/sanic/config.py @@ -71,7 +71,6 @@ def add(self, cast: Callable[[str], Any]) -> None: class Config(dict): - __registry__ = CastRegistry((int, float, str_to_bool, str)) ACCESS_LOG: bool AUTO_RELOAD: bool @@ -112,6 +111,7 @@ def __init__( defaults = defaults or {} super().__init__({**DEFAULT_CONFIG, **defaults}) + self._cast_registry = CastRegistry((int, float, str_to_bool, str)) self._app = app self._LOGO = "" @@ -238,7 +238,7 @@ def __init__(self, name) -> None: _, config_key = key.split(prefix, 1) - for converter in self.__registry__: + for converter in self._cast_registry: try: self[config_key] = converter(value) break @@ -321,4 +321,4 @@ def register_type(self, *cast: Callable[[str], Any]) -> None: correct type. """ for item in cast: - self.__registry__.add(item) + self._cast_registry.add(item) diff --git a/tests/test_config.py b/tests/test_config.py index 5ad137b249..41e36f105a 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -152,7 +152,6 @@ def test_env_w_custom_converter(): assert isinstance(app.config.TEST_ANSWER, UltimateAnswer) assert app.config.TEST_ANSWER.answer == 42 del environ["SANIC_TEST_ANSWER"] - config.__registry__.remove(UltimateAnswer) def test_add_converter_multiple_times(caplog): @@ -166,8 +165,7 @@ def converter(): config.register_type(converter) assert ("sanic.error", logging.WARNING, message) in caplog.record_tuples - assert len(config.__registry__) == 5 - config.__registry__.remove(converter) + assert len(config._cast_registry) == 5 def test_load_from_file(app):