Skip to content

Commit

Permalink
refactor: adopt ruff as a linter
Browse files Browse the repository at this point in the history
  • Loading branch information
AlessandroMiola committed Jan 8, 2024
1 parent 2e2ff0c commit c0179d3
Show file tree
Hide file tree
Showing 16 changed files with 126 additions and 40 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/actions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@ jobs:
- name: Install project
run: poetry install --no-interaction

- name: Enforce code style (Ruff)
run: poetry run ruff check --show-source --show-fixes .

- name: Verify code formatting (Black)
run: poetry run black --check --diff .

- name: Enforce code style (flake8)
continue-on-error: true
run: poetry run flake8 --show-source .

- name: Run tests
run: poetry run pytest --verbose

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ __pycache__
.mypy_cache/
.coverage
coverage.xml
.ruff_cache
11 changes: 6 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ repos:
- id: check-toml
- repo: local
hooks:
- id: ruff
name: ruff (linter)
entry: poetry run ruff check .
language: system
types: [python]
args: [--fix]
- id: black
name: black
entry: poetry run black .
language: system
types: [python]
- id: flake8
name: flake8
entry: poetry run flake8 .
language: system
types: [python]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Personal attempt to solve the `advent-of-code`[^aoc] puzzles in a `TDD` fashion
- 🚧 try out `ruff` as a linter, thus replacing `flake8`
- 🚧 rely on a unique configuration file (possibly `pyproject.toml`)
- 🚧 add `mypy` (`--strict`?) in the pipeline
- 🚧 try out `ruff` as a formatter, thus replacing `black`?
- 🚧 `makefile` | `justfile`
- 🚧 Dockerization?
- 🚧 else?
Expand Down
28 changes: 27 additions & 1 deletion poetry.lock

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

64 changes: 64 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ flake8 = "^6.1.0"
mypy = "^1.8.0"
coverage = "^7.4.0"
pytest-cov = "^4.1.0"
ruff = "^0.1.11"

[build-system]
requires = ["poetry-core"]
Expand All @@ -27,3 +28,66 @@ build-backend = "poetry.core.masonry.api"
line-length = 100
target-version = ["py310"]
preview = true

[tool.ruff]
# Exclude a variety of commonly ignored directories.
exclude = [
".bzr",
".direnv",
".eggs",
".git",
".git-rewrite",
".hg",
".ipynb_checkpoints",
".mypy_cache",
".nox",
".pants.d",
".pyenv",
".pytest_cache",
".pytype",
".ruff_cache",
".svn",
".tox",
".venv",
".vscode",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"site-packages",
"venv",
]
line-length = 100
indent-width = 4
target-version = "py310"
show-source = true
show-fixes = true

[tool.ruff.lint]
select = [
# default
"E", # pycodestyle error
"F", # flake8 error
# extra
"A", # builtin shadowing
"B", # flake8 bugbear
"BLE", # avoid bare excepts
"C4", # simplify comprehensions
"DTZ", # datetime errors
"FBT", # avoid boolean trap
"G", # logging format
"I", # isort imports
"N", # conform to PEP8 naming rules
"RET", # return values
"S", # bandit
"TRY", # exceptions antipatterns
"UP", # upgrade syntax
"W", # pycodestyle warning
"YTT", # wrong usage of sys.info
]
fixable = ["ALL"]

[tool.ruff.lint.per-file-ignores]
"test*.py" = ["S101"]
12 changes: 6 additions & 6 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[flake8]
max-line-length = 100
extend-ignore = E203, W503
filename =
./src/*.py,
./tests/*.py
# [flake8]
# max-line-length = 100
# extend-ignore = E203, W503
# filename =
# ./src/*.py,
# ./tests/*.py
4 changes: 3 additions & 1 deletion src/year_2023/day02/part1.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def get_ncubes_combination_per_game(input_str: str) -> list[tuple[int]]:
get_ncubes_by_colour_per_game(input_str, "red"),
get_ncubes_by_colour_per_game(input_str, "green"),
get_ncubes_by_colour_per_game(input_str, "blue"),
strict=False,
)
)

Expand All @@ -31,10 +32,11 @@ def _read_file(file_path: str) -> list[str]:
int(re.search(r"\d+(?=:)", line).group())
for line in lines
if all(
all(el1 <= el2 for el1, el2 in zip(combination, target))
all(el1 <= el2 for el1, el2 in zip(combination, target, strict=False))
for combination, target in zip(
get_ncubes_combination_per_game(line),
[(12, 13, 14)] * len(get_ncubes_combination_per_game(line)),
strict=False,
)
)
)
Expand Down
3 changes: 1 addition & 2 deletions src/year_2023/day02/part2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os

from functools import reduce
from operator import mul

Expand All @@ -8,7 +7,7 @@


def get_min_ncubes_to_play(input_str: str) -> list[int]:
return [max(cube) for cube in zip(*get_ncubes_combination_per_game(input_str))]
return [max(cube) for cube in zip(*get_ncubes_combination_per_game(input_str), strict=False)]


def sum_power_ncubes(file_path: str) -> int:
Expand Down
16 changes: 7 additions & 9 deletions src/year_2023/day03/part1.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ def get_symbols_pos(input_lines: list[str]) -> list[int]:
def get_candidate_pnum_symbols_pos(number_pos: tuple[int], len_input: int) -> list[int]:
num_p_start, num_p_end, _ = number_pos
return sorted(
list(
set(range(num_p_start - len_input - 1, num_p_end - len_input + 1))
.union(set(range(num_p_start - 1, num_p_start)))
.union(set(range(num_p_end, num_p_end + 1)))
.union(set(range(num_p_start + len_input - 1, num_p_end + len_input + 1)))
)
set(range(num_p_start - len_input - 1, num_p_end - len_input + 1))
.union(set(range(num_p_start - 1, num_p_start)))
.union(set(range(num_p_end, num_p_end + 1)))
.union(set(range(num_p_start + len_input - 1, num_p_end + len_input + 1)))
)


Expand All @@ -43,16 +41,16 @@ def sum_part_numbers(file_path: str) -> int:
len_input = get_len_input(input_lines)
numbers_pos = get_numbers_pos(input_lines)
symbols_pos = get_symbols_pos(input_lines)
sum = 0
result = 0
for num_p_start, num_p_end, num in numbers_pos:
candidate_symbols_pos = get_candidate_pnum_symbols_pos(
(num_p_start, num_p_end, num), len_input
)
for sym_p_start in symbols_pos:
if sym_p_start in candidate_symbols_pos:
sum += num
result += num
break
return sum
return result


if __name__ == "__main__":
Expand Down
3 changes: 1 addition & 2 deletions src/year_2023/day06/part1.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import re
import typing

from functools import reduce

from src.year_2023.utils import read_file
Expand All @@ -10,7 +9,7 @@
def get_time_and_winning_distance_per_race(lines: list[str]) -> typing.Iterator:
times = [int(num) for num in re.findall(r"\b\d+", lines[0])]
distances = [int(num) for num in re.findall(r"\b\d+", lines[1])]
return zip(times, distances)
return zip(times, distances, strict=False)


def get_n_winning_combinations(time: int, distance: int) -> int:
Expand Down
2 changes: 1 addition & 1 deletion src/year_2023/day06/part2.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
import re

from src.year_2023.utils import read_file
from src.year_2023.day06.part1 import get_n_winning_combinations as _get_n_winning_combinations
from src.year_2023.utils import read_file


def get_time_and_winning_distance(lines: list[str]) -> tuple[int]:
Expand Down
6 changes: 2 additions & 4 deletions src/year_2023/day07/part1.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import os

from collections import Counter

from src.year_2023.utils import read_file


camel_cards_p1 = ["2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"]


Expand Down Expand Up @@ -51,7 +49,7 @@ def _map_ladder_to_rank(ladder: list[int]) -> int:
def get_sorted_hand_values_by_rank(
ladders: list[list[int]], hands_to_values: list[list[int]]
) -> list[tuple[list[int]]]:
ladders_and_values = list(zip(ladders, hands_to_values))
ladders_and_values = list(zip(ladders, hands_to_values, strict=False))
return sorted(
ladders_and_values, key=lambda x: (-_map_ladder_to_rank(x[0]), tuple(-el for el in x[1]))
)
Expand All @@ -67,7 +65,7 @@ def get_hands_to_rank(lines: list[str], camel_cards: list[str]) -> dict[str, int
]
return {
map_values_to_hand(hand_values, camel_cards): rank
for (_, hand_values), rank in zip(sorted_hand_values, hand_values_rank)
for (_, hand_values), rank in zip(sorted_hand_values, hand_values_rank, strict=False)
}


Expand Down
4 changes: 1 addition & 3 deletions src/year_2023/day07/part2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os

from collections import Counter

from src.year_2023.day07.part1 import (
Expand All @@ -11,7 +10,6 @@
)
from src.year_2023.utils import read_file


camel_cards_p2 = ["J", "2", "3", "4", "5", "6", "7", "8", "9", "T", "Q", "K", "A"]


Expand Down Expand Up @@ -44,7 +42,7 @@ def get_hands_to_rank(lines: list[str], camel_cards: list[int]) -> dict[str, int
]
return {
map_values_to_hand(hand_values, camel_cards): rank
for (_, hand_values), rank in zip(sorted_hand_values, hand_values_rank)
for (_, hand_values), rank in zip(sorted_hand_values, hand_values_rank, strict=False)
}


Expand Down
2 changes: 1 addition & 1 deletion src/year_2023/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def read_file(file_path: str) -> list[str]:
with open(file_path, "r") as file:
with open(file_path) as file:
return list(file.read().split("\n"))
2 changes: 1 addition & 1 deletion tests/year_2023/day07/test_day07_part1.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
get_hands_to_rank,
get_sorted_hand_values_by_rank,
get_total_winnings,
map_hand_to_values,
map_hand_to_ladder,
map_hand_to_values,
map_values_to_hand,
)
from src.year_2023.utils import read_file
Expand Down

0 comments on commit c0179d3

Please sign in to comment.