Skip to content

Commit

Permalink
Move cast registry to instance variable
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins committed Dec 19, 2021
1 parent eb843c6 commit c78b246
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
6 changes: 3 additions & 3 deletions sanic/config.py
Expand Up @@ -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
Expand Down Expand Up @@ -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 = ""

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
4 changes: 1 addition & 3 deletions tests/test_config.py
Expand Up @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit c78b246

Please sign in to comment.