Skip to content
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

Change Subroutine Wrapped Callable to a class with call method #171

Merged
merged 7 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyteal/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ __all__ = [
"SubroutineDefinition",
"SubroutineDeclaration",
"SubroutineCall",
"SubroutineFnWrapper",
"ScratchSlot",
"ScratchLoad",
"ScratchStore",
Expand Down
2 changes: 2 additions & 0 deletions pyteal/ast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
SubroutineDefinition,
SubroutineDeclaration,
SubroutineCall,
SubroutineFnWrapper,
)
from .while_ import While
from .for_ import For
Expand Down Expand Up @@ -221,6 +222,7 @@
"SubroutineDefinition",
"SubroutineDeclaration",
"SubroutineCall",
"SubroutineFnWrapper",
"ScratchSlot",
"ScratchLoad",
"ScratchStore",
Expand Down
58 changes: 41 additions & 17 deletions pyteal/ast/subroutine.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from typing import Callable, Tuple, List, Optional, cast, TYPE_CHECKING
from typing import Callable, List, Optional, TYPE_CHECKING
from inspect import Parameter, signature
from functools import wraps

from ..types import TealType, require_type
from ..types import TealType
from ..ir import TealOp, Op, TealBlock
from ..errors import TealInputError, verifyTealVersion
from .expr import Expr
Expand Down Expand Up @@ -167,6 +166,39 @@ def has_return(self):
SubroutineCall.__module__ = "pyteal"


class SubroutineFnWrapper:
def __init__(
self,
fnImplementation: Callable[..., Expr],
returnType: TealType,
name: str = None,
) -> None:
self.subroutine = SubroutineDefinition(
fnImplementation, returnType=returnType, nameStr=name
)

def __call__(self, *args: Expr, **kwargs) -> Expr:
if len(kwargs) != 0:
raise TealInputError(
"Subroutine cannot be called with keyword arguments. Received keyword arguments: {}".format(
",".join(kwargs.keys())
)
)
return self.subroutine.invoke(list(args))

def name(self) -> str:
return self.subroutine.name()

def type_of(self):
return self.subroutine.getDeclaration().type_of()

def has_return(self):
return self.subroutine.getDeclaration().has_return()


SubroutineFnWrapper.__module__ = "pyteal"


class Subroutine:
"""Used to create a PyTeal subroutine from a Python function.

Expand Down Expand Up @@ -194,20 +226,12 @@ def __init__(self, returnType: TealType, name: str = None) -> None:
self.returnType = returnType
self.name = name

def __call__(self, fnImplementation: Callable[..., Expr]) -> Callable[..., Expr]:
subroutine = SubroutineDefinition(fnImplementation, self.returnType, self.name)

@wraps(fnImplementation)
def subroutineCall(*args: Expr, **kwargs) -> Expr:
if len(kwargs) != 0:
raise TealInputError(
"Subroutine cannot be called with keyword arguments. Received keyword arguments: {}".format(
",".join(kwargs.keys())
)
)
return subroutine.invoke(list(args))

return subroutineCall
def __call__(self, fnImplementation: Callable[..., Expr]) -> SubroutineFnWrapper:
return SubroutineFnWrapper(
fnImplementation=fnImplementation,
returnType=self.returnType,
name=self.name,
)


Subroutine.__module__ = "pyteal"
Expand Down
2 changes: 1 addition & 1 deletion pyteal/ast/subroutine_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def test_decorator():
def mySubroutine(a):
return Return()

assert callable(mySubroutine)
assert isinstance(mySubroutine, SubroutineFnWrapper)

invocation = mySubroutine(Int(1))
assert isinstance(invocation, SubroutineCall)
Expand Down