jk-shah opened a new issue, #41374:
URL: https://github.com/apache/beam/issues/41374
### What happened?
Custom Python `beam.CombineFn` classes lose their output type hints in
`decorators.get_type_hints()`
(`sdks/python/apache_beam/typehints/decorators.py`, lines `701-725` and
`894-909`) and in `PyJsYamlCombine`
(`sdks/python/apache_beam/yaml/yaml_combine.py`, lines `126-145`):
1. **`decorators.get_type_hints(fn)` ignores class-level
`@with_output_types` and `default_type_hints()` on `WithTypeHints` instances
(`decorators.py:701-702, 894-909`)**:
Decorating a `CombineFn` subclass with
`@beam.typehints.with_output_types(int)` sets `CountDistinctFn._type_hints` on
the **class**. When instantiated (`fn = CountDistinctFn()`),
`WithTypeHints.__init__` sets `self._type_hints = IOTypeHints.empty()` on the
**instance**. While `fn.get_type_hints()` (`decorators.py:713-724`) merges
`self.default_type_hints()` and `get_type_hints(self.__class__)`, module-level
`decorators.get_type_hints(fn)` (`decorators.py:900-908`, called by
`PyJsYamlCombine.expand` at `yaml_combine.py:138`) returns `fn._type_hints`
directly (`IOTypeHints.empty()`). Downstream schema-requiring sinks such as
`WriteToParquet` then fail because the aggregated column is inferred as
`typing.Any`.
2. **Zero-arg `CombineFn` classes without `config:` stay wrapped in
`PythonCallableWithSource` (`yaml_combine.py:127-131`)**:
`create_combine_fn` only instantiates `fn` when `'config' in fn_spec`
(`if 'config' in fn_spec: fn = fn(**fn_spec['config'])`). Referencing a
zero-arg `CombineFn` class as `fn: {type: pkg.CountDistinctFn}` without
`config: {}` leaves `fn` as a `PythonCallableWithSource` object
(`isinstance(fn, beam.CombineFn) == False`), falling back to `Any` at line
`168`. *(Minor note: line `133` also has a missing `f`-string prefix in `raise
TypeError('Unknown CombineFn: {fn_spec}')`.)*
Minimal reproducer:
```python
import apache_beam as beam
from apache_beam.typehints import decorators
@beam.typehints.with_output_types(int)
class CountDistinctFn(beam.CombineFn):
def create_accumulator(self):
return set()
def add_input(self, acc, x):
acc.add(x)
return acc
def merge_accumulators(self, accs):
return set().union(*accs)
def extract_output(self, acc):
return len(acc)
fn = CountDistinctFn()
print(fn.get_type_hints()) # IOTypeHints[inputs=None,
outputs=((<class 'int'>,), {})]
print(decorators.get_type_hints(fn)) # IOTypeHints[inputs=None,
outputs=None]
```
### Cause
`decorators.get_type_hints(fn)` checks `hasattr(fn, '_type_hints')` and
returns `fn._type_hints` before checking `isinstance(fn, WithTypeHints)` ->
`fn.get_type_hints()` (or `fn.get_output_type()` on `CombineFn`), and
`PyJsYamlCombine.expand` does not unwrap/instantiate `CombineFn` subclasses
when `config` is omitted.
### Issue Priority
Priority: 2 (default / most bugs should be filed as P2)
### Issue Components
- [x] Component: Python SDK
- [ ] Component: Java SDK
- [ ] Component: Go SDK
- [ ] Component: Typescript SDK
- [ ] Component: IO connector
- [x] Component: Beam YAML
- [ ] Component: Beam ML
- [ ] Component: Beam examples
- [ ] Component: Beam playground
- [ ] Component: Beam katas
- [ ] Component: Website
- [ ] Component: Infrastructure
- [ ] Component: Spark Runner
- [ ] Component: Flink Runner
- [ ] Component: Prism Runner
- [ ] Component: Hazelcast Jet Runner
- [ ] Component: Google Cloud Dataflow Runner
--
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]