Skip to content

Commit 15a9190

Browse files
authored
using ruff instead of flake8 (AtsushiSakai#787)
* using ruff instead of flake8 * using ruff instead of flake8 * using ruff instead of flake8
1 parent ffd8602 commit 15a9190

File tree

5 files changed

+62
-19
lines changed

5 files changed

+62
-19
lines changed

docs/conf.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
#
32
# Configuration file for the Sphinx documentation builder.
43
#

docs/getting_started_main.rst

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,22 @@ Requirements
3434

3535
For development:
3636

37-
- pytest (for unit tests)
38-
- pytest-xdist (for parallel unit tests)
39-
- mypy (for type check)
40-
- sphinx (for document generation)
41-
- pycodestyle (for code style check)
37+
- `pytest`_ (for unit tests)
38+
- `pytest-xdist`_ (for parallel unit tests)
39+
- `mypy`_ (for type check)
40+
- `sphinx`_ (for document generation)
41+
- `ruff`_ (for code style check)
4242

4343
.. _`Python 3.11.x`: https://www.python.org/
4444
.. _`NumPy`: https://numpy.org/
4545
.. _`SciPy`: https://scipy.org/
4646
.. _`Matplotlib`: https://matplotlib.org/
4747
.. _`cvxpy`: https://www.cvxpy.org/
48+
.. _`pytest`: https://docs.pytest.org/en/latest/
49+
.. _`pytest-xdist`: https://github.com/pytest-dev/pytest-xdist
50+
.. _`mypy`: https://mypy-lang.org/
51+
.. _`sphinx`: https://www.sphinx-doc.org/en/master/index.html
52+
.. _`ruff`: https://github.com/charliermarsh/ruff
4853

4954

5055
How to use

requirements/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ cvxpy == 1.3.0
55
pytest == 7.2.1 # For unit test
66
pytest-xdist == 3.1.0 # For unit test
77
mypy == 0.991 # For unit test
8-
flake8 == 5.0.4 # For unit test
8+
ruff == 0.0.237 # For unit test

ruff.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
line-length = 88
2+
3+
select = ["F", "E", "W", "UP"]
4+
ignore = ["E501", "E741"]
5+
exclude = [
6+
]
7+
8+
# Assume Python 3.11
9+
target-version = "py311"
10+
11+
[per-file-ignores]
12+
13+
[mccabe]
14+
# Unlike Flake8, default to a complexity level of 10.
15+
max-complexity = 10
16+
17+
[pydocstyle]
18+
convention = "numpy"

tests/test_diff_codestyle.py renamed to tests/test_codestyle.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""
2-
Diff based code style checker with flake8
2+
Diff code style checker with ruff
33
44
This code come from:
5-
https://github.com/scipy/scipy/blob/main/tools/lint_diff.py
5+
https://github.com/scipy/scipy/blob/main/tools/lint.py
66
77
Scipy's licence: https://github.com/scipy/scipy/blob/main/LICENSE.txt
88
Copyright (c) 2001-2002 Enthought, Inc. 2003-2022, SciPy Developers.
@@ -37,9 +37,30 @@
3737
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3838
"""
3939
import conftest
40+
import os
4041
import subprocess
4142

4243

44+
CONFIG = os.path.join(
45+
os.path.abspath(os.path.dirname(os.path.dirname(__file__))),
46+
'ruff.toml',
47+
)
48+
49+
ROOT_DIR = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
50+
51+
52+
def run_ruff(files, fix):
53+
if not files:
54+
return 0, ""
55+
args = ['--fix'] if fix else []
56+
res = subprocess.run(
57+
['ruff', f'--config={CONFIG}'] + args + files,
58+
stdout=subprocess.PIPE,
59+
encoding='utf-8'
60+
)
61+
return res.returncode, res.stdout
62+
63+
4364
def rev_list(branch, num_commits):
4465
"""List commits in reverse chronological order.
4566
Only the first `num_commits` are shown.
@@ -89,23 +110,23 @@ def find_diff(sha):
89110
return res.stdout
90111

91112

92-
def run_flake8(diff):
93-
"""Run flake8 on the given diff."""
113+
def diff_files(sha):
114+
"""Find the diff since the given SHA."""
94115
res = subprocess.run(
95-
['flake8', '--diff', '--ignore',
96-
'E402' # top level import for sys.path.append
97-
],
98-
input=diff,
116+
['git', 'diff', '--name-only', '--diff-filter=ACMR', '-z', sha, '--',
117+
'*.py', '*.pyx', '*.pxd', '*.pxi'],
99118
stdout=subprocess.PIPE,
100-
encoding='utf-8',
119+
encoding='utf-8'
101120
)
102-
return res.returncode, res.stdout
121+
res.check_returncode()
122+
return [os.path.join(ROOT_DIR, f) for f in res.stdout.split('\0') if f]
103123

104124

105125
def test():
106126
branch_commit = find_branch_point("origin/master")
107-
diff = find_diff(branch_commit)
108-
rc, errors = run_flake8(diff)
127+
files = diff_files(branch_commit)
128+
print(files)
129+
rc, errors = run_ruff(files, fix=False)
109130
if errors:
110131
print(errors)
111132
else:

0 commit comments

Comments
 (0)