Skip to content

Commit

Permalink
use ruff instead of prospector and isort (Refs #336)
Browse files Browse the repository at this point in the history
  • Loading branch information
cwmeijer committed Sep 6, 2023
1 parent a53c275 commit c36c520
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 77 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ an empty Python package. Features include:
- [Python static setup configuration]({{cookiecutter.directory_name}}/setup.cfg),
- Open source software license,
- Continuous integration with [GitHub action workflows]({{cookiecutter.directory_name}}/.github/workflows) for building, testing, link checking and linting,
- Code style checking with [prospector](https://pypi.org/project/prospector/),
- Code style checking with [ruff](https://beta.ruff.rs/),
- [Editorconfig]({{cookiecutter.directory_name}}/.editorconfig),
- Usage and contribution documents:
- [README.md]({{cookiecutter.directory_name}}/README.md) for package users,
Expand Down
20 changes: 4 additions & 16 deletions {{cookiecutter.directory_name}}/.githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,17 @@
echo "Script $0 triggered ..."

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
echo "Starting prospector analysis using configuration from .prospector.yml..."
echo "Starting ruff analysis..."

# quietly run prospector
prospector 1>/dev/null
# quietly run ruff
ruff . --fix

# use return code to abort commit if necessary
if [ $? != "0" ]; then
echo "Commit aborted. Run 'prospector' to see the errors."
echo "Commit aborted. Fix linter issues found by ruff before committing."
exit 1
fi

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
echo "Starting isort analysis using configuration from setup.cfg..."

# recursively run isort on {{ cookiecutter.package_name }}/ directory, don't try to automatically fix anything
isort --recursive --check-only {{ cookiecutter.package_name }}

if [ $? != "0" ]; then
echo "Commit aborted."
echo " Run 'isort --check-only --diff {{ cookiecutter.package_name }}' to see what's wrong."
echo " Run 'isort {{ cookiecutter.package_name }}' to let isort fix problems automatically."
exit 1
fi

echo "Pre-commit checks completed successfully."
exit 0
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Your repository has a [workflow]({{ cookiecutter.repository }}/blob/main/.github
Linter workflow may fail if `description` or `keywords` field in [setup.cfg]({{ cookiecutter.repository }}/blob/main/setup.cfg) is empty. Please update these fields. To validate your changes run:

```shell
prospector
ruff .
```

Enabling [githook](https://git-scm.com/docs/githooks) will automatically lint your code in every commit. You can enable it by running the command below.
Expand Down
6 changes: 2 additions & 4 deletions {{cookiecutter.directory_name}}/.github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,5 @@ jobs:
run: |
python -m pip install --upgrade pip setuptools
python -m pip install .[dev,publishing]
- name: Check style against standards using prospector
run: prospector
- name: Check import order
run: isort --check-only {{ cookiecutter.package_name }} --diff
- name: Check style against standards using ruff
run: ruff
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ jobs:
python --version
- name: Install dependencies
run: python -m pip install .[dev]
- name: Check style against standards using prospector
run: prospector --zero-exit --output-format grouped --output-format pylint:pylint-report.txt
- name: Run unit tests with coverage
run: python -m pytest --cov --cov-report term --cov-report xml --junitxml=xunit-result.xml tests/
- name: Correct coverage paths
Expand Down
29 changes: 0 additions & 29 deletions {{cookiecutter.directory_name}}/.prospector.yml

This file was deleted.

20 changes: 6 additions & 14 deletions {{cookiecutter.directory_name}}/README.dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,20 @@ coverage report

## Running linters locally

For linting we will use [prospector](https://pypi.org/project/prospector/) and to sort imports we will use
[isort](https://pycqa.github.io/isort/). Running the linters requires an activated virtual environment with the
development tools installed.
For linting and sorting imports we will use [ruff](https://beta.ruff.rs/docs/). Running the linters requires an
activated virtual environment with the development tools installed.

```shell
# linter
prospector
ruff .

# recursively check import style for the {{ cookiecutter.package_name }} module only
isort --check-only {{ cookiecutter.package_name }}

# recursively check import style for the {{ cookiecutter.package_name }} module only and show
# any proposed changes as a diff
isort --check-only --diff {{ cookiecutter.package_name }}

# recursively fix import style for the {{ cookiecutter.package_name }} module only
isort {{ cookiecutter.package_name }}
# linter with automatic fixing
ruff . --fix
```

To fix readability of your code style you can use [yapf](https://github.com/google/yapf).

You can enable automatic linting with `prospector` and `isort` on commit by enabling the git hook from `.githooks/pre-commit`, like so:
You can enable automatic linting with `ruff` on commit by enabling the git hook from `.githooks/pre-commit`, like so:

```shell
git config --local core.hooksPath .githooks
Expand Down
74 changes: 74 additions & 0 deletions {{cookiecutter.directory_name}}/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,77 @@ skip_missing_interpreters = true
commands = pytest
extras = dev
"""

[tool.ruff]
# Enable Pyflakes `E` and `F` codes by default.
select = [
"F", # Pyflakes
"E", # pycodestyle (error)
"W", # pycodestyle (warning)
# "C90", # mccabe
"I", # isort
"D", # pydocstyle
# "PL", # Pylint
# "PLC", # Convention
# "PLE", # Error
# "PLR", # Refactor
# "PLW", # Warning

]
ignore = [
'D100', # Missing module docstring
# The following list excludes rules irrelevant to the Google style
'D203',
'D204',
'D213',
'D215',
'D400',
'D401',
'D404',
'D406',
'D407',
'D408',
'D409',
'D413',
]

# Allow autofix for all enabled rules (when `--fix`) is provided.
fixable = ["A", "B", "C", "D", "E", "F", "I"]
unfixable = []

exclude = [
".bzr",
".direnv",
".eggs",
".git",
".hg",
".mypy_cache",
".nox",
".pants.d",
".ruff_cache",
".svn",
".tox",
".venv",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"venv",
".venv",
"scripts",
]
per-file-ignores = {}


# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

target-version = "py39"
line-length = 120

[tool.ruff.isort]
known-first-party = ["distance_explainer"]
force-single-line = true
no-lines-before = ["future","standard-library","third-party","first-party","local-folder"]
11 changes: 1 addition & 10 deletions {{cookiecutter.directory_name}}/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ install_requires =
dev =
bump2version
coverage [toml]
prospector[with_pyroma]
isort
ruff
pytest
pytest-cov
sphinx
Expand All @@ -66,11 +65,3 @@ publishing =

[options.packages.find]
include = {{ cookiecutter.package_name }}, {{ cookiecutter.package_name }}.*

[isort]
lines_after_imports = 2
force_single_line = 1
no_lines_before = FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
known_first_party = {{ cookiecutter.package_name }}
src_paths = {{ cookiecutter.package_name }},tests
line_length = 120

0 comments on commit c36c520

Please sign in to comment.