Repository navigation
Fix forward references that do not resolve to types - #41184
Conversation
There was a problem hiding this comment.
📋 Review Summary
This PR fixes a regression from #40625 where string or forward-reference type annotations that resolve to non-type objects (such as Protobuf EnumTypeWrapper instances in TFDV/TFX) cause a TypeError during Beam composite type hint validation. The implementation validates resolved annotations with _is_valid_type_hint and cleanly falls back to leaving invalid resolved hints unresolved so convert_to_beam_type maps them to Any.
🔍 General Feedback
- The recursive validation in
_is_valid_type_hintproperly accounts forNone,Ellipsis,Callableparameter lists,Literal, andAnnotatedmetadata before delegating leaf validation tovalidate_composite_type_param. - Unit tests thoroughly cover direct string annotations,
from __future__ import annotations, andbeam.Mapintegration with Protobuf enum descriptors.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #41184 +/- ##
============================================
+ Coverage 58.43% 58.55% +0.12%
Complexity 2277 2277
============================================
Files 1139 1132 -7
Lines 169991 169769 -222
Branches 1485 1485
============================================
+ Hits 99327 99410 +83
+ Misses 68117 67812 -305
Partials 2547 2547
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Assigning reviewers: R: @claudevdm for label python. This pull request likely touches a core component ("core" label). Please review with scrutiny. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
|
@claudevdm fixed those cases, PTAL |
claudevdm
left a comment
There was a problem hiding this comment.
Left two questions but LGTM
| try: | ||
| validate_composite_type_param( | ||
| convert_to_beam_type(hint), 'Resolved type annotation') | ||
| except (TypeError, ValueError): |
There was a problem hiding this comment.
Should a recursive type alias fall back to Any here as well? It gets past this except:
from __future__ import annotations
import apache_beam as beam
type JSON = dict[str, JSON] | list[JSON] | str | int | None
def parse(s: str) -> JSON:
return {}
print([''] | beam.Map(parse))
# RecursionError: maximum recursion depth exceededconvert_to_beam_type unwraps the alias, finds the alias again inside, and never stops, and RecursionError isn't a TypeError or ValueError. This ran before #40625 and already fails on master. Is that something to handle in this PR or separately?
There was a problem hiding this comment.
I'm actually in favor of a recursive type definition getting flagged here as invalid, functionally I do not think there's a ton of value in being permissive in that case. Without using future annotations that type hint isn't even something you could write normally, so I don't expect this to be a broadly breaking change.
Co-authored-by: claudevdm <33973061+claudevdm@users.noreply.github.com>
* Fix forward references that do not resolve to types * yapf * Fix extra broken edge cases * Apply suggestion from @claudevdm --------- Co-authored-by: claudevdm <33973061+claudevdm@users.noreply.github.com>
#40625 introduced handling for forward reference type hints in callables, but introduced an edge case where resolved results that do not contain a type (like resolution to a protobuf EnumTypeWrapper instance in TFDV's code) would fail with a type error. This resolves that case to
Anyto allow TFX pipelines to pass type checking.