From e62fc4f42791f67c9a122dd73469e8189a8e0629 Mon Sep 17 00:00:00 2001 From: Hiroshi Horii Date: Mon, 20 Feb 2023 20:05:34 +0900 Subject: [PATCH] add black and clang-format --- .clang-format | 3 +++ .github/workflows/tests.yml | 7 ++++++- .pylintrc | 41 ++++++++++++++++--------------------- CONTRIBUTING.md | 21 +++++++++++++++++++ requirements-dev.txt | 3 ++- tox.ini | 20 +++++++++++++----- 6 files changed, 65 insertions(+), 30 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000000..7bc502b33e --- /dev/null +++ b/.clang-format @@ -0,0 +1,3 @@ +BasedOnStyle: LLVM +AlwaysBreakTemplateDeclarations: Yes +RemoveBracesLLVM: Yes diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index d540b0978e..4620e24322 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -34,10 +34,15 @@ jobs: pip install -U pip wheel pip install -U -c constraints.txt -r requirements-dev.txt qiskit-terra numpy scipy shell: bash + - name: Run clang-format + run: | + set -e + find src -iname *.hpp -o -iname *.cpp | xargs clang-format -n 1 + shell: bash - name: Run Lint run: | set -e - pycodestyle --ignore=E402,W503,W504 --max-line-length=100 qiskit_aer + black --check qiskit_aer test tools pylint -j 2 -rn qiskit_aer sdist: runs-on: ${{ matrix.platform.os }} diff --git a/.pylintrc b/.pylintrc index c8443f43e8..15ea8bdb79 100644 --- a/.pylintrc +++ b/.pylintrc @@ -21,7 +21,7 @@ persistent=yes # List of plugins (as comma separated values of python modules names) to load, # usually to register additional checkers. load-plugins=pylint.extensions.docparams, # enable checking of docstring args - pylint.extensions.docstyle # basic docstring stle checks + pylint.extensions.docstyle, # basic docstring style checks # Use multiple processes to speed up Pylint. jobs=1 @@ -33,8 +33,7 @@ unsafe-load-any-extension=no # A comma-separated list of package or module names from where C extensions may # be loaded. Extensions are loading into the active Python interpreter and may # run arbitrary code -extension-pkg-whitelist= - +extension-pkg-allow-list= [MESSAGES CONTROL] @@ -43,10 +42,10 @@ extension-pkg-whitelist= confidence= # Enable the message, report, category or checker with the given id(s). You can -# either give multiple identifiers separated by comma (,) or put this option +# either give multiple identifier separated by comma (,) or put this option # multiple time (only on the command line, not in the configuration file where # it should appear only once). See also the "--disable" option for examples. -#enable= +enable=use-symbolic-message-instead # Disable the message, report, category or checker with the given id(s). You # can either give multiple identifiers separated by comma (,) or put this @@ -57,7 +56,8 @@ confidence= # --enable=similarities". If you want to run only the classes checker, but have # no Warning level messages displayed, use"--disable=all --enable=classes # --disable=W" -disable=no-self-use, # disabled as it is too verbose +disable=spelling, # way too noisy + no-self-use, # disabled as it is too verbose fixme, # disabled as TODOs would show up as warnings protected-access, # disabled as we don't follow the public vs private # convention strictly @@ -70,8 +70,9 @@ disable=no-self-use, # disabled as it is too verbose unnecessary-pass, # allow for methods with just "pass", for clarity no-else-return, # relax "elif" after a clause with a return docstring-first-line-empty, # relax docstring style - unsubscriptable-object, # pylint can't determine this for numpy types - bad-continuation, bad-whitespace # differences of opinion with black + import-outside-toplevel, + bad-continuation, bad-whitespace, # differences of opinion with black + import-error # overzealous with our optionals/dynamic packages @@ -83,12 +84,6 @@ disable=no-self-use, # disabled as it is too verbose # mypackage.mymodule.MyReporterClass. output-format=text -# Put messages in a separate file for each module / package specified on the -# command line instead of printing them on stdout. Reports (if any) will be -# written in a file name "pylint_global.[txt|html]". This option is deprecated -# and it will be removed in Pylint 2.0. -files-output=no - # Tells whether to display a full report or only the messages reports=yes @@ -117,8 +112,8 @@ evaluation=10.0 - ((float(5 * error + warning + refactor + convention) / stateme # pi = the PI constant # op = operation iterator # b = basis iterator -good-names=i,j,k,n,m,ex,v,w,x,y,z,Run,_,logger,q,c,r,qr,cr,qc,nd,pi,op,b,ar,br,dt, - __unittest +good-names=a,b,i,j,k,d,n,m,ex,v,w,x,y,z,Run,_,logger,q,c,r,qr,cr,qc,nd,pi,op,b,ar,br,p,cp,dt, + __unittest,iSwapGate,mu # Bad variable names which should always be refused, separated by a comma bad-names=foo,bar,toto,tutu,tata @@ -171,16 +166,16 @@ attr-rgx=[a-z_][a-z0-9_]{2,30}$ attr-name-hint=[a-z_][a-z0-9_]{2,30}$ # Regular expression matching correct argument names -argument-rgx=[a-z_][a-z0-9_]{2,30}$ +argument-rgx=[a-z_][a-z0-9_]{2,30}|ax|dt$ # Naming hint for argument names argument-name-hint=[a-z_][a-z0-9_]{2,30}$ # Regular expression matching correct variable names -variable-rgx=[a-z_][a-z0-9_]{2,30}$ +variable-rgx=[a-z_][a-z0-9_]{1,30}$ # Naming hint for variable names -variable-name-hint=[a-z_][a-z0-9_]{2,30}$ +variable-name-hint=[a-z_][a-z0-9_]{1,30}$ # Regular expression matching correct class attribute names class-attribute-rgx=([A-Za-z_][A-Za-z0-9_]{2,30}|(__.*__))$ @@ -212,7 +207,7 @@ max-nested-blocks=5 [FORMAT] # Maximum number of characters on a single line. -max-line-length=100 +max-line-length=105 # Regexp for a line that is allowed to be longer than the limit. ignore-long-lines=^\s*(# )??$ @@ -279,7 +274,7 @@ spelling-dict= spelling-ignore-words= # A path to a file that contains private dictionary; one word per line. -spelling-private-dict-file= +spelling-private-dict-file=.local-spellings # Tells whether to store unknown words to indicated private dictionary in # --spelling-private-dict-file option instead of raising a message. @@ -296,7 +291,7 @@ ignore-mixin-members=yes # (useful for modules/projects where namespaces are manipulated during runtime # and thus existing member attributes cannot be deduced by static analysis. It # supports qualified module names, as well as Unix pattern matching. -ignored-modules=matplotlib.cm,numpy.random,qiskit.providers +ignored-modules= # List of class names for which member attributes should not be checked (useful # for classes with dynamically set attributes). This supports the use of @@ -333,7 +328,7 @@ callbacks=cb_,_cb # List of qualified module names which can have objects that can redefine # builtins. -redefining-builtins-modules=six.moves,future.builtins,tools.compiler +redefining-builtins-modules=six.moves,future.builtins [CLASSES] diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 307136e3c5..253cc5da1a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -18,6 +18,11 @@ please ensure that: 1. The code follows the code style of the project and successfully passes the tests. For convenience, you can execute `tox` locally, which will run these checks and report any issues. + + If your code fails the local style checks, you can use `tox -eblack` + and `tox-eclang` to automatically fix update the code formatting + in python and C++ respectively. + 2. The documentation has been updated accordingly. In particular, if a function or class has been modified during the PR, please update the *docstring* accordingly. @@ -185,6 +190,22 @@ like for the current state of the repo, you can run: `tox -edocs` which will build all the documentation into `docs/_build/html` and the release notes in particular will be located at `docs/_build/html/release_notes.html` +## Style and lint + +Qiskit Aer uses 3 tools for verify code formatting and lint checking. The +first tool is [black](https://github.com/psf/black) which is a Python 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`, `pylint` and `pre-commit` +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. + ### Development Cycle The development cycle for qiskit-aer is all handled in the open using diff --git a/requirements-dev.txt b/requirements-dev.txt index 4381a3941f..2ab9396c6e 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -5,7 +5,8 @@ scikit-build>=0.11.0 asv cvxpy>=1.0.0,<1.1.15;platform_system != 'Windows' and python_version < '3.11' pylint -pycodestyle +black[jupyter]~=22.0 +clang-format~=15.0.7 Sphinx>=1.8.3 sphinx-rtd-theme>=0.4.0 sphinx-tabs>=1.1.11 diff --git a/tox.ini b/tox.ini index 699976c607..d9b9dbe1ec 100644 --- a/tox.ini +++ b/tox.ini @@ -22,20 +22,30 @@ commands = basepython = python3 setenv = {[testenv]setenv} - PYTHON=coverage3 run --source qiskit --parallel-mode + PYTHON=coverage3 run --source qiskit_aer --parallel-mode commands = stestr run {posargs} coverage3 combine coverage3 report [testenv:lint] -deps = - pycodestyle - pylint +envdir = .tox/lint +basepython = python3 commands = - pycodestyle --ignore=E402,W503,W504 --max-line-length=100 qiskit_aer + black --check {posargs} qiskit_aer test tools examples setup.py pylint -j 2 -rn qiskit_aer +[testenv:black] +envdir = .tox/lint +commands = black qiskit_aer test tools + +[pycodestyle] +max-line-length = 105 +# default ignores + E741 because of opflow global variable I +# + E203 because of a difference of opinion with black +# codebase does currently comply with: E133, E242, E704, W505 +ignore = E121, E123, E126, E133, E226, E241, E242, E704, W503, W504, W505, E741, E203 + [testenv:docs] deps = -r requirements-dev.txt