Skip to content

Commit

Permalink
Added ruff checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
udifuchs committed Feb 12, 2024
1 parent b83094a commit 04d715c
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 23 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 10 additions & 10 deletions pylint_silent/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -224,24 +224,24 @@ 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()

if comment.endswith("; silent"):
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
Expand Down
2 changes: 1 addition & 1 deletion pylint_silent/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
5 changes: 2 additions & 3 deletions tests/test_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 9 additions & 7 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
envlist =
mypy
pep8
ruff
pylint
{py38,py312}-pytest

Expand All @@ -19,7 +20,7 @@ deps =
pytest

commands =
mypy --config-file=tox.ini --strict {[tox]files}
mypy {[tox]files}

[testenv:pep8]
deps =
Expand All @@ -28,6 +29,13 @@ deps =
commands =
pycodestyle {[tox]files}

[testenv:ruff]
deps =
ruff ~= 0.2.0

commands =
ruff {[tox]files}

[testenv:pylint]
deps =
pylint
Expand All @@ -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
Expand Down

0 comments on commit 04d715c

Please sign in to comment.