Skip to content

Commit

Permalink
ABIReturnSubroutine doc/test-case on name_override decorator (#557)
Browse files Browse the repository at this point in the history
* abi overridden name subr doc testcase

* minor typo fix

Co-authored-by: Michael Diamant <michaeldiamant@users.noreply.github.com>

Co-authored-by: Michael Diamant <michaeldiamant@users.noreply.github.com>
  • Loading branch information
ahangsu and michaeldiamant authored Oct 7, 2022
1 parent 2e23833 commit f2b2cea
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/abi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,21 @@ Notice that even though the original :code:`get_account_status` function returns

The only exception to this transformation is if the subroutine has no return value. Without a return value, a :code:`ComputedValue` is unnecessary and the subroutine will still return an :code:`Expr` to the caller. In this case, the :code:`@ABIReturnSubroutine` decorator acts identically the :code:`@Subroutine` decorator.

The name of the subroutine constructed by the :code:`@ABIReturnSubroutine` decorator is by default the function name. In order to override the default subroutine name, the decorator :any:`ABIReturnSubroutine.name_override <ABIReturnSubroutine.name_override>` is introduced to construct a subroutine with its name overridden. An example is below:

.. code-block:: python
from pyteal import *
@ABIReturnSubroutine.name_override("increment")
def add_by_one(prev: abi.Uint32, *, output: abi.Uint32) -> Expr:
return output.set(prev.get() + Int(1))
# NOTE! In this case, the `ABIReturnSubroutine` is initialized with a name "increment"
# overriding its original name "add_by_one"
assert add_by_one.method_spec().dictify()["name"] == "increment"
Creating an ARC-4 Program
----------------------------------------------------

Expand Down
7 changes: 7 additions & 0 deletions pyteal/ast/subroutine_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1475,3 +1475,10 @@ def abi_meth(a: pt.abi.Uint64, b: pt.abi.Uint64, *, output: pt.abi.Uint64):

mspec = ABIReturnSubroutine(abi_meth, overriding_name="add").method_spec().dictify()
assert mspec["name"] == "add"

@ABIReturnSubroutine.name_override("overriden_add")
def abi_meth_2(a: pt.abi.Uint64, b: pt.abi.Uint64, *, output: pt.abi.Uint64):
return output.set(a.get() + b.get())

mspec = abi_meth_2.method_spec().dictify()
assert mspec["name"] == "overriden_add"

0 comments on commit f2b2cea

Please sign in to comment.