-
Notifications
You must be signed in to change notification settings - Fork 118
feat: Support strongly typed functions signature #208
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
Merged
kappratiksha
merged 23 commits into
GoogleCloudPlatform:master
from
kappratiksha:master
Dec 12, 2022
Merged
Changes from 17 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
7780b58
testing 1p
kappratiksha 7b8128f
handle response
kappratiksha aac3cae
handle exceptions
kappratiksha e7d5cf3
remove print statements
kappratiksha 56f63a8
Merge branch 'GoogleCloudPlatform:master' into master
kappratiksha 8a26b33
some changes
kappratiksha 1d1fbfb
Merge branch 'master' of https://github.com/kappratiksha/functions-fr…
kappratiksha 9855468
push more 1p changes
kappratiksha c7f94d1
Merge branch 'GoogleCloudPlatform:master' into master
kappratiksha 0c74e60
add exceptions
kappratiksha a7e26e8
add unit tests
kappratiksha 0108b31
lint fix
kappratiksha 7febdf2
fix imports
kappratiksha 4526f7a
use built-in exceptions
kappratiksha f288727
Merge branch 'GoogleCloudPlatform:master' into master
kappratiksha 2593071
add more tests
kappratiksha a38248e
Merge branch 'master' of https://github.com/kappratiksha/functions-fr…
kappratiksha 66e5569
address comments
kappratiksha fc2e7c5
refactor the decorator function
kappratiksha 7dbbfed
add more tests
kappratiksha 29db0b9
Merge branch 'master' into master
kappratiksha 1265c12
add comments
kappratiksha 2f04db3
Merge branch 'master' of https://github.com/kappratiksha/functions-fr…
kappratiksha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Copyright 2021 Google LLC | ||
kappratiksha marked this conversation as resolved.
Show resolved
Hide resolved
kappratiksha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
kappratiksha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import inspect | ||
|
||
from inspect import signature | ||
|
||
from functions_framework import _function_registry | ||
|
||
|
||
def register_typed_event(decorator_type, func): | ||
kappratiksha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sig = signature(func) | ||
annotation_type = list(sig.parameters.values())[0].annotation | ||
kappratiksha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
type_validity_check(decorator_type, annotation_type) | ||
if decorator_type == "": | ||
kappratiksha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
decorator_type = annotation_type | ||
|
||
_function_registry.INPUT_MAP[func.__name__] = decorator_type | ||
_function_registry.REGISTRY_MAP[ | ||
func.__name__ | ||
] = _function_registry.TYPED_SIGNATURE_TYPE | ||
|
||
|
||
def validate_return_type(response): | ||
if not (hasattr(response, "to_dict") and callable(getattr(response, "to_dict"))): | ||
raise AttributeError( | ||
"The type {response} does not have the required method called " | ||
" 'to_dict'.".format(response=response) | ||
kappratiksha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
|
||
def type_validity_check(decorator_type, annotation_type): | ||
if decorator_type == "" and annotation_type is inspect._empty: | ||
raise TypeError( | ||
"The function defined does not contain Type of the input object." | ||
kappratiksha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
if ( | ||
decorator_type != "" | ||
kappratiksha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
and annotation_type is not inspect._empty | ||
and decorator_type != annotation_type | ||
): | ||
raise TypeError( | ||
"The object type provided via 'typed' {decorator_type}" | ||
kappratiksha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"is different from the one in the function annotation {annotation_type}.".format( | ||
decorator_type=decorator_type, annotation_type=annotation_type | ||
) | ||
) | ||
|
||
if decorator_type == "": | ||
kappratiksha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
decorator_type = annotation_type | ||
|
||
if not ( | ||
hasattr(decorator_type, "from_dict") | ||
and callable(getattr(decorator_type, "from_dict")) | ||
): | ||
raise AttributeError( | ||
"The type {decorator_type} does not have the required method called " | ||
" 'from_dict'.".format(decorator_type=decorator_type) | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Function used to test handling functions using typed decorators.""" | ||
|
||
import flask | ||
|
||
import functions_framework | ||
|
||
|
||
class TestType1: | ||
name: str | ||
age: int | ||
|
||
def __init__(self, name: str, age: int) -> None: | ||
self.name = name | ||
self.age = age | ||
|
||
|
||
class TestType2: | ||
name: str | ||
|
||
def __init__(self, name: str) -> None: | ||
self.name = name | ||
|
||
|
||
@functions_framework.typed(TestType2) | ||
def function_typed_mismatch_types(test_type: TestType1): | ||
valid_event = test_type.name == "john" and test_type.age == 10 | ||
if not valid_event: | ||
flask.abort(500) | ||
return test_type |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Copyright 2021 Google LLC | ||
kappratiksha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Function used to test handling functions using typed decorators.""" | ||
from typing import Any, TypeVar | ||
|
||
import flask | ||
|
||
import functions_framework | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
def from_str(x: Any) -> str: | ||
assert isinstance(x, str) | ||
return x | ||
|
||
|
||
def from_int(x: Any) -> int: | ||
assert isinstance(x, int) and not isinstance(x, bool) | ||
return x | ||
|
||
|
||
class TestTypeMissingFromDict: | ||
name: str | ||
age: int | ||
|
||
def __init__(self, name: str, age: int) -> None: | ||
self.name = name | ||
self.age = age | ||
|
||
def to_dict(self) -> dict: | ||
result: dict = {} | ||
result["name"] = from_str(self.name) | ||
result["age"] = from_int(self.age) | ||
return result | ||
|
||
|
||
@functions_framework.typed(TestTypeMissingFromDict) | ||
def function_typed_missing_from_dict(test_type: TestTypeMissingFromDict): | ||
valid_event = test_type.name == "john" and test_type.age == 10 | ||
if not valid_event: | ||
flask.abort(500) | ||
return test_type |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.