VCF Automation Blog

from Stefan Schnell

There are Python Enhancement Proposals (PEP), which can be used to submit ideas for improving Python. The PEP 3107 introduced function annotations in Python Release 3.0. Based on this, the PEP 484 introduced type hints with Python Release 3.5. Type hints is not static typing, but it makes it possible to specify the type of a variable. This is in the form of documentation, so that the Python code is more open for a static analysis tool. This raised the question whether type hints can also be used in the context of VCF Automation. This will be considered in this post.

Using Type Hints in Python Runtime Environments


VMware Aria Automation 8.18.1 uses in its Python runtime environment the release 3.10.5 of Python. This suggests that the use of typ hints should be possible. And indeed, typ hints can be used seamlessly. The standard Python handler function of VCF Automation looks like this with type hints:

import json

def handler(context: dict, inputs: dict) -> dict:
    jsonOut: str = json.dumps(inputs, separators=(",", ":"))
    print("Inputs were {0}".format(jsonOut))

    outputs: dict = {
        "status": "done"
    }

    return outputs

A slightly updated standard Python handler could look like this:

import json

def handler(context: dict, inputs: dict) -> dict:
    """ VCF Automation standard handler.
    """

    result: dict = {}
    outputs: dict = {}

    try:

        vcoUrl: str = context["vcoUrl"]
        bearerToken: str = context["getToken"]()

        jsonOut: str = json.dumps(inputs, separators=(",", ":"))
        print(f"Inputs were {jsonOut}")



        outputs = {
            "status": "done",
            "error": None,
            "result": result
        }

    except Exception as err:

        outputs = {
            "status": "incomplete",
            "error": repr(err)
        }

    return outputs

vcf automation example usage of python type hints

Conclusion

Type hints can be used in the Python Runtime Environment of VCF Automation without any problems.