Skip to content

Commit

Permalink
Initial typing of the public API
Browse files Browse the repository at this point in the history
This adds type annotations for the 2 main decorators of the library.

Closes: GoogleCloudPlatform#190
  • Loading branch information
multani committed May 30, 2023
1 parent 6420b67 commit 891817c
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 5 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Create an `main.py` file with the following contents:
import functions_framework

@functions_framework.http
def hello(request):
def hello(request: flask.Request) -> flask.typing.ResponseReturnValue:
return "Hello world!"
```

Expand Down Expand Up @@ -98,9 +98,10 @@ Create an `main.py` file with the following contents:

```python
import functions_framework
from cloudevents.http.event import CloudEvent

@functions_framework.cloud_event
def hello_cloud_event(cloud_event):
def hello_cloud_event(cloud_event: CloudEvent) -> None:
print(f"Received event with ID: {cloud_event['id']} and data {cloud_event.data}")
```

Expand Down
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
with open(path.join(here, "README.md"), encoding="utf-8") as f:
long_description = f.read()


setup(
name="functions-framework",
version="3.4.0",
Expand All @@ -46,6 +47,10 @@
],
keywords="functions-framework",
packages=find_packages(where="src"),
package_data={
"functions_framework": ["py.typed"],
},
zip_safe=False,
namespace_packages=["google", "google.cloud"],
package_dir={"": "src"},
python_requires=">=3.5, <4",
Expand Down
9 changes: 6 additions & 3 deletions src/functions_framework/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,6 +46,8 @@

_CLOUDEVENT_MIME_TYPE = "application/cloudevents+json"

CloudEventFunction = Callable[[CloudEvent], None]
HTTPFunction = Callable[[flask.Request], flask.typing.ResponseReturnValue]

class _LoggingHandler(io.TextIOWrapper):
"""Logging replacement for stdout and stderr in GCF Python 3.7."""
Expand All @@ -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__
Expand Down Expand Up @@ -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__
Expand Down
Empty file.
14 changes: 14 additions & 0 deletions tests/test_typing.py
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}")
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ deps =
black
twine
isort
mypy
commands =
mypy tests/test_typing.py
black --check src tests setup.py conftest.py --exclude tests/test_functions/background_load_error/main.py
isort -c src tests setup.py conftest.py
python setup.py --quiet sdist bdist_wheel
Expand Down

0 comments on commit 891817c

Please sign in to comment.