Skip to content

Commit

Permalink
Replace flake8 with ruff (#73)
Browse files Browse the repository at this point in the history
* Replace flake8 and isort with ruff
* added tox configuration
  • Loading branch information
fpgmaas authored Jan 26, 2023
1 parent 92d382c commit 65fb877
Show file tree
Hide file tree
Showing 14 changed files with 193 additions and 118 deletions.
12 changes: 1 addition & 11 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,4 @@ repos:
rev: "22.8.0"
hooks:
- id: black
exclude: ^{{cookiecutter.project_name}}

- repo: https://github.com/PyCQA/isort
rev: "5.10.1"
hooks:
- id: isort

- repo: https://github.com/PyCQA/flake8
rev: "5.0.4"
hooks:
- id: flake8
exclude: ^{{cookiecutter.project_name}}
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ check: ## Run code quality tools.
@poetry lock --check
@echo "🚀 Linting code: Running pre-commit"
@poetry run pre-commit run -a
@echo "🚀 Linting with ruff"
@poetry run ruff hooks tests cookiecutter_poetry --config pyproject.toml
@echo "🚀 Static type checking: Running mypy"
@poetry run mypy
@echo "🚀 Checking for obsolete dependencies: Running deptry"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ repository to generate the file structure for a Python project that uses
- [Poetry](https://python-poetry.org/) for dependency management
- CI/CD with [GitHub Actions](https://github.com/features/actions)
- Pre-commit hooks with [pre-commit](https://pre-commit.com/)
- Formatting with [black](https://pypi.org/project/black/) and [isort](https://pycqa.github.io/isort/index.html)
- Linting with [flake8](https://flake8.pycqa.org/en/latest/)
- Formatting with [black](https://pypi.org/project/black/).
- Linting with [ruff](https://github.com/charliermarsh/ruff)
- Static type checking with [mypy](https://mypy.readthedocs.io/en/stable/)
- Dependency checking with [cookiecutter-poetry](https://fpgmaas.github.io/cookiecutter-poetry/)
- Publishing to [Pypi](https://pypi.org) or [Artifactory](https://jfrog.com/artifactory) by creating a new release on GitHub
Expand Down
79 changes: 49 additions & 30 deletions docs/features/linting.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ Note that this requires the pre-commit hooks to be installed.

This command will run the following tools:

## isort

[isort](https://pycqa.github.io/isort/index.html) is run to sort the imports and it is configured through `pyproject.toml`:

```toml
[tool.isort]
profile = "black"
```

## black

[black](https://pypi.org/project/black/) is used to format the code, and it is configured through `pyproject.toml`:
Expand All @@ -34,30 +25,58 @@ fast = true
To exclude directories or files, add an `exclude` argument to `pre-commit-config.yaml`. Note that adding an `exclude` argument to `pyproject.toml`
will not work, see also [here](https://stackoverflow.com/a/61046953/8037249).

## flake8
## ruff

[flake8](https://flake8.pycqa.org/en/latest/) is used to check the code style, and it is configured through `tox.ini`:
[ruff](https://github.com/charliermarsh/ruff) is used to check the code style, and it is configured through `pyproject.toml`:

```
[flake8]
per-file-ignores = __init__.py:F401
# PEP-8 The following are ignored:
# E731 do not assign a lambda expression, use a def
# E203 whitespace before ':'
# E501 line too long
# W503 line break before binary operator
# W605 invalid escape sequence
ignore = E731, E203, E501, W503, W605
exclude =
.git,
__pycache__,
docs/source/conf.py,
old,
build,
dist,
.venv,
max-complexity = 10
max-line-length = 120
[tool.ruff]
target-version = "py37"
line-length = 120
fix = false
select = [
# flake8-2020
"YTT",
# flake8-bandit
"S",
# flake8-bugbear
"B",
# flake8-builtins
"A",
# flake8-comprehensions
"C4",
# flake8-debugger
"T10",
# flake8-print
"T20",
# flake8-simplify
"SIM",
# isort
"I",
# mccabe
"C90",
# pycodestyle
"E", "W",
# pyflakes
"F",
# pygrep-hooks
"PGH",
# pyupgrade
"UP",
# ruff
"RUF",
# tryceratops
"TRY",
]
ignore = [
# LineTooLong
"E501",
# DoNotAssignLambda
"E731",
]
[tool.ruff.per-file-ignores]
"tests/*" = ["S101"]
```

# mypy
Expand Down
2 changes: 1 addition & 1 deletion docs/features/makefile.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ available:

```
install Install the poetry environment and install the pre-commit hooks
check Lint and check code by running black, isort, flake8, mypy and deptry.
check Lint and check code by running black, ruff, mypy and deptry.
test Test the code with pytest
build Build wheel file using poetry
clean-build clean build artifacts
Expand Down
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ A project generated with ``cookiecutter-poetry`` supports the following features
- [Poetry](https://python-poetry.org/) for dependency management
- CI/CD with [GitHub Actions](https://github.com/features/actions)
- Pre-commit hooks with [pre-commit](https://pre-commit.com/)
- Formatting with [black](https://pypi.org/project/black/) and [isort](https://pycqa.github.io/isort/index.html)
- Linting with [flake8](https://flake8.pycqa.org/en/latest/)
- Formatting with [black](https://pypi.org/project/black/).
- Linting with [ruff](https://github.com/charliermarsh/ruff)
- Static type checking with [mypy](https://mypy.readthedocs.io/en/stable/)
- Dependency checking with [cookiecutter-poetry](https://fpgmaas.github.io/cookiecutter-poetry/)
- Publishing to [Pypi](https://pypi.org) or [Artifactory](https://jfrog.com/artifactory) by creating a new release on GitHub
Expand Down
28 changes: 27 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 48 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ tox = "^3.25.1"
deptry = "^0.6.4"
mypy = "^0.991"
pytest-cov = "^4.0.0"
ruff = "^0.0.235"

[tool.poetry.group.docs.dependencies]
mkdocs = "^1.4.2"
Expand All @@ -42,10 +43,6 @@ line-length = 120
target-version = ['py37']
preview = true

[tool.isort]
profile = "black"
skip = ["{{cookiecutter.project_name}}",".venv"]

[tool.poetry.scripts]
ccp = 'cookiecutter_poetry.cli:main'

Expand All @@ -70,4 +67,50 @@ show_error_codes = "True"
extend_exclude = [
"{{cookiecutter.project_name}}"
]
ignore_obsolete = ["cookiecutter"]
ignore_obsolete = ["cookiecutter"]

[tool.ruff]
target-version = "py37"
line-length = 120
fix = true
select = [
# flake8-2020
"YTT",
# flake8-bandit
"S",
# flake8-bugbear
"B",
# flake8-builtins
"A",
# flake8-comprehensions
"C4",
# flake8-debugger
"T10",
# flake8-simplify
"SIM",
# isort
"I",
# mccabe
"C90",
# pycodestyle
"E", "W",
# pyflakes
"F",
# pygrep-hooks
"PGH",
# pyupgrade
"UP",
# ruff
"RUF",
# tryceratops
"TRY",
]
ignore = [
# LineTooLong
"E501",
# DoNotAssignLambda
"E731",
]

[tool.ruff.per-file-ignores]
"tests/*" = ["S101"]
7 changes: 4 additions & 3 deletions tests/test_cookiecutter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def run_within_dir(path: str):


def file_contains_text(file: str, text: str) -> bool:
return open(file, "r").read().find(text) != -1
with open(file) as f:
return f.read().find(text) != -1


def test_bake_project(cookies):
Expand All @@ -39,8 +40,8 @@ def test_using_pytest(cookies, tmp_path):

# Install the poetry environment and run the tests.
with run_within_dir(str(result.project_path)):
subprocess.check_call(shlex.split("poetry install --no-interaction")) == 0
subprocess.check_call(shlex.split("poetry run make test")) == 0
assert subprocess.check_call(shlex.split("poetry install --no-interaction")) == 0
assert subprocess.check_call(shlex.split("poetry run make test")) == 0


def test_cicd_contains_artifactory_secrets(cookies, tmp_path):
Expand Down
25 changes: 2 additions & 23 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,8 @@ python =

[testenv]
passenv = PYTHON_VERSION
whitelist_externals = poetry
allowlist_externals = poetry
commands =
poetry install -v
pytest --doctest-modules tests --cov --cov-config=pyproject.toml --cov-report=xml
mypy

[flake8]
per-file-ignores = __init__.py:F401
# PEP-8 The following are ignored:
# E731 do not assign a lambda expression, use a def
# E203 whitespace before ':'
# E501 line too long
# W503 line break before binary operator
# W605 invalid escape sequence
ignore = E731, E203, E501, W503, W605
exclude =
.git,
__pycache__,
docs/source/conf.py,
old,
build,
dist,
.venv,
*cookiecutter.project_name*
max-complexity = 10
max-line-length = 120
mypy
21 changes: 6 additions & 15 deletions {{cookiecutter.project_name}}/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
repos:
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.0.230"
hooks:
- id: ruff

- repo: https://github.com/psf/black
rev: "22.8.0"
hooks:
- id: black

- repo: https://github.com/PyCQA/isort
rev: "5.10.1"
hooks:
- id: isort

- repo: https://github.com/PyCQA/flake8
rev: "5.0.4"
hooks:
- id: flake8
additional_dependencies:
- flake8-bugbear==22.9.23
- flake8-comprehensions==3.10.0
- flake8-simplify==0.19.3
- id: black
Loading

0 comments on commit 65fb877

Please sign in to comment.