test/static-code: change approach to running ruff

Instead of running ruff on *.py, with a large number of exemptions (all
of test/verify, test/common, tools/, etc) we now run it on all of the
Python files that test/static-code can find.

Previous commits have done some amount of code cleanups on the
newly-exposed code, but we deal with most remaining issues by using
per-directory ruff.toml files which let us adjust the configuration in a
subdirectory.  We make sure to add those to test/static-code so that
they will be available for partial linting during hooks.
This commit is contained in:
Allison Karlitskaya 2023-05-25 15:35:51 +02:00
parent 816e5815b5
commit fafe29acf1
8 changed files with 81 additions and 6 deletions

View File

@ -0,0 +1,5 @@
extend = "../../../pyproject.toml"
ignore = [
"PT009", # Use a regular `assert` instead of unittest-style `assertEqual`
"FBT003" # Boolean positional value in function call
]

13
pkg/ruff.toml Normal file
View File

@ -0,0 +1,13 @@
extend = "../pyproject.toml"
ignore = [
"E501", # https://github.com/charliermarsh/ruff/issues/3206#issuecomment-1562681390
"A001", # Variable is shadowing a Python builtin
"A002", # Argument is shadowing a Python builtin
"EXE001", # Shebang is present but file is not executable
"F821", # Undefined name
"FBT002", # Boolean default value in function definition
"FBT003", # Boolean positional value in function call
"I001", # Import block is un-sorted or un-formatted
"RUF005", # Consider `[..., *var, ...]` instead of concatenation
]

View File

@ -50,10 +50,6 @@ exclude = [
".git/",
"modules/",
"node_modules/",
"pkg/",
"test/common/",
"test/verify/",
"tools/",
]
ignore = [
"A003", # Class attribute is shadowing a python builtin

4
src/client/ruff.toml Normal file
View File

@ -0,0 +1,4 @@
extend = "../../pyproject.toml"
ignore = [
"FBT003", # Boolean positional value in function call
]

39
test/common/ruff.toml Normal file
View File

@ -0,0 +1,39 @@
extend = "../../pyproject.toml"
ignore = [
"E501", # https://github.com/charliermarsh/ruff/issues/3206#issuecomment-1562681390
"A001", # Variable is shadowing a Python builtin
"A002", # Argument is shadowing a Python builtin
"B006", # Do not use mutable data structures for argument defaults
"B007", # Loop control variable` not used within loop body
"B010", # Do not call `setattr` with a constant attribute value. It is not any safer than normal property access.
"C417", # Unnecessary `map` usage (rewrite using a `list` comprehension)
"FBT001", # Boolean positional arg in function definition
"FBT002", # Boolean default value in function definition
"FBT003", # Boolean positional value in function call
"PT009", # Use a regular `assert` instead of unittest-style `assertEqual`
]
[isort]
section-order = [
"future", "standard-library", "third-party",
"test-common", "bots",
"first-party", "local-folder"
]
force-to-top = ["parent"]
[isort.sections]
"bots" = [
"lib",
"machine_core",
"task",
"testvm",
]
"test-common" = [
"cdp",
"parent",
"netlib",
"packagelib",
"storagelib",
"testlib",
]

View File

@ -4,6 +4,12 @@
set -eu
# requires: pyproject.toml
# requires: containers/flatpak/test/ruff.toml
# requires: pkg/ruff.toml
# requires: src/client/ruff.toml
# requires: test/common/ruff.toml
# requires: test/verify/ruff.toml
# requires: tools/vulture-suppressions/ruff.toml
# we consider any function named test_* to be a test case
# each test is considered to succeed if it exits with no output
@ -33,8 +39,7 @@ test_flake8() {
test_ruff() {
command -v ruff >/dev/null || skip 'no ruff'
test -n "$(ruff check --show-files . 2>/dev/null)" || return 0 # no python changes
ruff check --no-cache .
find_python_files | xargs -r -0 ruff check --no-cache
}
if [ "${WITH_PARTIAL_TREE:-0}" = 0 ]; then

9
test/verify/ruff.toml Normal file
View File

@ -0,0 +1,9 @@
extend = "../common/ruff.toml"
ignore = [
"PT", # This is not pytest code
"B023", # Function definition does not bind loop variable
"B905", # `zip()` without an explicit `strict=` parameter
"DTZ005", # The use of `datetime.datetime.now()` without `tz` argument is not allowed
"RUF005", # Consider `[..., *var, ...]` instead of concatenation
]

View File

@ -0,0 +1,4 @@
extend = "../../pyproject.toml"
ignore = [
"B018" # Found useless expression. Either assign it to a variable or remove it.
]