GoogleCloudPlatform / GoogleCloudPlatform/functions-framework-python

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

Đang mở
#317 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
enhancement P3
Ngôn ngữ chính
Python
Star
968
Fork
128
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

I am making an adapter for running ASGI applications in GCP Cloud Function. ([Repo](https://github.com/junah201/vellox))
Simply, I want to run my ASGI application (such as FastAPI or Django) in Cloud Function.

My adapter(`Vellox`) works like this:
```py
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.

```py
# 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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.