Skip to content

Commit

Permalink
More annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
xfenix committed Sep 25, 2023
1 parent 1870cbd commit 7fb5a69
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 31 deletions.
4 changes: 3 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
15 changes: 6 additions & 9 deletions tests/test_dict_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/"

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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(),
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
13 changes: 8 additions & 5 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/test_spell_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions whole_app/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand All @@ -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


Expand Down
27 changes: 15 additions & 12 deletions whole_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,23 @@
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
word: str
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):
Expand All @@ -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):
Expand Down

0 comments on commit 7fb5a69

Please sign in to comment.