GoogleCloudPlatform / GoogleCloudPlatform/functions-framework-python

Support for postponed annnotations

Đang mở
#291 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
bug P2
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ả

Consider `main.py`:
```python
from __future__ import annotations
import functions_framework
import flask

class HelloRequest:
name: str

def __init__(self, name: str) -> None:
self.name = name

@classmethod
def from_dict(cls, d: dict) -> HellowRequest:
return cls(**d)

def to_dict(self) -> dict:
return self.__dict__

@functions_framework.typed
def hello(hr: HelloRequest) -> flask.typing.ResponseReturnValue:
return f"Hello, {hr.name}"
```

Running `import main` yields:
```
input_type=HelloRequest, type(input_type)=
Traceback (most recent call last):
File "/home/....venv/lib/python3.11/site-packages/functions_framework/_typed_event.py", line 37, in register_typed_event
_validate_input_type(input_type)
File "/home/.../.venv/lib/python3.11/site-packages/functions_framework/_typed_event.py", line 103, in _validate_input_type
raise AttributeError(
AttributeError: The type HelloRequest does not have the required method called 'from_dict'.
```

The culprit is `from __future__ import annotations`, which is required to annotate `from_dict` properly.

The issue is [here](https://github.com/GoogleCloudPlatform/functions-framework-python/blob/main/src/functions_framework/_typed_event.py#L34) where `inspect.signature` will return annotations as strings (by default) if they are postponed.

The suggested solution is to pass `eval_str=True` when calling `inspect.signature`.

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.