diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index 6bf0ad86d35..8cde6fa693b 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -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 diff --git a/.github/workflows/scripts/validate_new_tests.sh b/.github/workflows/scripts/validate_new_tests.sh deleted file mode 100755 index fcea1b57c40..00000000000 --- a/.github/workflows/scripts/validate_new_tests.sh +++ /dev/null @@ -1,38 +0,0 @@ -#!/bin/bash -missing_files=() -list_file=tests/tests_passing_pylint.txt - -for file in $@ -do - # it's in the list, check the next one - if grep -Fxq $file $list_file - then - continue - fi - - # check if this file's folder is in the list - filedir=$file - while [ $filedir != "." ] - do - filedir=$(dirname $filedir) - if grep -Fxq "${filedir}/**" $list_file - then - break - fi - done - - # loop reached termination condition without finding a match, user needs to add this file - if [ ${filedir} == "." ] - then - missing_files+=($file) - fi -done - -if [ ${#missing_files[@]} -eq 0 ]; then - echo "All new test files are already added" - exit 0 -fi - -echo "Please add the following test files to tests/tests_passing_pylint.txt:" -for file in ${missing_files[@]}; do echo $file; done -exit 1 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2cfedfce775..3220e71667e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 @@ -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/ diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index 3e11de148cc..e5bfa534e19 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -7,7 +7,10 @@

Improvements 🛠

* `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) diff --git a/tests/.pylintrc b/tests/.pylintrc index 9e80021f83c..3847296e984 100644 --- a/tests/.pylintrc +++ b/tests/.pylintrc @@ -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] diff --git a/tests/legacy/qnn/conftest.py b/tests/legacy/qnn/conftest.py index e97d5f4090b..077fb05f459 100644 --- a/tests/legacy/qnn/conftest.py +++ b/tests/legacy/qnn/conftest.py @@ -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 @@ -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.""" @@ -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))) @@ -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 diff --git a/tests/legacy/qnn/test_cost_old.py b/tests/legacy/qnn/test_legacy_cost_old.py similarity index 100% rename from tests/legacy/qnn/test_cost_old.py rename to tests/legacy/qnn/test_legacy_cost_old.py diff --git a/tests/legacy/qnn/test_keras_old.py b/tests/legacy/qnn/test_legacy_keras_old.py similarity index 100% rename from tests/legacy/qnn/test_keras_old.py rename to tests/legacy/qnn/test_legacy_keras_old.py diff --git a/tests/legacy/qnn/test_qnn_torch_old.py b/tests/legacy/qnn/test_legacy_qnn_torch_old.py similarity index 100% rename from tests/legacy/qnn/test_qnn_torch_old.py rename to tests/legacy/qnn/test_legacy_qnn_torch_old.py diff --git a/tests/legacy/test_adjoint_metric_tensor_old.py b/tests/legacy/test_legacy_adjoint_metric_tensor_old.py similarity index 100% rename from tests/legacy/test_adjoint_metric_tensor_old.py rename to tests/legacy/test_legacy_adjoint_metric_tensor_old.py diff --git a/tests/legacy/test_autograd_old.py b/tests/legacy/test_legacy_autograd_old.py similarity index 100% rename from tests/legacy/test_autograd_old.py rename to tests/legacy/test_legacy_autograd_old.py diff --git a/tests/legacy/test_autograd_qnode_old.py b/tests/legacy/test_legacy_autograd_qnode_old.py similarity index 100% rename from tests/legacy/test_autograd_qnode_old.py rename to tests/legacy/test_legacy_autograd_qnode_old.py diff --git a/tests/legacy/test_batch_input_old.py b/tests/legacy/test_legacy_batch_input_old.py similarity index 100% rename from tests/legacy/test_batch_input_old.py rename to tests/legacy/test_legacy_batch_input_old.py diff --git a/tests/legacy/test_batch_params_old.py b/tests/legacy/test_legacy_batch_params_old.py similarity index 100% rename from tests/legacy/test_batch_params_old.py rename to tests/legacy/test_legacy_batch_params_old.py diff --git a/tests/legacy/test_classical_shadows_old.py b/tests/legacy/test_legacy_classical_shadows_old.py similarity index 100% rename from tests/legacy/test_classical_shadows_old.py rename to tests/legacy/test_legacy_classical_shadows_old.py diff --git a/tests/legacy/test_counts_old.py b/tests/legacy/test_legacy_counts_old.py similarity index 100% rename from tests/legacy/test_counts_old.py rename to tests/legacy/test_legacy_counts_old.py diff --git a/tests/legacy/test_default_mixed_autograd_old.py b/tests/legacy/test_legacy_default_mixed_autograd_old.py similarity index 100% rename from tests/legacy/test_default_mixed_autograd_old.py rename to tests/legacy/test_legacy_default_mixed_autograd_old.py diff --git a/tests/legacy/test_finite_difference_old.py b/tests/legacy/test_legacy_finite_difference_old.py similarity index 100% rename from tests/legacy/test_finite_difference_old.py rename to tests/legacy/test_legacy_finite_difference_old.py diff --git a/tests/legacy/test_fisher_old.py b/tests/legacy/test_legacy_fisher_old.py similarity index 100% rename from tests/legacy/test_fisher_old.py rename to tests/legacy/test_legacy_fisher_old.py diff --git a/tests/legacy/test_hadamard_gradient_old.py b/tests/legacy/test_legacy_hadamard_gradient_old.py similarity index 100% rename from tests/legacy/test_hadamard_gradient_old.py rename to tests/legacy/test_legacy_hadamard_gradient_old.py diff --git a/tests/legacy/test_hamiltonian_expand_old.py b/tests/legacy/test_legacy_hamiltonian_expand_old.py similarity index 100% rename from tests/legacy/test_hamiltonian_expand_old.py rename to tests/legacy/test_legacy_hamiltonian_expand_old.py diff --git a/tests/legacy/test_jax_old.py b/tests/legacy/test_legacy_jax_old.py similarity index 100% rename from tests/legacy/test_jax_old.py rename to tests/legacy/test_legacy_jax_old.py diff --git a/tests/legacy/test_jax_qnode_old.py b/tests/legacy/test_legacy_jax_qnode_old.py similarity index 100% rename from tests/legacy/test_jax_qnode_old.py rename to tests/legacy/test_legacy_jax_qnode_old.py diff --git a/tests/legacy/test_measurements_old.py b/tests/legacy/test_legacy_measurements_old.py similarity index 100% rename from tests/legacy/test_measurements_old.py rename to tests/legacy/test_legacy_measurements_old.py diff --git a/tests/legacy/test_metric_tensor_old.py b/tests/legacy/test_legacy_metric_tensor_old.py similarity index 100% rename from tests/legacy/test_metric_tensor_old.py rename to tests/legacy/test_legacy_metric_tensor_old.py diff --git a/tests/legacy/test_mitigate_old.py b/tests/legacy/test_legacy_mitigate_old.py similarity index 100% rename from tests/legacy/test_mitigate_old.py rename to tests/legacy/test_legacy_mitigate_old.py diff --git a/tests/legacy/test_optimize_shot_adaptive_old.py b/tests/legacy/test_legacy_optimize_shot_adaptive_old.py similarity index 100% rename from tests/legacy/test_optimize_shot_adaptive_old.py rename to tests/legacy/test_legacy_optimize_shot_adaptive_old.py diff --git a/tests/legacy/test_parameter_shift_cv_old.py b/tests/legacy/test_legacy_parameter_shift_cv_old.py similarity index 100% rename from tests/legacy/test_parameter_shift_cv_old.py rename to tests/legacy/test_legacy_parameter_shift_cv_old.py diff --git a/tests/legacy/test_parameter_shift_hessian_old.py b/tests/legacy/test_legacy_parameter_shift_hessian_old.py similarity index 100% rename from tests/legacy/test_parameter_shift_hessian_old.py rename to tests/legacy/test_legacy_parameter_shift_hessian_old.py diff --git a/tests/legacy/test_parameter_shift_old.py b/tests/legacy/test_legacy_parameter_shift_old.py similarity index 100% rename from tests/legacy/test_parameter_shift_old.py rename to tests/legacy/test_legacy_parameter_shift_old.py diff --git a/tests/legacy/test_pulse_gradient_old.py b/tests/legacy/test_legacy_pulse_gradient_old.py similarity index 100% rename from tests/legacy/test_pulse_gradient_old.py rename to tests/legacy/test_legacy_pulse_gradient_old.py diff --git a/tests/legacy/test_qcut_old.py b/tests/legacy/test_legacy_qcut_old.py similarity index 100% rename from tests/legacy/test_qcut_old.py rename to tests/legacy/test_legacy_qcut_old.py diff --git a/tests/legacy/test_qnspsa_old.py b/tests/legacy/test_legacy_qnspsa_old.py similarity index 100% rename from tests/legacy/test_qnspsa_old.py rename to tests/legacy/test_legacy_qnspsa_old.py diff --git a/tests/legacy/test_qscript_old.py b/tests/legacy/test_legacy_qscript_old.py similarity index 100% rename from tests/legacy/test_qscript_old.py rename to tests/legacy/test_legacy_qscript_old.py diff --git a/tests/legacy/test_quantum_gradients_old.py b/tests/legacy/test_legacy_quantum_gradients_old.py similarity index 100% rename from tests/legacy/test_quantum_gradients_old.py rename to tests/legacy/test_legacy_quantum_gradients_old.py diff --git a/tests/legacy/test_qubit_device_adjoint_jacobian_old.py b/tests/legacy/test_legacy_qubit_device_adjoint_jacobian_old.py similarity index 100% rename from tests/legacy/test_qubit_device_adjoint_jacobian_old.py rename to tests/legacy/test_legacy_qubit_device_adjoint_jacobian_old.py diff --git a/tests/legacy/test_readout_old.py b/tests/legacy/test_legacy_readout_old.py similarity index 100% rename from tests/legacy/test_readout_old.py rename to tests/legacy/test_legacy_readout_old.py diff --git a/tests/legacy/test_split_non_commuting_old.py b/tests/legacy/test_legacy_split_non_commuting_old.py similarity index 100% rename from tests/legacy/test_split_non_commuting_old.py rename to tests/legacy/test_legacy_split_non_commuting_old.py diff --git a/tests/legacy/test_spsa_gradient_old.py b/tests/legacy/test_legacy_spsa_gradient_old.py similarity index 100% rename from tests/legacy/test_spsa_gradient_old.py rename to tests/legacy/test_legacy_spsa_gradient_old.py diff --git a/tests/legacy/test_tensorflow_old.py b/tests/legacy/test_legacy_tensorflow_old.py similarity index 100% rename from tests/legacy/test_tensorflow_old.py rename to tests/legacy/test_legacy_tensorflow_old.py diff --git a/tests/legacy/test_tensorflow_qnode_old.py b/tests/legacy/test_legacy_tensorflow_qnode_old.py similarity index 100% rename from tests/legacy/test_tensorflow_qnode_old.py rename to tests/legacy/test_legacy_tensorflow_qnode_old.py diff --git a/tests/legacy/test_torch_old.py b/tests/legacy/test_legacy_torch_old.py similarity index 100% rename from tests/legacy/test_torch_old.py rename to tests/legacy/test_legacy_torch_old.py diff --git a/tests/legacy/test_torch_qnode_old.py b/tests/legacy/test_legacy_torch_qnode_old.py similarity index 100% rename from tests/legacy/test_torch_qnode_old.py rename to tests/legacy/test_legacy_torch_qnode_old.py diff --git a/tests/legacy/test_transforms_old.py b/tests/legacy/test_legacy_transforms_old.py similarity index 100% rename from tests/legacy/test_transforms_old.py rename to tests/legacy/test_legacy_transforms_old.py diff --git a/tests/legacy/test_vjp_old.py b/tests/legacy/test_legacy_vjp_old.py similarity index 100% rename from tests/legacy/test_vjp_old.py rename to tests/legacy/test_legacy_vjp_old.py diff --git a/tests/tests_passing_pylint.txt b/tests/tests_passing_pylint.txt deleted file mode 100644 index aba2e9f36e2..00000000000 --- a/tests/tests_passing_pylint.txt +++ /dev/null @@ -1,74 +0,0 @@ -tests/circuit_graph/** -tests/data/** -tests/devices/experimental/** -tests/devices/qubit/** -tests/devices/** -tests/docs/** -tests/drawer/** -tests/fermi/test_fermi_mapping.py -tests/fermi/test_fermionic.py -tests/fermi/test_fermionic_operators.py -tests/fourier/** -tests/gpu/** -tests/gradients/core/** -tests/gradients/finite_diff/** -tests/gradients/parameter_shift/** -tests/interfaces/default_qubit_2_integration/** -tests/interfaces/** -tests/kernels/** -tests/legacy/test_measurements_old.py -tests/legacy/test_pulse_gradient_old.py -tests/math/** -tests/numpy/** -tests/measurements/** -tests/ops/functions/** -tests/ops/op_math/** -tests/ops/qubit/** -tests/ops/qutrit/** -tests/ops/** -tests/optimize/** -tests/pulse/** -tests/pauli/grouping/** -tests/pauli/** -tests/qchem/** -tests/qinfo/** -tests/qnn/** -tests/resource/** -tests/shadow/** -tests/tape/** -tests/templates/test_embeddings/** -tests/templates/test_layers/** -tests/templates/test_state_preparations/** -tests/templates/test_subroutines/** -tests/templates/test_swapnetworks/** -tests/templates/test_tensornetworks/** -tests/templates/** -tests/transforms/test_experimental/** -tests/transforms/** -tests/conftest.py -tests/gate_data.py -tests/test_about.py -tests/test_boolean_fn.py -tests/test_classical_gradients.py -tests/test_configuration.py -tests/test_debugging.py -tests/test_device.py -tests/test_grad.py -tests/test_hermitian_edge_cases.py -tests/test_io.py -tests/test_measurement_transformations.py -tests/test_observable.py -tests/test_operation.py -tests/test_qaoa.py -tests/test_qnode.py -tests/test_qubit_device.py -tests/test_queuing.py -tests/test_qutrit_device.py -tests/test_return_types.py -tests/test_return_types_qnode.py -tests/test_tensor_measurements.py -tests/test_tracker.py -tests/test_typing.py -tests/test_utils.py -tests/test_vqe.py -tests/test_wires.py