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

ABI Method Return #175

Merged
merged 7 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions pyteal/ast/abi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from .array_static import StaticArrayTypeSpec, StaticArray
from .array_dynamic import DynamicArrayTypeSpec, DynamicArray
from .util import type_spec_from_annotation
from .method_return import MethodReturn

__all__ = [
"TypeSpec",
Expand Down Expand Up @@ -65,4 +66,5 @@
"DynamicArrayTypeSpec",
"DynamicArray",
"type_spec_from_annotation",
"MethodReturn",
]
36 changes: 36 additions & 0 deletions pyteal/ast/abi/method_return.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from typing import TYPE_CHECKING, Tuple
from . import BaseType
from ...types import TealType
from ...errors import TealInputError
from .. import Expr, Log
from ...ir import TealBlock, TealSimpleBlock, Op

if TYPE_CHECKING:
from ...compiler import CompileOptions


class MethodReturn(Expr):
def __init__(self, arg: BaseType):
super().__init__()
if not isinstance(arg, BaseType):
raise TealInputError(f"Expecting an ABI type argument but get {arg}")
self.arg = arg

def __teal__(self, options: "CompileOptions") -> Tuple[TealBlock, TealSimpleBlock]:
if options.version < Op.log.min_version:
raise TealInputError(
f"current version {options.version} is lower than log's min version {Op.log.min_version}"
)
return Log(self.arg.encode()).__teal__(options)
ahangsu marked this conversation as resolved.
Show resolved Hide resolved

def __str__(self) -> str:
return f"(MethodReturn {self.arg.type_spec()})"

def type_of(self) -> TealType:
return TealType.none

def has_return(self) -> bool:
return False


MethodReturn.__module__ = "pyteal"
35 changes: 35 additions & 0 deletions pyteal/ast/abi/method_return_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import pytest

from . import *
from .. import Int, Bytes
from ...types import TealType
from ...errors import TealInputError


POSITIVE_CASES = [
Uint16(),
Uint32(),
StaticArray(BoolTypeSpec(), 12),
]


@pytest.mark.parametrize("case", POSITIVE_CASES)
def test_method_return(case):
m_ret = MethodReturn(case)
assert m_ret.type_of() == TealType.none
assert not m_ret.has_return()
assert str(m_ret) == f"(MethodReturn {case.type_spec()})"


NEGATIVE_CASES = [
Int(0),
Bytes("aaaaaaa"),
Uint16,
Uint32,
]


@pytest.mark.parametrize("case", NEGATIVE_CASES)
def test_method_return_error(case):
with pytest.raises(TealInputError):
MethodReturn(case)