damccorm commented on code in PR #41320:
URL: https://github.com/apache/beam/pull/41320#discussion_r4698643340


##########
sdks/python/apache_beam/transforms/util.py:
##########
@@ -126,6 +126,12 @@
 U = TypeVar('U')
 
 RESHUFFLE_TYPEHINT_BREAKING_CHANGE_VERSION = "2.64.0"
+REIFY_TYPEHINT_BREAKING_CHANGE_VERSION = "2.79.0"
+_ReifiedWindowInfo = tuple[Any, Timestamp, window.BoundedWindow]
+_ReifiedKeyedWindowInfo = tuple[Any, _ReifiedWindowInfo]
+# Subscripting TimestampedValue on every element is slow, so do it once.
+_TimestampedWindowInfo = TimestampedValue[_ReifiedWindowInfo]
+_TimestampedKeyedWindowInfo = TimestampedValue[_ReifiedKeyedWindowInfo]

Review Comment:
   Instead of instantiating subscripted `TimestampedValue[...]` aliases on 
every element here, we can fix the underlying gap in 
`sdks/python/apache_beam/typehints/typecheck.py` directly.
   
   Calling `_GenericAlias.__call__` (`TimestampedValue[...](...)`) adds ~2.7x 
per-element allocation overhead and attaches `__orig_class__` even when 
`runtime_type_check=False`, and under `runtime_type_check=True` it makes 
`TypeCheckWrapperDoFn` check against `_ReifiedWindowInfo` (using `Any`) instead 
of `self._output_type_hint`.
   
   Unwrapping plain `TimestampedValue` in `TypeCheckWrapperDoFn` when 
`self._output_type_hint` doesn't accept `TimestampedValue` avoids any 
per-element overhead in `util.py`, checks the real output type hint under 
`runtime_type_check=True`, and still preserves DoFns typed `-> 
TimestampedValue` / `-> TimestampedValue[T]`:
   
   ```diff
   diff --git a/sdks/python/apache_beam/typehints/typecheck.py 
b/sdks/python/apache_beam/typehints/typecheck.py
   --- a/sdks/python/apache_beam/typehints/typecheck.py
   +++ b/sdks/python/apache_beam/typehints/typecheck.py
   @@ -40,6 +40,7 @@
    from apache_beam.typehints.typehints import CompositeTypeHintError
    from apache_beam.typehints.typehints import SimpleTypeHintError
    from apache_beam.typehints.typehints import check_constraint
   +from apache_beam.typehints.typehints import is_consistent_with
    from apache_beam.typehints.typehints import normalize
    
    
   @@ -135,6 +136,12 @@ class TypeCheckWrapperDoFn(AbstractDoFnWrapper):
          self._input_hints = None
        # TODO(robertwb): Multi-output.
        self._output_type_hint = type_hints.simple_output_type(label)
   +    # Runners unwrap TimestampedValue before passing elements downstream, so
   +    # check the wrapped value unless the declared output hint explicitly 
accepts
   +    # TimestampedValue (e.g. `-> TimestampedValue` or `Any`).
   +    self._unwrap_timestamped_value = (
   +        self._output_type_hint is not None and
   +        not is_consistent_with(TimestampedValue, self._output_type_hint))
    
      def wrapper(self, method, args, kwargs):
        result = method(*args, **kwargs)
   @@ -166,6 +173,8 @@ class TypeCheckWrapperDoFn(AbstractDoFnWrapper):
          else:
            # TODO(robertwb): Multi-output.
            x = o.value if isinstance(o, (TaggedOutput, WindowedValue)) else o
   +        if isinstance(x, TimestampedValue) and 
self._unwrap_timestamped_value:
   +          x = x.value
    
            self.type_check(self._output_type_hint, x, is_input=False)
   ```
   
   With that in place, we can drop `_ReifiedWindowInfo`, 
`_ReifiedKeyedWindowInfo`, `_TimestampedWindowInfo`, and 
`_TimestampedKeyedWindowInfo`, and keep `add_window_info` yielding plain 
`TimestampedValue(...)`.



-- 
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]

Reply via email to