keep icon indicating copy to clipboard operation
keep copied to clipboard

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

Open Prabal864 opened this issue 3 months ago • 1 comments

Describe the bug

Step._get_foreach_items uses the classic Python X and Y or Z idiom to decide whether to iterate a single resolved value directly or zip() multiple &&-combined ones together:

# keep/step/step.py, line 130
return len(foreach_items) == 1 and foreach_items[0] or zip(*foreach_items)

This idiom is only safe when Y (foreach_items[0]) is never falsy. But foreach_items[0] is whatever a workflow's foreach: "{{ ... }}" reference resolves to at runtime — and if that's a legitimate falsy scalar like 0 or False (e.g. a count that happens to be zero), the expression falls through to zip(*foreach_items) even though there's only one item, and zip() requires an iterable argument. Since 0/False aren't iterable, this crashes with a TypeError instead of doing anything sensible with the resolved value.

To Reproduce

With a real workflow:

steps:
  - name: check-count
    provider:
      type: mock
      with:
        results: 0   # or any step whose output can legitimately be 0
actions:
  - name: notify
    foreach: "{{ steps.check-count.results }}"
    provider:
      type: ...

Running this step raises TypeError: 'int' object is not iterable (caught by Step.run()'s try/except and re-raised as ActionError), instead of treating a single falsy value the same way a single truthy value would be handled.

To confirm this without needing a full workflow run, here's the exact return statement extracted and exercised standalone:

repro_foreach_bug.py
def get_foreach_items(foreach_items):
    """Verbatim logic of Step._get_foreach_items's return statement."""
    if not foreach_items:
        return []
    return len(foreach_items) == 1 and foreach_items[0] or zip(*foreach_items)


print("foreach_items[0] = 0 (e.g. foreach: \"{{ steps.check-count.results }}\" resolving to 0):")
try:
    print("  result:", list(get_foreach_items([0])))
except TypeError as e:
    print("  CRASHED:", e)

print("foreach_items[0] = False:")
try:
    print("  result:", list(get_foreach_items([False])))
except TypeError as e:
    print("  CRASHED:", e)

print("foreach_items[0] = [1, 2, 3] (normal case, for comparison):")
print("  result:", list(get_foreach_items([[1, 2, 3]])))

Output:

foreach_items[0] = 0 (e.g. foreach: "{{ steps.check-count.results }}" resolving to 0):
  CRASHED: 'int' object is not iterable
foreach_items[0] = False:
  CRASHED: 'bool' object is not iterable
foreach_items[0] = [1, 2, 3] (normal case, for comparison):
  result: [1, 2, 3]

Expected behavior

Whether foreach_items[0] is truthy or falsy shouldn't change which branch of the len(foreach_items) == 1 check is taken — that decision should depend only on how many &&-combined references were in the foreach config, not on the resolved value of the single one. A falsy-but-real resolved value (0, False, "") shouldn't crash the step with an unrelated TypeError about zip().

Additional context

  • Verified against upstream/main at 4003992272eeddd649e23e21d63f09892a149291.
  • Searched existing issues/PRs for foreach, _get_foreach_items, and not iterable — the closest hit is #5016 (closed, fixed by #5061), which is a different bug (a shared/exhausted iterator between two separate foreach steps consuming the same step's results) — that PR didn't touch this line.
  • Small fix: replace the and/or idiom with an explicit conditional, e.g. foreach_items[0] if len(foreach_items) == 1 else zip(*foreach_items), which doesn't have this footgun since it branches on len(foreach_items) == 1 directly rather than on the truthiness of foreach_items[0]. Happy to send a PR for this if useful.

Prabal864 avatar Aug 21 '26 19:08 Prabal864

@Prabal864 the bug is confirmed. Line 125 of keep/step/step.py contains exactly the problematic idiom:

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

When foreach_items[0] is 0, False, "", or any other falsy-but-valid value, the and/or short-circuit causes the expression to fall through to zip(*foreach_items) — which then crashes with TypeError because scalars aren't iterable.

Your suggested fix is exactly right. Replacing it with an explicit conditional:

return foreach_items[0] if len(foreach_items) == 1 else zip(*foreach_items)

branches solely on the count of &&-combined references, not on the truthiness of the resolved value. A PR with that one-line change would be a minimal, safe fix with no behavioral change for the happy path.

To reply, just mention @dosu.


Docs are dead. Just use Dosu.

Leave Feedback Ask Dosu about keep Share Dosu with your team

dosubot[bot] avatar Aug 21 '26 19:08 dosubot[bot]