-
Notifications
You must be signed in to change notification settings - Fork 117
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: Support strongly typed functions signature #208
feat: Support strongly typed functions signature #208
Conversation
Could you add a description to the PR explaining the new feature and a code snippet sample of how to use it? I would define the typed class with the to_dict and from_dict functions in the code snippet to for illustrative purposes. |
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.
Whew, pretty big one! Nice work getting this done!
I think I reviewed everything except tests. But gonna get these comments out for now.
Oh btw, for the lint check, you can copy the commands executed here to reproduce locally: https://github.com/GoogleCloudPlatform/functions-framework-python/blob/master/.github/workflows/lint.yml |
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.
Just nits and a existential-ish question
@@ -67,6 +72,24 @@ def wrapper(*args, **kwargs): | |||
return wrapper | |||
|
|||
|
|||
def typed(*args): |
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.
Hmm I've been pondering for a while whether there's a way for us to provide typing here where like someone could hover over the definition of @typed and see that it's supposed to just take one, optional input type parameter instead of this kind of ambiguous *args situation.
I'm not sure it's possible though unless we enforce a named parameter like @typed(input_type=MyType)
...
Can you think of anything?
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 believe that is the only way to get type hints for a function. Also, it's not very useful for generics also.
if I do something like this-def typed(input_type=T)
, the type hints are input_type: Any
We can leave it as is right now, I added documentation for this decorator so it should be a little better.
Good day, Does it supersede
essentially replace
? Will it work on Google Cloud Functions? |
Answering to myself:
from __future__ import annotations
from pydantic import BaseModel
class HelloRequest(BaseModel):
name: str
@classmethod
def from_dict(cls, d: dict[str, Any]) -> HelloRequest:
return cls(**d)
def to_dict(self) -> dict[str, Any]:
return self.model_dump()
@functions_framework.typed
def hello(hr: HelloRequest):
return f"Hello, {hr.name}" Well done guys! |
@haizaar Thanks for trying it out and sharing an example! As you may have found, 'typed' is in additional feature and does not deprecate 'http'. |
Add a new signature in functions framework that will support strongly typed objects. This signature will take a strong type T as an input and can return a strong type T, built in types supported by flask or None as an output.
Sample declarations:
The new signature will only support types abiding by the below contract to convert a python object to and from json:
Here is sample python object: