Skip to content

Commit

Permalink
add code-formatting with black for python and with clang-format for c…
Browse files Browse the repository at this point in the history
…++ (#1630)

* add black and clang-format

* apply black and clang-format

* manually format codes that were not covered in black and clang-format

* add targets of clang-format and clean up tox.ini

* apply black with new configuration

* Update CONTRIBUTING.md

Co-authored-by: Matthew Treinish <[email protected]>

* Update .github/workflows/tests.yml

Co-authored-by: Matthew Treinish <[email protected]>

* reformat qiskit_aer, test, tools, and setup.py with length 100

---------

Co-authored-by: Matthew Treinish <[email protected]>
  • Loading branch information
hhorii and mtreinish authored Mar 13, 2023
1 parent 6f600ad commit 0366c51
Show file tree
Hide file tree
Showing 344 changed files with 32,609 additions and 30,688 deletions.
3 changes: 3 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BasedOnStyle: LLVM
AlwaysBreakTemplateDeclarations: Yes
RemoveBracesLLVM: No
7 changes: 6 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
sh tools/clang-format.sh --Werror -n
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 setup.py
pylint -j 2 -rn qiskit_aer
sdist:
runs-on: ${{ matrix.platform.os }}
Expand Down
45 changes: 22 additions & 23 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]

Expand All @@ -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
Expand All @@ -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
Expand All @@ -70,8 +70,13 @@ 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
consider-using-f-string, # pass re-lint old commits
broad-exception-raised, # pass re-lint old commits
unsubscriptable-object # pass re-lint old commits




Expand All @@ -83,12 +88,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

Expand Down Expand Up @@ -117,8 +116,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
Expand Down Expand Up @@ -171,16 +170,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}|(__.*__))$
Expand Down Expand Up @@ -212,7 +211,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*(# )?<?https?://\S+>?$
Expand Down Expand Up @@ -279,7 +278,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.
Expand All @@ -296,7 +295,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=numpy

# List of class names for which member attributes should not be checked (useful
# for classes with dynamically set attributes). This supports the use of
Expand Down Expand Up @@ -333,7 +332,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]
Expand Down
23 changes: 23 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -185,6 +190,24 @@ 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. The third tool is
[clang-format](https://clang.llvm.org/docs/ClangFormat.html) which is a
C++ code formatting tool that will automatically update codes with a consitent style.

You can check that your local modifications conform to the style rules
by running `tox -elint` which will run `black`, `pylint` and `clang-format`
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
Expand Down
Loading

0 comments on commit 0366c51

Please sign in to comment.