chrisqiqiu opened a new pull request, #38429:
URL: https://github.com/apache/beam/pull/38429
fixes #18712
## Background
A user-written `DoFn` whose `process` accidentally returns a
`str`/`bytes`/`dict` instead of an iterable wrapping one — e.g.:
```python
class MyDoFn(beam.DoFn):
def process(self, element):
return element + '-processed'
— silently iterates per-character/byte/key instead of erroring. The string
'apple-processed' becomes 13 single-character output elements ('a', 'b', 'c',
...). The pipeline runs to completion with wrong output, and downstream errors
point at the wrong place. This is hard to debug.
This existing check lives in typehints/typecheck.py:_check_type but is gated
behind both @with_output_types(...) and --runtime_type_check — so most users
never see it.
## Fix
Add a cheap isinstance(results, (str, bytes, dict)) guard in
_OutputHandler.handle_process_outputs (runners/common.py) that raises a clear
TypeError pointing the user at list(...). The error message reuses the wording
from the existing _check_type.
## Why scoped to user class-based DoFns
The check is gated by a new _check_user_dofn_output flag set at DoFnRunner
construction time, computed as not isinstance(fn, core.CallableWrapperDoFn).
This deliberately skips the check for Map/FlatMap callables. In particular,
FlatMap() with the default identity function is a documented pattern for
flattening iterables — including strings into characters. Blocking
str/bytes/dict universally would break that legitimate use case (and the
existing FlatMapTest::test_default test).
## Tests
<img width="1440" height="668" alt="image"
src="https://github.com/user-attachments/assets/bc51cc06-40a3-46a8-b878-ace6914e84d1"
/>
```bash
pytest \
apache_beam/transforms/core_test.py::FlatMapTest \
apache_beam/runners/common_test.py::DoFnProcessTest \
apache_beam/typehints/typecheck_test.py -v
```
Three regression tests in runners/common_test.py::DoFnProcessTest covering
str, bytes, and dict return values from a class-based DoFn. Verified the
previously-cryptic case now raises:
```
TypeError: Returning a str from a ParDo or FlatMap is discouraged.
Please use list("abc-processed") if you really want this behavior.
```
Existing typehints/typecheck_test.py (13 tests) and broader transforms/
suite (787 tests) all still pass.
------------------------
Thank you for your contribution! Follow this checklist to help us
incorporate your contribution quickly and easily:
- [x] Mention the appropriate issue in your description (for example:
`addresses #123`), if applicable. This will automatically add a link to the
pull request in the issue. If you would like the issue to automatically close
on merging the pull request, comment `fixes #<ISSUE NUMBER>` instead.
- [x] Update `CHANGES.md` with noteworthy changes.
- [ ] If this contribution is large, please file an Apache [Individual
Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
See the [Contributor Guide](https://beam.apache.org/contribute) for more
tips on [how to make review process
smoother](https://github.com/apache/beam/blob/master/CONTRIBUTING.md#make-the-reviewers-job-easier).
To check the build health, please visit
[https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md](https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md)
GitHub Actions Tests Status (on master branch)
------------------------------------------------------------------------------------------------
[](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Go+tests%22+branch%3Amaster+event%3Aschedule)
See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more
information about GitHub Actions CI or the [workflows
README](https://github.com/apache/beam/blob/master/.github/workflows/README.md)
to see a list of phrases to trigger workflows.
--
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]