Skip to content

annotations for WsgiMiddleware cause app to not be startable #71

@asottile

Description

@asottile
$ ./node_modules/.bin/func host start --python --functions MyFunction
Found Python version 3.8.2 (python3).
Azure Functions Core Tools (3.0.2912 Commit hash: bfcbbe48ed6fdacdf9b309261ecc8093df3b83f2)
Function Runtime Version: 3.0.14287.0
Hosting environment: Production
Content root path: /home/asottile/workspace/pre-commit-ci/azure-sample-application
Now listening on: http://0.0.0.0:7071
Application started. Press Ctrl+C to shut down.

Functions:

	MyFunction: [GET,POST] http://localhost:7071/api/MyFunction

For detailed output, run func with --verbose flag.
Worker process started and initialized.
Worker failed to function id 6333f1be-d5f7-4f0d-8cb8-e8ed6f42210a.
Result: Failure
Exception: FunctionLoadError: cannot load the MyFunction function: the "context" parameter is expected to be of type azure.functions.Context, got typing.Union[azure.functions._abc.Context, NoneType]
Stack:   File "/home/asottile/workspace/pre-commit-ci/azure-sample-application/node_modules/azure-functions-core-tools/bin/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py", line 268, in _handle__function_load_request
    self._functions.add_function(
  File "/home/asottile/workspace/pre-commit-ci/azure-sample-application/node_modules/azure-functions-core-tools/bin/workers/python/3.8/LINUX/X64/azure_functions_worker/functions.py", line 105, in add_function
    raise FunctionLoadError(
import azure.functions
import flask

app = flask.Flask(__name__)


@app.route('/', methods=['GET'])
def index() -> str:
    return 'hello hello world'


main = azure.functions.WsgiMiddleware(app).main

it looks like it is being a little too strict on the typing here

the @property is annotated correctly here

but the function it returns, uses Optional[Context] instead here

mypy allows this because the _handler is a more-permissive compatible signature, but the func tool forbids this (incorrectly)

I can work around this by wrapping the function trivially -- another alternative would be to make main call that instead of having a property (this would incur a function call overhead per call though, but that's probably ok?) -- that would be the easiest patch without having to refactor the type checking code in func

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions