Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

argparse metavar parentheses dropped on usage line #101979

Closed
tonyle72 opened this issue Feb 17, 2023 · 0 comments
Closed

argparse metavar parentheses dropped on usage line #101979

tonyle72 opened this issue Feb 17, 2023 · 0 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@tonyle72
Copy link

tonyle72 commented Feb 17, 2023

Parentheses in argparse metavars are dropped on the usage line but appear as expected in the argument list. In particular, the very first and very last parentheses disappear. It looks from other issues (e.g. #56083) that braces, brackets and parentheses in metavars are problematic, so this may be part of a wider and more difficult set of puzzlers to fix.

Illustrative code:

#!/usr/bin/python3
"""Demonstrate parenthesis drops in argparse metavar."""
import argparse

# The very first and last parentheses disappear in the usage line, but are
# present in the argument list.
parser = argparse.ArgumentParser(
    description='Demonstrate parenthesis drops in argument meta-descriptions')
parser.add_argument(
    'positional',
    help='positional argument',
    metavar='(example) positional')
parser.add_argument(
    '-p',
    '--optional',
    help='optional argument',
    type=int,
    choices=[1, 2],
    metavar='{1 (option A), 2 (option B)}',
    default=1)
arguments = parser.parse_args()

print(arguments)

When this is run with -h, the help text is rendered as shown below. Note the parentheses are missing before option A and after example:

usage: parens.py [-h] [-p {1 option A), 2 (option B)}] (example positional

Demonstrate parenthesis drops in argument meta-descriptions

positional arguments:
  (example) positional  positional argument

optional arguments:
  -h, --help            show this help message and exit
  -p {1 (option A), 2 (option B)}, --optional {1 (option A), 2 (option B)}
                        optional argument

I've tried this on Python 3.8.10 and Python 3.10.6.

Linked PRs

@tonyle72 tonyle72 added the type-bug An unexpected behavior, bug, or error label Feb 17, 2023
@arhadthedev arhadthedev added the stdlib Python modules in the Lib dir label Feb 17, 2023
JelleZijlstra pushed a commit that referenced this issue Mar 5, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 5, 2023
…ument of add_argument() were dropped (pythonGH-102318)

(cherry picked from commit 9a478be)

Co-authored-by: Yeojin Kim <yeojin.dev@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 5, 2023
…ument of add_argument() were dropped (pythonGH-102318)

(cherry picked from commit 9a478be)

Co-authored-by: Yeojin Kim <yeojin.dev@gmail.com>
miss-islington added a commit that referenced this issue Mar 5, 2023
…of add_argument() were dropped (GH-102318)

(cherry picked from commit 9a478be)

Co-authored-by: Yeojin Kim <yeojin.dev@gmail.com>
miss-islington added a commit that referenced this issue Mar 5, 2023
…of add_argument() were dropped (GH-102318)

(cherry picked from commit 9a478be)

Co-authored-by: Yeojin Kim <yeojin.dev@gmail.com>
@corona10 corona10 closed this as completed Mar 6, 2023
hugovk pushed a commit to hugovk/cpython that referenced this issue Mar 6, 2023
carljm added a commit to carljm/cpython that referenced this issue Mar 6, 2023
* main: (21 commits)
  pythongh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives in sub interpreters module (python#102472)
  pythongh-95672: Fix versionadded indentation of get_pagesize in test.rst (pythongh-102455)
  pythongh-102416: Do not memoize incorrectly loop rules in the parser (python#102467)
  pythonGH-101362: Optimise PurePath(PurePath(...)) (pythonGH-101667)
  pythonGH-101362: Check pathlib.Path flavour compatibility at import time (pythonGH-101664)
  pythonGH-101362: Call join() only when >1 argument supplied to pathlib.PurePath() (python#101665)
  pythongh-102444: Fix minor bugs in `test_typing` highlighted by pyflakes (python#102445)
  pythonGH-102341: Improve the test function for pow (python#102342)
  Fix unused classes in a typing test (pythonGH-102437)
  pythongh-101979: argparse: fix a bug where parentheses in metavar argument of add_argument() were dropped (python#102318)
  pythongh-102356: Add thrashcan macros to filter object dealloc (python#102426)
  Move around example in to_bytes() to avoid confusion (python#101595)
  pythonGH-97546: fix flaky asyncio `test_wait_for_race_condition` test (python#102421)
  pythongh-96821: Add config option `--with-strict-overflow` (python#96823)
  pythongh-101992: update pstlib module documentation (python#102133)
  pythongh-63301: Set exit code when tabnanny CLI exits on error (python#7699)
  pythongh-101863: Fix wrong comments in EUC-KR codec (pythongh-102417)
  pythongh-102302 Micro-optimize `inspect.Parameter.__hash__` (python#102303)
  pythongh-102179: Fix `os.dup2` error reporting for negative fds (python#102180)
  pythongh-101892: Fix `SystemError` when a callable iterator call exhausts the iterator (python#101896)
  ...
carljm added a commit to carljm/cpython that referenced this issue Mar 7, 2023
* main: (37 commits)
  pythongh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives in sub interpreters module (python#102472)
  pythongh-95672: Fix versionadded indentation of get_pagesize in test.rst (pythongh-102455)
  pythongh-102416: Do not memoize incorrectly loop rules in the parser (python#102467)
  pythonGH-101362: Optimise PurePath(PurePath(...)) (pythonGH-101667)
  pythonGH-101362: Check pathlib.Path flavour compatibility at import time (pythonGH-101664)
  pythonGH-101362: Call join() only when >1 argument supplied to pathlib.PurePath() (python#101665)
  pythongh-102444: Fix minor bugs in `test_typing` highlighted by pyflakes (python#102445)
  pythonGH-102341: Improve the test function for pow (python#102342)
  Fix unused classes in a typing test (pythonGH-102437)
  pythongh-101979: argparse: fix a bug where parentheses in metavar argument of add_argument() were dropped (python#102318)
  pythongh-102356: Add thrashcan macros to filter object dealloc (python#102426)
  Move around example in to_bytes() to avoid confusion (python#101595)
  pythonGH-97546: fix flaky asyncio `test_wait_for_race_condition` test (python#102421)
  pythongh-96821: Add config option `--with-strict-overflow` (python#96823)
  pythongh-101992: update pstlib module documentation (python#102133)
  pythongh-63301: Set exit code when tabnanny CLI exits on error (python#7699)
  pythongh-101863: Fix wrong comments in EUC-KR codec (pythongh-102417)
  pythongh-102302 Micro-optimize `inspect.Parameter.__hash__` (python#102303)
  pythongh-102179: Fix `os.dup2` error reporting for negative fds (python#102180)
  pythongh-101892: Fix `SystemError` when a callable iterator call exhausts the iterator (python#101896)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
Status: Doc issues
Development

No branches or pull requests

3 participants