iotile / typedargs

API typechecking and command line shell generation package
GNU Lesser General Public License v3.0
1 stars 2 forks source link

Modernize typedargs annotation system for Python 3 #58

Open timburke opened 4 years ago

timburke commented 4 years ago

We use the typedargs package extensively to automatically build shell tools around python packages that let you call individual python functions from the cli with typed bindings between strings and the python function parameters that are automatically inferred from the functions themselves.

So, you can write:

@docannotate
def do_thing(action, flag=False):
    """Do a cool thing.

        Args:
            action (str): The name of the action to do
            flag (bool): An optional flag to control the action.
    """

You could then expose this function inside a console entry_point script and call it as:

script-name do_thing my_action -f true

and it would invoke your function, passing the string my_action as the first parameter and a bool value of True as the second optional parameter.

This generally works and is the basis for many arch tools, such as the iotile tool included as part of CoreTools.

However, it was initially intended to support Python 2 and 3 so we had to rely on a lot of custom ways to communicate the type information we needed. Now that we have dropped python 2 support, we should leverage the fact that we are python 3 only to take advantage of things we can now rely on like function annotations to simplify how we declare annotated functions.