Skip to content

In unit tests, specifying the --target-version flag raises AttributeError: 'tuple' object has no attribute 'append'. #4721

Description

@ranjodhsingh1729

Describe the bug

In unit tests, specifying the --target-version flag raises AttributeError: 'tuple' object has no attribute 'append'.

To Reproduce

Just add # flags: --target-version=py310 on top of any unit test and then run with tox -e py.

The resulting error is:

AttributeError: 'tuple' object has no attribute 'append'.

Expected behavior

The unit test should run successfully with target_versions set to exactly what was specified.

Environment

  • Black's version: main at bdd7fcd (latest as of now)
  • OS and Python version: Ubuntu 24.04 LTS, Python 3.12 and Python 3.14 (checked on both)

Additional context

The error occurs due to this code:

parser.add_argument(
    "--target-version",
    action="append",
    type=lambda val: TargetVersion[val.upper()],
    default=(),
)

The issue arises because default is a tuple, but action="append" expects a list. When the flag is used, it attempts to call .append() on the tuple, leading to an AttributeError.

A possible fix is:

parser.add_argument(
    "--target-version",
    action="store",
    type=lambda val: (TargetVersion[val.upper()],),
    default=(),
)

Alternatively, setting default=[] would also work with action="append".
However, the original use of a tuple as the default was likely intentional.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T: bugSomething isn't working
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions