Skip to content

action="append" on a list-typed field wraps each appended value in an extra list #366

Description

@carlosgmartin

Describe the bug
simple_parsing silently produces a nested (corrupted) result when a list-annotated dataclass field uses action="append", instead of raising or producing a flat list.

To Reproduce

import dataclasses
import simple_parsing

@dataclasses.dataclass
class Args:
    header: list[str] = simple_parsing.field(default_factory=list, action="append")

print(simple_parsing.parse(Args, args=["--header", "Auth", "--header", "Accept"]))

Expected behavior

Args(header=['Auth', 'Accept'])

Actual behavior

Args(header=[['Auth'], ['Accept']])

Desktop (please complete the following information):

  • Version: 0.1.9
  • Python version: 3.14.6

Additional context

Root cause: In simple_parsing/wrappers/field_wrapper.py, the is_list branch that builds add_argument kwargs unconditionally sets nargs="*", regardless of the requested action.

That's correct for the default store action (nargs="*" collects all tokens from one occurrence into the list), but wrong for action="append": each occurrence should be parsed as a single element, and AppendAction appends that element to a running list.

Because nargs="*" is still forced, each single --header Auth occurrence gets bundled into its own one-item list (["Auth"]) before AppendAction appends it, yielding a list of one-item lists.

No error is raised, so this fails silently.

Confirmed workaround: explicitly passing nargs=None overrides the bad default and produces the correct flat list:

header: list[str] = simple_parsing.field(default_factory=list, action="append", nargs=None)

This suggests the fix is for the is_list branch to skip (or set differently) nargs="*" when action is "append" (and possibly "extend").

Activity

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

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions