Skip to content
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

Update type check in get_user_function to accept Callable instead of only types.FunctionType #317

Open
junah201 opened this issue Apr 11, 2024 · 0 comments

Comments

@junah201
Copy link

junah201 commented Apr 11, 2024

I am making an adapter for running ASGI applications in GCP Cloud Function. (Repo)
Simply, I want to run my ASGI application (such as FastAPI or Django) in Cloud Function.

My adapter(Vellox) works like this:

from fastapi import FastAPI
from vellox import Vellox

app = FastAPI()


@app.get("/")
def read_root():
    return {"Hello": "World"}


@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
    return {"item_id": item_id, "q": q}

vellox = Vellox(app=app, lifespan="off")

def handler(request):
    return vellox(request)

I confirmed that my adapter (vellox) works "hello world" example properly. But functions_framework throw error when handler is not types.FunctionType. In abvoe code, handler is instance of Vellox and also it is callable.
The code below is the part that is problematic.

# src/functions_framework/_function_registry.py

def get_user_function(source, source_module, target):
    """Returns user function, raises exception for invalid function."""

    # . . .

    function = getattr(source_module, target)
    # Check that it is a function
    if not isinstance(function, types.FunctionType):
        raise InvalidTargetTypeException(
            "The function defined in file {source} as '{target}' needs to be of "
            "type function. Got: invalid type {target_type}".format(
                source=source, target=target, target_type=type(function)
            )
        )
    return function

Instead of forcing the function type to be function, I suggest changing it to Callable, which has a slightly wider scope.

junah201 added a commit to junah201/functions-framework-python that referenced this issue Apr 11, 2024
In the get_user_function function within _function_registry.py, change the type check to accept callable objects instead of only types.FunctionType. This allows for instances of classes with a __call__ method.
@HKWinterhalter HKWinterhalter removed their assignment Jun 3, 2024
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

No branches or pull requests

2 participants