Skip to content

Fix forward references that do not resolve to types - #41184

Merged
jrmccluskey merged 4 commits into
apache:masterfrom
jrmccluskey:oneStepForward
Nov 12, 2026
Merged

jrmccluskey merged 4 commits into
apache:masterfrom
jrmccluskey:oneStepForward

Conversation

@jrmccluskey

Copy link
Copy Markdown
Contributor

#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 Any to allow TFX pipelines to pass type checking.

@jrmccluskey jrmccluskey added this to the 2.78.0 Release milestone Nov 10, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

📋 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_hint properly accounts for None, Ellipsis, Callable parameter lists, Literal, and Annotated metadata before delegating leaf validation to validate_composite_type_param.
  • Unit tests thoroughly cover direct string annotations, from __future__ import annotations, and beam.Map integration with Protobuf enum descriptors.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

@github-actions github-actions Bot added python core size/s S: 10-49 lines changed labels Nov 10, 2026
@codecov

codecov Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.90909% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.55%. Comparing base (fa9c1eb) to head (89611aa).
⚠️ Report is 32 commits behind head on master.

Files with missing lines Patch % Lines
sdks/python/apache_beam/typehints/decorators.py 90.90% 2 Missing ⚠️
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              
Flag Coverage Δ
python 80.55% <90.90%> (+0.31%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @claudevdm for label python.
Backup reviewer: tvalentyn (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

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 assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@github-actions github-actions Bot added size/m M: 50-249 lines changed and removed size/s S: 10-49 lines changed labels Nov 11, 2026
@jrmccluskey

Copy link
Copy Markdown
Contributor Author

@claudevdm fixed those cases, PTAL

@claudevdm claudevdm left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Left two questions but LGTM

try:
validate_composite_type_param(
convert_to_beam_type(hint), 'Resolved type annotation')
except (TypeError, ValueError):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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 exceeded

convert_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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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>
@jrmccluskey
jrmccluskey merged commit e8dc331 into apache:master Nov 12, 2026
84 of 86 checks passed
jrmccluskey added a commit that referenced this pull request Nov 12, 2026
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants