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

Ignore fewer flake8 rules, add flake8-bugbear #1133

Merged
merged 9 commits into from
Dec 24, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 11 additions & 2 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
[flake8]
# we choose to use 100 rather than Black's default 88
max-line-length = 100
ignore = E203,E999,E741,E126,E127,F401,F403,F405,F811,F841,E743,W503
exclude = gen3,external
# E203 : space before ":" (not PEP8 compliant)
# E741 : ambiguous variable name (unfortunately we need to use "I")
# E743 : ambiguous function definition (unfortunately we need to use "I")
# W503 : line break before binary operator (not PEP8 compliant)
ignore = E203, E741, E743, W503
# __init__.py : unused imports for maintaining backwards compatibility / exposing an API
# external : vendored external libraries
# gen3 : autogenerated parser code
# operator_estimation.py : unused imports for maintaining backwards compatibility
exclude = __init__.py, external, gen3, operator_estimation.py
40 changes: 20 additions & 20 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,70 +15,70 @@ services:
alias: quilc
command: ["-R"]

test-py36:
build-docs:
image: python:3.6
tags:
- github
script:
- apt-get update && apt-get install -y pandoc
- make install
- make requirements
- make test
coverage: '/TOTAL.*\s(\d+)%/'
- make docs

test-py37:
image: python:3.7
check-format:
image: python:3.6
tags:
- github
script:
- make install
- make requirements
- make test
- make check-format

test-py38:
image: python:3.8
check-style:
image: python:3.6
tags:
- github
script:
- make install
- make requirements
- make test
- make check-style

style:
check-types:
image: python:3.6
tags:
- github
script:
- make install
- make requirements
- make style
- make check-types

typecheck:
test-py36:
image: python:3.6
tags:
- github
script:
- make install
- make requirements
- make typecheck
- make test
coverage: '/TOTAL.*\s(\d+)%/'

formatcheck:
image: python:3.6
test-py37:
image: python:3.7
tags:
- github
script:
- make install
- make requirements
- make formatcheck
- make test

docs:
image: python:3.6
test-py38:
image: python:3.8
tags:
- github
script:
- apt-get update && apt-get install -y pandoc
- make install
- make requirements
- make docs
- make test

####################################################################################################
# MASTER-ONLY JOBS
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ Changelog
- Use [Black](https://black.readthedocs.io/en/stable/index.html) for code style
and enforce it (along with a line length of 100) via the `style` (`flake8`)
and `formatcheck` (`black --check`) CI jobs (@karalekas, gh-1132).
- Ignore fewer `flake8` style rules, add the `flake8-bugbear` plugin, and
rename the style-related `Makefile` targets and CI jobs so that they have
a uniform naming convention: `check-all`, `check-format`, `check-style`,
and `check-types` (@karalekas, gh-1133).

### Bugfixes

Expand Down
31 changes: 18 additions & 13 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,40 +86,45 @@ We've made opening one easy by providing a [Pull Request Template](.github/PULL_
that includes a checklist of things to complete before asking for code review. We look
forward to reviewing your work! 🙂


Developer How-Tos
-----------------

### Style Guidelines

We use [Black](https://black.readthedocs.io/en/stable/index.html) and `flake8` to automatically
lint the code and enforce style requirements as part of the CI pipeline. You can run these style
tests yourself locally by running `make style` (to check for violations of the `flake8` rules)
and `make formatcheck` (to see if `black` would reformat the code) in the top-level directory of
tests yourself locally by running `make check-style` (to check for violations of the `flake8` rules)
and `make check-format` (to see if `black` would reformat the code) in the top-level directory of
the repository. If you aren't presented with any errors, then that means your code is good enough
for the linter (`flake8`) and formatter (`black`). If `make formatcheck` fails, it will present
you with a diff, which you can resolve by running `make format`. Black is very opinionated, but
for the linter (`flake8`) and formatter (`black`). If `make check-format` fails, it will present
you with a diff, which you can resolve by running `make format`. Black is very opinionated, but
saves a lot of time by removing the need for style nitpicks in PR review. We only deviate from its
default behavior in one category: we choose to use a line length of 100 rather than the Black
default of 88 (this is configured in the [`pyproject.toml`](pyproject.toml) file).
default of 88 (this is configured in the [`pyproject.toml`](pyproject.toml) file). As for `flake8`,
we ignore a couple of its rules (all for good reasons), and the specific configuration can be
found in the [`.flake8`](.flake8) file. We additionally use the [`flake8-bugbear`][bugbear]
plugin to add a collection of helpful and commonly observed style rules.

In addition to linting and formatting, we are in the process of rolling out the use of type hints
for all parameters and return values, using the [PEP 484 syntax][pep-484]. This is being done on
a file-by-file basis, and for ones that have been completed we now have a `make typecheck` command
that will enforce the use of types in those files as part of the CI. When a file is transitioned,
it should be added to the list in the `typecheck` target of the [`Makefile`](Makefile). Because we
use the `typing` module, types (e.g. `type` and `rtype` entries) should be omitted when writing
(useful) [Sphinx-style](https://sphinx-rtd-tutorial.readthedocs.io/en/latest/docstrings.html)
docstrings for classes, methods, and functions.
a file-by-file basis, and for ones that have been completed we now have a `make check-types` command
that will enforce the use of types in those files as part of the CI, using the popular static
typechecker [mypy](http://mypy-lang.org/). When a file is transitioned, it should be added to the
list in the `check-types` target of the [`Makefile`](Makefile). For more information on the specific
configuration of `mypy` that we use for typechecking, please refer to the [`mypy.ini`](mypy.ini)
file. Also, because we use the `typing` module, types (e.g. `type` and `rtype` entries) should be
omitted when writing (useful) [Sphinx-style][sphinx] docstrings for classes, methods, and functions.

As useful shorthand, all of these style-related tests can be run locally with a single command,
by running the following:

```bash
make checkall
make check-all
```

[bugbear]: https://github.com/PyCQA/flake8-bugbear
[pep-484]: https://www.python.org/dev/peps/pep-0484/
[sphinx]: https://sphinx-rtd-tutorial.readthedocs.io/en/latest/docstrings.html

### Running the Unit Tests

Expand Down
31 changes: 15 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,21 @@ DOCKER_TAG=rigetti/forest:$(COMMIT_HASH)
.PHONY: all
all: dist

.PHONY: checkall
checkall: formatcheck style typecheck
.PHONY: check-all
check-all: check-format check-types check-style

.PHONY: check-format
check-format:
black --check --diff pyquil

# The dream is to one day run mypy on the whole tree. For now, limit checks to known-good files.
.PHONY: check-types
check-types:
mypy pyquil/quilatom.py pyquil/quilbase.py pyquil/gates.py

.PHONY: check-style
check-style:
flake8

.PHONY: clean
clean:
Expand Down Expand Up @@ -39,10 +52,6 @@ docker: Dockerfile
format:
black pyquil

.PHONY: formatcheck
formatcheck:
black --check --diff pyquil

.PHONY: info
info:
python -V
Expand All @@ -56,20 +65,10 @@ install:
requirements: requirements.txt
pip install -r requirements.txt

.PHONY: style
style:
flake8

.PHONY: test
test:
pytest -v --runslow --cov=pyquil

# The dream is to one day run mypy on the whole tree. For now, limit
# checks to known-good files.
.PHONY: typecheck
typecheck:
mypy pyquil/quilatom.py pyquil/quilbase.py pyquil/gates.py

.PHONY: upload
upload:
twine upload --repository-url https://test.pypi.org/legacy/ dist/*
Expand Down
7 changes: 4 additions & 3 deletions examples/1.3_vqe_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
This is a demo of VQE through the forest stack. We will do the H2 binding from the Google paper
using OpenFermion to generate Hamiltonians and Forest to simulate the system
"""
import sys
import numpy as np
import matplotlib.pyplot as plt

Expand All @@ -18,12 +17,14 @@

from pyquil.quil import Program
from pyquil.paulis import sX, sY, exponentiate, PauliSum
from pyquil.gates import X, I
from pyquil.gates import X
from pyquil.api import QVMConnection
from pyquil.unitary_tools import tensor_up

from grove.measurements.estimation import estimate_locally_commuting_operator

QVM_CONNECTION = QVMConnection(endpoint="http://localhost:5000")


def get_h2_dimer(bond_length):
# Set molecule parameters.
Expand Down Expand Up @@ -60,7 +61,7 @@ def ucc_circuit(theta):


def objective_fun(
theta, hamiltonian=None, quantum_resource=QVMConnection(endpoint="http://localhost:5000")
theta, hamiltonian=None, quantum_resource=QVM_CONNECTION
):
"""
Evaluate the Hamiltonian bny operator averaging
Expand Down
1 change: 0 additions & 1 deletion examples/qaoa_ansatz.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from pyquil.quil import Program
from pyquil.gates import H
from pyquil.paulis import sI, sX, sZ, exponentiate_commuting_pauli_sum
from pyquil.api import QVMConnection

# Create a 4-node array graph: 0-1-2-3.
graph = [(0, 1), (1, 2), (2, 3)]
Expand Down
2 changes: 1 addition & 1 deletion examples/website-script.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"""

from pyquil import Program, get_qc
from pyquil.gates import *
from pyquil.gates import H, CNOT

# construct a Bell State program
p = Program(H(0), CNOT(0, 1))
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ exclude = '''
| build
| dist
)/
| gen3 # pyquil-specific
| external
| external # pyquil-specific
| gen3

)
'''
'''
1 change: 0 additions & 1 deletion pyquil/api/_base_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from pyquil.api._error_reporting import _record_call
from pyquil.api._errors import error_mapping, UserMessageError, UnknownApiError, TooManyQubitsError
from pyquil.api._logger import logger
from pyquil.device import Specs, ISA
from pyquil.wavefunction import Wavefunction

TYPE_EXPECTATION = "expectation"
Expand Down
2 changes: 1 addition & 1 deletion pyquil/api/_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from urllib.parse import urljoin

from pyquil import __version__
from pyquil.api._base_connection import ForestSession, get_session
from pyquil.api._base_connection import ForestSession
from pyquil.api._qac import AbstractCompiler
from pyquil.api._error_reporting import _record_call
from pyquil.api._errors import UserMessageError
Expand Down
2 changes: 1 addition & 1 deletion pyquil/api/_qpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from pyquil import Program
from pyquil.parser import parse
from pyquil.api._base_connection import ForestSession, get_session
from pyquil.api._base_connection import ForestSession
from pyquil.api._error_reporting import _record_call
from pyquil.api._errors import UserMessageError
from pyquil.api._logger import logger
Expand Down
2 changes: 1 addition & 1 deletion pyquil/api/_quantum_computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,7 @@ def hadamard(n, dtype=int):
H = np.array([[1]], dtype=dtype)

# Sylvester's construction
for i in range(0, lg2):
for _ in range(0, lg2):
H = np.vstack((np.hstack((H, H)), np.hstack((H, -H))))

return H
Expand Down
1 change: 0 additions & 1 deletion pyquil/api/_qvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
from pyquil.api._config import PyquilConfig
from pyquil.api._error_reporting import _record_call
from pyquil.api._qam import QAM
from pyquil.device import Device
from pyquil.gates import MOVE, MemoryReference
from pyquil.noise import apply_noise_model
from pyquil.paulis import PauliSum
Expand Down
1 change: 0 additions & 1 deletion pyquil/api/tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
import os

from pyquil.api._config import PyquilConfig
from pyquil.api._errors import UserMessageError
Expand Down
1 change: 0 additions & 1 deletion pyquil/experiment/_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import numpy as np

from pyquil.paulis import PauliTerm
from pyquil.experiment._symmetrization import SymmetrizationLevel


def euler_angles_RX(theta: float) -> Tuple[float, float, float]:
Expand Down
4 changes: 0 additions & 4 deletions pyquil/experiment/_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
from typing import Iterable, Tuple

from pyquil.paulis import PauliTerm, sI, is_identity
from pyquil.experiment._memory import (
pauli_term_to_measurement_memory_map,
pauli_term_to_preparation_memory_map,
)

if sys.version_info < (3, 7):
from pyquil.external.dataclasses import dataclass
Expand Down
Loading