Skip to content

using ruff instead of flake8 #787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# Configuration file for the Sphinx documentation builder.
#
Expand Down
15 changes: 10 additions & 5 deletions docs/getting_started_main.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 18 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -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"
45 changes: 33 additions & 12 deletions tests/test_diff_codestyle.py → tests/test_codestyle.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -37,9 +37,30 @@
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.
Expand Down Expand Up @@ -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:
Expand Down