-
Notifications
You must be signed in to change notification settings - Fork 3
docs: Add section to explain Guppy type checker errors and fixes #120
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,3 +55,91 @@ This Guppy function doesn't currently have compilation to HUGR. Please find/rais | |
| ## Why am I getting "no lowering found" errors? | ||
|
|
||
| Though Guppy can compile your operation to HUGR, the lowering to LLVM executable code for the Selene simulator doesn't yet work. Please find a workaround or raise an issue on the [HUGR repository](https://github.com/quantinuum/hugr/). | ||
|
|
||
| ## Why is my type checker raising errors with Guppy? | ||
|
|
||
| When using a Python type checker (such as [mypy](https://www.mypy-lang.org/)) with Guppy you may encounter errors that arise from a fundamental difference between Python and Guppy syntax. | ||
|
|
||
| For example, using `@ owned` annotations in function signatures, | ||
|
|
||
| ```{code-cell} ipython3 | ||
| from guppylang.std.builtins import owned | ||
|
|
||
| @guppy | ||
| def foo(q: qubit @ owned) -> None: ... | ||
| ``` | ||
|
|
||
| is not valid Python syntax and `mypy` will raise the following error: | ||
|
|
||
| ``` | ||
| error: Invalid type comment or annotation [valid-type] | ||
| ``` | ||
|
|
||
| As this is a fundamental difference between Python and Guppy, it is not possible to resolve these errors. Instead, these errors must be suppressed in order for the type checker to run without error. There are two methods to suppress errors raised by `mypy`, although this may differ if another checker is used. | ||
|
|
||
| To suppress individual errors, the `type: ignore[...]` comment can be used on the line that causes the error. In this example, we can suppress the `valid-type` error with: | ||
|
|
||
| ```{code-cell} ipython3 | ||
| @guppy | ||
| def foo(q: qubit @ owned) -> None: ... # type: ignore[valid-type] | ||
| ``` | ||
|
|
||
| In the case where multiple errors are raised in the same function, it may be more convenient to suppress all errors using `@no_type_check`: | ||
|
|
||
| ```{code-cell} ipython3 | ||
| from typing import no_type_check | ||
|
|
||
| @guppy | ||
| @no_type_check | ||
| def foo(q: qubit @ owned) -> None: ... | ||
| ``` | ||
|
|
||
| Below are a few common examples in which Guppy will cause an error with `mypy` that will need to be suppressed. | ||
|
|
||
| ### Generic Guppy variables | ||
|
|
||
| Using generic Guppy variables in function signatures can cause `mypy` to raise errors. For example, the following use of a generic type variable will raise the `valid-type` error: | ||
|
|
||
| ```{code-cell} ipython3 | ||
| T = guppy.type_var("T") | ||
|
|
||
| @guppy | ||
| def foo(x: T) -> T: ... | ||
| ``` | ||
|
|
||
| Using generic natural variables can cause `mypy` to raise `call-overload` errors when used as arguments to other functions within a generic function definition. For example, using `range` with a generic will raise a `call-overload` error: | ||
|
|
||
| ```{code-cell} ipython3 | ||
| from guppylang.std.builtins import array | ||
|
|
||
| N = guppy.nat_var("N") | ||
|
|
||
| @guppy | ||
| def foo(arr: array[qubit, N]) -> None: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah this is a pylance thing rather than mypy. |
||
| for _ in range(N): ... | ||
| ``` | ||
|
|
||
| ### Guppy structs | ||
|
|
||
| Guppy structs can be a significant source of type check errors, especially when they are defined using generic variables. Consider the following example that defines a Guppy struct that has a generic size for the array: | ||
|
|
||
|
|
||
| ```{code-cell} ipython3 | ||
| from typing import Generic | ||
|
|
||
| @guppy.struct | ||
| class Foo(Generic[N]): | ||
| arr: array[qubit, N] | ||
| ``` | ||
|
|
||
| This will raise two type check errors from `mypy`: | ||
| 1. `Unsupported dynamic base class "Generic" [misc]` | ||
| 2. `Variable "N" is not valid as a type [valid-type]` | ||
|
|
||
| Unfortunately, `@no_type_check` cannot be used to suppress errors in Guppy structs, so instead individual errors must be suppressed with `type: ignore`. In the example above, this would look like: | ||
|
|
||
| ```{code-cell} ipython3 | ||
| @guppy.struct | ||
| class Foo(Generic[N]): # type: ignore[misc] | ||
| arr: array[qubit, N] # type: ignore[valid-type] | ||
| ``` | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems this cell needs to import
ownedin order to excute.If you don't want a specific cell to be executed you can add this
skip-executionmetadata like so.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strangely I cannot reproduce the C.I. error locally which is puzzling
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's strange, I tested locally and it was fine. I'll add the
skip-executiontag as it's not necessary for these to run.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change my mind and imported
ownedto be explicit.