Skip to content

Commit 0a445ed

Browse files
joe-palgoidurovic
authored andcommitted
Call type_of() in require_type() for better exception messages (algorand#151)
* call type_of in require_type to catch exceptions * fix formatting for types.py and types_test.py
1 parent 2f62030 commit 0a445ed

23 files changed

+100
-81
lines changed

pyteal/ast/app.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ def optedIn(cls, account: Expr, app: Expr) -> "App":
9595
must be evaluated to uint64 (or, since v4, an application id that appears in
9696
Txn.ForeignApps or is the CurrentApplicationID, must be evaluated to bytes).
9797
"""
98-
require_type(account.type_of(), TealType.anytype)
99-
require_type(app.type_of(), TealType.uint64)
98+
require_type(account, TealType.anytype)
99+
require_type(app, TealType.uint64)
100100
return cls(AppField.optedIn, [account, app])
101101

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

116116
@classmethod
@@ -126,9 +126,9 @@ def localGetEx(cls, account: Expr, app: Expr, key: Expr) -> MaybeValue:
126126
Txn.ForeignApps or is the CurrentApplicationID, must be evaluated to bytes).
127127
key: The key to read from the account's local state. Must evaluate to bytes.
128128
"""
129-
require_type(account.type_of(), TealType.anytype)
130-
require_type(app.type_of(), TealType.uint64)
131-
require_type(key.type_of(), TealType.bytes)
129+
require_type(account, TealType.anytype)
130+
require_type(app, TealType.uint64)
131+
require_type(key, TealType.bytes)
132132
return MaybeValue(
133133
AppField.localGetEx.get_op(), TealType.anytype, args=[account, app, key]
134134
)
@@ -140,7 +140,7 @@ def globalGet(cls, key: Expr) -> "App":
140140
Args:
141141
key: The key to read from the global application state. Must evaluate to bytes.
142142
"""
143-
require_type(key.type_of(), TealType.bytes)
143+
require_type(key, TealType.bytes)
144144
return cls(AppField.globalGet, [key])
145145

146146
@classmethod
@@ -153,8 +153,8 @@ def globalGetEx(cls, app: Expr, key: Expr) -> MaybeValue:
153153
Txn.ForeignApps or is the CurrentApplicationID, must be evaluated to uint64).
154154
key: The key to read from the global application state. Must evaluate to bytes.
155155
"""
156-
require_type(app.type_of(), TealType.uint64)
157-
require_type(key.type_of(), TealType.bytes)
156+
require_type(app, TealType.uint64)
157+
require_type(key, TealType.bytes)
158158
return MaybeValue(
159159
AppField.globalGetEx.get_op(), TealType.anytype, args=[app, key]
160160
)
@@ -170,9 +170,9 @@ def localPut(cls, account: Expr, key: Expr, value: Expr) -> "App":
170170
key: The key to write in the account's local state. Must evaluate to bytes.
171171
value: The value to write in the account's local state. Can evaluate to any type.
172172
"""
173-
require_type(account.type_of(), TealType.anytype)
174-
require_type(key.type_of(), TealType.bytes)
175-
require_type(value.type_of(), TealType.anytype)
173+
require_type(account, TealType.anytype)
174+
require_type(key, TealType.bytes)
175+
require_type(value, TealType.anytype)
176176
return cls(AppField.localPut, [account, key, value])
177177

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

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

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

214214

@@ -224,7 +224,7 @@ def approvalProgram(cls, app: Expr) -> MaybeValue:
224224
app: An index into Txn.ForeignApps that correspond to the application to check.
225225
Must evaluate to uint64.
226226
"""
227-
require_type(app.type_of(), TealType.uint64)
227+
require_type(app, TealType.uint64)
228228
return MaybeValue(
229229
Op.app_params_get,
230230
TealType.bytes,
@@ -240,7 +240,7 @@ def clearStateProgram(cls, app: Expr) -> MaybeValue:
240240
app: An index into Txn.ForeignApps that correspond to the application to check.
241241
Must evaluate to uint64.
242242
"""
243-
require_type(app.type_of(), TealType.uint64)
243+
require_type(app, TealType.uint64)
244244
return MaybeValue(
245245
Op.app_params_get,
246246
TealType.bytes,
@@ -256,7 +256,7 @@ def globalNumUnit(cls, app: Expr) -> MaybeValue:
256256
app: An index into Txn.ForeignApps that correspond to the application to check.
257257
Must evaluate to uint64.
258258
"""
259-
require_type(app.type_of(), TealType.uint64)
259+
require_type(app, TealType.uint64)
260260
return MaybeValue(
261261
Op.app_params_get,
262262
TealType.uint64,
@@ -272,7 +272,7 @@ def globalNumByteSlice(cls, app: Expr) -> MaybeValue:
272272
app: An index into Txn.ForeignApps that correspond to the application to check.
273273
Must evaluate to uint64.
274274
"""
275-
require_type(app.type_of(), TealType.uint64)
275+
require_type(app, TealType.uint64)
276276
return MaybeValue(
277277
Op.app_params_get,
278278
TealType.uint64,
@@ -288,7 +288,7 @@ def localNumUnit(cls, app: Expr) -> MaybeValue:
288288
app: An index into Txn.ForeignApps that correspond to the application to check.
289289
Must evaluate to uint64.
290290
"""
291-
require_type(app.type_of(), TealType.uint64)
291+
require_type(app, TealType.uint64)
292292
return MaybeValue(
293293
Op.app_params_get,
294294
TealType.uint64,
@@ -304,7 +304,7 @@ def localNumByteSlice(cls, app: Expr) -> MaybeValue:
304304
app: An index into Txn.ForeignApps that correspond to the application to check.
305305
Must evaluate to uint64.
306306
"""
307-
require_type(app.type_of(), TealType.uint64)
307+
require_type(app, TealType.uint64)
308308
return MaybeValue(
309309
Op.app_params_get,
310310
TealType.uint64,
@@ -320,7 +320,7 @@ def extraProgramPages(cls, app: Expr) -> MaybeValue:
320320
app: An index into Txn.ForeignApps that correspond to the application to check.
321321
Must evaluate to uint64.
322322
"""
323-
require_type(app.type_of(), TealType.uint64)
323+
require_type(app, TealType.uint64)
324324
return MaybeValue(
325325
Op.app_params_get,
326326
TealType.uint64,
@@ -336,7 +336,7 @@ def creator(cls, app: Expr) -> MaybeValue:
336336
app: An index into Txn.ForeignApps that correspond to the application to check.
337337
Must evaluate to uint64.
338338
"""
339-
require_type(app.type_of(), TealType.uint64)
339+
require_type(app, TealType.uint64)
340340
return MaybeValue(
341341
Op.app_params_get, TealType.bytes, immediate_args=["AppCreator"], args=[app]
342342
)
@@ -349,7 +349,7 @@ def address(cls, app: Expr) -> MaybeValue:
349349
app: An index into Txn.ForeignApps that correspond to the application to check.
350350
Must evaluate to uint64.
351351
"""
352-
require_type(app.type_of(), TealType.uint64)
352+
require_type(app, TealType.uint64)
353353
return MaybeValue(
354354
Op.app_params_get, TealType.bytes, immediate_args=["AppAddress"], args=[app]
355355
)

pyteal/ast/arg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __init__(self, index: Union[int, Expr]) -> None:
2929
if index < 0 or index > 255:
3030
raise TealInputError("invalid arg index {}".format(index))
3131
else:
32-
require_type(cast(Expr, index).type_of(), TealType.uint64)
32+
require_type(cast(Expr, index), TealType.uint64)
3333

3434
self.index = index
3535

pyteal/ast/assert_.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __init__(self, cond: Expr) -> None:
1818
cond: The condition to check. Must evaluate to a uint64.
1919
"""
2020
super().__init__()
21-
require_type(cond.type_of(), TealType.uint64)
21+
require_type(cond, TealType.uint64)
2222
self.cond = cond
2323

2424
def __teal__(self, options: "CompileOptions"):

pyteal/ast/asset.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def balance(cls, account: Expr, asset: Expr) -> MaybeValue:
1919
asset: The ID of the asset to get, must be evaluated to uint64 (or, since v4,
2020
a Txn.ForeignAssets offset).
2121
"""
22-
require_type(account.type_of(), TealType.anytype)
23-
require_type(asset.type_of(), TealType.uint64)
22+
require_type(account, TealType.anytype)
23+
require_type(asset, TealType.uint64)
2424
return MaybeValue(
2525
Op.asset_holding_get,
2626
TealType.uint64,
@@ -41,8 +41,8 @@ def frozen(cls, account: Expr, asset: Expr) -> MaybeValue:
4141
asset: The ID of the asset to get, must be evaluated to uint64 (or, since v4,
4242
a Txn.ForeignAssets offset).
4343
"""
44-
require_type(account.type_of(), TealType.anytype)
45-
require_type(asset.type_of(), TealType.uint64)
44+
require_type(account, TealType.anytype)
45+
require_type(asset, TealType.uint64)
4646
return MaybeValue(
4747
Op.asset_holding_get,
4848
TealType.uint64,
@@ -64,7 +64,7 @@ def total(cls, asset: Expr) -> MaybeValue:
6464
must be evaluated to uint64 (or since v4, an asset ID that appears in
6565
Txn.ForeignAssets).
6666
"""
67-
require_type(asset.type_of(), TealType.uint64)
67+
require_type(asset, TealType.uint64)
6868
return MaybeValue(
6969
Op.asset_params_get,
7070
TealType.uint64,
@@ -81,7 +81,7 @@ def decimals(cls, asset: Expr) -> MaybeValue:
8181
must be evaluated to uint64 (or since v4, an asset ID that appears in
8282
Txn.ForeignAssets).
8383
"""
84-
require_type(asset.type_of(), TealType.uint64)
84+
require_type(asset, TealType.uint64)
8585
return MaybeValue(
8686
Op.asset_params_get,
8787
TealType.uint64,
@@ -98,7 +98,7 @@ def defaultFrozen(cls, asset: Expr) -> MaybeValue:
9898
must be evaluated to uint64 (or since v4, an asset ID that appears in
9999
Txn.ForeignAssets).
100100
"""
101-
require_type(asset.type_of(), TealType.uint64)
101+
require_type(asset, TealType.uint64)
102102
return MaybeValue(
103103
Op.asset_params_get,
104104
TealType.uint64,
@@ -115,7 +115,7 @@ def unitName(cls, asset: Expr) -> MaybeValue:
115115
must be evaluated to uint64 (or since v4, an asset ID that appears in
116116
Txn.ForeignAssets).
117117
"""
118-
require_type(asset.type_of(), TealType.uint64)
118+
require_type(asset, TealType.uint64)
119119
return MaybeValue(
120120
Op.asset_params_get,
121121
TealType.bytes,
@@ -132,7 +132,7 @@ def name(cls, asset: Expr) -> MaybeValue:
132132
must be evaluated to uint64 (or since v4, an asset ID that appears in
133133
Txn.ForeignAssets).
134134
"""
135-
require_type(asset.type_of(), TealType.uint64)
135+
require_type(asset, TealType.uint64)
136136
return MaybeValue(
137137
Op.asset_params_get,
138138
TealType.bytes,
@@ -149,7 +149,7 @@ def url(cls, asset: Expr) -> MaybeValue:
149149
must be evaluated to uint64 (or since v4, an asset ID that appears in
150150
Txn.ForeignAssets).
151151
"""
152-
require_type(asset.type_of(), TealType.uint64)
152+
require_type(asset, TealType.uint64)
153153
return MaybeValue(
154154
Op.asset_params_get,
155155
TealType.bytes,
@@ -168,7 +168,7 @@ def metadataHash(cls, asset: Expr) -> MaybeValue:
168168
must be evaluated to uint64 (or since v4, an asset ID that appears in
169169
Txn.ForeignAssets).
170170
"""
171-
require_type(asset.type_of(), TealType.uint64)
171+
require_type(asset, TealType.uint64)
172172
return MaybeValue(
173173
Op.asset_params_get,
174174
TealType.bytes,
@@ -185,7 +185,7 @@ def manager(cls, asset: Expr) -> MaybeValue:
185185
must be evaluated to uint64 (or since v4, an asset ID that appears in
186186
Txn.ForeignAssets).
187187
"""
188-
require_type(asset.type_of(), TealType.uint64)
188+
require_type(asset, TealType.uint64)
189189
return MaybeValue(
190190
Op.asset_params_get,
191191
TealType.bytes,
@@ -202,7 +202,7 @@ def reserve(cls, asset: Expr) -> MaybeValue:
202202
must be evaluated to uint64 (or since v4, an asset ID that appears in
203203
Txn.ForeignAssets).
204204
"""
205-
require_type(asset.type_of(), TealType.uint64)
205+
require_type(asset, TealType.uint64)
206206
return MaybeValue(
207207
Op.asset_params_get,
208208
TealType.bytes,
@@ -219,7 +219,7 @@ def freeze(cls, asset: Expr) -> MaybeValue:
219219
must be evaluated to uint64 (or since v4, an asset ID that appears in
220220
Txn.ForeignAssets).
221221
"""
222-
require_type(asset.type_of(), TealType.uint64)
222+
require_type(asset, TealType.uint64)
223223
return MaybeValue(
224224
Op.asset_params_get,
225225
TealType.bytes,
@@ -236,7 +236,7 @@ def clawback(cls, asset: Expr) -> MaybeValue:
236236
must be evaluated to uint64 (or since v4, an asset ID that appears in
237237
Txn.ForeignAssets).
238238
"""
239-
require_type(asset.type_of(), TealType.uint64)
239+
require_type(asset, TealType.uint64)
240240
return MaybeValue(
241241
Op.asset_params_get,
242242
TealType.bytes,
@@ -252,7 +252,7 @@ def creator(cls, asset: Expr) -> MaybeValue:
252252
asset: An index into Txn.ForeignAssets that corresponds to the asset to check. Must
253253
evaluate to uint64.
254254
"""
255-
require_type(asset.type_of(), TealType.uint64)
255+
require_type(asset, TealType.uint64)
256256
return MaybeValue(
257257
Op.asset_params_get,
258258
TealType.bytes,

pyteal/ast/binaryexpr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def __init__(
2626
else:
2727
leftType = cast(TealType, inputType)
2828
rightType = leftType
29-
require_type(argLeft.type_of(), leftType)
30-
require_type(argRight.type_of(), rightType)
29+
require_type(argLeft, leftType)
30+
require_type(argRight, rightType)
3131

3232
self.op = op
3333
self.outputType = outputType

pyteal/ast/cond.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ def __init__(self, *argv: List[Expr]):
4646
if len(arg) != 2:
4747
raise TealInputError(msg.format(arg))
4848

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

5151
if value_type is None: # the types of all branches should be the same
5252
value_type = arg[1].type_of()
5353
else:
54-
require_type(arg[1].type_of(), value_type)
54+
require_type(arg[1], value_type)
5555

5656
self.value_type = value_type
5757
self.args = argv

pyteal/ast/for_.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ def __init__(self, start: Expr, cond: Expr, step: Expr) -> None:
2727
step: Expression to update the variable's value.
2828
"""
2929
super().__init__()
30-
require_type(cond.type_of(), TealType.uint64)
31-
require_type(start.type_of(), TealType.none)
32-
require_type(step.type_of(), TealType.none)
30+
require_type(cond, TealType.uint64)
31+
require_type(start, TealType.none)
32+
require_type(step, TealType.none)
3333

3434
self.start = start
3535
self.cond = cond
@@ -94,7 +94,7 @@ def has_return(self):
9494
def Do(self, doBlock: Expr):
9595
if self.doBlock is not None:
9696
raise TealCompileError("For expression already has a doBlock", self)
97-
require_type(doBlock.type_of(), TealType.none)
97+
require_type(doBlock, TealType.none)
9898
self.doBlock = doBlock
9999
return self
100100

pyteal/ast/gaid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __init__(self, txnIndex: Union[int, Expr]) -> None:
3434
)
3535
)
3636
else:
37-
require_type(cast(Expr, txnIndex).type_of(), TealType.uint64)
37+
require_type(cast(Expr, txnIndex), TealType.uint64)
3838
self.txnIndex = txnIndex
3939

4040
def __str__(self):

pyteal/ast/gload.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def __init__(self, txnIndex: Union[int, Expr], slotId: int) -> None:
3737
)
3838
)
3939
else:
40-
require_type(cast(Expr, txnIndex).type_of(), TealType.uint64)
40+
require_type(cast(Expr, txnIndex), TealType.uint64)
4141
if slotId < 0 or slotId >= NUM_SLOTS:
4242
raise TealInputError(
4343
"Invalid slot ID {}, shoud be in [0, {})".format(slotId, NUM_SLOTS)

0 commit comments

Comments
 (0)