-
Notifications
You must be signed in to change notification settings - Fork 118
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
feat: initial typing of the public API #248
Changes from 4 commits
fe17813
13bb933
6e0929c
cf75b83
c7be816
18de9e6
977cf96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,13 +23,14 @@ | |
import types | ||
|
||
from inspect import signature | ||
from typing import Type | ||
from typing import Callable, Type | ||
|
||
import cloudevents.exceptions as cloud_exceptions | ||
import flask | ||
import werkzeug | ||
|
||
from cloudevents.http import from_http, is_binary | ||
from cloudevents.http.event import CloudEvent | ||
|
||
from functions_framework import _function_registry, _typed_event, event_conversion | ||
from functions_framework.background_event import BackgroundEvent | ||
|
@@ -45,6 +46,8 @@ | |
|
||
_CLOUDEVENT_MIME_TYPE = "application/cloudevents+json" | ||
|
||
CloudEventFunction = Callable[[CloudEvent], None] | ||
HTTPFunction = Callable[[flask.Request], flask.typing.ResponseReturnValue] | ||
Comment on lines
+49
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The signature of the HTTP function is a bit verbose :/ Also, if the type checking fails, the error message is ... interesting :)
I reckon it's more a Flask issue, would you have an idea how to make that better? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't remember exactly why I returned False in this example, I think the idea was just to return a non-valid type to see what the type checker would show in this case. So basically, if a developer returns a non-valid type from a |
||
|
||
class _LoggingHandler(io.TextIOWrapper): | ||
"""Logging replacement for stdout and stderr in GCF Python 3.7.""" | ||
|
@@ -59,7 +62,7 @@ def write(self, out): | |
return self.stderr.write(json.dumps(payload) + "\n") | ||
|
||
|
||
def cloud_event(func): | ||
def cloud_event(func: CloudEventFunction) -> CloudEventFunction: | ||
"""Decorator that registers cloudevent as user function signature type.""" | ||
_function_registry.REGISTRY_MAP[ | ||
func.__name__ | ||
|
@@ -99,7 +102,7 @@ def wrapper(*args, **kwargs): | |
return _typed | ||
|
||
|
||
def http(func): | ||
def http(func: HTTPFunction) -> HTTPFunction: | ||
"""Decorator that registers http as user function signature type.""" | ||
_function_registry.REGISTRY_MAP[ | ||
func.__name__ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import typing | ||
|
||
if typing.TYPE_CHECKING: # pragma: no cover | ||
import flask | ||
import functions_framework | ||
from cloudevents.http.event import CloudEvent | ||
|
||
@functions_framework.http | ||
def hello(request: flask.Request) -> flask.typing.ResponseReturnValue: | ||
return "Hello world!" | ||
|
||
@functions_framework.cloud_event | ||
def hello_cloud_event(cloud_event: CloudEvent) -> None: | ||
print(f"Received event: id={cloud_event['id']} and data={cloud_event.data}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how to best show this to users:
🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Including these type annotations for the sake of illustration in this developer focused README.md seems like a reasonable doc update, some of the essence of the simplicity of this sample is preserved on the GCP docs in https://cloud.google.com/functions/docs/samples/functions-helloworld-get#functions_helloworld_get-python.