Skip to content

Commit 51a3087

Browse files
committed
Migrate linting to ruff
- Configure `ruff` as a drop in replacement for `flake8`, `isort` and `pyupgrade` - Lint for required `from __future__ import annotations` in `src/` - Enable automatic fixing of linting errors in pre-commit - Applied ruff `--fix` for `I001` on `src/pytest_bdd/parser.py` - Applied ruff `--fix` for `UP032` on `src/pytest_bdd/gherkin_terminal_reporter.py` - Applied ruff `--fix` for `tests/feature/test_feature_base_dir.py` - Applied ruff `--fix` for `tests/feature/test_feature_base_dir.py` - Ignore `B904` error on `src/pytest_bdd/scenario.py` - Fix invalid pre-commit config error due to indentation syntax error - Freeze pre-commit dependency versions
1 parent 7decb11 commit 51a3087

File tree

6 files changed

+39
-59
lines changed

6 files changed

+39
-59
lines changed

.pre-commit-config.yaml

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,16 @@
11
# See https://pre-commit.com for more information
22
# See https://pre-commit.com/hooks.html for more hooks
33
repos:
4-
- repo: https://github.com/psf/black
5-
# If you update the version here, also update it in tox.ini (py*-pytestlatest-linters)
6-
rev: 24.10.0
4+
- repo: https://github.com/pre-commit/pre-commit-hooks
5+
rev: cef0300fd0fc4d2a87a85fa2093c6b283ea36f4b # frozen: v5.0.0
76
hooks:
8-
- id: black
9-
- repo: https://github.com/pycqa/isort
10-
rev: 5.13.2
7+
- id: trailing-whitespace
8+
- id: end-of-file-fixer
9+
- id: check-yaml
10+
- id: check-added-large-files
11+
- repo: https://github.com/astral-sh/ruff-pre-commit
12+
rev: f0b5944bef86f50d875305821a0ab0d8c601e465 # frozen: v0.8.4
1113
hooks:
12-
- id: isort
13-
name: isort (python)
14-
- repo: https://github.com/pre-commit/pre-commit-hooks
15-
rev: v5.0.0
16-
hooks:
17-
- id: trailing-whitespace
18-
- id: end-of-file-fixer
19-
- id: check-yaml
20-
- id: check-added-large-files
21-
- repo: https://github.com/asottile/pyupgrade
22-
rev: v3.19.0
23-
hooks:
24-
- id: pyupgrade
25-
args: ["--py39-plus"]
26-
- repo: https://github.com/pycqa/flake8
27-
rev: "7.1.1"
28-
hooks:
29-
- id: flake8
30-
additional_dependencies: [
31-
"flake8-pyproject",
32-
"flake8-bugbear",
33-
]
14+
- id: ruff
15+
args: [ --fix ]
16+
- id: ruff-format

pyproject.toml

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,25 @@ sphinx-autobuild = "*"
5757
requires = ["poetry-core>=1.0.0"]
5858
build-backend = "poetry.core.masonry.api"
5959

60-
[tool.black]
60+
[tool.ruff]
6161
line-length = 120
62-
target-version = ["py39", "py310", "py311", "py312", "py313"]
63-
64-
[tool.flake8]
65-
# E1: indentation: already covered by `black`
66-
# E2: whitespace: already covered by `black`
67-
# E3: blank line: already covered by `black`
68-
# E501: line length: already covered by `black`
69-
extend-ignore = "E1,E2,E3,E501"
62+
target-version = "py39"
63+
lint.select = [
64+
"B", # flake8-bugbear
65+
"E4", # pycodestyle - error - import
66+
"E7", # pycodestyle - error - statement
67+
"E9", # pycodestyle - error - runtime
68+
"F", # pyflakes
69+
"I", # isort
70+
"UP", # pyupgrade
71+
]
72+
lint.isort.required-imports = [
73+
"from __future__ import annotations",
74+
]
7075

71-
[tool.isort]
72-
profile = "black"
73-
line_length = 120
74-
multi_line_output = 3
76+
[tool.ruff.lint.per-file-ignores]
77+
# Lint `I002` (required imports) for `from __future__ import annotations` in `src/`
78+
"!src/**.py" = ["I002"]
7579

7680
[tool.coverage.report]
7781
exclude_lines = [

src/pytest_bdd/gherkin_terminal_reporter.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ def configure(config: Config) -> None:
3131
raise Exception(
3232
"gherkin-terminal-reporter is not compatible with any other terminal reporter."
3333
"You can use only one terminal reporter."
34-
"Currently '{0}' is used."
35-
"Please decide to use one by deactivating {0} or gherkin-terminal-reporter.".format(
36-
current_reporter.__class__
37-
)
34+
f"Currently '{current_reporter.__class__}' is used."
35+
f"Please decide to use one by deactivating {current_reporter.__class__} "
36+
"or gherkin-terminal-reporter."
3837
)
3938
gherkin_reporter = GherkinTerminalReporter(config)
4039
config.pluginmanager.unregister(current_reporter)

src/pytest_bdd/parser.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@
1010

1111
from .exceptions import StepError
1212
from .gherkin_parser import Background as GherkinBackground
13-
from .gherkin_parser import DataTable
13+
from .gherkin_parser import DataTable, GherkinDocument, get_gherkin_document
1414
from .gherkin_parser import Feature as GherkinFeature
15-
from .gherkin_parser import GherkinDocument
1615
from .gherkin_parser import Rule as GherkinRule
1716
from .gherkin_parser import Scenario as GherkinScenario
1817
from .gherkin_parser import Step as GherkinStep
1918
from .gherkin_parser import Tag as GherkinTag
20-
from .gherkin_parser import get_gherkin_document
2119
from .types import STEP_TYPE_BY_PARSER_KEYWORD
2220

2321
PARAM_RE = re.compile(r"<(.+?)>")

src/pytest_bdd/scenario.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ def scenario(
384384
scenario = feature.scenarios[scenario_name]
385385
except KeyError:
386386
feature_name = feature.name or "[Empty]"
387-
raise exceptions.ScenarioNotFound(
387+
raise exceptions.ScenarioNotFound( # noqa: B904
388388
f'Scenario "{scenario_name}" in feature "{feature_name}" in {feature.filename} is not found.'
389389
)
390390

tests/feature/test_feature_base_dir.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,10 @@ def test_feature_path_by_param_ok(pytester, base_dir):
5959

6060
def prepare_testdir(pytester, ini_base_dir):
6161
pytester.makeini(
62-
"""
62+
f"""
6363
[pytest]
64-
bdd_features_base_dir={}
65-
""".format(
66-
ini_base_dir
67-
)
64+
bdd_features_base_dir={ini_base_dir}
65+
"""
6866
)
6967

7068
feature_file = pytester.mkdir("features").joinpath("steps.feature")
@@ -77,7 +75,7 @@ def prepare_testdir(pytester, ini_base_dir):
7775
)
7876

7977
pytester.makepyfile(
80-
"""
78+
f"""
8179
import os.path
8280
8381
import pytest
@@ -103,7 +101,7 @@ def test_not_found_by_ini(scenario_name, multiple):
103101
scenarios(FEATURE)
104102
else:
105103
scenario(FEATURE, scenario_name)
106-
assert os.path.abspath(os.path.join('{}', FEATURE)) in str(exc.value)
104+
assert os.path.abspath(os.path.join('{ini_base_dir}', FEATURE)) in str(exc.value)
107105
108106
109107
@pytest.mark.parametrize(
@@ -145,7 +143,5 @@ def test_ok_by_param(scenario_name, multiple):
145143
else:
146144
scenario(FEATURE, scenario_name, features_base_dir='features')
147145
148-
""".format(
149-
ini_base_dir
150-
)
146+
"""
151147
)

0 commit comments

Comments
 (0)