From ef74ee7354c8e83aa7fac6b2b31eda9b59c369f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Sp=C3=B6rk?= Date: Sun, 21 Jan 2024 16:14:09 +0100 Subject: [PATCH] Replace flake8 by ruff Fix lint issues --- .github/workflows/test.yml | 11 +-- const.py | 2 - foosball/arUcos/__init__.py | 2 - foosball/detectors/color.py | 4 +- foosball/models.py | 3 - foosball/sink/opencv.py | 12 ++-- foosball/source/gear.py | 2 +- foosball/tracking/preprocess.py | 3 +- foosball/tracking/tracker.py | 8 +-- tests/setup.cfg | 116 +++++++++++++++++++++----------- 10 files changed, 93 insertions(+), 70 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f7c38e7..fbb6ab6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -3,7 +3,7 @@ name: Tests on: [pull_request, push] jobs: - build: + test: runs-on: ubuntu-latest strategy: matrix: @@ -36,15 +36,10 @@ jobs: python -m venv .venv . .venv/bin/activate python -m pip install --upgrade pip - pip install flake8 pip install -r requirements_dev.txt if: steps.cache-venv.outputs.cache-hit != 'true' - - name: Lint with flake8 - run: | - . .venv/bin/activate - # stop the build if there are Python syntax errors or undefined names - flake8 foosball --count --select=E9,F63,F7,F82 --show-source --statistics --config tests/setup.cfg - flake8 foosball --count --exit-zero --max-complexity=10 --statistics --config tests/setup.cfg + - name: Lint with ruff + uses: chartboost/ruff-action@v1 - name: Test with pytest run: | . .venv/bin/activate diff --git a/const.py b/const.py index c5f04f7..534b823 100644 --- a/const.py +++ b/const.py @@ -1,5 +1,3 @@ -from enum import Enum - CALIBRATION_MODE = "calibrationMode" CALIBRATION_IMG_PATH = "calibrationImagePath" CALIBRATION_VIDEO = "calibrationVideo" diff --git a/foosball/arUcos/__init__.py b/foosball/arUcos/__init__.py index ccb888e..e69de29 100644 --- a/foosball/arUcos/__init__.py +++ b/foosball/arUcos/__init__.py @@ -1,2 +0,0 @@ -from .calibration import Calibration -from .models import Aruco \ No newline at end of file diff --git a/foosball/detectors/color.py b/foosball/detectors/color.py index a5562ff..86b8159 100644 --- a/foosball/detectors/color.py +++ b/foosball/detectors/color.py @@ -24,7 +24,7 @@ class BallConfig: invert_mask: bool = False def store(self): - filename = f"ball.yaml" + filename = "ball.yaml" print(f"Store config {filename}" + (" " * 50), end="\n\n") with open(filename, "w") as f: yaml.dump(self.to_dict(), f) @@ -66,7 +66,7 @@ class GoalConfig: invert_mask: bool = True def store(self): - filename = f"goal.yaml" + filename = "goal.yaml" print(f"Store config {filename}" + (" " * 50), end="\n\n") with open(filename, "w") as f: yaml.dump(self.to_dict(), f) diff --git a/foosball/models.py b/foosball/models.py index 0d846b3..99d2fa1 100644 --- a/foosball/models.py +++ b/foosball/models.py @@ -1,13 +1,10 @@ import collections -import logging -import os from dataclasses import dataclass from enum import Enum from typing import Optional, Union import cv2 import numpy as np -import yaml HSV = np.ndarray # list[int, int, int] RGB = np.ndarray # list[int, int, int] diff --git a/foosball/sink/opencv.py b/foosball/sink/opencv.py index 87e8d23..4ca0fda 100644 --- a/foosball/sink/opencv.py +++ b/foosball/sink/opencv.py @@ -81,8 +81,8 @@ def add_config_input(calibrationMode, config): def add_ball_config_input(bounds: BallConfig): [lower_hsv, upper_hsv] = bounds.bounds - cv2.createTrackbar(f'invert_frame', BALL, 1 if bounds.invert_frame else 0, 1, lambda v: None) - cv2.createTrackbar(f'invert_mask', BALL, 1 if bounds.invert_mask else 0, 1, lambda v: None) + cv2.createTrackbar('invert_frame', BALL, 1 if bounds.invert_frame else 0, 1, lambda v: None) + cv2.createTrackbar('invert_mask', BALL, 1 if bounds.invert_mask else 0, 1, lambda v: None) # create trackbars for color change cv2.createTrackbar('Hue', BALL, avg(lower_hsv[0], upper_hsv[0]), 179, lambda v: None) cv2.createTrackbar(slider_label('S', 'low'), BALL, lower_hsv[1], 255, lambda v: None) @@ -94,8 +94,8 @@ def add_ball_config_input(bounds: BallConfig): def add_goals_config_input(config: GoalConfig): [lower, upper] = config.bounds - cv2.createTrackbar(f'invert_frame', GOAL, 1 if config.invert_frame else 0, 1, lambda v: None) - cv2.createTrackbar(f'invert_mask', GOAL, 1 if config.invert_mask else 0, 1, lambda v: None) + cv2.createTrackbar('invert_frame', GOAL, 1 if config.invert_frame else 0, 1, lambda v: None) + cv2.createTrackbar('invert_mask', GOAL, 1 if config.invert_mask else 0, 1, lambda v: None) # create trackbars for color change cv2.createTrackbar("lower", GOAL, lower, 255, lambda v: None) cv2.createTrackbar("upper", GOAL, upper, 255, lambda v: None) @@ -138,7 +138,7 @@ def reset_goal_config(config: GoalConfig): def store_ball_config(config: BallConfig): - filename = f"ball.yaml" + filename = "ball.yaml" [lower, upper] = config.bounds print(f"Store config {filename}" + (" " * 50), end="\n\n") with open(filename, "w") as f: @@ -151,7 +151,7 @@ def store_ball_config(config: BallConfig): def store_goals_config(config: GoalConfig): - filename = f"goal.yaml" + filename = "goal.yaml" [lower, upper] = config.bounds print(f"Store config {filename}" + (" " * 50), end="\n\n") with open(filename, "w") as f: diff --git a/foosball/source/gear.py b/foosball/source/gear.py index ceadd2a..57dfa87 100644 --- a/foosball/source/gear.py +++ b/foosball/source/gear.py @@ -15,7 +15,7 @@ def __init__(self, video=None, resolution=(640, 480), framerate=60, **kwargs): self.eos = False # if a video path was not supplied, grab the reference # to the webcam - if video is None or type(video) == int: + if video is None or isinstance(video, int): options = { "CAP_PROP_FRAME_WIDTH": resolution[0], "CAP_PROP_FRAME_HEIGHT": resolution[1], diff --git a/foosball/tracking/preprocess.py b/foosball/tracking/preprocess.py index 9eb4911..490299e 100644 --- a/foosball/tracking/preprocess.py +++ b/foosball/tracking/preprocess.py @@ -7,7 +7,8 @@ import numpy as np from const import CalibrationMode, OFF -from ..arUcos import calibration, Aruco +from ..arUcos import calibration +from ..arUcos.models import Aruco from ..detectors.color import GoalDetector, GoalConfig from ..models import Frame, PreprocessResult, Point, Rect, Blob, Goals, FrameDimensions, ScaleDirection, \ InfoLog, Info diff --git a/foosball/tracking/tracker.py b/foosball/tracking/tracker.py index 98ea6c4..745ac84 100644 --- a/foosball/tracking/tracker.py +++ b/foosball/tracking/tracker.py @@ -58,10 +58,10 @@ def get_info(self, ball_track: Track) -> InfoLog: ]) if self.ball_calibration: [lower, upper] = self.ball_detector.config.bounds - info.append(Info(verbosity=0, title=f"lower", value=f'({",".join(map(str,lower))})')) - info.append(Info(verbosity=0, title=f"upper", value=f'({",".join(map(str,upper))})')) - info.append(Info(verbosity=0, title=f"invert frame", value=f'{self.ball_detector.config.invert_frame}')) - info.append(Info(verbosity=0, title=f"invert mask", value=f'{self.ball_detector.config.invert_mask}')) + info.append(Info(verbosity=0, title="lower", value=f'({",".join(map(str,lower))})')) + info.append(Info(verbosity=0, title="upper", value=f'({",".join(map(str,upper))})')) + info.append(Info(verbosity=0, title="invert frame", value=f'{self.ball_detector.config.invert_frame}')) + info.append(Info(verbosity=0, title="invert mask", value=f'{self.ball_detector.config.invert_mask}')) return info @property diff --git a/tests/setup.cfg b/tests/setup.cfg index ede7a42..3c94435 100644 --- a/tests/setup.cfg +++ b/tests/setup.cfg @@ -22,47 +22,81 @@ addopts = --cov=foosball --cov-branch --cov-report=html +[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", +] -[flake8] -# https://github.com/ambv/black#line-length -max-line-length = 120 -# E501: line too long -# W503: Line break occurred before a binary operator -# E203: Whitespace before ':' -# D202 No blank lines allowed after function docstring -# W504 line break after binary operator -ignore = - E501, - W503, - E203, - D202, - W504 +# Same as Black. +line-length = 120 +indent-width = 4 -[isort] -# https://github.com/timothycrosley/isort -# https://github.com/timothycrosley/isort/wiki/isort-Settings -# splits long import on multiple lines indented by 4 spaces -multi_line_output = 3 -include_trailing_comma=True -force_grid_wrap=0 -use_parentheses=True -line_length=120 -indent = " " -# by default isort don't check module indexes -not_skip = __init__.py -# will group `import x` and `from x import` of the same module. -force_sort_within_sections = true -sections = FUTURE,STDLIB,INBETWEENS,THIRDPARTY,FIRSTPARTY,LOCALFOLDER -default_section = THIRDPARTY -known_first_party = foosball,tests -forced_separate = tests -combine_as_imports = true +# Assume Python 3.11 +target-version = "py311" -[mypy] -python_version = 3.10 -ignore_errors = true -follow_imports = silent -ignore_missing_imports = true -warn_incomplete_stub = true -warn_redundant_casts = true -warn_unused_configs = true +[tool.ruff.lint] +# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default. +# Unlike Flake8, Ruff doesn't enable pycodestyle warnings (`W`) or +# McCabe complexity (`C901`) by default. +select = ["E4", "E7", "E9", "F"] +ignore = E501,W503,E203,D202,W504 + +# Allow fix for all enabled rules (when `--fix`) is provided. +fixable = ["ALL"] +unfixable = [] + +# Allow unused variables when underscore-prefixed. +dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" + +[tool.ruff.format] +# Like Black, use double quotes for strings. +quote-style = "double" + +# Like Black, indent with spaces, rather than tabs. +indent-style = "space" + +# Like Black, respect magic trailing commas. +skip-magic-trailing-comma = false + +# Like Black, automatically detect the appropriate line ending. +line-ending = "auto" + +# Enable auto-formatting of code examples in docstrings. Markdown, +# reStructuredText code/literal blocks and doctests are all supported. +# +# This is currently disabled by default, but it is planned for this +# to be opt-out in the future. +docstring-code-format = false + +# Set the line length limit used when formatting code snippets in +# docstrings. +# +# This only has an effect when the `docstring-code-format` setting is +# enabled. +docstring-code-line-length = "dynamic" \ No newline at end of file