adarshsm commented on issue #607:
URL: https://github.com/apache/burr/issues/607#issuecomment-5139932746
I dug into where `stream_type` actually flows, and I think this is the
missing piece here — there are really two parts, and the annotation is only one.
`stream_type` becomes the action's `intermediate_result_type`, which **is**
used at runtime, in `Application._validate_result` (`burr/core/application.py`):
```python
result_type = schema.intermediate_result_type()
if not isinstance(result, result_type):
raise ValueError(...)
```
So to @elijahbenizzy's question — yes, it does an `isinstance` per result,
and that's the part that matters for a union. Widening the annotation (what the
open PRs do) makes `stream_type = A | B` type-check, but this
`isinstance(result, result_type)` is where a union needs handling:
- `isinstance(result, A | B)` (PEP 604) works on 3.10+.
- `isinstance(result, typing.Union[A, B])` isn't reliable across versions
(`TypeError: Subscripted generics cannot be used with class and instance
checks`).
The robust, version-independent way — which also gives the per-instance
detection — is to check against the union's members:
```python
import typing, types
def _matches(result, result_type):
if typing.get_origin(result_type) in (typing.Union, types.UnionType):
return isinstance(result, tuple(typing.get_args(result_type)))
return isinstance(result, result_type)
```
i.e. a result is valid if it's *any* member of the union. As far as I can
tell none of the current PRs touch `application.py`, so they'd accept the union
annotation but leave this check unchanged.
Happy to put up a focused PR doing both — widen `stream_type` to accept
unions and make `_validate_result` union-aware, with tests. Or if you'd rather
fold the runtime part into one of the existing PRs, glad to help there. Which
would you prefer?
--
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]