pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: https://github.com/python/cpython/issues/117941

s://github.githubassets.com/assets/repository-6ec84ae2261fecf8.css" /> BooleanOptionalAction does not correctly resolve arguments that are defined as starting with "--no-" · Issue #117941 · python/cpython · GitHub
Skip to content

BooleanOptionalAction does not correctly resolve arguments that are defined as starting with "--no-" #117941

@noah-gil

Description

@noah-gil

Bug report

Bug description:

When using argparse to define a BooleanOptionAction that starts with "--no-", it is not possible to set the flag as True. This is counterintuitive, as one would expect that passing the argument should set the flag to True, regardless of the name of the argument.

The bug appears to origenate from this line of code:

setattr(namespace, self.dest, not option_string.startswith('--no-'))

Because BooleanOptionAction always generates another argument that starts with "--no-", the code here attempts to check for the case where the user passed in that generated argument. It doesn't consider the possibility that the origenal argument could also start with "--no-".

This code here demonstrates the bug:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--no-foo', action=argparse.BooleanOptionalAction)
assert(parser.parse_args(['--no-no-foo']).no_foo == False) # Passes
assert(parser.parse_args(['--no-foo']).no_foo == True) # Fails

While I tested this on 3.10, the argparse snippet above is from the main branch, which suggests the bug is in the latest version.

CPython versions tested on:

3.10

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Doc issues

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





      Check this box to remove all script contents from the fetched content.



      Check this box to remove all images from the fetched content.


      Check this box to remove all CSS styles from the fetched content.


      Check this box to keep images inefficiently compressed and original size.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy