Skip to content

Commit

Permalink
chore: replace pylint and black with ruff
Browse files Browse the repository at this point in the history
Ruff is a drop-in replacement for black and also handles linting
for us in a lot nicer way than pylint. More specifically it is a lot
faster than the alternatives and allows us to enforce PyFormat
for strings rather than percent-format or f-strings.

Signed-off-by: Freya Gustavsson <[email protected]>
  • Loading branch information
Venefilyn committed Sep 24, 2024
1 parent 065ff23 commit dfd5207
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 148 deletions.
3 changes: 2 additions & 1 deletion .devcontainer/alma9/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"GitHub.vscode-pull-request-github",
"Cameron.vscode-pytest",
"njpwerner.autodocstring",
"ms-python.python"
"ms-python.python",
"charliermarsh.ruff"
]
}
}
Expand Down
32 changes: 10 additions & 22 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
minimum_pre_commit_version: "2.9.0"
repos:
- repo: local
hooks:
- id: black
name: black
entry: black
types: [python]
language: python
language_version: "python3"
additional_dependencies: [click==7.1.2, black==21.12b0]
- repo: https://github.com/pylint-dev/pylint
rev: v3.2.7
hooks:
- id: pylint
args: [
"-sn", # Don't display the score
"--rcfile=.pylintrc", # Link to your config file
]
- repo: "https://github.com/timothycrosley/isort"
rev: 5.13.2
hooks:
- id: isort
additional_dependencies: [toml]
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.6.7
hooks:
# Run the linter.
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format

- repo: "https://github.com/pre-commit/pre-commit-hooks"
rev: "v4.6.0"
hooks:
Expand Down
98 changes: 0 additions & 98 deletions .pylintrc

This file was deleted.

26 changes: 15 additions & 11 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,17 @@ can run a series of pre-defined hooks against our codebase to keep it clean and
maintainable. Here is an example of output from `pre-commit` being run:

```
(.venv3) [r0x0d@fedora convert2rhel]$ pre-commit run --all-files
Format code (black)......................................................Passed
isort....................................................................Passed
Fix End of Files.........................................................Passed
Trim Trailing Whitespace.................................................Passed
Check JSON...........................................(no files to check)Skipped
Check Toml...............................................................Passed
Check Yaml...............................................................Passed
Check for merge conflicts................................................Passed
$ pre-commit run --all-files
ruff.....................................................................Passed
ruff-format..............................................................Passed
fix end of files.........................................................Passed
trim trailing whitespace.................................................Passed
check toml...............................................................Passed
check yaml...............................................................Passed
check for merge conflicts................................................Passed
check json5..............................................................Passed
tmt tests lint...........................................................Passed
vulture..................................................................Passed
```

We automatically run `pre-commit` as part of our CI infrastructure as well. If you have a PR it will run and see if everything passes. Sometimes there may be an outage or unexpected result from `pre-commit`, if that happens you can create a new comment on the PR saying:
Expand Down Expand Up @@ -216,8 +218,10 @@ Code](https://www.python.org/dev/peps/pep-0008/) as well some
linters/formatters that are run directly from
[pre-commit](https://pre-commit.com).

Don't worry, most of the code formatting and styleguide is handled by
[black](https://github.com/psf/black), an awesome formatter for python code.
Don't worry, most of the code formatting and linting is handled by
[ruff](https://docs.astral.sh/ruff/), an awesome formatter for python code.

We make use of [PyFormat instead of percent-formatting](https://pyformat.info/), read more in the link. We cannot make use of f-strings due to requiring backwards compatibility.

#### Documenting the code

Expand Down
5 changes: 5 additions & 0 deletions convert2rhel/unit_tests/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[tool.ruff]
# Extend the `pyproject.toml` file in the parent directory...
extend = "../../pyproject.toml"

ignore = ["E402"]
40 changes: 24 additions & 16 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
[tool.black]
line-length=120
target-version=["py27", "py36", "py37", "py38", "py39", "py310"]

[tool.isort]
profile = "black"
atomic=true
line_length = 120
lines_after_imports=2
lines_between_types=1
known_third_party=[
"pytest",
"click",
]
known_first_party=["convert2rhel"]

[tool.vulture]
exclude = ["convert2rhel/unit_tests/actions/data"]
ignore_decorators = ["@pytest.fixture"]
ignore_names = ["pretend_os", "stage_actions"]
min_confidence = 90
paths = ["convert2rhel", "scripts/whitelist.py"]

[tool.ruff]
builtins = ["convert2rhel", "six"]


line-length = 120
fix = true

# We technically only support py27 py36 due to el7 and el8 but ruff only
# supports py37 as minimum. This is not a huge issue as any rules we do not
# want we can disable
target-version = "py37"

[tool.ruff.lint]
# Set our root_logger as the de-facto logger
logger-objects = ["convert2rhel.logger.root_logger"]
extend-select = [
"UP031", # Forbid percent-string formatting
]

[tool.ruff.lint.per-file-ignores]
"whitelist.py" = [
"F821", # For ignoring undefined variables in this file
]

0 comments on commit dfd5207

Please sign in to comment.