Skip to content

Commit

Permalink
⚡ [refactoring/linting]: bring new linters, refactor the code using m…
Browse files Browse the repository at this point in the history
…ultiple linters
  • Loading branch information
GLEF1X committed Dec 20, 2022
1 parent fcb9a93 commit 249f20b
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 82 deletions.
26 changes: 0 additions & 26 deletions .coveragerc

This file was deleted.

22 changes: 22 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
root = true

[**]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
max_line_length = 99

[**.{yml,yaml}]
indent_size = 2

[**.json]
indent_size = 2

[**.{md,txt,rst}]
trim_trailing_whitespace = false

[Makefile]
indent_style = tab
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,5 @@ dmypy.json

docker_test/*
docker_test

.ruff_cache/*
42 changes: 28 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: "trailing-whitespace"
- id: "check-case-conflict"
- id: "check-merge-conflict"
- id: "debug-statements"
- id: "end-of-file-fixer"
- id: "mixed-line-ending"
- id: "check-yaml"
- id: "detect-private-key"
- id: "check-toml"

- repo: https://github.com/psf/black
rev: 22.10.0
rev: 22.12.0
hooks:
- id: black
- repo: https://github.com/PyCQA/isort
rev: 5.10.1
files: &files '^(glQiwiApi|tests|examples|benchmarks|docs)'

- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.10.1
hooks:
- id: isort
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: 'v4.3.0'
additional_dependencies: [ toml ]
files: *files

- repo: https://github.com/floatingpurr/sync_with_poetry
rev: 0.4.0
hooks:
- id: check-merge-conflict
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
- id: sync_with_poetry

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.189
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: debug-statements
- id: double-quote-string-fixer
- id: requirements-txt-fixer
- id: ruff
2 changes: 1 addition & 1 deletion .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ formats:
- epub

python:
version: 3.8
version: 3.10
install:
- method: pip
path: .
Expand Down
8 changes: 8 additions & 0 deletions glQiwiApi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ def default_cache_storage() -> CacheStorage:
except metadata.PackageNotFoundError:
__version__ = '99.99.99'

try:
import uvloop as _uvloop

_uvloop.install()
del _uvloop
except ImportError: # pragma: no cover
pass

__all__ = (
# clients
'YooMoneyAPI',
Expand Down
3 changes: 3 additions & 0 deletions glQiwiApi/core/event_fetching/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ def wallet(self) -> Union[QiwiWallet, QiwiWrapper]:
return cast(Union[QiwiWallet, QiwiWrapper], self[WALLET_CTX_KEY])


Context = HandlerContext


class BaseExecutor(abc.ABC):
def __init__(
self,
Expand Down
49 changes: 38 additions & 11 deletions glQiwiApi/core/request_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
from glQiwiApi.utils.compat import Protocol
from glQiwiApi.utils.payload import make_payload

logger = logging.getLogger('glQiwiApi.RequestService')

T = TypeVar('T')


Expand Down Expand Up @@ -45,9 +43,6 @@ async def send_request(
) -> HTTPResponse:
...

async def warmup(self) -> Any:
...

async def shutdown(self) -> None:
...

Expand Down Expand Up @@ -88,9 +83,6 @@ async def get_json_content(
response = await self.send_request(**prepared_payload)
return response.json()

async def warmup(self) -> Any:
return await self._session_holder.get_session()

async def shutdown(self) -> None:
await self._session_holder.close()

Expand Down Expand Up @@ -120,6 +112,44 @@ async def send_request(
)


class RequestServiceLoggingDecorator(RequestServiceProto):
__slots__ = ('_logger', '_request_service')

def __init__(self, request_service: RequestServiceProto):
self._logger = logging.getLogger("glQiwiApi.request_service")
self._request_service = request_service

async def execute_api_method(self, method: APIMethod[T], **url_kw: Any) -> T:
log_extra = {"api_method_instance": method, "url_kwargs": url_kw}
api_method_name_including_module = f"{method.__module__}.{method.__class__.__qualname__}"
try:
session_holder = self._request_service._session_holder
session_holder_name = (
f"{session_holder.__module__}.{session_holder.__class__.__qualname__}"
)
except AttributeError:
session_holder_name = "unknown"

self._logger.debug(
"[Using %s] Start executing API method %s, and try to reach %s",
session_holder_name,
api_method_name_including_module,
method.url,
extra=log_extra,
)
response = await self._request_service.execute_api_method(method, **url_kw)
self._logger.debug(
"API method %s was executed successfully",
api_method_name_including_module,
extra=log_extra,
)
return response

async def shutdown(self) -> None:
self._logger.debug("Shutdown request service")
return await super().shutdown()


class RequestServiceCacheDecorator(RequestServiceProto):
def __init__(
self,
Expand Down Expand Up @@ -167,9 +197,6 @@ async def send_request(
url, method, cookies, json, data, headers, params, **kwargs
)

async def warmup(self) -> Any:
return await self._request_service.shutdown()

async def shutdown(self) -> None:
await self._request_service.shutdown()

Expand Down
4 changes: 2 additions & 2 deletions glQiwiApi/utils/certificates.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
if TYPE_CHECKING:
try:
from cryptography import x509 # NOQA # pragma: no cover
from cryptography.hazmat.primitives.asymmetric.rsa import (
RSAPrivateKeyWithSerialization, # pragma: no cover; NOQA; NOQA
from cryptography.hazmat.primitives.asymmetric.rsa import ( # pragma: no cover; NOQA; NOQA
RSAPrivateKeyWithSerialization,
)
except ImportError: # pragma: no cover
pass
Expand Down
2 changes: 1 addition & 1 deletion glQiwiApi/yoo_money/methods/operation_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ def build_request(self, **url_format_kw: Any) -> 'Request':
return Request(
endpoint=self.url.format(**url_format_kw, **self._get_runtime_path_values()),
http_method=self.http_method,
data=filter_dictionary_none_values(payload),
params=filter_dictionary_none_values(payload),
)
Empty file added playground.py
Empty file.
49 changes: 23 additions & 26 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,52 +29,49 @@ classifiers = [
"Environment :: Console",
"Framework :: AsyncIO",
"Intended Audience :: Developers",
"Intended Audience :: System Administrators",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Topic :: Software Development :: Libraries :: Application Frameworks",
"Programming Language :: Python :: Implementation :: PyPy",
"Topic :: Software Development :: Libraries :: Python Modules",
"Framework :: AsyncIO",
"Typing :: Typed",
]

[tool.poetry.dependencies]
python = "^3.7"
"backports.zoneinfo" = { version = "^0.2.1", python = "<3.9" }
tzdata = "^2022.6"
tzdata = "^2022.7"
aiohttp = "^3.8.3"
pydantic = "^1.10.2"
typing_extensions = { version = "^4.0.1", python = "<=3.7" }

# Fast
uvloop = { version = "^0.16.0", markers = "sys_platform == 'darwin' or sys_platform == 'linux'", optional = true }
# accelerators
uvloop = { version = "^0.17.0", markers = "sys_platform == 'darwin' or sys_platform == 'linux'", optional = true }
aiofiles = { version = "^22.1.0", optional = true }

# Docs

[tool.poetry.group.docs.dependencies]
Sphinx = { version = "^4.3.2", optional = true }
sphinx-intl = { version = "^2.0.1", optional = true }
sphinx-autobuild = { version = "^2021.3.14", optional = true }
sphinx-copybutton = { version = "^0.5.0", optional = true }
furo = { version = "^2022.2.23", optional = true }
furo = { version = "^2022.9.29", optional = true }
sphinx-prompt = { version = "^1.5.0", optional = true }
Sphinx-Substitution-Extensions = { version = ">=2020.9.30,<2023.0.0", optional = true }
towncrier = { version = "^21.9.0", optional = true }
pygments = { version = "^2.4", optional = true }
pymdown-extensions = { version = "^9.4", optional = true }
markdown-include = { version = ">=0.6,<0.8", optional = true }
sphinxemoji = { version = "*", optional = true }
sphinx-notfound-page = { version = "*", optional = true }



[tool.poetry.dev-dependencies]
# tests
[tool.poetry.group.tests.dependencies]
pytest-mock = "^3.8.2"
pytest = "^7.0.1"
pytest-asyncio = "^0.20.1"
pytest-asyncio = "^0.20.3"
pytest-cov = "^4.0.0"
pytest-lazy-fixture = "^0.6.3"
pytest-aiohttp = "^1.0.4"
Expand All @@ -83,21 +80,18 @@ asynctest = "^0.13.0"
pytest-timeout = "^2.1.0"
pytest-benchmark = { version = "^4.0.0", extras = ["histogram"] }

# development
black = { version = "^22.6.0", python = ">=3.6" }
isort = "^5.8.0"
mypy = "^0.982"
flake8 = "^5.0.4"
[tool.poetry.group.dev.dependencies]
black = { version = "^22.12.0", python = ">=3.6" }
isort = "^5.11.3"
mypy = "^0.991"
async-timeout = "^4.0.2"


# For CI and mypy
types-orjson = "^3.6.2"
types-pytz = "^2022.1.1"
types-pytz = "^2022.7.0.0"
types-aiofiles = "^22.1.0"
aiogram = "^2.21"
cryptography = "^38.0.1"
cryptography = "^38.0.4"
aiofiles = "^22.1.0"
ruff = "^0.0.189"

[tool.poetry.extras]
fast = ["uvloop", "aiofiles"]
Expand All @@ -110,7 +104,6 @@ docs = [
"black",
"sphinx-prompt",
"Sphinx-Substitution-Extensions",
"towncrier",
"pygments",
"pymdown-extensions",
"markdown-include",
Expand All @@ -120,7 +113,7 @@ docs = [

[tool.black]
line-length = 99
target-version = ['py37', 'py38', 'py39']
target-version = ['py37', 'py38', 'py39', 'py310']
skip-string-normalization = true
exclude = '''
(
Expand Down Expand Up @@ -178,6 +171,10 @@ show_absolute_path = true
module = "uvloop"
ignore_missing_imports = true

[tool.ruff]
line-length = 100


[tool.coverage]
exclude_lines = """
pragma: no cover
Expand Down Expand Up @@ -206,5 +203,5 @@ omit = """


[build-system]
requires = ["poetry-core>=1.0.0"]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import asyncio
import json
import logging
from asyncio import AbstractEventLoop

Expand Down

0 comments on commit 249f20b

Please sign in to comment.