Skip to content

annotations for WsgiMiddleware cause app to not be startable #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
asottile opened this issue Sep 24, 2020 · 4 comments · Fixed by #88
Closed

annotations for WsgiMiddleware cause app to not be startable #71

asottile opened this issue Sep 24, 2020 · 4 comments · Fixed by #88
Assignees

Comments

@asottile
Copy link
Contributor

$ ./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

@asottile
Copy link
Contributor Author

I can work on fixing this if you give me some pointers on how you'd like to see this resolved!

@ropeladder
Copy link

Just chiming in to say I ran into this issue today. I worked around it by using the "old syntax" (#2) described here, though this is obviously not ideal.

@tonybaloney
Copy link
Contributor

Thanks for raising this.

For now, you'll have to use the workaround of the "old syntax", this bug should be against the worker process. I'll add an issue there.

Example workaround:

import logging

import azure.functions as func
from ..FlaskApp.wsgi import application

def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
    return func.WsgiMiddleware(application).handle(req, context)

@asottile
Copy link
Contributor Author

asottile commented May 4, 2021

@Hazhzeng it looks like main (which is now _handle) is missing annotations entirely -- won't that be rejected as well? https://github.com/Azure/azure-functions-python-library/pull/88/files#diff-c374d52c21cc9ee6bbbab46bf614574eb91752a38d74200a781c58a155e21c15R186

the actual fix needs to happen in the code which checks the annotations -- it needs to recognize that f(Union[T, T2, ...]) is substitutable for f(T) because it is a less-restrictive function shape

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants