more-itertools: value_chain and swallowed errors

Merged into more-itertools. value_chain wrapped a try and except TypeError around a lazy yield from, so the guard stayed active for the whole consumption instead of only classifying the argument. A TypeError raised from inside an argument was swallowed, and a half-consumed object was emitted in its place. I reported the issue and sent the fix.

September 3, 2026

What was wrong

value_chain classified each argument by wrapping yield from value in a try and except TypeError. Because yield from consumes lazily, the guard covered the whole consumption rather than only the classification.

So an error raised while iterating an argument, such as calling len on an int inside a map, never reached the caller. The partly drained object was yielded in its place, after the items it had already produced, carrying no more data. Both itertools.chain and always_iterable propagate on the same input.

The change

Classify with iter and consume outside the guard, the same approach always_iterable already uses. The call to iter decides whether the argument is iterable, and the actual iteration happens in the else branch where no guard hides its errors.

Arguments that cannot be iterated at all, including ones whose iterator raises TypeError, are still emitted as-is, so the documented fallback is unchanged.

Tests

  • One test pins that the TypeError now propagates after the already produced items.
  • A second pins that the non-iterable fallback still holds.
  • Checks run locally: the full unittest suite of 907 tests, ruff format and ruff check, stubtest, and coverage at 100%.

more-itertools/more-itertools #1251, merged