From 29269833e316a4c6ba09d09a676a3678ee88e912 Mon Sep 17 00:00:00 2001 From: Agustin Borgna Date: Thu, 18 Jan 2024 15:03:32 +0000 Subject: [PATCH 1/4] chore: Fix pre-commit config, fix linting --- .gitignore | 1 - .pre-commit-config.yaml | 65 +++++++++++++--------------- devenv.yaml | 2 +- guppylang/hugr/hugr.py | 2 +- guppylang/hugr/visualise.py | 9 ++-- tests/error/py_errors/python_err.err | 1 - 6 files changed, 39 insertions(+), 41 deletions(-) diff --git a/.gitignore b/.gitignore index f2582958..1fe6322b 100644 --- a/.gitignore +++ b/.gitignore @@ -160,4 +160,3 @@ devenv.local.nix # pre-commit .pre-commit-config.yaml - diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b40b54ec..d49f1d7a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,63 +1,60 @@ repos: -- repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.5.0 # Use the ref you want to point at + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 # Use the ref you want to point at hooks: - - id: check-added-large-files - - id: check-case-conflict - - id: check-executables-have-shebangs - - id: check-merge-conflict - - id: check-toml - - id: check-vcs-permalinks - - id: check-yaml - - id: detect-private-key - - id: end-of-file-fixer - - id: fix-byte-order-marker - - id: mixed-line-ending - # - id: trailing-whitespace - # Python-specific - - id: check-ast - - id: check-docstring-first - - id: debug-statements + - id: check-added-large-files + - id: check-case-conflict + - id: check-executables-have-shebangs + - id: check-merge-conflict + - id: check-toml + - id: check-vcs-permalinks + - id: check-yaml + - id: detect-private-key + - id: end-of-file-fixer + - id: fix-byte-order-marker + - id: mixed-line-ending + # - id: trailing-whitespace + # Python-specific + - id: check-ast + - id: check-docstring-first + - id: debug-statements -- repo: https://github.com/crate-ci/typos + - repo: https://github.com/crate-ci/typos rev: v1.16.23 hooks: - - id: typos + - id: typos args: [] -- repo: https://github.com/astral-sh/ruff-pre-commit + - repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.1.7 hooks: - id: ruff args: [--fix, --exit-non-zero-on-fix] - id: ruff-format -- repo: https://github.com/pre-commit/mirrors-mypy - rev: 'v1.3.0' + - repo: https://github.com/pre-commit/mirrors-mypy + rev: "v1.8.0" hooks: - - id: mypy - pass_filenames: false - args: [--package=guppy] - additional_dependencies: [ - ormsgpack, - pydantic, - ] + - id: mypy + pass_filenames: false + args: [--package=guppylang] + additional_dependencies: [ormsgpack, pydantic] -- repo: local + - repo: local hooks: - - id: cargo-check + - id: cargo-check name: Cargo check entry: bash -c 'cd validator && exec cargo check' pass_filenames: false types: [file, rust] language: system - - id: rust-linting + - id: rust-linting name: Rust linting entry: bash -c 'cd validator && exec cargo fmt --all --' pass_filenames: true types: [file, rust] language: system - - id: rust-clippy + - id: rust-clippy name: Rust clippy entry: bash -c 'cd validator && exec cargo clippy --all-targets --all-features -- -Dclippy::all' pass_filenames: false diff --git a/devenv.yaml b/devenv.yaml index 87a8faa4..62b57224 100644 --- a/devenv.yaml +++ b/devenv.yaml @@ -5,4 +5,4 @@ inputs: url: github:nix-community/fenix inputs: nixpkgs: - follows: nixpkgs \ No newline at end of file + follows: nixpkgs diff --git a/guppylang/hugr/hugr.py b/guppylang/hugr/hugr.py index 9059c3c7..07db8f1d 100644 --- a/guppylang/hugr/hugr.py +++ b/guppylang/hugr/hugr.py @@ -5,7 +5,7 @@ from dataclasses import dataclass, field from typing import Any, Optional -import networkx as nx # type: ignore[import-untyped] +import networkx as nx # type: ignore[import-not-found] import guppylang.hugr.ops as ops import guppylang.hugr.raw as raw diff --git a/guppylang/hugr/visualise.py b/guppylang/hugr/visualise.py index 08375a4f..d1428a73 100644 --- a/guppylang/hugr/visualise.py +++ b/guppylang/hugr/visualise.py @@ -3,7 +3,7 @@ from collections.abc import Iterable from typing import TYPE_CHECKING -import graphviz as gv # type: ignore[import-untyped] +import graphviz as gv # type: ignore[import-not-found] from guppylang.cfg.analysis import ( DefAssignmentDomain, @@ -257,14 +257,17 @@ def cfg_to_graphviz( ) -> gv.Digraph: graph = gv.Digraph("CFG", strict=False) for bb in cfg.bbs: - label = f""" + label = ( + f""" assigned: {commas(*bb.vars.assigned)} used: {commas(*bb.vars.used)} maybe_ass_before: {commas(*maybe_ass_before[bb])} ass_before: {commas(*ass_before[bb])} live_before: {commas(*live_before[bb])} -------- -""" + "\n".join(ast.unparse(s) for s in bb.statements) +""" + + "\n".join(ast.unparse(s) for s in bb.statements) + ) if bb.branch_pred is not None: label += f"\n{ast.unparse(bb.branch_pred)} ?" graph.node(str(bb.idx), label, shape="rect") diff --git a/tests/error/py_errors/python_err.err b/tests/error/py_errors/python_err.err index 5d21c3e8..70ba149c 100644 --- a/tests/error/py_errors/python_err.err +++ b/tests/error/py_errors/python_err.err @@ -9,4 +9,3 @@ GuppyError: Error occurred while evaluating Python expression: Traceback (most recent call last): File "", line 1, in ZeroDivisionError: division by zero - From 95cd3a173dca2d4031b9baa9c34cf340d37f745a Mon Sep 17 00:00:00 2001 From: Agustin Borgna Date: Thu, 18 Jan 2024 17:31:26 +0000 Subject: [PATCH 2/4] Add missing mypy dependencies See https://github.com/pre-commit/mirrors-mypy?tab=readme-ov-file#using-mypy-with-pre-commit --- .pre-commit-config.yaml | 2 +- guppylang/hugr/hugr.py | 2 +- guppylang/hugr/visualise.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d49f1d7a..ce7135c7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -38,7 +38,7 @@ repos: - id: mypy pass_filenames: false args: [--package=guppylang] - additional_dependencies: [ormsgpack, pydantic] + additional_dependencies: [ormsgpack, pydantic, graphviz, networkx] - repo: local hooks: diff --git a/guppylang/hugr/hugr.py b/guppylang/hugr/hugr.py index 07db8f1d..9059c3c7 100644 --- a/guppylang/hugr/hugr.py +++ b/guppylang/hugr/hugr.py @@ -5,7 +5,7 @@ from dataclasses import dataclass, field from typing import Any, Optional -import networkx as nx # type: ignore[import-not-found] +import networkx as nx # type: ignore[import-untyped] import guppylang.hugr.ops as ops import guppylang.hugr.raw as raw diff --git a/guppylang/hugr/visualise.py b/guppylang/hugr/visualise.py index d1428a73..590861ec 100644 --- a/guppylang/hugr/visualise.py +++ b/guppylang/hugr/visualise.py @@ -3,7 +3,7 @@ from collections.abc import Iterable from typing import TYPE_CHECKING -import graphviz as gv # type: ignore[import-not-found] +import graphviz as gv # type: ignore[import-untyped] from guppylang.cfg.analysis import ( DefAssignmentDomain, From d0f7b6e91adb0716d1a832247ab05dec91867993 Mon Sep 17 00:00:00 2001 From: Agustin Borgna Date: Thu, 18 Jan 2024 17:36:54 +0000 Subject: [PATCH 3/4] Update ruff too (it formatted things differently otherwise) --- .pre-commit-config.yaml | 5 ++--- guppylang/hugr/visualise.py | 7 ++----- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ce7135c7..fdc6c4af 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -20,13 +20,12 @@ repos: - id: debug-statements - repo: https://github.com/crate-ci/typos - rev: v1.16.23 + rev: v1.17.1 hooks: - id: typos - args: [] - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.1.7 + rev: v0.1.13 hooks: - id: ruff args: [--fix, --exit-non-zero-on-fix] diff --git a/guppylang/hugr/visualise.py b/guppylang/hugr/visualise.py index 590861ec..08375a4f 100644 --- a/guppylang/hugr/visualise.py +++ b/guppylang/hugr/visualise.py @@ -257,17 +257,14 @@ def cfg_to_graphviz( ) -> gv.Digraph: graph = gv.Digraph("CFG", strict=False) for bb in cfg.bbs: - label = ( - f""" + label = f""" assigned: {commas(*bb.vars.assigned)} used: {commas(*bb.vars.used)} maybe_ass_before: {commas(*maybe_ass_before[bb])} ass_before: {commas(*ass_before[bb])} live_before: {commas(*live_before[bb])} -------- -""" - + "\n".join(ast.unparse(s) for s in bb.statements) - ) +""" + "\n".join(ast.unparse(s) for s in bb.statements) if bb.branch_pred is not None: label += f"\n{ast.unparse(bb.branch_pred)} ?" graph.node(str(bb.idx), label, shape="rect") From 784d8af0b89a3e0282195e8cd85cc73064fa783f Mon Sep 17 00:00:00 2001 From: Kartik Singhal Date: Fri, 19 Jan 2024 07:00:38 +0000 Subject: [PATCH 4/4] style: fix remaining issues raised by pre-commit, ignore eof fixer --- .github/workflows/pull-request.yaml | 1 - .pre-commit-config.yaml | 12 +++++++++--- tests/error/py_errors/python_err.err | 1 + tests/error/test_py_errors.py | 9 +++------ tests/integration/test_py.py | 12 ++++-------- 5 files changed, 17 insertions(+), 18 deletions(-) diff --git a/.github/workflows/pull-request.yaml b/.github/workflows/pull-request.yaml index 0acd0081..4334d783 100644 --- a/.github/workflows/pull-request.yaml +++ b/.github/workflows/pull-request.yaml @@ -72,4 +72,3 @@ jobs: - name: Rerun `py(...)` expression tests with tket2 installed run: poetry run pytest tests/integration/test_py.py tests/error/test_py_errors.py - diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index fdc6c4af..62dd3eaa 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -10,10 +10,10 @@ repos: - id: check-vcs-permalinks - id: check-yaml - id: detect-private-key - - id: end-of-file-fixer + # - id: end-of-file-fixer + # - id: trailing-whitespace - id: fix-byte-order-marker - id: mixed-line-ending - # - id: trailing-whitespace # Python-specific - id: check-ast - id: check-docstring-first @@ -37,7 +37,13 @@ repos: - id: mypy pass_filenames: false args: [--package=guppylang] - additional_dependencies: [ormsgpack, pydantic, graphviz, networkx] + additional_dependencies: [ + graphviz, + networkx, + ormsgpack, + pydantic, + pytket, + ] - repo: local hooks: diff --git a/tests/error/py_errors/python_err.err b/tests/error/py_errors/python_err.err index 70ba149c..5d21c3e8 100644 --- a/tests/error/py_errors/python_err.err +++ b/tests/error/py_errors/python_err.err @@ -9,3 +9,4 @@ GuppyError: Error occurred while evaluating Python expression: Traceback (most recent call last): File "", line 1, in ZeroDivisionError: division by zero + diff --git a/tests/error/test_py_errors.py b/tests/error/test_py_errors.py index 84b4cb60..6bcc57e4 100644 --- a/tests/error/test_py_errors.py +++ b/tests/error/test_py_errors.py @@ -1,15 +1,12 @@ +from importlib.util import find_spec + import pathlib import pytest from tests.error.util import run_error_test -try: - import tket2 - - tket2_installed = True -except ImportError: - tket2_installed = False +tket2_installed = find_spec("tket2") is not None path = pathlib.Path(__file__).parent.resolve() / "py_errors" diff --git a/tests/integration/test_py.py b/tests/integration/test_py.py index 93dbf299..1908d85e 100644 --- a/tests/integration/test_py.py +++ b/tests/integration/test_py.py @@ -1,3 +1,5 @@ +from importlib.util import find_spec + import pytest from guppylang.decorator import guppy @@ -6,13 +8,7 @@ from tests.integration.util import py from tests.util import compile_guppy - -try: - import tket2 - - tket2_installed = True -except ImportError: - tket2_installed = False +tket2_installed = find_spec("tket2") is not None def test_basic(validate): @@ -107,7 +103,7 @@ def test_pytket_multi_qubit(validate): module.load(quantum) @guppy(module) - def foo(q1: Qubit, q2:Qubit, q3:Qubit) -> tuple[Qubit, Qubit, Qubit]: + def foo(q1: Qubit, q2: Qubit, q3: Qubit) -> tuple[Qubit, Qubit, Qubit]: return py(circ)(q1, q2, q3) validate(module.compile())