Skip to content

Commit

Permalink
Added TealSeqError for better error reporting. (#661)
Browse files Browse the repository at this point in the history
  • Loading branch information
BarryJRowe committed Feb 10, 2023
1 parent 76347f2 commit 5dbbc85
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
2 changes: 2 additions & 0 deletions pyteal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from pyteal.errors import (
TealInternalError,
TealTypeError,
TealSeqError,
TealInputError,
TealCompileError,
TealPragmaError,
Expand Down Expand Up @@ -47,6 +48,7 @@
"TealType",
"TealInternalError",
"TealTypeError",
"TealSeqError",
"TealInputError",
"TealCompileError",
"TealPragmaError",
Expand Down
2 changes: 2 additions & 0 deletions pyteal/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ from pyteal.types import TealType
from pyteal.errors import (
TealInternalError,
TealTypeError,
TealSeqError,
TealInputError,
TealCompileError,
TealPragmaError,
Expand Down Expand Up @@ -210,6 +211,7 @@ __all__ = [
"TealLabel",
"TealOp",
"TealPragmaError",
"TealSeqError",
"TealSimpleBlock",
"TealType",
"TealTypeError",
Expand Down
14 changes: 12 additions & 2 deletions pyteal/ast/seq.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List, TYPE_CHECKING, overload

from pyteal.types import TealType, require_type
from pyteal.errors import TealInputError
from pyteal.errors import TealInputError, TealTypeError, TealSeqError
from pyteal.ir import TealSimpleBlock
from pyteal.ast.expr import Expr

Expand Down Expand Up @@ -51,7 +51,17 @@ def __init__(self, *exprs):
if not isinstance(expr, Expr):
raise TealInputError("{} is not a pyteal expression.".format(expr))
if i + 1 < len(exprs):
require_type(expr, TealType.none)
try:
require_type(expr, TealType.none)
except TealTypeError:
message = "{} must have a return type of TealType.none. ".format(
expr
)
message += (
"Only the last entry of a Seq array can have a return value."
)
seq_error = TealSeqError(message)
raise seq_error

self.args = exprs

Expand Down
6 changes: 3 additions & 3 deletions pyteal/ast/seq_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ def test_seq_has_return():


def test_seq_invalid():
with pytest.raises(pt.TealTypeError):
with pytest.raises(pt.TealSeqError):
pt.Seq([pt.Int(1), pt.Pop(pt.Int(2))])

with pytest.raises(pt.TealTypeError):
with pytest.raises(pt.TealSeqError):
pt.Seq([pt.Int(1), pt.Int(2)])

with pytest.raises(pt.TealTypeError):
with pytest.raises(pt.TealSeqError):
pt.Seq([pt.Seq([pt.Pop(pt.Int(1)), pt.Int(2)]), pt.Int(3)])


Expand Down
11 changes: 11 additions & 0 deletions pyteal/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ def __str__(self) -> str:
TealTypeError.__module__ = "pyteal"


class TealSeqError(TealTypeError):
def __init__(self, message: str) -> None:
self.message = message

def __str__(self):
return self.message


TealSeqError.__module__ = "pyteal"


class TealInputError(Exception):
def __init__(self, msg: str) -> None:
self.message = msg
Expand Down

0 comments on commit 5dbbc85

Please sign in to comment.