Azure / azure-functions-python-worker

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

[BUG] Using modern python types instead of typing.XXX with queue output binding #1176

Closed kummerer94 closed 1 year ago

kummerer94 commented 1 year ago

Investigative information

Hi, I think the queue output binding does not yet play nice with the modern type declarations that are part of newer python models.

Repro steps

I tried using func.Out[list[str]] for my output binding instead of func.Out[typing.List[str]] and it gave me this error:

[2023-02-17T10:32:04.022Z] Worker failed to index functions
[2023-02-17T10:32:04.023Z] Result: Failure
Exception: TypeError: issubclass() arg 1 must be a class
Stack:   File "C:\Users\ME\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.10\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 332, in _handle__functions_metadata_request
    fx_metadata_results = loader.process_indexed_function(
  File "C:\Users\ME\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.10\WINDOWS\X64\azure_functions_worker\loader.py", line 69, in process_indexed_function
    function_info = functions_registry.add_indexed_function(
  File "C:\Users\ME\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.10\WINDOWS\X64\azure_functions_worker\functions.py", line 405, in add_indexed_function
    input_types, output_types = self.validate_function_params(params,
  File "C:\Users\ME\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.10\WINDOWS\X64\azure_functions_worker\functions.py", line 228, in validate_function_params
    checks_out = bindings_utils.check_output_type_annotation(
  File "C:\Users\ME\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.10\WINDOWS\X64\azure_functions_worker\bindings\meta.py", line 50, in check_output_type_annotation
    return binding.check_output_type_annotation(pytype)
  File "C:\Users\ME\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.10\WINDOWS\X64\azure\functions\queue.py", line 104, in check_output_type_annotation
    or (isinstance(pytype, type) and issubclass(pytype, valid_types))
  File "C:\Program Files\Python310\lib\abc.py", line 123, in __subclasscheck__
    return _abc_subclasscheck(cls, subclass)

I am using the new python programming model v2.

To reproduce this, I am using the following function:

@bp.function_name(name="QueueOutputBindingFunction")
@bp.route(route="QueueOutputBindingFunction")
@bp.queue_output(
    arg_name="out", queue_name="myqueue", connection="AzureStorageQueuesConnectionString"
)
def test_binding(req: func.HttpRequest, out: func.Out[list[str]]) -> func.HttpResponse:
    out.set(["something"])
    return func.HttpResponse(status_code=202)

Known workarounds

Right now, the workaround is to use the List class provided by the typing module. But more and more people are moving away from using the typing module because it is not necessary anymore.

For now, it would also be great to make this error more descriptive as it took me some time to figure out what was going on.

bhagyshricompany commented 1 year ago

Thanks for update will update on this asap.

bhagyshricompany commented 1 year ago

@bp instance for which function instance created? is it from flask import Blueprint or some thing else pls specify

kummerer94 commented 1 year ago

Ah, sorry, yes. This is the blueprint bp = func.Blueprint() but I think you will also see the problem with @app instead of @bp.

bhagyshricompany commented 1 year ago

can you share zip file for the same Thanks

bhagyshricompany commented 1 year ago

from your error msg it suggest that the problem is with the issubclass() method, which expects the first argument to be a class. This indicates that the list class is not being recognized as a valid type for the output binding.

To fix this, you can try using typing.List[str] instead of list[str] for the output binding type annotation. This should resolve the error and allow you to use the correct type annotation for the output binding.

Here's an example of how to use typing.List[str] for the output binding type annotation: import logging import azure.functions as func import typing

def main(req: func.HttpRequest, outputQueueItem: func.Out[typing.List[str]]) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.')

names = ["demo1", "demo2", "demo3"]
outputQueueItem.set(names)

return func.HttpResponse(f"Processed {len(names)} names.")

I hope this helps and Meanwhile you can share your solution with zip file.

bhagyshricompany commented 1 year ago

@madhankumar1984 pls update with your file. Thanks

vrdmr commented 1 year ago

It is related to #1034 - Needs investigation.

gavin-aguiar commented 1 year ago

@kummerer94 Currently the python worker does not support type hinting. We are working on getting that support in. Updates on this will be tracked in https://github.com/Azure/azure-functions-python-worker/issues/1034