algorand / pyteal

Algorand Smart Contracts in Python
https://pyteal.readthedocs.io
MIT License
285 stars 131 forks source link

Added TealSeqError for better error reporting. #661

Closed BarryJRowe closed 1 year ago

BarryJRowe commented 1 year ago

Syntax errors on Seq arrays do not clearly indicate where the syntax error is when the return type of a non-final expression is not TealType.none and will only point to the parent Seq statement. This change will reference the offending statement in the error message.

This code:

Seq([
    InnerTxnBuilder.Begin(),
    InnerTxnBuilder.SetFields({
        TxnField.type_enum: TxnType.AssetTransfer,
        TxnField.xfer_asset: Txn.assets[0],
        TxnField.asset_amount: Int(0) ,
        TxnField.asset_receiver: Global.current_application_address()
    }),
    InnerTxnBuilder.Submit(),
    #create box:
    App.box_create(Itob(Txn.assets[0]),Int(16)),
    App.globalPut(Bytes("AssetsRatioTotal"),  Btoi(Txn.application_args[1])),
])

Will now produce this error:

  ...
  File "/home/user/my_contract.py", line 210, in generate_approval_program_v1
    Seq([
  File "/home/user/ENV3/lib/python3.10/site-packages/pyteal/ast/seq.py", line 60, in __init__
    raise e
  File "/home/user/ENV3/lib/python3.10/site-packages/pyteal/ast/seq.py", line 55, in __init__
    require_type(expr, TealType.none)
  File "/home/user/ENV3/lib/python3.10/site-packages/pyteal/types.py", line 38, in require_type
    raise TealTypeError(actual, expected)
pyteal.TealSeqError: (box_create (Itob (Txna Assets 0)) (Int 16)) must have a return type of TealType.none. Only the last entry of a Seq array can have a return value.
CLAassistant commented 1 year ago

CLA assistant check
All committers have signed the CLA.