azure-functions-python-worker icon indicating copy to clipboard operation
azure-functions-python-worker copied to clipboard

[Bug] Cannot access Context in blueprint function

Open hamish-rose opened this issue 1 year ago • 1 comments

Expected Behavior

add func.Context as argument to http trigger blueprint function Context has invocation_id to identify the request

Actual Behavior

Exception is thrown System.Private.CoreLib: Exception while executing function: Functions.test. System.Private.CoreLib: Result: Failure Exception: TypeError: test() got an unexpected keyword argument 'context'

Steps to Reproduce

  1. Create python function app using v2 model and blueprints
  2. Create http trigger function, with func.HttpRequest and func.Context, with import azure.functions as func
  3. Run function

Relevant code being tried

@bp.route(
    auth_level="anonymous", methods=["GET"], route="test"
)
def test(req: func.HttpRequest, context: func.Context):
    logging.info(context.invocation_id)

app = func.FunctionApp()
for bp in bp_collection:
    app.register_functions(bp)

Relevant log output

System.Private.CoreLib: Exception while executing function: Functions.test. System.Private.CoreLib: Result: Failure Exception: TypeError: test() got an unexpected keyword argument 'context'

requirements.txt file

No response

Where are you facing this problem?

Local - Core Tools

Function app name

No response

Additional Information

No response

hamish-rose avatar Oct 25 '24 04:10 hamish-rose

Hi @hamish-rose, thanks for reporting.

I wasn't able to repro this. What versions of Core Tools and azure-functions are you using? If you're not using the latest, could you update and try again?

Also, does this function execute successfully if it's not in a blueprint? If it's an HttpTrigger, it does need a non-None return value. One other thing to check could be how you're registering the blueprint functions. bp_collection should be a list of the func.Blueprint() objects.

blueprint_directory/blueprint.py:
import azure.functions as func
import logging

blueprint = func.Blueprint()

@blueprint.route(
    auth_level="anonymous", methods=["GET"], route="test"
)
def test(req: func.HttpRequest, context: func.Context):
    logging.info(context.invocation_id)
    return 'ok'
function_app.py:
import azure.functions as func
import logging
from blueprint_directory.blueprint import blueprint

app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)

blueprint_collection = [blueprint]

for bp in blueprint_collection:
    app.register_functions(bp)

hallvictoria avatar Oct 25 '24 15:10 hallvictoria

Thanks for the quick reply. Turns out we have another decorator wrapping function around the function def, which didn't like the additional argument being added. Thanks again, Hamish

hamish-rose avatar Oct 27 '24 21:10 hamish-rose