Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to using black for code formatting #6361

Merged
merged 6 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ disable=spelling, # way too noisy
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
import-outside-toplevel
import-outside-toplevel,
bad-continuation, bad-whitespace # differences of opinion with black



Expand Down Expand Up @@ -212,7 +213,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
23 changes: 22 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ 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 (specifically the black
code formatting check) you can use `tox -eblack` to automatically
fix update the code formatting.
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 @@ -358,7 +362,24 @@ you just need to update the reference images as follows:
new tests should now pass.

Note: If you have run `test/ipynb/mpl_tester.ipynb` locally it is possible some file metadata has changed, **please do not commit and push changes to this file unless they were intentional**.
### Development Cycle

## Style and lint

Qiskit Terra uses 2 tools for verify code formatting and lint checking. The
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather phase out the name Terra from docs that users see - just saying Qiskit (especially as black will likely be done in other repos too).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can clean this up later, I was planning to update the overall contributing documentation when we've unified on using black everywhere: https://qiskit.org/documentation/contributing_to_qiskit.html#style-guide right now it just says pycodestyle and pylint

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.

## Development Cycle

The development cycle for qiskit-terra is all handled in the open using
the project boards in Github for project management. We use milestones
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ lint:
python tools/find_optional_imports.py

style:
pycodestyle qiskit test
black --check qiskit test tools

black:
black qiskit test tools

# Use the -s (starting directory) flag for "unittest discover" is necessary,
# otherwise the QuantumCircuit header will be modified during the discovery.
Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ stages:
- bash: |
set -e
source test-job/bin/activate
pycodestyle qiskit test
black --check qiskit test tools
pylint -rn qiskit test
tools/verify_headers.py qiskit test
python tools/find_optional_imports.py
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
[build-system]
requires = ["Cython>=0.27.1", "setuptools", "wheel"]

[tool.black]
line-length = 100
target-version = ['py36', 'py37', 'py38', 'py39']
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ipywidgets>=7.3.0
jupyter
matplotlib>=2.1,<3.4
pillow>=4.2.1
pycodestyle
black==21.4b2
pydot
astroid==2.5
pylint==2.7.1
Expand Down
16 changes: 13 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@ commands =
stestr run {posargs}

[testenv:lint]
envdir = .tox/lint
basepython = python3
commands =
pycodestyle qiskit test
black --check {posargs} qiskit test tools
pylint -rn qiskit test
{toxinidir}/tools/verify_headers.py qiskit test
{toxinidir}/tools/find_optional_imports.py
reno lint

[testenv:black]
envdir = .tox/lint
commands = black {posargs} qiskit test tools

[testenv:coverage]
basepython = python3
setenv =
Expand All @@ -49,7 +54,12 @@ commands =
sphinx-build -W -b html -j auto docs/ docs/_build/html {posargs}

[pycodestyle]
max-line-length = 100
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
ignore = E121, E123, E126, E133, E226, E241, E242, E704, W503, W504, W505, E741, E203

[flake8]
max-line-length = 105
extend-ignore = E203, E741