Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Use ruff for all lints #347

Merged
merged 5 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,12 @@ jobs:
- name: Install Dependencies
run: |
poetry install
- name: Run flake8
- name: Run ruff
run: |
poetry run flake8 src tests
poetry run ruff check src tests
- name: Run mypy
run: |
poetry run mypy src tests
- name: Run pylint
run: |
poetry run pylint src tests
- name: Run black
run: |
poetry run black --check --verbose src tests
Expand Down
6 changes: 2 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@ zipapp:

.PHONY: lint
lint:
flake8 src tests
mypy src tests
pylint src tests
ruff check src tests
black --check src tests
isort --check src tests
reuse lint

.PHONY: fmt
fmt:
isort src tests
ruff check --fix-only src tests
black src tests

.PHONY: docs
Expand Down
400 changes: 106 additions & 294 deletions poetry.lock

Large diffs are not rendered by default.

57 changes: 19 additions & 38 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ exitcode = "^0.1.0"
[tool.poetry.group.dev.dependencies]
black = "^22.10"
Sphinx = "^5.2"
isort = "^5.10"
mypy = "^1.0"
pylsp-mypy = "^0.6"
pylsp-rope = "^0.1"
Expand All @@ -67,10 +66,8 @@ sphinx-rtd-theme = "^1.0"
reuse = "^1.0.0"
construct-typing = "^0.5.2"
pytest-cov = "^4.0"
pyls-isort = "^0.2.2"
ruff = "^0.0.206"
flake8 = "^6.0.0"
pylint = "~2.15.0"
ruff = "^0.0.251"
python-lsp-ruff = "^1.0.5"

[tool.poetry.scripts]
"gallia" = "gallia.cli:main"
Expand All @@ -89,43 +86,27 @@ module = [
ignore_missing_imports = true

[tool.ruff]
line-length = 120
target-version = "py310"
select = [
"C4", # flake8-comprehensions
"E", # pycodestlye
"F", # pyflakes
"I", # isort
"PL", # pylint
"PTH", # flake8-use-pathlib
"TID", # flake8-tidy-imports
"UP", # pyupgrade
]
ignore = [
"E501", # line length
"PLR2004", # magic value used in comparison
"PLR0911", # too many return statements
"PLR0912", # too many branches
"PLR0915", # too many statements
]

[tool.black]
target-version = ['py310']

[tool.isort]
profile = "black"
py_version= "310"

[tool.pytest.ini_options]
asyncio_mode = "auto"

[tool.pylint.messages_control]
disable = [
"broad-except",
"duplicate-code", # https://github.com/Fraunhofer-AISEC/gallia/issues/306
"fixme",
"invalid-name",
"line-too-long", # handled by black
"logging-fstring-interpolation", # https://github.com/Fraunhofer-AISEC/gallia/issues/234
"logging-not-lazy", # https://github.com/Fraunhofer-AISEC/gallia/issues/234
"missing-class-docstring",
"missing-class-docstring",
"missing-function-docstring",
"missing-module-docstring",
"no-member", # mypy handles this better
"no-name-in-module", # mypy handles this better
"too-few-public-methods",
"too-many-arguments",
"too-many-branches",
"too-many-instance-attributes",
"too-many-lines",
"too-many-locals",
"too-many-nested-blocks",
"too-many-public-methods",
"too-many-return-statements",
"too-many-statements",
"unused-argument",
]
11 changes: 0 additions & 11 deletions setup.cfg

This file was deleted.

15 changes: 6 additions & 9 deletions src/cursed_hr/cursed_hr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#
# SPDX-License-Identifier: Apache-2.0

# pylint: disable=too-many-lines,eval-used

from __future__ import annotations

import curses
Expand All @@ -26,7 +24,6 @@
from typing import Any, BinaryIO, cast

import zstandard as zstd

from gallia.log import PenlogPriority, PenlogRecord
from gallia.services.uds.core.service import NegativeResponse, UDSRequest, UDSResponse

Expand Down Expand Up @@ -174,7 +171,7 @@ def __init__(
filters: list[str] | None = None,
):
self.in_file = in_file
self.level_pointers: list[array[int]] = list(array("L") for _ in range(9))
self.level_pointers: list[array[int]] = [array("L") for _ in range(9)]
self.entry_positions = array("L")
self.configuration_history = [
Configuration(
Expand Down Expand Up @@ -262,7 +259,7 @@ def uncompressed_file(self) -> BinaryIO:
)
self.window.refresh()

file = tempfile.TemporaryFile() # pylint: disable=consider-using-with
file = tempfile.TemporaryFile()

match self.in_file.suffix:
case ".zst":
Expand Down Expand Up @@ -1145,9 +1142,9 @@ def line_up() -> None:
self.configuration.interpret = not self.configuration.interpret
case "f":
# fh is short for filter_history to reduce long unreadable lines
fh_tmp = list(
fh_tmp = [
"; ".join(filter_commands) for filter_commands in filter_history
)
]
fh_tmp.append("")
fh_index = len(fh_tmp) - 1

Expand Down Expand Up @@ -1315,9 +1312,9 @@ def parse_filter(text: str) -> list[str]:
tags=[],
)

commands = list(
commands = [
command.strip() for command in text.split(";") if len(command.strip()) > 0
)
]

with warnings.catch_warnings():
warnings.simplefilter("ignore", SyntaxWarning)
Expand Down
14 changes: 5 additions & 9 deletions src/gallia/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ def _get_cli_defaults(parser: argparse.ArgumentParser, out: dict[str, Any]) -> N
if isinstance(
action,
(
argparse._StoreAction, # pylint: disable=protected-access
argparse._StoreTrueAction, # pylint: disable=protected-access
argparse._StoreFalseAction, # pylint: disable=protected-access
argparse._StoreAction,
argparse._StoreTrueAction,
argparse._StoreFalseAction,
argparse.BooleanOptionalAction,
),
):
Expand Down Expand Up @@ -241,9 +241,7 @@ def _get_cli_defaults(parser: argparse.ArgumentParser, out: dict[str, Any]) -> N
d[keys[-1]] = value
break

if isinstance(
action, argparse._SubParsersAction # pylint: disable=protected-access
):
if isinstance(action, argparse._SubParsersAction):
for subparser in action.__dict__["choices"].values():
_get_cli_defaults(subparser, out)

Expand All @@ -256,9 +254,7 @@ def get_cli_defaults(parser: argparse.ArgumentParser) -> dict[str, Any]:

def _get_command_tree(parser: argparse.ArgumentParser, out: dict[str, Any]) -> None:
for action in parser.__dict__["_actions"]:
if isinstance(
action, argparse._SubParsersAction # pylint: disable=protected-access
):
if isinstance(action, argparse._SubParsersAction):
for cmd, subparser in action.__dict__["choices"].items():
out[cmd] = {}
d = out[cmd]
Expand Down
4 changes: 2 additions & 2 deletions src/gallia/command/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def __init__(self, parser: ArgumentParser, config: Config = Config()) -> None:
self.logger = get_logger(self.LOGGER_NAME)
self.parser = parser
self.config = config
self.artifacts_dir = Path(".")
self.artifacts_dir = Path()
self.run_meta = RunMeta(
command=sys.argv,
command_meta=CommandMeta(
Expand Down Expand Up @@ -149,7 +149,7 @@ def run_hook(
hook_id = f"{variant.value}-hook"

argv = sys.argv[:]
argv[0] = os.path.basename(argv[0])
argv[0] = Path(argv[0]).name
env = {
"GALLIA_ARTIFACTS_DIR": str(self.artifacts_dir),
"GALLIA_HOOK": variant.value,
Expand Down
2 changes: 1 addition & 1 deletion src/gallia/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@
WriteByIdentifierPrimitive,
]

__all__ = list(map(lambda x: x.__name__, registry))
__all__ = [x.__name__ for x in registry]
2 changes: 1 addition & 1 deletion src/gallia/commands/discover/uds/doip.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def configure_parser(self) -> None:
help="set end address",
)

async def probe(
async def probe( # noqa: PLR0913
self,
conn: DoIPConnection,
host: str,
Expand Down
8 changes: 4 additions & 4 deletions src/gallia/commands/scan/uds/identifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,19 @@ async def main(self, args: Namespace) -> None:
if args.sessions is None:
self.logger.info("No sessions specified, starting with session scan")
# Only until 0x80 because the eight bit is "SuppressResponse"
sessions = list(
sessions = [
s
for s in range(1, 0x80)
if s not in args.skip or args.skip[s] is not None
)
]
sessions = await self.ecu.find_sessions(sessions)
self.logger.result(f"Found {len(sessions)} sessions: {g_repr(sessions)}")
else:
sessions = list(
sessions = [
s
for s in args.sessions
if s not in args.skip or args.skip[s] is not None
)
]

self.logger.info(f"testing sessions {g_repr(sessions)}")

Expand Down
10 changes: 5 additions & 5 deletions src/gallia/commands/scan/uds/reset.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,26 @@ async def main(self, args: Namespace) -> None:
if args.sessions is None:
self.logger.info("No sessions specified, starting with session scan")
# Only until 0x80 because the eight bit is "SuppressResponse"
sessions = list(
sessions = [
s
for s in range(1, 0x80)
if s not in args.skip or args.skip[s] is not None
)
]
sessions = await self.ecu.find_sessions(sessions)
self.logger.result(f"Found {len(sessions)} sessions: {g_repr(sessions)}")
else:
sessions = list(
sessions = [
s
for s in args.sessions
if s not in args.skip or args.skip[s] is not None
)
]

self.logger.info(f"testing sessions {g_repr(sessions)}")

# TODO: Unified shortened output necessary here
self.logger.info(f"skipping identifiers {reprlib.repr(args.skip)}")

for session in sessions: # pylint: disable=too-many-nested-blocks
for session in sessions:
self.logger.notice(f"Switching to session {g_repr(session)}")
resp: UDSResponse = await self.ecu.set_session(session)
if isinstance(resp, NegativeResponse):
Expand Down
8 changes: 4 additions & 4 deletions src/gallia/commands/scan/uds/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,19 @@ async def main(self, args: Namespace) -> None:
if args.sessions is None:
self.logger.info("No sessions specified, starting with session scan")
# Only until 0x80 because the eight bit is "SuppressResponse"
sessions = list(
sessions = [
s
for s in range(1, 0x80)
if s not in args.skip or args.skip[s] is not None
)
]
sessions = await self.ecu.find_sessions(sessions)
self.logger.result(f"Found {len(sessions)} sessions: {g_repr(sessions)}")
else:
sessions = list(
sessions = [
s
for s in args.sessions
if s not in args.skip or args.skip[s] is not None
)
]

self.logger.info(f"testing sessions {g_repr(sessions)}")

Expand Down
1 change: 0 additions & 1 deletion src/gallia/commands/scan/uds/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ async def main(self, args: Namespace) -> None:
sessions = list(range(1, 0x80))
depth = 0

# pylint: disable=too-many-nested-blocks
while (args.depth is None or depth < args.depth) and len(found[depth]) > 0:
depth += 1

Expand Down
2 changes: 0 additions & 2 deletions src/gallia/commands/script/vecu.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#
# SPDX-License-Identifier: Apache-2.0

# pylint: disable=eval-used

import json
import random
import sys
Expand Down
8 changes: 4 additions & 4 deletions src/gallia/db/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ async def insert_discovery_result(self, target: str) -> None:

await self.connection.execute(query, (target, self.discovery_run))

async def insert_scan_result(
async def insert_scan_result( # noqa: PLR0913
self,
state: dict[str, Any],
request: UDSRequest,
Expand Down Expand Up @@ -320,7 +320,7 @@ async def insert_scan_result(
and len(value) > 0
and isinstance(value[0], (bytes, bytearray))
):
request_attributes[attr] = list(bytes_repr(v) for v in value)
request_attributes[attr] = [bytes_repr(v) for v in value]

if response is not None:
response_attributes = {"service_id": response.service_id}
Expand All @@ -342,7 +342,7 @@ async def insert_scan_result(
and len(value) > 0
and isinstance(value[0], (bytes, bytearray))
):
response_attributes[attr] = list(bytes_repr(v) for v in value)
response_attributes[attr] = [bytes_repr(v) for v in value]

query = (
"INSERT INTO scan_result(run, state, request_pdu, request_time, request_timezone, request_data, "
Expand Down Expand Up @@ -413,7 +413,7 @@ async def get_sessions(self) -> list[int]:
parameters = (self.target,)

cursor: aiosqlite.Cursor = await self.connection.execute(query, parameters)
return list(x[0] for x in await cursor.fetchall())
return [x[0] for x in await cursor.fetchall()]

async def get_session_transition(self, destination: int) -> list[int] | None:
assert self.connection is not None, "Not connected to the database"
Expand Down
2 changes: 1 addition & 1 deletion src/gallia/dumpcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
class Dumpcap:
BUFSIZE = io.DEFAULT_BUFFER_SIZE

def __init__(
def __init__( # noqa: PLR0913
self,
proc: subprocess.Process,
logger: Logger,
Expand Down
Loading