Skip to content

Replace mutable default list with None sentinel in basic_collation_fn#84

Open
lonexreb wants to merge 1 commit intoNVlabs:mainfrom
lonexreb:fix/qwen-processor-mutable-default-arg
Open

Replace mutable default list with None sentinel in basic_collation_fn#84
lonexreb wants to merge 1 commit intoNVlabs:mainfrom
lonexreb:fix/qwen-processor-mutable-default-arg

Conversation

@lonexreb
Copy link
Copy Markdown
Contributor

@lonexreb lonexreb commented May 4, 2026

Problem

basic_collation_fn in src/alpamayo_r1/processor/qwen_processor.py uses a mutable list as the default for unstackable_keys:

def basic_collation_fn(batch, unstackable_keys: list[str] = []):

Mutable default arguments are a well-known Python pitfall — the empty list is created once at function-definition time and reused across every call, so any mutation persists across invocations. Even though the current body only iterates over the argument (no mutation today), this pattern is brittle to future edits and is flagged by ruff B006 / pylint W0102.

Fix

Switch to the standard list[str] | None = None pattern and treat None as "no extra unstackable keys":

def basic_collation_fn(batch, unstackable_keys: list[str] | None = None):
    ...
    for k in unstackable_keys or []:
        ...

Two-line change. Behavior is identical for every call site (the default is still effectively an empty iterable). The function is exposed at module level, so callers passing an explicit list continue to work unchanged.

Verification

  • unstackable_keys=NoneNone or [] == [] → loop is skipped (matches old behavior).
  • unstackable_keys=["foo"] → loop runs over ["foo"] (matches old behavior).
  • unstackable_keys=[] → loop is skipped (matches old behavior).

Mutable default arguments are a well-known Python pitfall: the empty
list is created once at function definition and reused across every
call, so any mutation persists across calls. While the current body
only iterates over `unstackable_keys` (no mutation), keeping the
mutable default makes the function fragile to future edits and
silently warned by linters (ruff B006).

Switch to the standard `list[str] | None = None` pattern and treat
None as "no extra unstackable keys" via `unstackable_keys or []` at
the iteration site. Behavior is unchanged.

Signed-off-by: lonexreb <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant