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>
This commit is contained in:
John Lapeyre 2023-05-30 18:43:36 -04:00 committed by GitHub
parent 291824a392
commit 901e3b8973
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 15 deletions

View File

@ -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"

View File

@ -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.
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` 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.
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 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 `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

View File

@ -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

View File

@ -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",

View File

@ -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

View File

@ -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