Azure / azure-functions-python-worker

Python worker for Azure Functions.
http://aka.ms/azurefunctions
MIT License
335 stars 103 forks source link

Feature: consider adding generic support for dataclass-based bindings #416

Open elprans opened 5 years ago

elprans commented 5 years ago

Following up on the conversations in #66, and #409.

One possible way to implement generic handling of bindings is to allow users to annotate function parameters with a dataclass. The argument data is then treated as a conforming JSON object.

For example:

import dataclasses

@dataclasses.dataclass
class MyType:
    id: int
    body: str

def func(arg: MyType) -> str:
    return arg.body

In function.json such bindings would then have to specify dataType: json.

\cc @asavaritayal @pragnagopa @maiqbal11

pragnagopa commented 5 years ago

given the annotation arg: MyType, can you please explain why dataType: json is needed?

elprans commented 5 years ago

@pragnagopa How does host decide which format to use when sending data to the worker?

pragnagopa commented 5 years ago

default behavior is to check if a string is a json valid. For example, queue trigger like {"id":99, "name":"test"} should be sent with TypedData set to json: https://github.com/Azure/azure-functions-language-worker-protobuf/blob/dev/src/proto/FunctionRpc.proto#L284