Skip to content

Commit

Permalink
touchup parameter defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
barnjamin committed Aug 30, 2022
1 parent 8dea728 commit 7064d8c
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 75 deletions.
Binary file modified docs/doctrees/environment.pickle
Binary file not shown.
Binary file modified docs/doctrees/usage.doctree
Binary file not shown.
4 changes: 2 additions & 2 deletions docs/html/_modules/beaker/decorators.html
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ <h1>Source code for beaker.decorators</h1><div class="highlight"><pre>
<span class="k">return</span> <span class="n">hc</span><span class="o">.</span><span class="n">method_spec</span><span class="o">.</span><span class="n">dictify</span><span class="p">()</span>


<div class="viewcode-block" id="ParameterAnnotation"><a class="viewcode-back" href="../../usage.html#beaker.decorators.ParameterAnnotation">[docs]</a><span class="nd">@dataclass</span>
<span class="nd">@dataclass</span>
<span class="k">class</span> <span class="nc">ParameterAnnotation</span><span class="p">:</span>
<span class="n">descr</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="n">field</span><span class="p">(</span><span class="n">kw_only</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
<span class="n">default</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">DefaultArgument</span><span class="p">]</span> <span class="o">=</span> <span class="n">field</span><span class="p">(</span><span class="n">kw_only</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
Expand All @@ -188,7 +188,7 @@ <h1>Source code for beaker.decorators</h1><div class="highlight"><pre>
<span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">default</span><span class="o">.</span><span class="n">resolvable_class</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">default</span><span class="o">.</span><span class="n">resolve_hint</span><span class="p">()</span>
<span class="p">}</span>

<span class="k">return</span> <span class="n">ret</span></div>
<span class="k">return</span> <span class="n">ret</span>


<span class="nd">@dataclass</span>
Expand Down
35 changes: 12 additions & 23 deletions docs/html/_sources/usage.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,42 +130,31 @@ Here we subclassed the ``OpUp`` contract which provides functionality to create

We inherit the methods and class variables that ``OpUp`` defined, allowing us to encapsulate and compose behavior.


.. _parameter_annotations:

Parameter Annotations
---------------------

.. currentmodule:: beaker.decorators
.. autoclass:: ParameterAnnotation
:members:


A caller of our application should be provided with all the information they might need in order to make a successful application call.

One example of this of information is of course the parameter name and type. These bits of information are already provided by the normal method definition.
Also note that the ``opup_app`` argument specifies a default value. This is a bit of magic that serves only to produce a hint for the caller in the resulting Application Spec.

.. _parameter_default:

Parameter Default Value
^^^^^^^^^^^^^^^^^^^^^^^

In the ``OpUp`` example the argument ``opup_app`` should be the id of the application that we use to increase our budget via inner app calls. This value should not change frequently, if at all, but is still required to be passed by the caller so we may _use_ it in our logic.
In the ``OpUp`` example above, the argument ``opup_app`` should be the id of the application that we use to increase our budget via inner app calls.
This value should not change frequently, if at all, but is still required to be passed by the caller so we may **use** it in our logic, namely to execute an application call against it.

Using the ``default`` field of the ``ParameterAnnotation``, we can specify a default value for the parameter.

This allows the caller to know this pseudo-magic number ahead of time and makes calling your application easier. The information is communicated through the full ApplicationSpec as a hint the caller can use to figure out what the value should be.
By specifying the default value of the argument in the method signature, we can communicate to the caller, through the hints of the Application Spec, what the value **should** be.

Options for default arguments are:

- A constant, `bytes | int | str`
- State Values, `ApplicationStateValue | AccountStateValue`
- A read-only ABI method
- A constant: one of ``bytes | int | str | Bytes | Int``
- State Values: one of ``ApplicationStateValue | AccountStateValue``
- A read-only ABI method: a method defined to produce some more complex value than a state value or constant would be able to produce.


The result is that we can call the method, omitting the `opup_app` argument:
The result of specifying the default value here is that we can call the method, omitting the `opup_app` argument:

.. code-block:: python
result = app_client.call(app.hash_it, input="hashme", iters=10)
When invoked, the `ApplicationClient` consults the method definition to check that all the expected arguments are passed. If it finds one missing, it will check for hints for the method that may be resolvable. Upon finding a resolvable it will look up the state value, call the method, or return the constant value. The resolved value is passed in for argument.
When invoked, the ``ApplicationClient`` consults the method definition to check that all the expected arguments are passed.
If it finds that an argument is not passed, it will check the hints for a default argument for the method that may be used directly (constant) or resolved (need to look it up on chain or call method).
Upon finding a resolvable it will look up the state value, call the method. The resulting value is passed in for argument to the application call.
4 changes: 2 additions & 2 deletions docs/html/decorators.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ <h1>Decorators<a class="headerlink" href="#decorators" title="Permalink to this
<span id="id2"></span><h3>Method Hints<a class="headerlink" href="#method-hints" title="Permalink to this heading"></a></h3>
<dl class="py class">
<dt class="sig sig-object py" id="beaker.decorators.MethodHints">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">beaker.decorators.</span></span><span class="sig-name descname"><span class="pre">MethodHints</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">read_only</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.10)"><span class="pre">bool</span></a></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">structs</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Optional" title="(in Python v3.10)"><span class="pre">Optional</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(in Python v3.10)"><span class="pre">dict</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.10)"><span class="pre">str</span></a><span class="p"><span class="pre">,</span></span><span class="w"> </span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(in Python v3.10)"><span class="pre">dict</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.10)"><span class="pre">str</span></a><span class="p"><span class="pre">,</span></span><span class="w"> </span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.10)"><span class="pre">str</span></a><span class="w"> </span><span class="p"><span class="pre">|</span></span><span class="w"> </span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#list" title="(in Python v3.10)"><span class="pre">list</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#tuple" title="(in Python v3.10)"><span class="pre">tuple</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.10)"><span class="pre">str</span></a><span class="p"><span class="pre">,</span></span><span class="w"> </span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.10)"><span class="pre">str</span></a><span class="p"><span class="pre">]</span></span><span class="p"><span class="pre">]</span></span><span class="p"><span class="pre">]</span></span><span class="p"><span class="pre">]</span></span><span class="p"><span class="pre">]</span></span></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param_annotations</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Optional" title="(in Python v3.10)"><span class="pre">Optional</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(in Python v3.10)"><span class="pre">dict</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.10)"><span class="pre">str</span></a><span class="p"><span class="pre">,</span></span><span class="w"> </span><a class="reference internal" href="usage.html#beaker.decorators.ParameterAnnotation" title="beaker.decorators.ParameterAnnotation"><span class="pre">beaker.decorators.ParameterAnnotation</span></a><span class="p"><span class="pre">]</span></span><span class="p"><span class="pre">]</span></span></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="_modules/beaker/decorators.html#MethodHints"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#beaker.decorators.MethodHints" title="Permalink to this definition"></a></dt>
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">beaker.decorators.</span></span><span class="sig-name descname"><span class="pre">MethodHints</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">read_only</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.10)"><span class="pre">bool</span></a></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">structs</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Optional" title="(in Python v3.10)"><span class="pre">Optional</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(in Python v3.10)"><span class="pre">dict</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.10)"><span class="pre">str</span></a><span class="p"><span class="pre">,</span></span><span class="w"> </span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(in Python v3.10)"><span class="pre">dict</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.10)"><span class="pre">str</span></a><span class="p"><span class="pre">,</span></span><span class="w"> </span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.10)"><span class="pre">str</span></a><span class="w"> </span><span class="p"><span class="pre">|</span></span><span class="w"> </span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#list" title="(in Python v3.10)"><span class="pre">list</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#tuple" title="(in Python v3.10)"><span class="pre">tuple</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.10)"><span class="pre">str</span></a><span class="p"><span class="pre">,</span></span><span class="w"> </span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.10)"><span class="pre">str</span></a><span class="p"><span class="pre">]</span></span><span class="p"><span class="pre">]</span></span><span class="p"><span class="pre">]</span></span><span class="p"><span class="pre">]</span></span><span class="p"><span class="pre">]</span></span></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param_annotations</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Optional" title="(in Python v3.10)"><span class="pre">Optional</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(in Python v3.10)"><span class="pre">dict</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.10)"><span class="pre">str</span></a><span class="p"><span class="pre">,</span></span><span class="w"> </span><span class="pre">beaker.decorators.ParameterAnnotation</span><span class="p"><span class="pre">]</span></span><span class="p"><span class="pre">]</span></span></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="_modules/beaker/decorators.html#MethodHints"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#beaker.decorators.MethodHints" title="Permalink to this definition"></a></dt>
<dd><p>MethodHints provides hints to the caller about how to call the method</p>
<dl class="py attribute">
<dt class="sig sig-object py" id="beaker.decorators.MethodHints.param_annotations">
<span class="sig-name descname"><span class="pre">param_annotations</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Optional" title="(in Python v3.10)"><span class="pre">Optional</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(in Python v3.10)"><span class="pre">dict</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.10)"><span class="pre">str</span></a><span class="p"><span class="pre">,</span></span><span class="w"> </span><a class="reference internal" href="usage.html#beaker.decorators.ParameterAnnotation" title="beaker.decorators.ParameterAnnotation"><span class="pre">beaker.decorators.ParameterAnnotation</span></a><span class="p"><span class="pre">]</span></span><span class="p"><span class="pre">]</span></span></em><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">None</span></em><a class="headerlink" href="#beaker.decorators.MethodHints.param_annotations" title="Permalink to this definition"></a></dt>
<span class="sig-name descname"><span class="pre">param_annotations</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Optional" title="(in Python v3.10)"><span class="pre">Optional</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(in Python v3.10)"><span class="pre">dict</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.10)"><span class="pre">str</span></a><span class="p"><span class="pre">,</span></span><span class="w"> </span><span class="pre">beaker.decorators.ParameterAnnotation</span><span class="p"><span class="pre">]</span></span><span class="p"><span class="pre">]</span></span></em><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">None</span></em><a class="headerlink" href="#beaker.decorators.MethodHints.param_annotations" title="Permalink to this definition"></a></dt>
<dd><p>annotations</p>
</dd></dl>

Expand Down
6 changes: 2 additions & 4 deletions docs/html/genindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -448,17 +448,15 @@ <h2 id="P">P</h2>
<table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="decorators.html#beaker.decorators.MethodHints.param_annotations">param_annotations (beaker.decorators.MethodHints attribute)</a>
</li>
<li><a href="usage.html#beaker.decorators.ParameterAnnotation">ParameterAnnotation (class in beaker.decorators)</a>
</li>
<li><a href="precompile.html#beaker.precompile.PrecompileTemplateValue.pc">pc (beaker.precompile.PrecompileTemplateValue attribute)</a>
</li>
<li><a href="precompile.html#beaker.precompile.Precompile.populate_template">populate_template() (beaker.precompile.Precompile method)</a>
</li>
</ul></td>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="precompile.html#beaker.precompile.Precompile.populate_template_expr">populate_template_expr() (beaker.precompile.Precompile method)</a>
</li>
</ul></td>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="precompile.html#beaker.precompile.Precompile">Precompile (class in beaker.precompile)</a>
</li>
<li><a href="precompile.html#beaker.precompile.PrecompileTemplateValue">PrecompileTemplateValue (class in beaker.precompile)</a>
Expand Down
Binary file modified docs/html/objects.inv
Binary file not shown.
Loading

0 comments on commit 7064d8c

Please sign in to comment.