Skip to content

Conversation

@gverkes
Copy link
Contributor

@gverkes gverkes commented Oct 23, 2025

Tracking issue

Closes flyteorg/flyte#6694

Why are the changes needed?

When using from __future__ import annotations (PEP 563), the __annotations__ dictionary contains string literals instead of actual type objects. The current implementation directly accesses __annotations__ in the PydanticTransformer.get_literal_type() method, which causes standard Python types (str, int, float, etc.) in Pydantic BaseModel fields to be treated as unsupported types. This results in fallback to PickleFile serialization, causing warnings and inefficient serialization.

This fix is important for compatibility with Python 3.14, where PEP 563 future annotations will become the default behavior.

What changes were proposed in this pull request?

Updated PydanticTransformer.get_literal_type() in flytekit/extras/pydantic_transformer/transformer.py to use Pydantic's built-in field APIs instead of directly accessing __annotations__:

  • model_fields for Pydantic v2
  • __fields__ for Pydantic v1 (backwards compatibility)

These APIs properly resolve string annotations to actual type objects, regardless of whether future annotations are enabled.

Added comprehensive test suite in test_future_annotations_bug.py with 4 tests covering:

  • Simple types (str, int, float, bool)
  • Complex types (List, Dict, nested structures)
  • Nested BaseModels
  • Literal type structure validation (regression test to ensure types are not treated as PickleFile)

How was this patch tested?

Setup process

  1. Created reproduction script with from __future__ import annotations
  2. Verified the bug (types treated as strings causing PickleFile fallback)
  3. Applied fix to transformer.py
  4. Verified fix resolves the issue (no more warnings)
  5. Added comprehensive test suite with 4 new tests
  6. Ran all pydantic transformer tests: 29 tests passed (25 existing + 4 new)

Screenshots

Before fix:

WARNING - Unsupported Type str found, Flyte will default to use PickleFile as the transport.
WARNING - Unsupported Type int found, Flyte will default to use PickleFile as the transport.

After fix: No warnings, types correctly identified.

Check all the applicable boxes

  • I updated the documentation accordingly.
  • All new and existing tests passed.
  • All commits are signed-off.

Related PRs

N/A

Docs link

N/A (code fix only, no user-facing documentation changes needed)

Summary by Bito

  • Updates the get_literal_type method in the Pydantic transformer to correctly handle type annotations with 'from __future__ import annotations'.
  • Prevents fallback to inefficient PickleFile serialization, ensuring compatibility with future Python versions.
  • Adds a comprehensive test suite to validate functionality across different scenarios, including simple and complex types, nested models, and literal type structure validation.
  • Overall summary: touches Pydantic transformer, type annotations, and serialization; introduces improvements in handling type annotations and testing.

@codecov
Copy link

codecov bot commented Nov 8, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.53%. Comparing base (0663d8e) to head (e02c0cf).
⚠️ Report is 6 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3343      +/-   ##
==========================================
- Coverage   81.40%   79.53%   -1.87%     
==========================================
  Files         369      216     -153     
  Lines       29749    22697    -7052     
  Branches     2971     2974       +3     
==========================================
- Hits        24217    18052    -6165     
+ Misses       4738     3809     -929     
- Partials      794      836      +42     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment on lines 28 to 36

# Use Pydantic's field APIs to get properly resolved types
# This handles 'from __future__ import annotations' correctly
if hasattr(t, 'model_fields'):
# Pydantic v2
fields = [(name, field_info.annotation) for name, field_info in t.model_fields.items()]
else:
# Pydantic v1
fields = [(name, field_info.annotation) for name, field_info in t.__fields__.items()]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just use fields = typing.get_type_hints(t).items() here?

@machichima
Copy link
Member

Thank you for this PR! Could you please run make lint to solve the lint error? Thank you!

@gverkes gverkes force-pushed the fix/pydantic-future-annotations branch from af1cbdb to 108f519 Compare November 10, 2025 11:57
Fixes #6694

When using `from __future__ import annotations`, the `__annotations__`
dictionary contains string literals instead of actual type objects. This
was causing the PydanticTransformer to fail to recognize standard Python
types (str, int, float, etc.) in BaseModel fields, resulting in fallback
to PickleFile serialization.

Changes:
- Updated `PydanticTransformer.get_literal_type()` to use Pydantic's
  `model_fields` API (v2) or `__fields__` (v1) instead of directly
  accessing `__annotations__`
- These Pydantic APIs properly resolve string annotations to actual
  type objects, regardless of whether future annotations are enabled
- Added comprehensive test suite covering simple types, complex types,
  nested models, and literal type structure validation

The fix ensures compatibility with PEP 563 future annotations, which
will become the default behavior in Python 3.14.

Signed-off-by: Govert Verkes <[email protected]>
@gverkes gverkes force-pushed the fix/pydantic-future-annotations branch from 108f519 to e02c0cf Compare November 10, 2025 14:47
Copy link
Member

@machichima machichima left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you!

@machichima machichima enabled auto-merge (squash) November 10, 2025 23:10
@machichima machichima merged commit c3818b3 into flyteorg:master Nov 10, 2025
116 of 119 checks passed
@welcome
Copy link

welcome bot commented Nov 10, 2025

Congrats on merging your first pull request! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] when using from __future__ import annotations the pydantic type transformer doesn't properly recognize types

2 participants