diff --git a/tests/conftest.py b/tests/conftest.py index d7d85b5..ebe0c96 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -16,7 +16,9 @@ def faker_obj() -> faker.Faker: @pytest.fixture(autouse=True) -def patch_file_provider_for_temp(monkeypatch) -> typing.Generator[None, None, None]: +def patch_file_provider_for_temp( + monkeypatch: typing.Any, +) -> typing.Generator[None, None, None]: """Patch settings, to rewrite dict path to temporary directory.""" with monkeypatch.context() as patcher, tempfile.TemporaryDirectory() as tmp_dir_name: yield patcher.setattr(SETTINGS, "dictionaries_path", pathlib.Path(tmp_dir_name)) diff --git a/tests/test_dict_views.py b/tests/test_dict_views.py index 7dff502..5125418 100644 --- a/tests/test_dict_views.py +++ b/tests/test_dict_views.py @@ -9,9 +9,6 @@ from whole_app import models, views from whole_app.settings import SETTINGS, StorageProviders -if typing.TYPE_CHECKING: - from requests.models import Response as RequestsResponse - DICT_ENDPOINT: typing.Final = f"{SETTINGS.api_prefix}/dictionaries/" @@ -44,7 +41,7 @@ def test_add_to_dict( fake_user_name, ) ) - server_response: RequestsResponse = app_client.post( + server_response = app_client.post( DICT_ENDPOINT, json=models.UserDictionaryRequestWithWord( user_name=fake_user_name, @@ -73,7 +70,7 @@ def test_remove_from_user_dict( path_to_dict_file.write_text(fake_exc_word) if SETTINGS.dictionaries_storage_provider == StorageProviders.FILE: assert fake_exc_word in path_to_dict_file.read_text() - server_response: RequestsResponse = app_client.delete( + server_response = app_client.delete( DICT_ENDPOINT, json=models.UserDictionaryRequestWithWord( user_name=fake_user_name, @@ -95,7 +92,7 @@ def test_dummy_provider_init( "dictionaries_storage_provider", StorageProviders.DUMMY, ) - server_response: RequestsResponse = app_client.post( + server_response = app_client.post( DICT_ENDPOINT, json=models.UserDictionaryRequestWithWord( user_name=faker_obj.user_name(), @@ -116,7 +113,7 @@ def test_disabled_dictionary_views( with monkeypatch.context() as patcher: patcher.setattr(SETTINGS, "dictionaries_disabled", True) importlib.reload(views) - server_response: RequestsResponse = TestClient(views.SPELL_APP).post( + server_response = TestClient(views.SPELL_APP).post( DICT_ENDPOINT, json=models.UserDictionaryRequestWithWord( user_name="test", @@ -129,7 +126,7 @@ def test_disabled_dictionary_views( @pytest.mark.parametrize("api_key", [None, ""]) def test_empty_auth_key(self: "TestVarious", api_key: str) -> None: - server_response: RequestsResponse = TestClient(views.SPELL_APP).post( + server_response = TestClient(views.SPELL_APP).post( DICT_ENDPOINT, json=models.UserDictionaryRequestWithWord( user_name="test", @@ -140,7 +137,7 @@ def test_empty_auth_key(self: "TestVarious", api_key: str) -> None: assert server_response.status_code == 403 def test_wrong_api_key(self: "TestVarious") -> None: - server_response: RequestsResponse = TestClient(views.SPELL_APP).post( + server_response = TestClient(views.SPELL_APP).post( DICT_ENDPOINT, json=models.UserDictionaryRequestWithWord( user_name="test", diff --git a/tests/test_misc.py b/tests/test_misc.py index 57a0f54..54d6566 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -12,13 +12,12 @@ from whole_app.settings import SETTINGS, SettingsOfMicroservice if TYPE_CHECKING: - from requests.models import Response as RequestsResponse import faker def test_main_py(monkeypatch: typing.Any) -> None: class FakeGunicorn: - def __init__(self: "FakeGunicorn", *_, **__) -> None: + def __init__(self: "FakeGunicorn", *_: typing.Any, **__: typing.Any) -> None: """Init.""" @property @@ -32,10 +31,14 @@ def settings(self: "FakeGunicorn") -> dict[str, None | int]: "workers": 666_13, } - def set(self: "FakeGunicorn", _, __) -> typing.Any: # noqa: A003 + def set( # noqa: A003 + self: "FakeGunicorn", + _: typing.Any, + __: typing.Any, + ) -> typing.Any: """Fake setter for «config» object.""" - def run(self: "FakeGunicorn", *_, **__) -> typing.Any: + def run(self: "FakeGunicorn", *_: typing.Any, **__: typing.Any) -> typing.Any: self.load_config() self.load() @@ -63,7 +66,7 @@ def test_sentry_integration(monkeypatch: typing.Any, faker_obj: "faker.Faker") - patcher.setattr(SETTINGS, "sentry_dsn", f"https://{faker_obj.pystr()}") patcher.setattr("sentry_sdk.init", lambda **_: None) importlib.reload(views) - server_response: RequestsResponse = TestClient(views.SPELL_APP).get( + server_response = TestClient(views.SPELL_APP).get( f"{SETTINGS.api_prefix}/health/", ) assert server_response.status_code == 200 diff --git a/tests/test_spell_views.py b/tests/test_spell_views.py index 7a8f84d..61dfae9 100644 --- a/tests/test_spell_views.py +++ b/tests/test_spell_views.py @@ -9,9 +9,9 @@ from whole_app.settings import SETTINGS, StorageProviders from ._fixtures import BAD_PAYLOAD +from requests.models import Response as RequestsResponse if typing.TYPE_CHECKING: - from requests.models import Response as RequestsResponse from fastapi.testclient import TestClient import faker @@ -91,7 +91,7 @@ def run_request() -> typing.Any: ).dict(), ) - def parse_words(server_response) -> typing.Any: + def parse_words(server_response: RequestsResponse) -> typing.Any: return [item["word"] for item in server_response.json()["corrections"]] user_name: typing.Final[str] = faker_obj.user_name() diff --git a/whole_app/__main__.py b/whole_app/__main__.py index f38ca49..768e767 100644 --- a/whole_app/__main__.py +++ b/whole_app/__main__.py @@ -12,7 +12,7 @@ # pylint: disable=abstract-method class GunicornCustomApplication(BaseApplication): - def load_config(self) -> None: + def load_config(self: "GunicornCustomApplication") -> None: _options: dict[str, str | int] = { "worker_class": "uvicorn.workers.UvicornWorker", "bind": f"0.0.0.0:{SETTINGS.port}", @@ -22,7 +22,7 @@ def load_config(self) -> None: if key in self.cfg.settings and value is not None: self.cfg.set(key.lower(), value) - def load(self) -> fastapi.FastAPI: + def load(self: "GunicornCustomApplication") -> fastapi.FastAPI: return SPELL_APP diff --git a/whole_app/models.py b/whole_app/models.py index 4d5f7d5..05fd3e0 100644 --- a/whole_app/models.py +++ b/whole_app/models.py @@ -7,15 +7,6 @@ from .settings import SETTINGS, AvailableLanguages, AvailableLanguagesType -class _RequestWithUserName(pydantic.BaseModel): - user_name: str | None = pydantic.Field( - example="username", - regex=SETTINGS.username_regex, - min_length=SETTINGS.username_min_length, - max_length=SETTINGS.username_max_length, - ) - - class OneCorrection(pydantic.BaseModel): first_position: int last_position: int @@ -23,9 +14,16 @@ class OneCorrection(pydantic.BaseModel): suggestions: set[str] -class SpellCheckRequest(_RequestWithUserName): +class SpellCheckRequest(pydantic.BaseModel): text: str = pydantic.Field(..., example="Привед как дила") language: AvailableLanguagesType + user_name: str | None = pydantic.Field( + None, + example="username", + regex=SETTINGS.username_regex, + min_length=SETTINGS.username_min_length, + max_length=SETTINGS.username_max_length, + ) class SpellCheckResponse(pydantic.BaseModel): @@ -34,8 +32,13 @@ class SpellCheckResponse(pydantic.BaseModel): corrections: list[OneCorrection] -class UserDictionaryRequest(_RequestWithUserName): - pass +class UserDictionaryRequest(pydantic.BaseModel): + user_name: str = pydantic.Field( + example="username", + regex=SETTINGS.username_regex, + min_length=SETTINGS.username_min_length, + max_length=SETTINGS.username_max_length, + ) class UserDictionaryRequestWithWord(UserDictionaryRequest):