Skip to content

Commit

Permalink
Enable CI and pre-commit hook to lint tests (#4335)
Browse files Browse the repository at this point in the history
* rename all legacy test files to match pylint pattern

* pylint all tests in CI and pre-commit

* lint legacy/qnn/conftest

* remove the custom pylint test handling

* run black before pylint

* changelog

---------

Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
  • Loading branch information
timmysilv and dwierichs committed Jul 10, 2023
1 parent 030447e commit 0f2cf7d
Show file tree
Hide file tree
Showing 46 changed files with 28 additions and 142 deletions.
22 changes: 2 additions & 20 deletions .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,10 @@ jobs:
extra-mypy-options: ""
extra-isort-options: ""

- name: Check for new test files that need whitelisting
uses: dorny/paths-filter@v2
id: test_files_added
with:
list-files: shell
filters: |
testsAdded:
- added: 'tests/**/test_*.py'
- added: 'tests/test_*.py'
- name: Ensure new test files are whitelisted if there are any
if: steps.test_files_added.outputs.testsAdded == 'true'
run: ./.github/workflows/scripts/validate_new_tests.sh ${{ steps.test_files_added.outputs.testsAdded_files }}

- name: Read whitelisted test files that pass pylint
id: tests_to_lint
run: echo "testfiles=$(cat tests/tests_passing_pylint.txt | tr '\n' ' ')" >> $GITHUB_OUTPUT

- name: Run pylint for whitelisted tests
- name: Run pylint (tests)
uses: ricardochaves/python-lint@v1.4.0
with:
python-root-list: "${{ steps.tests_to_lint.outputs.testfiles }}"
python-root-list: "tests"
use-pylint: True
use-pycodestyle: false
use-flake8: false
Expand Down
38 changes: 0 additions & 38 deletions .github/workflows/scripts/validate_new_tests.sh

This file was deleted.

22 changes: 17 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
repos:
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
exclude: ^doc/
- repo: local
hooks:
- id: pylint
Expand All @@ -13,8 +18,15 @@ repos:
"--rcfile=.pylintrc", # Link to your config file
]
exclude: ^(doc/|tests/)
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
exclude: ^doc/
- id: pylint-test
name: pylint-test
entry: pylint
language: system
types: [python]
args:
[
"-rn", # Only display messages
"-sn", # Don't display the score
"--rcfile=tests/.pylintrc", # Link to your config file
]
files: ^tests/
5 changes: 4 additions & 1 deletion doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
<h3>Improvements 🛠</h3>

* `PauliWord` sparse matrices are much faster, which directly improves `PauliSentence`.
[#4272](https://github.com/PennyLaneAI/pennylane/pull/4272)
[(#4272)](https://github.com/PennyLaneAI/pennylane/pull/4272)

* Enable linting of all tests in CI and the pre-commit hook.
[(#4335)](https://github.com/PennyLaneAI/pennylane/pull/4335)

* Added a function `qml.math.fidelity_statevector` that computes the fidelity between two state vectors.
[(#4322)](https://github.com/PennyLaneAI/pennylane/pull/4322)
Expand Down
1 change: 1 addition & 0 deletions tests/.pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# be loaded. Extensions are loading into the active Python interpreter and may
# run arbitrary code
extension-pkg-whitelist=numpy,scipy,autograd,toml,appdir,autograd.numpy,autograd.numpy.linalg,autograd.numpy.builtins,semantic_version,torch,tensorflow,tensorflow.contrib,tensorflow.contrib.eager,LazyLoader,networkx,networkx.dag
ignore-patterns=test_legacy*

[TYPECHECK]

Expand Down
8 changes: 4 additions & 4 deletions tests/legacy/qnn/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
Common fixtures for the qnn module.
"""
import pytest
import pennylane as qml
import numpy as np
import pennylane as qml


@pytest.fixture
Expand All @@ -36,7 +36,7 @@ def get_circuit(n_qubits, output_dim, interface):
}

@qml.qnode(dev, interface=interface)
def circuit(inputs, w1, w2, w3, w4, w5, w6, w7):
def circuit(inputs, w1, w2, w3, w4, w5, w6, w7): # pylint:disable=too-many-arguments
"""A circuit that embeds data using the AngleEmbedding and then performs a variety of
operations. The output is a PauliZ measurement on the first output_dim qubits. One set of
parameters, w5, are specified as non-trainable."""
Expand Down Expand Up @@ -71,7 +71,7 @@ def get_circuit_dm(n_qubits, output_dim, interface):
}

@qml.qnode(dev, interface=interface)
def circuit(inputs, w1, w2, w3, w4, w5, w6, w7):
def circuit(inputs, w1, w2, w3, w4, w5, w6, w7): # pylint:disable=too-many-arguments
"""Sample circuit to be used for testing density_matrix() return type."""
qml.templates.AngleEmbedding(inputs, wires=list(range(n_qubits)))
qml.templates.StronglyEntanglingLayers(w1, wires=list(range(n_qubits)))
Expand All @@ -84,6 +84,6 @@ def circuit(inputs, w1, w2, w3, w4, w5, w6, w7):

# Using np.log2() here because output_dim is sampled from varying the number of
# qubits (say, nq) and calculated as (2 ** nq, 2 ** nq)
return qml.density_matrix(wires=[i for i in range(int(np.log2(output_dim[0])))])
return qml.density_matrix(wires=list(range(int(np.log2(output_dim[0])))))

return circuit, weight_shapes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
74 changes: 0 additions & 74 deletions tests/tests_passing_pylint.txt

This file was deleted.

0 comments on commit 0f2cf7d

Please sign in to comment.