Skip to content

Commit

Permalink
Move compile=True decorator to a test util. Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Jan 18, 2024
1 parent e453188 commit 4e52141
Show file tree
Hide file tree
Showing 115 changed files with 310 additions and 294 deletions.
31 changes: 7 additions & 24 deletions guppylang/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ def __init__(self) -> None:
self._modules = {}

@pretty_errors
def __call__(
self, arg: PyFunc | GuppyModule | None = None, *, compile: bool = False
) -> Hugr | None | FuncDecorator:
def __call__(self, arg: PyFunc | GuppyModule) -> None | FuncDecorator:
"""Decorator to annotate Python functions as Guppy code.
Optionally, the `GuppyModule` in which the function should be placed can
Expand All @@ -70,11 +68,6 @@ def __call__(
function is compiled immediately as an standalone module and the Hugr is
returned.
"""
if arg is not None and not isinstance(arg, GuppyModule):
# Decorator used without any arguments.
f = arg
decorator: FuncDecorator = self.__call__(None) # type: ignore[assignment]
return decorator(f)

def make_dummy(wraps: PyFunc) -> Callable[..., Any]:
@functools.wraps(wraps)
Expand All @@ -85,20 +78,11 @@ def dummy(*args: Any, **kwargs: Any) -> Any:

return dummy

if arg is None and compile:
# No module passed, and compile option is set.
def dec(f: Callable[..., Any]) -> Callable[..., Any] | Hugr:
module = GuppyModule("module")
module.register_func_def(f)
compiled = module.compile()
assert compiled is not None
return compiled

return dec
if not isinstance(arg, GuppyModule):
# Decorator used without any arguments.
# We default to a module associated with the caller of the decorator.
f = arg

if arg is None and not compile:
# No module specified, and `compile` option is not set.
# We use a module associate with the caller of the decorator.
def dec(f: Callable[..., Any]) -> Callable[..., Any] | Hugr:
caller = self._get_python_caller()
if caller not in self._modules:
Expand All @@ -107,11 +91,10 @@ def dec(f: Callable[..., Any]) -> Callable[..., Any] | Hugr:
module.register_func_def(f)
return make_dummy(f)

return dec
return dec(f)

if isinstance(arg, GuppyModule):
# Module passed. Ignore `compile` option.

# Module passed.
def dec(f: Callable[..., Any]) -> Callable[..., Any] | Hugr:
arg.register_func_def(f)
return make_dummy(f)
Expand Down
5 changes: 5 additions & 0 deletions guppylang/hugr/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Python definitions for the Hierarchical Unified Graph Representation."""

from .hugr import Hugr

__all__ = ["Hugr"]
2 changes: 1 addition & 1 deletion tests/error/comprehension_errors/illegal_short_circuit.err
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Guppy compilation failed. Error in file $FILE:6

4: @guppy
4: @compile_guppy
5: def foo(xs: list[int]) -> None:
6: [x for x in xs if x < 5 and x != 6]
^^^^^^^^^^^^^^^^
Expand Down
4 changes: 2 additions & 2 deletions tests/error/comprehension_errors/illegal_short_circuit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from guppylang.decorator import guppy
from tests.util import compile_guppy


@guppy
@compile_guppy
def foo(xs: list[int]) -> None:
[x for x in xs if x < 5 and x != 6]
2 changes: 1 addition & 1 deletion tests/error/comprehension_errors/illegal_ternary.err
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Guppy compilation failed. Error in file $FILE:6

4: @guppy
4: @compile_guppy
5: def foo(xs: list[int], ys: list[int], b: bool) -> None:
6: [x for x in (xs if b else ys)]
^^^^^^^^^^^^^^^
Expand Down
4 changes: 2 additions & 2 deletions tests/error/comprehension_errors/illegal_ternary.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from guppylang.decorator import guppy
from tests.util import compile_guppy


@guppy
@compile_guppy
def foo(xs: list[int], ys: list[int], b: bool) -> None:
[x for x in (xs if b else ys)]
2 changes: 1 addition & 1 deletion tests/error/comprehension_errors/illegal_walrus.err
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Guppy compilation failed. Error in file $FILE:6

4: @guppy
4: @compile_guppy
5: def foo(xs: list[int]) -> None:
6: [y := x for x in xs]
^^^^^^
Expand Down
4 changes: 2 additions & 2 deletions tests/error/comprehension_errors/illegal_walrus.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from guppylang.decorator import guppy
from tests.util import compile_guppy


@guppy
@compile_guppy
def foo(xs: list[int]) -> None:
[y := x for x in xs]
4 changes: 2 additions & 2 deletions tests/error/errors_on_usage/and_not_defined.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from guppylang.decorator import guppy
from tests.util import compile_guppy


@guppy(compile=True)
@compile_guppy
def foo(x: bool, y: int) -> int:
if x and (z := y + 1):
return z
Expand Down
4 changes: 2 additions & 2 deletions tests/error/errors_on_usage/else_expr_not_defined.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from guppylang.decorator import guppy
from tests.util import compile_guppy


@guppy(compile=True)
@compile_guppy
def foo(x: bool) -> int:
(y := 1) if x else (z := 2)
return z
4 changes: 2 additions & 2 deletions tests/error/errors_on_usage/else_expr_type_change.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from guppylang.decorator import guppy
from tests.util import compile_guppy


@guppy(compile=True)
@compile_guppy
def foo(x: bool) -> int:
y = 3
(y := y + 1) if x else (y := True)
Expand Down
4 changes: 2 additions & 2 deletions tests/error/errors_on_usage/else_not_defined.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from guppylang.decorator import guppy
from tests.util import compile_guppy


@guppy(compile=True)
@compile_guppy
def foo(x: bool) -> int:
if x:
y = 1
Expand Down
2 changes: 1 addition & 1 deletion tests/error/errors_on_usage/else_not_defined_functional.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from guppylang.decorator import guppy


@guppy(compile=True)
@compile_guppy
def foo(x: bool) -> int:
_@functional
if x:
Expand Down
4 changes: 2 additions & 2 deletions tests/error/errors_on_usage/else_type_change.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from guppylang.decorator import guppy
from tests.util import compile_guppy


@guppy(compile=True)
@compile_guppy
def foo(x: bool) -> int:
y = 3
if x:
Expand Down
2 changes: 1 addition & 1 deletion tests/error/errors_on_usage/else_type_change_functional.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from guppylang.decorator import guppy


@guppy(compile=True)
@compile_guppy
def foo(x: bool) -> int:
y = 3
_@functional
Expand Down
4 changes: 2 additions & 2 deletions tests/error/errors_on_usage/for_new_var.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from guppylang.decorator import guppy
from tests.util import compile_guppy


@guppy
@compile_guppy
def foo(xs: list[int]) -> int:
for _ in xs:
y = 5
Expand Down
4 changes: 2 additions & 2 deletions tests/error/errors_on_usage/for_target.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from guppylang.decorator import guppy
from tests.util import compile_guppy


@guppy
@compile_guppy
def foo(xs: list[int]) -> int:
for x in xs:
pass
Expand Down
4 changes: 2 additions & 2 deletions tests/error/errors_on_usage/for_target_type_change.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from guppylang.decorator import guppy
from tests.util import compile_guppy


@guppy
@compile_guppy
def foo(xs: list[bool]) -> int:
x = 5
for x in xs:
Expand Down
4 changes: 2 additions & 2 deletions tests/error/errors_on_usage/for_type_change.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from guppylang.decorator import guppy
from tests.util import compile_guppy


@guppy
@compile_guppy
def foo(xs: list[int]) -> int:
y = 5
for x in xs:
Expand Down
4 changes: 2 additions & 2 deletions tests/error/errors_on_usage/if_different_types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from guppylang.decorator import guppy
from tests.util import compile_guppy


@guppy(compile=True)
@compile_guppy
def foo(x: bool) -> int:
if x:
y = 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from guppylang.decorator import guppy


@guppy(compile=True)
@compile_guppy
def foo(x: bool) -> int:
_@functional
if x:
Expand Down
4 changes: 2 additions & 2 deletions tests/error/errors_on_usage/if_expr_cond_type_change.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from guppylang.decorator import guppy
from tests.util import compile_guppy


@guppy(compile=True)
@compile_guppy
def foo(x: bool) -> int:
y = 4
0 if (y := x) else (y := 6)
Expand Down
4 changes: 2 additions & 2 deletions tests/error/errors_on_usage/if_expr_not_defined.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from guppylang.decorator import guppy
from tests.util import compile_guppy


@guppy(compile=True)
@compile_guppy
def foo(x: bool) -> int:
(y := 1) if x else 0
return y
4 changes: 2 additions & 2 deletions tests/error/errors_on_usage/if_expr_type_change.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from guppylang.decorator import guppy
from tests.util import compile_guppy


@guppy(compile=True)
@compile_guppy
def foo(x: bool, a: int) -> int:
y = 3
(y := False) if x or a > 5 else 0
Expand Down
2 changes: 1 addition & 1 deletion tests/error/errors_on_usage/if_expr_type_conflict.err
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Guppy compilation failed. Error in file $FILE:6

4: @guppy(compile=True)
4: @compile_guppy
5: def foo(x: bool) -> None:
6: y = True if x else 42
^^^^^^^^^^^^^^^^^
Expand Down
4 changes: 2 additions & 2 deletions tests/error/errors_on_usage/if_expr_type_conflict.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from guppylang.decorator import guppy
from tests.util import compile_guppy


@guppy(compile=True)
@compile_guppy
def foo(x: bool) -> None:
y = True if x else 42
4 changes: 2 additions & 2 deletions tests/error/errors_on_usage/if_not_defined.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from guppylang.decorator import guppy
from tests.util import compile_guppy


@guppy(compile=True)
@compile_guppy
def foo(x: bool) -> int:
if x:
y = 1
Expand Down
2 changes: 1 addition & 1 deletion tests/error/errors_on_usage/if_not_defined_functional.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from guppylang.decorator import guppy


@guppy(compile=True)
@compile_guppy
def foo(x: bool) -> int:
_@functional
if x:
Expand Down
4 changes: 2 additions & 2 deletions tests/error/errors_on_usage/if_type_change.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from guppylang.decorator import guppy
from tests.util import compile_guppy


@guppy(compile=True)
@compile_guppy
def foo(x: bool) -> int:
y = 3
if x:
Expand Down
2 changes: 1 addition & 1 deletion tests/error/errors_on_usage/if_type_change_functional.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from guppylang.decorator import guppy


@guppy(compile=True)
@compile_guppy
def foo(x: bool) -> int:
y = 3
_@functional
Expand Down
4 changes: 2 additions & 2 deletions tests/error/errors_on_usage/or_not_defined.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from guppylang.decorator import guppy
from tests.util import compile_guppy


@guppy(compile=True)
@compile_guppy
def foo(x: bool, y: int) -> int:
if x or (z := y + 1):
return z
Expand Down
4 changes: 2 additions & 2 deletions tests/error/errors_on_usage/while_new_var.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from guppylang.decorator import guppy
from tests.util import compile_guppy


@guppy(compile=True)
@compile_guppy
def foo(x: bool) -> int:
while x:
y = 5
Expand Down
2 changes: 1 addition & 1 deletion tests/error/errors_on_usage/while_new_var_functional.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from guppylang.decorator import guppy


@guppy(compile=True)
@compile_guppy
def foo(x: bool) -> int:
_@functional
while x:
Expand Down
4 changes: 2 additions & 2 deletions tests/error/errors_on_usage/while_type_change.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from guppylang.decorator import guppy
from tests.util import compile_guppy


@guppy(compile=True)
@compile_guppy
def foo(x: bool) -> int:
y = 5
while x:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from guppylang.decorator import guppy


@guppy(compile=True)
@compile_guppy
def foo(x: bool) -> int:
y = 5
_@functional
Expand Down
2 changes: 1 addition & 1 deletion tests/error/misc_errors/arg_not_annotated.err
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Guppy compilation failed. Error in file $FILE:5

3: @guppy(compile=True)
3: @compile_guppy
4: def foo(x: bool, y) -> int:
^
GuppyError: Argument type must be annotated
4 changes: 2 additions & 2 deletions tests/error/misc_errors/arg_not_annotated.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from guppylang.decorator import guppy
from tests.util import compile_guppy


@guppy(compile=True)
@compile_guppy
def foo(x: bool, y) -> int:
return y
Loading

0 comments on commit 4e52141

Please sign in to comment.