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

Support for postponed annnotations #291

Open
haizaar opened this issue Nov 30, 2023 · 0 comments · May be fixed by #292
Open

Support for postponed annnotations #291

haizaar opened this issue Nov 30, 2023 · 0 comments · May be fixed by #292
Labels
bug Something isn't working P2

Comments

@haizaar
Copy link

haizaar commented Nov 30, 2023

Consider main.py:

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)=<class 'str'>
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 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.

haizaar added a commit to haizaar/functions-framework-python that referenced this issue Nov 30, 2023
@haizaar haizaar linked a pull request Nov 30, 2023 that will close this issue
@HKWinterhalter HKWinterhalter added bug Something isn't working P2 labels Dec 4, 2023
@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
bug Something isn't working P2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants