From 53d3f4d2f05f04837f92a3d767446cf95369e9e7 Mon Sep 17 00:00:00 2001 From: Nikita Kozlovtsev Date: Tue, 16 Jul 2024 13:17:16 +0300 Subject: [PATCH] CR fixes --- README.md | 4 +--- tests/test_spell.py | 2 +- whole_app/settings.py | 22 ++++++++++++++-------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 4f41043..bbdc2a3 100644 --- a/README.md +++ b/README.md @@ -35,9 +35,7 @@ You can change config of the service by changing the environment variables. Here * `SPELLCHECK_DICTIONARIES_DISABLED` switches off user dictionaries API no matter what. Default value is `False`. * `SPELLCHECK_USERNAME_MIN_LENGTH` minimum length of username. Default value is `3`. * `SPELLCHECK_USERNAME_MAX_LENGTH` maximum length of username. Default value is `60`. -* `SPELLCHECK_EXCLUSION_WORDS_STR` list of words which will ignored by default(string separated by comma). Example: 'foo, bar'. Default value is empty string. -* `SPELLCHECK_EXCLUSION_WORDS_SET` set of words which will ignored by default(filled from exclusion_words_str). - Example: '["foo", "bar"]' . Default value is `set()`. +* `SPELLCHECK_EXCLUSION_WORDS_STR` String with list of words which will be ignored in /api/check endpoint each request. Example: `'foo, bar'`. Default value is empty string. ### Deployment Note: all docker & docker-compose variants use named volumes to store user dictionaries. diff --git a/tests/test_spell.py b/tests/test_spell.py index af4f035..b5a9510 100644 --- a/tests/test_spell.py +++ b/tests/test_spell.py @@ -60,7 +60,7 @@ def test_default_excluded_words( monkeypatch: typing.Any, ) -> None: with monkeypatch.context() as patcher: - patcher.setattr(SETTINGS, "exclusion_words_set", excluded_words) + patcher.setattr(SETTINGS, "_exclusion_words_set", excluded_words) fake_engine: SpellCheckService = SpellCheckService() prepared = fake_engine.prepare( models.SpellCheckRequest(text=wannabe_user_input, language=RU_LANG, exclude_urls=False), diff --git a/whole_app/settings.py b/whole_app/settings.py index a1258e0..d682e50 100644 --- a/whole_app/settings.py +++ b/whole_app/settings.py @@ -6,6 +6,7 @@ import structlog import toml import typing_extensions +from pydantic import computed_field from pydantic_settings import BaseSettings @@ -199,23 +200,28 @@ class SettingsOfMicroservice(BaseSettings): exclusion_words_str: typing.Annotated[ str, pydantic.Field( - description="list of words which will ignored by default(string separated by comma). " "Example: 'foo, bar'" + description="String with list of words which will be ignored in /api/check endpoint each request. " + "Example: `'foo, bar'`" ), ] = "" - exclusion_words_set: typing.Annotated[ + _exclusion_words_set: typing.Annotated[ set[str], pydantic.Field( description="""set of words which will ignored by default(filled from exclusion_words_str). - Example: '["foo", "bar"]' """, + Example: `'["foo", "bar"]'` """, ), ] = set() + @computed_field # type: ignore[misc] + @property + def exclusion_words_set(self) -> set[str]: + return self._exclusion_words_set + @pydantic.model_validator(mode="after") - def assemble_exclusion_words_set(self) -> "typing_extensions.Self": - if not self.exclusion_words_set: - self.exclusion_words_set = { - one_word.strip().lower() for one_word in self.exclusion_words_str.split(",") if one_word - } + def _assemble_exclusion_words_set(self) -> "typing_extensions.Self": + self._exclusion_words_set = { + one_word.strip().lower() for one_word in self.exclusion_words_str.split(",") if one_word + } return self class Config: