Skip to content

Commit

Permalink
Call type_of() in require_type() for better exception messages (#151)
Browse files Browse the repository at this point in the history
* call type_of in require_type to catch exceptions

* fix formatting for types.py and types_test.py
  • Loading branch information
joe-p authored Dec 30, 2021
1 parent 07d60f2 commit a9c42e3
Show file tree
Hide file tree
Showing 23 changed files with 100 additions and 81 deletions.
54 changes: 27 additions & 27 deletions pyteal/ast/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ def optedIn(cls, account: Expr, app: Expr) -> "App":
must be evaluated to uint64 (or, since v4, an application id that appears in
Txn.ForeignApps or is the CurrentApplicationID, must be evaluated to bytes).
"""
require_type(account.type_of(), TealType.anytype)
require_type(app.type_of(), TealType.uint64)
require_type(account, TealType.anytype)
require_type(app, TealType.uint64)
return cls(AppField.optedIn, [account, app])

@classmethod
Expand All @@ -109,8 +109,8 @@ def localGet(cls, account: Expr, key: Expr) -> "App":
Txn.Accounts or is Txn.Sender, must be evaluated to bytes).
key: The key to read from the account's local state. Must evaluate to bytes.
"""
require_type(account.type_of(), TealType.anytype)
require_type(key.type_of(), TealType.bytes)
require_type(account, TealType.anytype)
require_type(key, TealType.bytes)
return cls(AppField.localGet, [account, key])

@classmethod
Expand All @@ -126,9 +126,9 @@ def localGetEx(cls, account: Expr, app: Expr, key: Expr) -> MaybeValue:
Txn.ForeignApps or is the CurrentApplicationID, must be evaluated to bytes).
key: The key to read from the account's local state. Must evaluate to bytes.
"""
require_type(account.type_of(), TealType.anytype)
require_type(app.type_of(), TealType.uint64)
require_type(key.type_of(), TealType.bytes)
require_type(account, TealType.anytype)
require_type(app, TealType.uint64)
require_type(key, TealType.bytes)
return MaybeValue(
AppField.localGetEx.get_op(), TealType.anytype, args=[account, app, key]
)
Expand All @@ -140,7 +140,7 @@ def globalGet(cls, key: Expr) -> "App":
Args:
key: The key to read from the global application state. Must evaluate to bytes.
"""
require_type(key.type_of(), TealType.bytes)
require_type(key, TealType.bytes)
return cls(AppField.globalGet, [key])

@classmethod
Expand All @@ -153,8 +153,8 @@ def globalGetEx(cls, app: Expr, key: Expr) -> MaybeValue:
Txn.ForeignApps or is the CurrentApplicationID, must be evaluated to uint64).
key: The key to read from the global application state. Must evaluate to bytes.
"""
require_type(app.type_of(), TealType.uint64)
require_type(key.type_of(), TealType.bytes)
require_type(app, TealType.uint64)
require_type(key, TealType.bytes)
return MaybeValue(
AppField.globalGetEx.get_op(), TealType.anytype, args=[app, key]
)
Expand All @@ -170,9 +170,9 @@ def localPut(cls, account: Expr, key: Expr, value: Expr) -> "App":
key: The key to write in the account's local state. Must evaluate to bytes.
value: The value to write in the account's local state. Can evaluate to any type.
"""
require_type(account.type_of(), TealType.anytype)
require_type(key.type_of(), TealType.bytes)
require_type(value.type_of(), TealType.anytype)
require_type(account, TealType.anytype)
require_type(key, TealType.bytes)
require_type(value, TealType.anytype)
return cls(AppField.localPut, [account, key, value])

@classmethod
Expand All @@ -183,8 +183,8 @@ def globalPut(cls, key: Expr, value: Expr) -> "App":
key: The key to write in the global application state. Must evaluate to bytes.
value: THe value to write in the global application state. Can evaluate to any type.
"""
require_type(key.type_of(), TealType.bytes)
require_type(value.type_of(), TealType.anytype)
require_type(key, TealType.bytes)
require_type(value, TealType.anytype)
return cls(AppField.globalPut, [key, value])

@classmethod
Expand All @@ -197,8 +197,8 @@ def localDel(cls, account: Expr, key: Expr) -> "App":
Txn.Accounts or is Txn.Sender, must be evaluated to bytes).
key: The key to delete from the account's local state. Must evaluate to bytes.
"""
require_type(account.type_of(), TealType.anytype)
require_type(key.type_of(), TealType.bytes)
require_type(account, TealType.anytype)
require_type(key, TealType.bytes)
return cls(AppField.localDel, [account, key])

@classmethod
Expand All @@ -208,7 +208,7 @@ def globalDel(cls, key: Expr) -> "App":
Args:
key: The key to delete from the global application state. Must evaluate to bytes.
"""
require_type(key.type_of(), TealType.bytes)
require_type(key, TealType.bytes)
return cls(AppField.globalDel, [key])


Expand All @@ -224,7 +224,7 @@ def approvalProgram(cls, app: Expr) -> MaybeValue:
app: An index into Txn.ForeignApps that correspond to the application to check.
Must evaluate to uint64.
"""
require_type(app.type_of(), TealType.uint64)
require_type(app, TealType.uint64)
return MaybeValue(
Op.app_params_get,
TealType.bytes,
Expand All @@ -240,7 +240,7 @@ def clearStateProgram(cls, app: Expr) -> MaybeValue:
app: An index into Txn.ForeignApps that correspond to the application to check.
Must evaluate to uint64.
"""
require_type(app.type_of(), TealType.uint64)
require_type(app, TealType.uint64)
return MaybeValue(
Op.app_params_get,
TealType.bytes,
Expand All @@ -256,7 +256,7 @@ def globalNumUnit(cls, app: Expr) -> MaybeValue:
app: An index into Txn.ForeignApps that correspond to the application to check.
Must evaluate to uint64.
"""
require_type(app.type_of(), TealType.uint64)
require_type(app, TealType.uint64)
return MaybeValue(
Op.app_params_get,
TealType.uint64,
Expand All @@ -272,7 +272,7 @@ def globalNumByteSlice(cls, app: Expr) -> MaybeValue:
app: An index into Txn.ForeignApps that correspond to the application to check.
Must evaluate to uint64.
"""
require_type(app.type_of(), TealType.uint64)
require_type(app, TealType.uint64)
return MaybeValue(
Op.app_params_get,
TealType.uint64,
Expand All @@ -288,7 +288,7 @@ def localNumUnit(cls, app: Expr) -> MaybeValue:
app: An index into Txn.ForeignApps that correspond to the application to check.
Must evaluate to uint64.
"""
require_type(app.type_of(), TealType.uint64)
require_type(app, TealType.uint64)
return MaybeValue(
Op.app_params_get,
TealType.uint64,
Expand All @@ -304,7 +304,7 @@ def localNumByteSlice(cls, app: Expr) -> MaybeValue:
app: An index into Txn.ForeignApps that correspond to the application to check.
Must evaluate to uint64.
"""
require_type(app.type_of(), TealType.uint64)
require_type(app, TealType.uint64)
return MaybeValue(
Op.app_params_get,
TealType.uint64,
Expand All @@ -320,7 +320,7 @@ def extraProgramPages(cls, app: Expr) -> MaybeValue:
app: An index into Txn.ForeignApps that correspond to the application to check.
Must evaluate to uint64.
"""
require_type(app.type_of(), TealType.uint64)
require_type(app, TealType.uint64)
return MaybeValue(
Op.app_params_get,
TealType.uint64,
Expand All @@ -336,7 +336,7 @@ def creator(cls, app: Expr) -> MaybeValue:
app: An index into Txn.ForeignApps that correspond to the application to check.
Must evaluate to uint64.
"""
require_type(app.type_of(), TealType.uint64)
require_type(app, TealType.uint64)
return MaybeValue(
Op.app_params_get, TealType.bytes, immediate_args=["AppCreator"], args=[app]
)
Expand All @@ -349,7 +349,7 @@ def address(cls, app: Expr) -> MaybeValue:
app: An index into Txn.ForeignApps that correspond to the application to check.
Must evaluate to uint64.
"""
require_type(app.type_of(), TealType.uint64)
require_type(app, TealType.uint64)
return MaybeValue(
Op.app_params_get, TealType.bytes, immediate_args=["AppAddress"], args=[app]
)
Expand Down
2 changes: 1 addition & 1 deletion pyteal/ast/arg.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(self, index: Union[int, Expr]) -> None:
if index < 0 or index > 255:
raise TealInputError("invalid arg index {}".format(index))
else:
require_type(cast(Expr, index).type_of(), TealType.uint64)
require_type(cast(Expr, index), TealType.uint64)

self.index = index

Expand Down
2 changes: 1 addition & 1 deletion pyteal/ast/assert_.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self, cond: Expr) -> None:
cond: The condition to check. Must evaluate to a uint64.
"""
super().__init__()
require_type(cond.type_of(), TealType.uint64)
require_type(cond, TealType.uint64)
self.cond = cond

def __teal__(self, options: "CompileOptions"):
Expand Down
32 changes: 16 additions & 16 deletions pyteal/ast/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def balance(cls, account: Expr, asset: Expr) -> MaybeValue:
asset: The ID of the asset to get, must be evaluated to uint64 (or, since v4,
a Txn.ForeignAssets offset).
"""
require_type(account.type_of(), TealType.anytype)
require_type(asset.type_of(), TealType.uint64)
require_type(account, TealType.anytype)
require_type(asset, TealType.uint64)
return MaybeValue(
Op.asset_holding_get,
TealType.uint64,
Expand All @@ -41,8 +41,8 @@ def frozen(cls, account: Expr, asset: Expr) -> MaybeValue:
asset: The ID of the asset to get, must be evaluated to uint64 (or, since v4,
a Txn.ForeignAssets offset).
"""
require_type(account.type_of(), TealType.anytype)
require_type(asset.type_of(), TealType.uint64)
require_type(account, TealType.anytype)
require_type(asset, TealType.uint64)
return MaybeValue(
Op.asset_holding_get,
TealType.uint64,
Expand All @@ -64,7 +64,7 @@ def total(cls, asset: Expr) -> MaybeValue:
must be evaluated to uint64 (or since v4, an asset ID that appears in
Txn.ForeignAssets).
"""
require_type(asset.type_of(), TealType.uint64)
require_type(asset, TealType.uint64)
return MaybeValue(
Op.asset_params_get,
TealType.uint64,
Expand All @@ -81,7 +81,7 @@ def decimals(cls, asset: Expr) -> MaybeValue:
must be evaluated to uint64 (or since v4, an asset ID that appears in
Txn.ForeignAssets).
"""
require_type(asset.type_of(), TealType.uint64)
require_type(asset, TealType.uint64)
return MaybeValue(
Op.asset_params_get,
TealType.uint64,
Expand All @@ -98,7 +98,7 @@ def defaultFrozen(cls, asset: Expr) -> MaybeValue:
must be evaluated to uint64 (or since v4, an asset ID that appears in
Txn.ForeignAssets).
"""
require_type(asset.type_of(), TealType.uint64)
require_type(asset, TealType.uint64)
return MaybeValue(
Op.asset_params_get,
TealType.uint64,
Expand All @@ -115,7 +115,7 @@ def unitName(cls, asset: Expr) -> MaybeValue:
must be evaluated to uint64 (or since v4, an asset ID that appears in
Txn.ForeignAssets).
"""
require_type(asset.type_of(), TealType.uint64)
require_type(asset, TealType.uint64)
return MaybeValue(
Op.asset_params_get,
TealType.bytes,
Expand All @@ -132,7 +132,7 @@ def name(cls, asset: Expr) -> MaybeValue:
must be evaluated to uint64 (or since v4, an asset ID that appears in
Txn.ForeignAssets).
"""
require_type(asset.type_of(), TealType.uint64)
require_type(asset, TealType.uint64)
return MaybeValue(
Op.asset_params_get,
TealType.bytes,
Expand All @@ -149,7 +149,7 @@ def url(cls, asset: Expr) -> MaybeValue:
must be evaluated to uint64 (or since v4, an asset ID that appears in
Txn.ForeignAssets).
"""
require_type(asset.type_of(), TealType.uint64)
require_type(asset, TealType.uint64)
return MaybeValue(
Op.asset_params_get,
TealType.bytes,
Expand All @@ -168,7 +168,7 @@ def metadataHash(cls, asset: Expr) -> MaybeValue:
must be evaluated to uint64 (or since v4, an asset ID that appears in
Txn.ForeignAssets).
"""
require_type(asset.type_of(), TealType.uint64)
require_type(asset, TealType.uint64)
return MaybeValue(
Op.asset_params_get,
TealType.bytes,
Expand All @@ -185,7 +185,7 @@ def manager(cls, asset: Expr) -> MaybeValue:
must be evaluated to uint64 (or since v4, an asset ID that appears in
Txn.ForeignAssets).
"""
require_type(asset.type_of(), TealType.uint64)
require_type(asset, TealType.uint64)
return MaybeValue(
Op.asset_params_get,
TealType.bytes,
Expand All @@ -202,7 +202,7 @@ def reserve(cls, asset: Expr) -> MaybeValue:
must be evaluated to uint64 (or since v4, an asset ID that appears in
Txn.ForeignAssets).
"""
require_type(asset.type_of(), TealType.uint64)
require_type(asset, TealType.uint64)
return MaybeValue(
Op.asset_params_get,
TealType.bytes,
Expand All @@ -219,7 +219,7 @@ def freeze(cls, asset: Expr) -> MaybeValue:
must be evaluated to uint64 (or since v4, an asset ID that appears in
Txn.ForeignAssets).
"""
require_type(asset.type_of(), TealType.uint64)
require_type(asset, TealType.uint64)
return MaybeValue(
Op.asset_params_get,
TealType.bytes,
Expand All @@ -236,7 +236,7 @@ def clawback(cls, asset: Expr) -> MaybeValue:
must be evaluated to uint64 (or since v4, an asset ID that appears in
Txn.ForeignAssets).
"""
require_type(asset.type_of(), TealType.uint64)
require_type(asset, TealType.uint64)
return MaybeValue(
Op.asset_params_get,
TealType.bytes,
Expand All @@ -252,7 +252,7 @@ def creator(cls, asset: Expr) -> MaybeValue:
asset: An index into Txn.ForeignAssets that corresponds to the asset to check. Must
evaluate to uint64.
"""
require_type(asset.type_of(), TealType.uint64)
require_type(asset, TealType.uint64)
return MaybeValue(
Op.asset_params_get,
TealType.bytes,
Expand Down
4 changes: 2 additions & 2 deletions pyteal/ast/binaryexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def __init__(
else:
leftType = cast(TealType, inputType)
rightType = leftType
require_type(argLeft.type_of(), leftType)
require_type(argRight.type_of(), rightType)
require_type(argLeft, leftType)
require_type(argRight, rightType)

self.op = op
self.outputType = outputType
Expand Down
4 changes: 2 additions & 2 deletions pyteal/ast/cond.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ def __init__(self, *argv: List[Expr]):
if len(arg) != 2:
raise TealInputError(msg.format(arg))

require_type(arg[0].type_of(), TealType.uint64) # cond_n should be int
require_type(arg[0], TealType.uint64) # cond_n should be int

if value_type is None: # the types of all branches should be the same
value_type = arg[1].type_of()
else:
require_type(arg[1].type_of(), value_type)
require_type(arg[1], value_type)

self.value_type = value_type
self.args = argv
Expand Down
8 changes: 4 additions & 4 deletions pyteal/ast/for_.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ def __init__(self, start: Expr, cond: Expr, step: Expr) -> None:
step: Expression to update the variable's value.
"""
super().__init__()
require_type(cond.type_of(), TealType.uint64)
require_type(start.type_of(), TealType.none)
require_type(step.type_of(), TealType.none)
require_type(cond, TealType.uint64)
require_type(start, TealType.none)
require_type(step, TealType.none)

self.start = start
self.cond = cond
Expand Down Expand Up @@ -94,7 +94,7 @@ def has_return(self):
def Do(self, doBlock: Expr):
if self.doBlock is not None:
raise TealCompileError("For expression already has a doBlock", self)
require_type(doBlock.type_of(), TealType.none)
require_type(doBlock, TealType.none)
self.doBlock = doBlock
return self

Expand Down
2 changes: 1 addition & 1 deletion pyteal/ast/gaid.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, txnIndex: Union[int, Expr]) -> None:
)
)
else:
require_type(cast(Expr, txnIndex).type_of(), TealType.uint64)
require_type(cast(Expr, txnIndex), TealType.uint64)
self.txnIndex = txnIndex

def __str__(self):
Expand Down
2 changes: 1 addition & 1 deletion pyteal/ast/gload.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(self, txnIndex: Union[int, Expr], slotId: int) -> None:
)
)
else:
require_type(cast(Expr, txnIndex).type_of(), TealType.uint64)
require_type(cast(Expr, txnIndex), TealType.uint64)
if slotId < 0 or slotId >= NUM_SLOTS:
raise TealInputError(
"Invalid slot ID {}, shoud be in [0, {})".format(slotId, NUM_SLOTS)
Expand Down
Loading

0 comments on commit a9c42e3

Please sign in to comment.