Skip to content

Commit a19dc4c

Browse files
Lint and format with Ruff (#13)
1 parent ff71a66 commit a19dc4c

File tree

6 files changed

+126
-20
lines changed

6 files changed

+126
-20
lines changed

Makefile

+34
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,40 @@
22
test:
33
poetry run pytest
44

5+
.PHONY: rufflintfix
6+
rufflintfix:
7+
poetry run ruff check . --fix
8+
9+
.PHONY: rufflintcheck
10+
rufflintcheck:
11+
@echo "Checking ruff..."
12+
poetry run ruff check .
13+
14+
.PHONY: rufflintwatch
15+
rufflintwatch:
16+
poetry run ruff check . --fix --watch
17+
18+
.PHONY: ruffformatfix
19+
ruffformatfix:
20+
poetry run ruff format . --preview
21+
22+
.PHONY: ruffformatcheck
23+
ruffformatcheck:
24+
poetry run ruff format . --check --preview
25+
26+
.PHONY: poetrycheck
27+
poetrycheck:
28+
poetry check --lock
29+
30+
.PHONY: pyformatcheck
31+
pyformatcheck: poetrycheck rufflintcheck ruffformatcheck
32+
33+
.PHONY: lint
34+
lint: pyformatcheck
35+
36+
.PHONY: autofmt
37+
autofmt: rufflintfix ruffformatfix
38+
539
.PHONY: patchrelease
640
patchrelease:
741
poetry version patch

poetry.lock

+28-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+45
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,51 @@ requests = ">=2.0"
3232
pytest = "^7.4.3"
3333
boto3-stubs = {extras = ["s3"], version = "^1.28.70"}
3434
pytest-asyncio = "^0.21.1"
35+
ruff = "^0.8.0"
36+
37+
[tool.ruff]
38+
target-version = "py38"
39+
line-length = 100
40+
41+
lint.select = [
42+
"E", # pycodestyle errors
43+
"W", # pycodestyle warnings
44+
"F", # pyflakes
45+
"I", # isort
46+
"C40", # flake8-comprehensions
47+
"C90", # mccabe
48+
"UP", # pyupgrade
49+
"B", # flake8-bugbear
50+
"A", # flake8-builtins
51+
"T10", # flake8-debugger
52+
"BLE", # flake8-blind-except
53+
"T20", # flake8-print
54+
"DTZ", # flake8-datetimez
55+
"PIE", # flake8-pie
56+
"ERA", # eradicate
57+
"PL", # pylint
58+
"SIM", # flake8-simplify
59+
"ERA", # eradicate,
60+
"RUF", # ruff-specific
61+
"TRY", # tryceratops
62+
"PTH", # flake8-use-pathlib"
63+
"PGH", # pygrep-hooks
64+
"TID", # tidy imports
65+
"ICN"
66+
]
67+
lint.ignore = [
68+
"E501", # line too long, handled by formatter
69+
"B008", # function calls in argument defaults
70+
"C401", # generator syntax for sets vs always force set comprehension
71+
"PLC0414", # allow explicit re-exports using 'as' without forcing __all__
72+
"TRY003", # long messages outside except classes
73+
"PLR2004", # magic values refactor to constants
74+
]
75+
76+
77+
[tool.ruff.format]
78+
docstring-code-format = true
79+
3580

3681
[tool.pytest.ini_options]
3782
testpaths = [

pytest_aioboto3/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .aioboto3_fixtures import moto_patch_session, aioboto3_s3_client, aioboto3_s3_resource
1+
from .aioboto3_fixtures import aioboto3_s3_client, aioboto3_s3_resource, moto_patch_session
22
from .moto_fixtures import moto_services
33

4-
__all__ = ["moto_services", "moto_patch_session", "aioboto3_s3_client", "aioboto3_s3_resource"]
4+
__all__ = ["aioboto3_s3_client", "aioboto3_s3_resource", "moto_patch_session", "moto_services"]

pytest_aioboto3/aioboto3_fixtures.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
AWS asyncio test fixtures
33
"""
4+
45
from typing import Any, AsyncIterator, Iterator, Mapping, Type, TypeVar
56
from unittest import mock
67

@@ -15,31 +16,29 @@
1516
def create_fake_session(base_class: Type[T], url_overrides: Mapping[str, str]) -> Type[T]:
1617
class FakeSession(base_class): # type:ignore[valid-type, misc]
1718
def __init__(self, *args: Any, **kwargs: Any) -> None:
18-
super(FakeSession, self).__init__(*args, **kwargs)
19+
super().__init__(*args, **kwargs)
1920

2021
self.__url_overrides = url_overrides
2122
self.__secret_key = "ABCDEFGABCDEFGABCDEF"
2223
self.__access_key = "YTYHRSshtrsTRHSrsTHRSTrthSRThsrTHsr"
2324

2425
def client(self, *args: Any, **kwargs: Any) -> Any:
25-
2626
if "endpoint_url" not in kwargs and args[0] in self.__url_overrides:
2727
kwargs["endpoint_url"] = self.__url_overrides[args[0]]
2828

2929
kwargs["aws_access_key_id"] = self.__secret_key
3030
kwargs["aws_secret_access_key"] = self.__access_key
3131

32-
return super(FakeSession, self).client(*args, **kwargs)
32+
return super().client(*args, **kwargs)
3333

3434
def resource(self, *args: Any, **kwargs: Any) -> Any:
35-
3635
if "endpoint_url" not in kwargs and args[0] in self.__url_overrides:
3736
kwargs["endpoint_url"] = self.__url_overrides[args[0]]
3837

3938
kwargs["aws_access_key_id"] = self.__secret_key
4039
kwargs["aws_secret_access_key"] = self.__access_key
4140

42-
return super(FakeSession, self).resource(*args, **kwargs)
41+
return super().resource(*args, **kwargs)
4342

4443
return FakeSession
4544

pytest_aioboto3/moto_fixtures.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from __future__ import annotations
2+
23
import logging
34
import shutil
45
import signal
56
import socket
67
import subprocess
78
import time
89
from subprocess import Popen
9-
from typing import Any, Dict, Iterator, Mapping
10+
from typing import Any, Iterator, Mapping
1011

1112
import pytest
1213
import requests
@@ -31,20 +32,20 @@ def start_moto_server(service_name: str, host: str, port: int) -> Popen[Any]:
3132
)
3233
args = [moto_svr_path, service_name, "-H", host, "-p", str(port)]
3334
# For debugging
34-
# args = f"moto_svr_path service_name -H host -p port 2>&1 | tee -a /tmp/moto.log"
35+
# args = f"moto_svr_path service_name -H host -p port 2>&1 | tee -a /tmp/moto.log" # noqa: ERA001
3536
logger.info(f"Starting moto server: {args}")
3637
process = subprocess.Popen(
3738
args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
3839
) # shell=True
3940
url = f"http://{host}:{port}"
4041

41-
for i in range(0, 30):
42+
for _ in range(30):
4243
output = process.poll()
4344
if output is not None:
4445
logger.error(f"moto_server exited status {output}")
4546
stdout, stderr = process.communicate()
46-
logger.error(f"moto_server stdout: {str(stdout)}")
47-
logger.error(f"moto_server stderr: {str(stderr)}")
47+
logger.error(f"moto_server stdout: {stdout!s}")
48+
logger.error(f"moto_server stderr: {stderr!s}")
4849
pytest.fail(f"Can not start service: {service_name}")
4950

5051
try:
@@ -55,7 +56,7 @@ def start_moto_server(service_name: str, host: str, port: int) -> Popen[Any]:
5556
time.sleep(0.5)
5657
else:
5758
stop_process(process) # pytest.fail doesn't call stop_process
58-
pytest.fail("Can not start moto service: {}".format(service_name))
59+
pytest.fail(f"Can not start moto service: {service_name}")
5960

6061
logger.info(f"Connected to moto server at {url}")
6162

@@ -66,12 +67,12 @@ def stop_process(process: Popen[Any]) -> None:
6667
try:
6768
process.send_signal(signal.SIGTERM)
6869
process.communicate(timeout=20)
69-
except subprocess.TimeoutExpired:
70+
except subprocess.TimeoutExpired as te:
7071
process.kill()
7172
outs, errors = process.communicate(timeout=20)
7273
exit_code = process.returncode
73-
msg = "Child process finished {} not in clean way: {} {}".format(exit_code, outs, errors)
74-
raise RuntimeError(msg)
74+
msg = f"Child process finished {exit_code} not in clean way: {outs} {errors}"
75+
raise RuntimeError(msg) from te
7576

7677

7778
def get_free_tcp_port() -> int:
@@ -90,7 +91,7 @@ def moto_services() -> Iterator[Mapping[str, str]]:
9091
Map of mocked services with moto where the key is the service name and the value is the moto url to that service
9192
"""
9293
processes = []
93-
services: Dict[str, str] = {}
94+
services: dict[str, str] = {}
9495
"""
9596
9697
1. Add a new entry to the 'extras' section in pyproject.toml for types-aiobotocore, moto and boto3-stubs like 'dynamodb' or 'ec2'
@@ -109,6 +110,6 @@ def moto_services() -> Iterator[Mapping[str, str]]:
109110
try:
110111
stop_process(process)
111112
logger.info(f"Stopped moto process {process.pid}")
112-
except Exception as e:
113+
except Exception:
113114
# Keep going through exceptions to stop as many as possible
114-
logger.exception(f"Problem stopping moto process {process}", e)
115+
logger.exception(f"Problem stopping moto process {process}")

0 commit comments

Comments
 (0)