From 15a91900608199e9bccdf68b04786b30f6772ad5 Mon Sep 17 00:00:00 2001 From: Atsushi Sakai Date: Mon, 30 Jan 2023 21:33:49 +0900 Subject: [PATCH] using ruff instead of flake8 (#787) * using ruff instead of flake8 * using ruff instead of flake8 * using ruff instead of flake8 --- docs/conf.py | 1 - docs/getting_started_main.rst | 15 ++++--- requirements/requirements.txt | 2 +- ruff.toml | 18 ++++++++ ...st_diff_codestyle.py => test_codestyle.py} | 45 ++++++++++++++----- 5 files changed, 62 insertions(+), 19 deletions(-) create mode 100644 ruff.toml rename tests/{test_diff_codestyle.py => test_codestyle.py} (78%) diff --git a/docs/conf.py b/docs/conf.py index 24b5518b..e7c64d07 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Configuration file for the Sphinx documentation builder. # diff --git a/docs/getting_started_main.rst b/docs/getting_started_main.rst index 0e90d672..497b85a2 100644 --- a/docs/getting_started_main.rst +++ b/docs/getting_started_main.rst @@ -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 diff --git a/requirements/requirements.txt b/requirements/requirements.txt index 944c1ce2..a3a337a1 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -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 diff --git a/ruff.toml b/ruff.toml new file mode 100644 index 00000000..fd02032f --- /dev/null +++ b/ruff.toml @@ -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" \ No newline at end of file diff --git a/tests/test_diff_codestyle.py b/tests/test_codestyle.py similarity index 78% rename from tests/test_diff_codestyle.py rename to tests/test_codestyle.py index 89c228e2..2ad3816e 100644 --- a/tests/test_diff_codestyle.py +++ b/tests/test_codestyle.py @@ -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: