From 0ee0bb003fa8b573e0cbff2e8dd2d54fdc209ff8 Mon Sep 17 00:00:00 2001 From: udifuchs Date: Tue, 5 Dec 2023 13:31:43 -0600 Subject: [PATCH] Added ruff checks. --- .github/workflows/run_tests.yml | 4 ++-- pylint_silent/__init__.py | 20 ++++++++++---------- pylint_silent/__main__.py | 2 +- pyproject.toml | 28 ++++++++++++++++++++++++++++ tests/test_samples.py | 5 ++--- tox.ini | 16 +++++++++------- 6 files changed, 52 insertions(+), 23 deletions(-) diff --git a/.github/workflows/run_tests.yml b/.github/workflows/run_tests.yml index f1552af..7efae60 100644 --- a/.github/workflows/run_tests.yml +++ b/.github/workflows/run_tests.yml @@ -26,7 +26,7 @@ jobs: - name: run linters # Linters are only run on the earliest supported python version. if: ${{ matrix.python-version == '3.8' && matrix.testsuite == 'linters' }} - run: tox -e py3-pytest + run: tox -e mypy,pep8,pylint,ruff - name: run pytest if: ${{ matrix.testsuite == 'pytest' }} - run: tox -e mypy,pep8,pylint + run: tox -e py3-pytest diff --git a/pylint_silent/__init__.py b/pylint_silent/__init__.py index 882c356..b73ce11 100644 --- a/pylint_silent/__init__.py +++ b/pylint_silent/__init__.py @@ -115,10 +115,10 @@ def apply(pylint_logfile: str, signature: str, max_line_length: int) -> None: code = line_parts[3] message = line_parts[4] - if code in ( + if code in { " R0401", # Cyclic import " R0801", # Similar lines in 2 files - ): + }: # Pylint reports the wrong file and line number for these messages. continue if code == " C0326": @@ -167,12 +167,12 @@ def reset(py_filename: str, signature: str) -> None: open(out_filename, "w", encoding="utf-8") as out_file: for line in py_file: - if line.rstrip() in ( + if line.rstrip() in { f"# pylint: disable=missing-module-docstring{signature}", f"# pylint: disable=too-many-lines{signature}", "# pylint: disable=invalid-name; silent invalid module name", "# pylint: enable=invalid-name; silent", - ): + }: something_changed = True continue if "# pylint: disable-next=" in line: @@ -193,9 +193,9 @@ def reset(py_filename: str, signature: str) -> None: # Other tooling comments may follow pylint comments # Make sure to add *back* that comment before proceeding - other_comment_pos = line.find('#', comment_pos + 1) + other_comment_pos = line.find("#", comment_pos + 1) if other_comment_pos > 0: - stripped_line += ' ' + line[other_comment_pos:].rstrip() + stripped_line += " " + line[other_comment_pos:].rstrip() line = stripped_line + EOL something_changed = True out_file.write(line) @@ -224,16 +224,16 @@ def statistics(py_filenames: List[str], signature: str) -> None: if ( comment_pos > 0 or line.lstrip().startswith("# pylint: disable-next=") - or line.rstrip() in ( + or line.rstrip() in { f"# pylint: disable=missing-module-docstring{signature}", f"# pylint: disable=too-many-lines{signature}", "# pylint: disable=invalid-name; silent invalid module name", - ) + } ): comment = line.lstrip()[comment_pos:].rstrip() # Other tooling comments may follow pylint comments - other_comment_pos = comment.find('#', 1) + other_comment_pos = comment.find("#", 1) if other_comment_pos > 0: comment = comment[:other_comment_pos].rstrip() @@ -241,7 +241,7 @@ def statistics(py_filenames: List[str], signature: str) -> None: comment = comment[:comment.find("; silent")] # 'comment' may disable several messages: # "# pylint: disable=too-many-branches,too-many-statements" - messages = comment[comment.rfind("=") + 1:].split(';')[0].split(",") + messages = comment[comment.rfind("=") + 1:].split(";")[0].split(",") for message in messages: if message in stats: stats[message] += 1 diff --git a/pylint_silent/__main__.py b/pylint_silent/__main__.py index a7e57b1..6e34fe1 100644 --- a/pylint_silent/__main__.py +++ b/pylint_silent/__main__.py @@ -38,7 +38,7 @@ def main() -> int: ) args = parser.parse_args() - signature = SIGNATURE if args.signature else '' + signature = SIGNATURE if args.signature else "" if args.command == "apply": pylint_logfile = args.filename[0] pylint_silent.apply(pylint_logfile, signature, args.max_line_length) diff --git a/pyproject.toml b/pyproject.toml index 62c01f0..3c43501 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -41,3 +41,31 @@ packages = ["pylint_silent"] [tool.setuptools.dynamic] version = {attr = "pylint_silent.VERSION"} + +[tool.mypy] +enable_error_code = "ignore-without-code" +strict = true + +[tool.ruff.lint] +select = [ + "F", # pyflakes + "E", # pycodestyle + "W", # pycodestyle + "N", # pep8-naming + "UP", # pyupgrade + "B", # flake-bugbear + "G", # flake8-logging-format + "PIE", # flake-pie + "PT", # flake8-pytest-style + "Q", # flake8-quotes + "RET", # flake8-return + "SIM", # flake8-simplify + "PL", # pylint + "RUF", # ruff +] + +ignore = [ + "UP015", # Unnecessary open mode parameters + "PLR2004", # Magic value used in comparison, consider replacing {value} with a constant variable + "PLW2901", # Outer {outer_kind} variable {name} overwritten by inner {inner_kind} target +] diff --git a/tests/test_samples.py b/tests/test_samples.py index dbe4c44..83fb275 100644 --- a/tests/test_samples.py +++ b/tests/test_samples.py @@ -64,9 +64,8 @@ def __init__(self, tmpdir: str) -> None: def run_pylint(self, *args: str) -> Optional[int]: """Run pylint on our python test files.""" - pylint_opts = ((self.temp_sample_filename, - self.temp_sample_after_apply) + args,) - proc = multiprocessing.Process(target=pylint.lint.Run, args=pylint_opts) + pylint_opts = (self.temp_sample_filename, self.temp_sample_after_apply, *args) + proc = multiprocessing.Process(target=pylint.lint.Run, args=(pylint_opts,)) proc.start() proc.join() return proc.exitcode diff --git a/tox.ini b/tox.ini index 85bd141..24fc3b0 100644 --- a/tox.ini +++ b/tox.ini @@ -2,6 +2,7 @@ envlist = mypy pep8 + ruff pylint {py38,py312}-pytest @@ -19,7 +20,7 @@ deps = pytest commands = - mypy --config-file=tox.ini --strict {[tox]files} + mypy {[tox]files} [testenv:pep8] deps = @@ -28,6 +29,13 @@ deps = commands = pycodestyle {[tox]files} +[testenv:ruff] +deps = + ruff ~= 0.2.0 + +commands = + ruff {[tox]files} + [testenv:pylint] deps = pylint @@ -47,12 +55,6 @@ commands = coverage html coverage report --show-missing --fail-under=100 -[mypy] -show_error_codes = True - -[mypy-pylint.*] -ignore_missing_imports = True - [pycodestyle] # Line length of 88 copied from black. max-line-length = 88