Skip to content

Conversation

@majiayu000
Copy link

Summary

  • Fixes issue where @fn decorated functions return strings instead of expected types when using PEP 563 (from __future__ import annotations)
  • Uses typing.get_type_hints() to properly resolve string annotations to actual types

Problem

When using from __future__ import annotations, all type annotations become strings by default (PEP 563). The PythonFunction.from_function() method was using sig.return_annotation directly, which would be a string like "Recipe" instead of the actual Recipe class. This caused decorated functions to fail or return wrong types.

Solution

Added annotation resolution using get_type_hints() in PythonFunction.from_function():

  • Attempts to resolve string annotations to actual types
  • Falls back to the raw annotation if resolution fails (forward references, etc.)

Test plan

  • Added unit tests in tests/basic/utilities/test_types_future_annotations.py
  • Tests verify return annotations are actual types, not strings
  • All existing tests pass (262 passed, 2 skipped)
  • Pre-commit checks pass (ruff, ruff-format)

Fixes #950

…ons`

When using PEP 563 (`from __future__ import annotations`), all
annotations become strings by default. This caused @fn decorated
functions to return strings instead of the expected type.

This fix uses `typing.get_type_hints()` to properly resolve string
annotations back to their actual types in PythonFunction.from_function().

Fixes PrefectHQ#950

Signed-off-by: majiayu000 <[email protected]>
@github-actions github-actions bot added the tests label Dec 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

from __future__ import annotations causes ai functions to return strings

1 participant