From 901e3b89731f9437ccbbe5d9eb5dff657854568f Mon Sep 17 00:00:00 2001 From: John Lapeyre Date: Tue, 30 May 2023 18:43:36 -0400 Subject: [PATCH] Add ruff to local tests and CI (#10116) * Add ruff to local tests and CI This adds linting using ruff to the relevant configuration files. Only a few rules are enabled and none of them trigger an error in the current state of the repo. * Add comments on running black separately from tox * Simplify and remove potentially bug causing instructions in CONTRIBUTING * Update pyproject.toml Co-authored-by: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com> --------- Co-authored-by: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com> --- .azure/lint-linux.yml | 2 ++ CONTRIBUTING.md | 39 ++++++++++++++++++++++++--------------- Makefile | 7 +++++-- pyproject.toml | 8 ++++++++ requirements-dev.txt | 1 + tox.ini | 2 ++ 6 files changed, 42 insertions(+), 17 deletions(-) diff --git a/.azure/lint-linux.yml b/.azure/lint-linux.yml index aac89bc005ce..5b79db09ae1b 100644 --- a/.azure/lint-linux.yml +++ b/.azure/lint-linux.yml @@ -43,6 +43,8 @@ jobs: - bash: | set -e source test-job/bin/activate + echo "Running ruff" + ruff qiskit test tools examples setup.py echo "Running pylint" pylint -rn qiskit test tools echo "Running Cargo Clippy" diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 73eacde8cf57..49d0d0ee376a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -422,25 +422,34 @@ Note: If you have run `test/ipynb/mpl_tester.ipynb` locally it is possible some ## Style and lint -Qiskit Terra uses 2 tools for verify code formatting and lint checking. The +Qiskit Terra uses three tools for verify code formatting and lint checking. The first tool is [black](https://github.com/psf/black) which is a code formatting tool that will automatically update the code formatting to a consistent style. The second tool is [pylint](https://www.pylint.org/) which is a code linter which does a deeper analysis of the Python code to find both style issues and -potential bugs and other common issues in Python. - -You can check that your local modifications conform to the style rules -by running `tox -elint` which will run `black` and `pylint` to check the local -code formatting and lint. If black returns a code formatting error you can -run `tox -eblack` to automatically update the code formatting to conform to -the style. However, if `pylint` returns any error you will have to fix these -issues by manually updating your code. - -Because `pylint` analysis can be slow, there is also a `tox -elint-incr` target, which only applies -`pylint` to files which have changed from the source github. On rare occasions this will miss some -issues that would have been caught by checking the complete source tree, but makes up for this by -being much faster (and those rare oversights will still be caught by the CI after you open a pull -request). +potential bugs and other common issues in Python. The third tool is the linter +[ruff](https://github.com/charliermarsh/ruff), which has been recently +introduced into Qiskit Terra on an experimental basis. Only a very small number +of rules are enabled. + +You can check that your local modifications conform to the style rules by +running `tox -elint` which will run `black`, `ruff`, and `pylint` to check the +local code formatting and lint. If black returns a code formatting error you can +run `tox -eblack` to automatically update the code formatting to conform to the +style. However, if `ruff` or `pylint` return any error you will have to fix +these issues by manually updating your code. + +Because `pylint` analysis can be slow, there is also a `tox -elint-incr` target, +which runs `black` and `ruff` just as `tox -elint` does, but only applies +`pylint` to files which have changed from the source github. On rare occasions +this will miss some issues that would have been caught by checking the complete +source tree, but makes up for this by being much faster (and those rare +oversights will still be caught by the CI after you open a pull request). + +Because they are so fast, it is sometimes convenient to run the tools `black` and `ruff` separately +rather than via `tox`. If you have installed the development packages in your python environment via +`pip install -r requirements-dev.txt`, then `ruff` and `black` will be available and can be run from +the command line. See [`tox.ini`](tox.ini) for how `tox` invokes them. ## Development Cycle diff --git a/Makefile b/Makefile index e6b8f5ce23d6..bea8a880e274 100644 --- a/Makefile +++ b/Makefile @@ -12,9 +12,9 @@ OS := $(shell uname -s) -.PHONY: default env lint lint-incr style black test test_randomized pytest pytest_randomized test_ci coverage coverage_erase clean +.PHONY: default ruff env lint lint-incr style black test test_randomized pytest pytest_randomized test_ci coverage coverage_erase clean -default: style lint-incr test ; +default: ruff style lint-incr test ; # Dependencies need to be installed on the Anaconda virtual environment. env: @@ -41,6 +41,9 @@ lint-incr: tools/verify_headers.py qiskit test tools examples tools/find_optional_imports.py +ruff: + ruff qiskit test tools examples setup.py + style: black --check qiskit test tools examples setup.py diff --git a/pyproject.toml b/pyproject.toml index 57400392b35e..53b83021584a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,6 +23,14 @@ environment = 'RUSTUP_TOOLCHAIN="stable"' before-all = "yum install -y wget && {package}/tools/install_rust.sh" environment = 'PATH="$PATH:$HOME/.cargo/bin" CARGO_NET_GIT_FETCH_WITH_CLI="true" RUSTUP_TOOLCHAIN="stable"' +[tool.ruff] +select = [ + "F631", + "F632", + "F634", + "F823", +] + [tool.pylint.main] extension-pkg-allow-list = [ "numpy", diff --git a/requirements-dev.txt b/requirements-dev.txt index cf0990e6a36a..fe67acb6ef98 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -11,6 +11,7 @@ black[jupyter]~=22.0 pydot astroid==2.14.2 pylint==2.16.2 +ruff==0.0.267 stestr>=2.0.0,!=4.0.0 pylatexenc>=1.4 ddt>=1.2.0,!=1.4.0,!=1.4.3 diff --git a/tox.ini b/tox.ini index 89c6ad734c03..764253f46d4b 100644 --- a/tox.ini +++ b/tox.ini @@ -24,6 +24,7 @@ commands = [testenv:lint] basepython = python3 commands = + ruff check qiskit test tools examples setup.py black --check {posargs} qiskit test tools examples setup.py pylint -rn qiskit test tools # This line is commented out until #6649 merges. We can't run this currently @@ -38,6 +39,7 @@ commands = basepython = python3 allowlist_externals = git commands = + ruff check qiskit test tools examples setup.py black --check {posargs} qiskit test tools examples setup.py -git fetch -q https://github.com/Qiskit/qiskit-terra.git :lint_incr_latest python {toxinidir}/tools/pylint_incr.py -rn -j4 -sn --paths :/qiskit/*.py :/test/*.py :/tools/*.py