Skip to content

argparse mx_group is required, when action value equal default will be ignore #74349

Closed

Description

@mlouielu
mannequin
BPO 30163
Nosy @mlouielu
Superseder
  • bpo-18943: argparse: default args in mutually exclusive groups
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2017-04-26.06:01:41.281>
    created_at = <Date 2017-04-26.02:33:05.582>
    labels = ['3.7', 'type-bug', 'library']
    title = 'argparse mx_group is required, when action value equal default will be ignore'
    updated_at = <Date 2017-04-27.10:06:35.609>
    user = 'https://github.com/mlouielu'

    bugs.python.org fields:

    activity = <Date 2017-04-27.10:06:35.609>
    actor = 'berker.peksag'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-04-26.06:01:41.281>
    closer = 'paul.j3'
    components = ['Library (Lib)']
    creation = <Date 2017-04-26.02:33:05.582>
    creator = 'louielu'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 30163
    keywords = []
    message_count = 6.0
    messages = ['292293', '292295', '292298', '292302', '292304', '292306']
    nosy_count = 2.0
    nosy_names = ['paul.j3', 'louielu']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '18943'
    type = 'behavior'
    url = 'https://bugs.python.org/issue30163'
    versions = ['Python 3.7']

    Activity

    1. mlouielu commented on Apr 26, 2017

      mlouielumannequin
      MannequinAuthor

      When adding mutually exclusive group and required is True, and the group argument has default value. If we type its default value, argparse will ignore the input and return argument is required

      ------- PoC --------

      import argparse
      
      parser = argparse.ArgumentParser()
      group = parser.add_mutually_exclusive_group(required=True)
      group.add_argument('-v', type=int, default=10)
      
      print(parser.parse_args())

      $ python tests.py -v 10
      usage: tests.py [-h] -v V
      tests.py: error: one of the arguments -v is required
      $ python tests.py -v 11
      Namespace(v=11)
    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Apr 26, 2017
    3. mlouielu commented on Apr 26, 2017

      mlouielumannequin
      MannequinAuthor

      Strange, this will only trigger when that argument type is int.

    4. mlouielu commented on Apr 26, 2017

      mlouielumannequin
      MannequinAuthor

      Maybe documentation should note that:
      """
      # error if this argument is not allowed with other previously
      # seen arguments, assuming that actions that use the default
      # value don't really count as "present"
      """

    5. paulj3 commented on Apr 26, 2017

      paulj3mannequin
      Mannequin

      And only for integers smaller than 257!

      The problem is with this test in take_action function

                  # error if this argument is not allowed with other previously
                  # seen arguments, assuming that actions that use the default
                  # value don't really count as "present"
                  if argument_values is not action.default:
                      seen_non_default_actions.add(action)
                      for conflict_action in action_conflicts.get(action, []):
                          if conflict_action in seen_non_default_actions:
                              msg = _('not allowed with argument %s')
                              action_name = _get_action_name(conflict_action)
                              raise ArgumentError(action, msg % action_name)

      Specifically with the

      argument_values is not action.default
      

      test. In CPython integers below 257 are unique, so that

      In [33]: int('10') is 10
      Out[33]: True
      In [34]: int('257') is 257
      Out[34]: False
      

      (and strings from 'sys.argv' do not match in the 'is' sense with strings set via 'default'. So the problem is unique to small integers.)

      The test is really meant to catch the default that is added in '_get_values()' for positionals with optional nargs ('?').

      This issue was raised by some PyPy developers several years ago.
      PyPy does not treat the small integers as unique. I'll have to dig around to see what the resolution was, if any.

      For now the solution is to use a string '10' as the default.

    6. paulj3 commented on Apr 26, 2017

      paulj3mannequin
      Mannequin

      This issue was raised in 2013. The proposed patch is still pending

      http://bugs.python.org/issue18943

      I'm going to close this new one, though it is a good reminder of a rare, but persistent bug.

    7. mlouielu commented on Apr 26, 2017

      mlouielumannequin
      MannequinAuthor

      Thanks, paul. Your msg help a lot.

      Will you work on bpo-18943? I can help for this or review the pending patch.

    8. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions