damccorm commented on code in PR #41320:
URL: https://github.com/apache/beam/pull/41320#discussion_r4671615548
##########
sdks/python/apache_beam/transforms/util.py:
##########
@@ -2451,7 +2451,7 @@ def expand(self, pcoll):
return pcoll | ParDo(self.add_timestamp_info)
@typehints.with_input_types(T)
- @typehints.with_output_types(T)
+ @typehints.with_output_types(Any)
Review 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:
```python
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`.
##########
sdks/python/apache_beam/transforms/util.py:
##########
@@ -2465,7 +2465,7 @@ def expand(self, pcoll):
return pcoll | ParDo(self.add_window_info)
@typehints.with_input_types(tuple[K, V])
- @typehints.with_output_types(tuple[K, V])
+ @typehints.with_output_types(tuple[K, Any])
Review 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`.)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]