Repository navigation
Conversation
There was a problem hiding this comment.
📋 Review Summary
This PR fixes an output type hint bug in Reify.Window, Reify.TimestampInValue, and Reify.WindowInValue where previously declared @typehints.with_output_types bound output types to T or tuple[K, V], causing coder mismatches when the input PCollection had a concrete inferred type (such as from beam.Create). Updating the declared output types to Any and tuple[K, Any] accurately prevents invalid value coder selection while preserving key types and maintaining coder compatibility with untyped inputs.
🔍 General Feedback
- The fix is clean, minimal, and preserves
FastPrimitivesCoderfallback behavior for the reified value payloads while keeping key type inference (K) intact for KV variants. - The added
ReifyTest.test_typed_inputregression test covers all three modified transforms with typed input and downstream consumption.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
|
Assigning reviewers: R: @damccorm 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). |
|
Reminder, please take a look at this pr: @damccorm |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #41320 +/- ##
=========================================
Coverage 59.40% 59.41%
Complexity 5137 5137
=========================================
Files 1470 1470
Lines 187619 187619
Branches 3819 3819
=========================================
+ Hits 111456 111465 +9
+ Misses 72756 72747 -9
Partials 3407 3407
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:
|
damccorm
left a comment
There was a problem hiding this comment.
Thanks for catching and fixing this! Left a couple of suggestions inline on how we can keep concrete output type hints here while still preserving streaming update compatibility.
|
|
||
| @typehints.with_input_types(T) | ||
| @typehints.with_output_types(T) | ||
| @typehints.with_output_types(Any) |
There was a problem hiding this comment.
Good call thinking about streaming update compatibility here. We can still retain concrete output type hints (tuple[T, Timestamp, window.BoundedWindow] here, and tuple[K, tuple[V, Timestamp, window.BoundedWindow]] in WindowInValue) by gating the fallback on update_compatibility_version in expand(), similar to what ReshufflePerKey does earlier in this file:
def expand(self, pcoll):
if pcoll.pipeline.options.is_compat_version_prior_to('2.79.0'):
return pcoll | ParDo(self.add_window_info).with_output_types(Any)
return (
pcoll
| ParDo(self.add_window_info)
.with_input_types(T)
.with_output_types(tuple[T, Timestamp, window.BoundedWindow]))Keeping @typehints.with_output_types(Any) on Window (and tuple[K, Any] on WindowInValue) keeps --type_check_strictness=ALL_REQUIRED happy, while Pipeline._infer_result_type preserves the concrete Tuple[...] element type emitted by the inner ParDo for new pipelines (giving downstream transforms full type inference and native coders for T / V), and existing streaming jobs updating with --update_compatibility_version=2.78.0 still get the old FastPrimitivesCoder.
If we do that, we could also assert the inferred element_type in test_typed_input and cover update_compatibility_version='2.78.0' and runtime_type_check=True.
|
|
||
| @typehints.with_input_types(tuple[K, V]) | ||
| @typehints.with_output_types(tuple[K, V]) | ||
| @typehints.with_output_types(tuple[K, Any]) |
There was a problem hiding this comment.
We can use tuple[K, TimestampedValue] here directly without even needing a compat version gate: CoderRegistry.get_coder(TimestampedValue) already falls back to FastPrimitivesCoder, so the output coder stays TupleCoder((coder(K), FastPrimitivesCoder())) (identical to tuple[K, Any]), while still preserving type checking on TimestampedValue.
(Just make sure it's unparameterized TimestampedValue rather than TimestampedValue[V] — native_type_compatibility doesn't have a Beam TypeConstraint for TimestampedValue, so a subscripted generic would fail is_consistent_with and --runtime_type_check.)
|
Also, could you please resolve conflicts? |
… TimestampInValue These transforms declared their output type as the input type, but they return (element, timestamp, window) tuples, or a (key, value) pair whose value is a tuple or a TimestampedValue. With an input that has an inferred type hint (for example the result of Create), the runner then failed to encode the output with the coder of the declared type. The declared output types now leave the reified part as Any.
…est with runtime_type_check
840d9d4 to
fa8b54f
Compare
|
I’ve rebased the branch onto master (89f08d2) and resolved the only conflict in CHANGES.md. I’ve also updated I’ve left The Reify and Reshuffle util tests pass (24 passed). The new test is still failing on master. Would you prefer a different approach for adding concrete type hints to |
|
We should be able to retain non-Any types for all 3 - please take a look at #41320 (comment) |
|
Also, please avoid force pushing - it breaks GitHub's review feature |
…nValue Reify.Window and Reify.WindowInValue now give the ParDo they expand to concrete output types, tuple[T, Timestamp, BoundedWindow] and tuple[K, tuple[V, Timestamp, BoundedWindow]], so downstream transforms get type inference and native coders. Pipelines with an update compatibility version before 2.79.0 keep Any for the reified part and the old coder. The transforms yield a subscripted TimestampedValue so that runtime_type_check checks the wrapped tuple. test_typed_input asserts the inferred element types and covers update_compatibility_version 2.78.0 and runtime_type_check.
The compat branch declared tuple[K, Any] without input types, so K was unbound and the key coder changed for str-keyed input. Bind the input types, assert the 2.78.0 element type and coder in the test, build the subscripted TimestampedValue aliases once, and note the update compatibility option in CHANGES.md.
Fixes #41306
util.Reify.Windowdeclared its output type as its input typeT, andWindowInValueandTimestampInValuedeclaredtuple[K, V], but they output(element, timestamp, window),(key, (value, timestamp, window))and(key, TimestampedValue(value, timestamp)). When the input has an inferred element type (for example the output ofCreate, also after aMap(lambda x: x)), the coder is picked from the declared output type and the runner fails to encode the reified elements. The existing tests did not see it because their inputs areTimestampedValue/WindowedValueobjects, whose element type has no specific coder. The reified part of the output types is nowAny(tuple[K, Any]for the two KV transforms). For inputs that already worked (element typeAny, or a KV whose value type isAny) the output coder does not change; a precise tuple hint forWindowwould have changed it fromFastPrimitivesCoderto aTupleCoder, which matters for streaming update compatibility.Test:
test_typed_inputruns the three transforms on aCreateof('a', 1)and('b', 2); it fails on master (the bundle fails while encoding the output) and passes with the change.pytest apache_beam/transforms/util_test.py -k Reify: 5 passed (1 failed, 4 passed on master source).Reify.WindowonCreate([1, 2])failed on master withTypeError: '<' not supported between instances of 'tuple' and 'int'(TypeError: an integer is requiredwith Beam 2.77.0 from PyPI) and returns(1, MIN_TIMESTAMP, GlobalWindow())and(2, MIN_TIMESTAMP, GlobalWindow())after the change.