I have a DoFn, where I return a regular and a tagged output:
class Foo(DoFn):
UNPARSABLE_TAG = "unparseable_events"
def process(
self, element: Tuple[str, date]
) -> Iterable[Union[Event, str]]:
ev, d = element
if ev == "1":
yield Event(ev)
else:
yield TaggedOutput(self.UNPARSABLE_TAG, ev)
Runtime type checking seems to expect a return hint that encompasses both regular and tagged output types.
While this is doable, when consuming both PCollections you then need to add a "decorator hack" to later get to correct types again:
valid, invalid = ... | ParDo(Foo()).with_outputs(Foo.UNPARSABLE_TAG, main="main")
valid | Map(_beamDecoratorHack)
...
def _beamDecoratorHack(
ev: Union[Event, str]
) -> Event:
assert isinstance(ev, Event)
return ev
This seems sluggish. Is there a way of not needing the hack?