using ruff instead of flake8 (#787)

* using ruff instead of flake8

* using ruff instead of flake8

* using ruff instead of flake8
This commit is contained in:
Atsushi Sakai
2023-01-30 21:33:49 +09:00
committed by GitHub
parent ffd8602e25
commit 15a9190060
5 changed files with 62 additions and 19 deletions

View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# Configuration file for the Sphinx documentation builder.
#

View File

@@ -34,17 +34,22 @@ Requirements
For development:
- pytest (for unit tests)
- pytest-xdist (for parallel unit tests)
- mypy (for type check)
- sphinx (for document generation)
- pycodestyle (for code style check)
- `pytest`_ (for unit tests)
- `pytest-xdist`_ (for parallel unit tests)
- `mypy`_ (for type check)
- `sphinx`_ (for document generation)
- `ruff`_ (for code style check)
.. _`Python 3.11.x`: https://www.python.org/
.. _`NumPy`: https://numpy.org/
.. _`SciPy`: https://scipy.org/
.. _`Matplotlib`: https://matplotlib.org/
.. _`cvxpy`: https://www.cvxpy.org/
.. _`pytest`: https://docs.pytest.org/en/latest/
.. _`pytest-xdist`: https://github.com/pytest-dev/pytest-xdist
.. _`mypy`: https://mypy-lang.org/
.. _`sphinx`: https://www.sphinx-doc.org/en/master/index.html
.. _`ruff`: https://github.com/charliermarsh/ruff
How to use

View File

@@ -5,4 +5,4 @@ cvxpy == 1.3.0
pytest == 7.2.1 # For unit test
pytest-xdist == 3.1.0 # For unit test
mypy == 0.991 # For unit test
flake8 == 5.0.4 # For unit test
ruff == 0.0.237 # For unit test

18
ruff.toml Normal file
View File

@@ -0,0 +1,18 @@
line-length = 88
select = ["F", "E", "W", "UP"]
ignore = ["E501", "E741"]
exclude = [
]
# Assume Python 3.11
target-version = "py311"
[per-file-ignores]
[mccabe]
# Unlike Flake8, default to a complexity level of 10.
max-complexity = 10
[pydocstyle]
convention = "numpy"

View File

@@ -1,8 +1,8 @@
"""
Diff based code style checker with flake8
Diff code style checker with ruff
This code come from:
https://github.com/scipy/scipy/blob/main/tools/lint_diff.py
https://github.com/scipy/scipy/blob/main/tools/lint.py
Scipy's licence: https://github.com/scipy/scipy/blob/main/LICENSE.txt
Copyright (c) 2001-2002 Enthought, Inc. 2003-2022, SciPy Developers.
@@ -37,9 +37,30 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import conftest
import os
import subprocess
CONFIG = os.path.join(
os.path.abspath(os.path.dirname(os.path.dirname(__file__))),
'ruff.toml',
)
ROOT_DIR = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
def run_ruff(files, fix):
if not files:
return 0, ""
args = ['--fix'] if fix else []
res = subprocess.run(
['ruff', f'--config={CONFIG}'] + args + files,
stdout=subprocess.PIPE,
encoding='utf-8'
)
return res.returncode, res.stdout
def rev_list(branch, num_commits):
"""List commits in reverse chronological order.
Only the first `num_commits` are shown.
@@ -89,23 +110,23 @@ def find_diff(sha):
return res.stdout
def run_flake8(diff):
"""Run flake8 on the given diff."""
def diff_files(sha):
"""Find the diff since the given SHA."""
res = subprocess.run(
['flake8', '--diff', '--ignore',
'E402' # top level import for sys.path.append
],
input=diff,
['git', 'diff', '--name-only', '--diff-filter=ACMR', '-z', sha, '--',
'*.py', '*.pyx', '*.pxd', '*.pxi'],
stdout=subprocess.PIPE,
encoding='utf-8',
encoding='utf-8'
)
return res.returncode, res.stdout
res.check_returncode()
return [os.path.join(ROOT_DIR, f) for f in res.stdout.split('\0') if f]
def test():
branch_commit = find_branch_point("origin/master")
diff = find_diff(branch_commit)
rc, errors = run_flake8(diff)
files = diff_files(branch_commit)
print(files)
rc, errors = run_ruff(files, fix=False)
if errors:
print(errors)
else: