Skip to content

Commit

Permalink
Fixed most of the lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
xfenix committed Sep 26, 2023
1 parent 2d79bd5 commit 55facd1
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 48 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ lint:
ruff .
mypy .
vulture whole_app --min-confidence 100
isort -c .
black . --check

lint-in-docker:
Expand Down
74 changes: 73 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 8 additions & 12 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,23 @@ vulture = "*"
types-toml = "*"
pytest-repeat = "*"
ruff = "*"

[tool.isort]
line_length = 120
multi_line_output = 3
include_trailing_comma = true
lines_after_imports = 2
lexicographical = true
sections = ["FUTURE", "STDLIB", "FIRSTPARTY", "THIRDPARTY", "LOCALFOLDER"]
no_lines_before = ["STDLIB", "THIRDPARTY"]
known_third_party = []
known_local_folder = ["whole_app"]
black = "*"

[tool.black]
line-length = 120

[tool.ruff]
fix = true
line-length = 120
fix = true
select = ["ALL"]
ignore = ["D1", "D203", "D213", "FA102", "I", "ANN101"]

[tool.ruff.isort]
lines-after-imports = 2
no-lines-before = ["standard-library", "local-folder"]
known-third-party = []
known-local-folder = ["whole_app"]

[tool.ruff.extend-per-file-ignores]
"tests/*.py" = [
"ANN001",
Expand Down
17 changes: 13 additions & 4 deletions scripts/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@


def _update_dockerhub_readme() -> None:
new_content: str = re.sub(r"\#\# Development.*", r"", README_PATH.read_text(), flags=re.I | re.S).strip()
new_content: str = re.sub(
r"\#\# Development.*",
r"",
README_PATH.read_text(),
flags=re.I | re.S,
).strip()
new_content = replace_tag_in_readme(new_content, parse_last_git_tag())
README_PATH.write_text(new_content + "\n")

Expand All @@ -23,8 +28,8 @@ def _update_readme() -> None:
from whole_app.settings import SETTINGS

new_content: str = README_PATH.read_text()
settings_schema: dict = SETTINGS.schema()["properties"]
pack_of_readme_lines: list = []
settings_schema: dict[str, typing.Any] = SETTINGS.schema()["properties"]
pack_of_readme_lines: list[str] = []
for props in settings_schema.values():
settings_env_key: str = props["env_names"].pop().upper()
if "description" not in props:
Expand All @@ -37,7 +42,11 @@ def _update_readme() -> None:
""
if "exclusiveMinimum" not in props
else f", allowed values from `{props['exclusiveMinimum'] + 1}`"
+ (f"to `{props['exclusiveMaximum'] - 1}`" if "exclusiveMaximum" in props else "")
+ (
f"to `{props['exclusiveMaximum'] - 1}`"
if "exclusiveMaximum" in props
else ""
)
)
pack_of_readme_lines.append(
f'`{settings_env_key}` {props["description"].rstrip(".")}. '
Expand Down
3 changes: 0 additions & 3 deletions scripts/_helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
"""Helper functions."""
import os
import re
import shlex
Expand All @@ -7,7 +6,6 @@


def parse_last_git_tag() -> str:
"""Return last git tag (works in CI and on localhost)."""
last_tag_from_environment: typing.Final[str | None] = os.getenv("GITHUB_REF_NAME")
if last_tag_from_environment is None:
git_tags_list: typing.Final = shlex.split(
Expand All @@ -29,7 +27,6 @@ def parse_last_git_tag() -> str:


def replace_tag_in_readme(readme_text: str, new_tag: str) -> str:
"""Place new tag in README text."""
return re.sub(
r"(xfenix/spellcheck-microservice\:)(\d{1,}\.\d{1,}\.\d{1,})",
r"\g<1>" + new_tag,
Expand Down
3 changes: 0 additions & 3 deletions tests/test_dict_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# pylint: disable=redefined-outer-name, unspecified-encoding
"""Basic test for views."""
import importlib
import typing

Expand Down Expand Up @@ -103,8 +102,6 @@ def test_dummy_provider_init(


class TestVarious:
"""Various dict api things."""

def test_disabled_dictionary_views(
self: "TestVarious",
monkeypatch: typing.Any,
Expand Down
6 changes: 6 additions & 0 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ def set( # noqa: A003
) -> typing.Any:
"""Fake setter for «config» object."""

def load_config(self: "FakeGunicorn") -> None:
pass

def load(self: "FakeGunicorn") -> None:
pass

def run(self: "FakeGunicorn", *_: typing.Any, **__: typing.Any) -> typing.Any:
self.load_config()
self.load()
Expand Down
2 changes: 1 addition & 1 deletion whole_app/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


# pylint: disable=abstract-method
class GunicornCustomApplication(BaseApplication):
class GunicornCustomApplication(BaseApplication): # type: ignore[misc]
def load_config(self: "GunicornCustomApplication") -> None:
_options: dict[str, str | int] = {
"worker_class": "uvicorn.workers.UvicornWorker",
Expand Down
1 change: 1 addition & 0 deletions whole_app/auth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import typing

import fastapi
from fastapi.security.api_key import APIKeyHeader

Expand Down
8 changes: 2 additions & 6 deletions whole_app/dictionaries/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
"""Initalizatior of user dictionaries."""
from loguru import logger

from whole_app import misc_helpers
from whole_app.settings import SETTINGS, StorageProviders

from . import dummy as dummy_storage
from . import file as file_storage
from . import protocol
from whole_app import misc_helpers
from whole_app.settings import SETTINGS, StorageProviders


misc_helpers.init_logger()


def init_storage() -> None:
"""General storage initializer."""
if SETTINGS.dictionaries_storage_provider == StorageProviders.FILE:
file_storage.init_storage()
elif SETTINGS.dictionaries_storage_provider == StorageProviders.DUMMY:
Expand All @@ -24,7 +21,6 @@ def init_storage() -> None:


def prepare_storage_engine() -> protocol.UserDictProtocol:
"""Storage engine factory."""
if SETTINGS.dictionaries_storage_provider == StorageProviders.FILE:
return file_storage.FileProvider()
return dummy_storage.DummyProvider()
9 changes: 0 additions & 9 deletions whole_app/dictionaries/file.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,26 @@
"""Basic file provider."""
import aiopath

from whole_app.settings import SETTINGS


def init_storage() -> None:
"""Initialize dictionaries storage helper."""
SETTINGS.dictionaries_path.mkdir(
parents=True,
exist_ok=True,
) # pylint: disable=no-member


class FileProvider:
"""File based storage provider for user dictionaries."""

_user_dict_path: aiopath.AsyncPath

def prepare(self: "FileProvider", user_name: str) -> "FileProvider":
"""Prepare class for user name."""
self._user_dict_path = aiopath.AsyncPath(SETTINGS.dictionaries_path / user_name)
return self

async def _store_lines(self: "FileProvider", lines: list[str]) -> None:
"""Store lines to user dictionary."""
await self._user_dict_path.write_text("\n".join(lines) + "\n")

async def save_record(self: "FileProvider", exception_word: str) -> None:
"""Save record to user dictionary."""
await self._user_dict_path.touch()
clean_word: str = exception_word.strip().lower()
file_content: list[str] = await self.fetch_records()
Expand All @@ -36,14 +29,12 @@ async def save_record(self: "FileProvider", exception_word: str) -> None:
await self._store_lines(file_content)

async def remove_record(self: "FileProvider", exception_word: str) -> None:
"""Remove record from user dictionary."""
file_content: list[str] = await self.fetch_records()
if exception_word in file_content:
file_content.remove(exception_word)
await self._store_lines(file_content)

async def fetch_records(self: "FileProvider") -> list[str]:
"""Fetch records from user dictionary."""
if await self._user_dict_path.exists():
return [
one_line.strip()
Expand Down
4 changes: 1 addition & 3 deletions whole_app/settings.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# pylint: disable=no-member, no-self-argument
import enum
import pathlib
import typing
import toml

import pydantic
import toml
from loguru import logger
from pydantic_settings import BaseSettings

Expand Down Expand Up @@ -48,7 +47,6 @@ def _warn_about_empty_api_key(
def _parse_version_from_local_file(
default_value: str,
) -> str:
"""Parse version from pyproject (this file will be updated in the CI pipeline)."""
try:
pyproject_obj: dict[str, dict[str, dict[str, str]]] = toml.loads(
PATH_TO_PYPROJECT.read_text(),
Expand Down
5 changes: 0 additions & 5 deletions whole_app/spell.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
"""Spellcheck service functions."""
import typing

import pylru
Expand All @@ -14,8 +13,6 @@


class SpellCheckService:
"""Spellcheck service class."""

__slots__ = ("_input_text", "_spellcheck_engine", "_exclusion_words")

_input_text: str
Expand All @@ -35,7 +32,6 @@ def prepare(

@staticmethod
def get_memorized_suggestions(word_spellcheck_result: SpellChecker) -> list[str]:
"""Try to get suggestions from lru cache or ask SpellChecker for them."""
misspelled_suggestions: list[str]
if word_spellcheck_result.word in _MISSPELED_CACHE:
misspelled_suggestions = _MISSPELED_CACHE[word_spellcheck_result.word]
Expand All @@ -49,7 +45,6 @@ def get_memorized_suggestions(word_spellcheck_result: SpellChecker) -> list[str]
)

def run_check(self: "SpellCheckService") -> list[models.OneCorrection]:
"""Run main checking procedure."""
corrections_output: list[models.OneCorrection] = []
self._spellcheck_engine.set_text(self._input_text)
for one_result in self._spellcheck_engine:
Expand Down

0 comments on commit 55facd1

Please sign in to comment.