Skip to content

fix(step): iterate a scalar foreach value once instead of crashing on falsy values - #6722

Open
yzxcj797 wants to merge 2 commits into
keephq:mainfrom
yzxcj797:fix/foreach-falsy-scalar-6721
Open

fix(step): iterate a scalar foreach value once instead of crashing on falsy values#6722
yzxcj797 wants to merge 2 commits into
keephq:mainfrom
yzxcj797:fix/foreach-falsy-scalar-6721

Conversation

@yzxcj797

Copy link
Copy Markdown

Fixes #6721.

Summary

Step._get_foreach_items decided between "iterate a single resolved reference" and "zip() multiple &&-combined references" with the classic X and Y or Z idiom:

return len(foreach_items) == 1 and foreach_items[0] or zip(*foreach_items)

The idiom is only safe when Y is never falsy. A single reference that resolves to 0, False or None — e.g. foreach: "{{ steps.check-count.results }}" where the count is zero — fell through to zip(*foreach_items) = zip(0), raising TypeError: 'int' object is not iterable. A truthy non-iterable scalar was returned bare and crashed the same way one line later in _run_foreach's for item in items.

The fix wraps a non-iterable single value in a list so the action runs exactly once with the resolved value:

if len(foreach_items) == 1:
    value = foreach_items[0]
    if isinstance(value, Iterable):
        return value
    return [value]
return zip(*foreach_items)

Iterables (lists, dicts, strings) keep their existing semantics and the multi-reference zip path is untouched.

Testing

Added test_get_foreach_items_single_reference (parametrized over 0, False, None, 5, lists and the empty list) and test_get_foreach_items_multiple_references_zip to tests/test_steps.py, next to the existing Step unit tests with a mocked context manager.

Behavior differential of the expression (old vs new, values materialized):

single resolved value old new
0 / False / None TypeError from zip(...) [value] (one iteration)
5 5 (then TypeError in the consumer's for-loop) [5] (one iteration)
[1, 2], [], other iterables unchanged unchanged
multiple refs zip(...) unchanged

Note: I could not run the full tests/test_steps.py suite locally — the project requires Python ≥ 3.11 (local interpreter is 3.10) and the conftest import chain needs the full API environment. The added tests follow the existing unit-test pattern in the same file and run under the project's CI.

… falsy values

_get_foreach_items used the 'X and Y or Z' idiom to pick between a
single resolved reference and zip() over multiple '&&'-combined ones.
The idiom routes any falsy Y into the Z branch: a single reference
resolving to 0, False or None therefore hit zip(0) and raised
TypeError: 'int' object is not iterable. A truthy non-iterable scalar
was returned bare and crashed the same way one line later, in the
consumer's for-loop.

Wrap non-iterable single values in a list so the action runs once with
the resolved value; iterables (lists, dicts, strings) keep their
existing semantics, and the zip path for multiple references is
unchanged.

Fixes keephq#6721
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. Bug Something isn't working labels Aug 21, 2026
@CLAassistant

CLAassistant commented Aug 21, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ shahargl
❌ yzxcj797
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Something isn't working size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[🐛 Bug]: Step._get_foreach_items crashes with TypeError when a single foreach reference resolves to 0 or False

3 participants