[🐛 Bug]: IOHandler silently drops empty list/dict/tuple (and literal False) arguments to keep.* template functions
Describe the bug
IOHandler's template-function-call parser (keep/iohandler/iohandler.py, inside _parse_token's nested _parse()) builds the positional-argument list for a keep.* function call with this guard:
# keep/iohandler/iohandler.py, lines 334-343
# if the value is empty '', we still need to pass it to the function
# also, if the value is 0 or 0.0, we need to pass it to the function
# 0 == False, so we need to check if the value is not False explicitly
if (
_arg
or _arg == ""
or (_arg == 0 or _arg == 0.0)
and _arg is not False
):
_args.append(_arg)
The comment says this exists specifically so "" and 0/0.0 aren't dropped as "empty" - but the guard only special-cases those two, and misses:
- Empty containers. A literal
[],{}, or()argument (parsed a few lines above viaast.literal_eval/astunparse.unparse, producing a real emptylist/dict/tupleobject) is falsy, not== "", and not== 0/0.0- so it's silently dropped from_argsinstead of being passed to the function. - Literal
False.False == 0isTruein Python, so(_arg == 0 or _arg == 0.0)isTruefor_arg is Falsetoo - but the guard then requiresand _arg is not False, which evaluates toFalse, so the whole condition isFalseand a literalFalseargument is also silently dropped.
To Reproduce
The exact guard, copied verbatim, exercised against the real ast-based parsing used for []/{}/()/False template-function arguments:
import ast
def compute_arg(arg):
if isinstance(arg, ast.Dict):
return ast.literal_eval(arg)
if isinstance(arg, (ast.Set, ast.List, ast.Tuple)):
return ast.literal_eval(ast.unparse(arg).strip())
if isinstance(arg, ast.Constant):
return arg.value # covers the literal `False` case
return arg.id
def parse_args(src):
tree = ast.parse(src, mode="eval").body
_args = []
for arg in tree.args:
_arg = compute_arg(arg)
# === exact guard from iohandler.py lines 337-343 ===
if _arg or _arg == "" or (_arg == 0 or _arg == 0.0) and _arg is not False:
_args.append(_arg)
# =====================================================
return _args
for src in ["f([], 'x')", "f({}, 'x')", "f((), 'x')", "f([1, 2], 'x')"]:
print(src, "->", parse_args(src))
Output:
f([], 'x') -> ['x'] # BUG: the [] vanished
f({}, 'x') -> ['x'] # BUG: the {} vanished
f((), 'x') -> ['x'] # BUG: the () vanished
f([1, 2], 'x') -> [[1, 2], 'x'] # correct: non-empty list preserved
Expected behavior
Any argument that was actually parsed to a real value - including [], {}, (), and False - should be passed through to the function positionally. Only a genuinely-unparseable/no-value case should be dropped.
Additional context
- Verified against
keep/iohandler/iohandler.pyon currentmain(commit range including through PR #6710 /f35afdd). - Failure scenario: any workflow YAML step calling a
keep.*template function where one positional argument is a field/step-output that legitimately renders to an empty list or dict (or the literalFalse), e.g. a literal{{ keep.some_function([], "fallback") }}, or a variable that resolves to[]/{}/Falseat runtime. The empty/Falseargument silently vanishes, which shifts every subsequent positional argument one slot to the left - either raising a confusingTypeErrorabout missing/extra arguments deep insidekeep.functions, or (worse) silently binding the wrong value to the wrong parameter with no error at all. - Small, safe fix: since
_argis only ever left as itsNonesentinel default when none of the parsing branches produced a value (e.g. a nestedkeep.*call that itself returned nothing), the guard can simply becomeif _arg is not None:- which correctly keeps"",0,0.0,False,[],{}, and(), and only drops the genuine "nothing was parsed" case. Happy to send a PR for this.
Great analysis, @Prabal864! I confirmed the code and the bug is real — though the guard in the current main is slightly simpler than the one you quoted. The actual line is [1]:
# if the value is empty '', we still need to pass it to the function
if _arg or _arg == "":
_args.append(_arg)
This still has the same core problem: any falsy _arg that isn't "" — including [], {}, (), False, and 0/0.0 — is silently dropped.
Your proposed fix (if _arg is not None:) is the right one. _arg is initialized to None on line 261 and is only left as None when no parsing branch matched (e.g. a nested keep.* call returned nothing), so is not None precisely captures "was parsed to a real value" vs. "nothing was produced." The fix would change line 297 to:
if _arg is not None:
_args.append(_arg)
Please do send that PR — it's a clean, minimal, and correct fix. It would also be worth adding tests covering keep.* template function calls with [], {}, (), False, and 0 arguments to prevent regressions.
To reply, just mention @dosu.
Share context across your team and agents. Try Dosu.