Skip to content

argparse.parse_args should have a generic return type #2366

@keysmashes

Description

@keysmashes

(sorry if generic is the wrong word to describe this)

argparse.parse_args(namespace=MyNamespace()) returns an instance of MyNamespace, but it's annotated to always return an argparse.Namespace. This means that in order to have type-safety when accessing arguments by using a custom type-annotated class:

class Arguments:
    verbose: bool
    ...
args = argparse.parse_args(namespace=Arguments())

you currently need to cast the return value of argparse.parse_args.

Also, the passed object doesn't actually have to be a (subclass of) argparse.Namespace – a simple subclass of object is documented to work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions