Skip to content

argparse: _AttributeHolder repr() prints out positional arguments twice #91622

@achhina

Description

@achhina

Bug Report
Overriding _get_args() to give positional arguments for classes subclassing _AttributeHolder get printed out twice.

Example of a public API Namespace which inherits from _AttributeHolder:

from argparse import Namespace


class Foo(Namespace):

    def __init__(self, bar, baz='baz'):
        self.bar = bar
        self.baz = baz

    def _get_args(self):
        return [self.bar]


print(Foo('bar'))

# Output
>>> Foo('bar', bar='bar', baz='baz')

Proposal
I was conflicted in calling this a bug, as when going through argparse this seemed more like unused code. Currently, _get_args() is unused throughout the codebase, also found no matches when searching through GitHub, and as it's private it should be safe to remove. However, to be safe, I propose to deprecate _get_args() in 3.11 and remove it at a later point.

Here's a patch to the above proposal: PR-91624

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
    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