Skip to content

Commit

Permalink
add black and clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
hhorii committed Feb 20, 2023
1 parent 7b4bc1e commit e62fc4f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 30 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: Yes
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
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 }}
Expand Down
41 changes: 18 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,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



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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}|(__.*__))$
Expand Down Expand Up @@ -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*(# )?<?https?://\S+>?$
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand Down
21 changes: 21 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,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
Expand Down
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 15 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e62fc4f

Please sign in to comment.