Skip to content

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

Description

@junah201

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:

fromfastapiimportFastAPIfromvelloximportVelloxapp=FastAPI()
@app.get("/")defread_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")defread_item(item_id: int, q: str=None):
return {"item_id": item_id, "q": q}
vellox=Vellox(app=app, lifespan="off")
defhandler(request):
returnvellox(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.pydefget_user_function(source, source_module, target):
"""Returns user function, raises exception for invalid function."""# . . .function=getattr(source_module, target)
# Check that it is a functionifnotisinstance(function, types.FunctionType):
raiseInvalidTargetTypeException(
"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)
)
)
returnfunction

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions