GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-96310: Fix a traceback in argparse when all options in a mutually exclusive group are suppressed by dmach · Pull Request #96311 · python/cpython

Conversation

@dmach

@dmach dmach commented Aug 26, 2022 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Reproducer depends on terminal size - the traceback occurs when there's
an option long enough so the usage line doesn't fit the terminal width.
Option order is also important for reproducibility.

Excluding empty groups (with all options suppressed) from inserts
fixes the problem.

  • Issue: argparse sometimes tracebacks when all options in a mutually exclusive group are suppressed #96310

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot

bedevere-bot commented Aug 26, 2022

Copy link
Copy Markdown

Most changes to Python require a NEWS entry.

Please add it using the blurb_it web app or the blurb command-line tool.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot added the awaiting review label Aug 26, 2022
@ghost

ghost commented Aug 26, 2022 •
edited by ghost
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.
CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@dmach
pythongh-96310: Fix a traceback in argparse when all options in a mut…
616c5fe
…ually exclusive group are suppressed

Reproducer depends on terminal size - the traceback occurs when there's
an option long enough so the usage line doesn't fit the terminal width.
Option order is also important for reproducibility.

Excluding empty groups (with all options suppressed) from inserts
fixes the problem.
@dmach
dmach force-pushed the fix-argparse-subparser_all_mutually_exclusive_group_members_suppressed branch from 0d6958c to 616c5fe Compare August 26, 2022 13:52
@dmach dmach changed the title gh-96310: Fix a traceback when all options in a mutually exclusive group are suppressed gh-96310: Fix a traceback in argparse when all options in a mutually exclusive group are suppressed Aug 26, 2022
hamdanal added a commit to hamdanal/cpython that referenced this pull request May 28, 2023
@hamdanal
Simplify argparse usage formatting
4336ac7
Rationale
=========

argparse performs a complex formatting of the usage for argument grouping
and for line wrapping to fit the terminal width. This formatting has been
a constant source of bugs for at least 10 years (see linked issues below)
where defensive assertion errors are triggered or brackets and paranthesis
are not properly handeled.

Problem
=======

The current implementation of argparse usage formatting relies on regular
expressions to group arguments usage only to separate them again later
with another set of regular expressions. This is a complex and error prone
approach that caused all the issues linked below. Special casing certain
argument formats has not solved the problem. The following are some of
the most common issues:
- empty `metavar`
- mutually exclusive groups with `SUPPRESS`ed arguments
- metavars with whitespace
- metavars with brackets or paranthesis

Solution
========

The following two comments summarize the solution:
- python#82091 (comment)
- python#77048 (comment)

Mainly, the solution is to rewrite the usage formatting to avoid the
group-then-separate approach. Instead, the usage parts are kept separate
and only joined together at the end. This allows for a much simpler
implementation that is easier to understand and maintain. It avoids the
regular expressions approach and fixes the corresponding issues.

This closes the following issues:
- Closes python#62090
- Closes python#62549
- Closes python#77048
- Closes python#82091
- Closes python#89743
- Closes python#96310
- Closes python#98666

These PRs become obsolete:
- Closes python#15372
- Closes python#96311
@hamdanal hamdanal mentioned this pull request May 28, 2023
gh-62090: Simplify argparse usage formatting #105039
Merged
@serhiy-storchaka
Merge branch 'main' into fix-argparse-subparser_all_mutually_exclusiv…
fa702d0
…e_group_members_suppressed
@bedevere-app bedevere-app Bot mentioned this pull request Feb 21, 2024
argparse sometimes tracebacks when all options in a mutually exclusive group are suppressed #96310
Closed
@serhiy-storchaka
Polishing.
3b9b636
serhiy-storchaka
serhiy-storchaka approved these changes Feb 21, 2024
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Feb 21, 2024
@serhiy-storchaka serhiy-storchaka added needs backport to 3.11 only security fixes needs backport to 3.12 only security fixes labels Feb 21, 2024
@serhiy-storchaka
serhiy-storchaka enabled auto-merge (squash) February 21, 2024 13:36
@serhiy-storchaka

serhiy-storchaka commented Feb 21, 2024

Copy link
Copy Markdown
Member

Thank you for your contribution @dmath and sorry for the delay. I resolved the merge conflict and made some simple tweaks.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka
serhiy-storchaka merged commit 5f7df88 into python:main Feb 21, 2024
@miss-islington-app

miss-islington-app Bot commented Feb 21, 2024

Copy link
Copy Markdown

Thanks @dmach for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11, 3.12.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Feb 21, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Feb 21, 2024
@dmach @miss-islington
pythongh-96310: Fix a traceback in argparse when all options in a mut…
7c16831
…ually exclusive group are suppressed (pythonGH-96311)

Reproducer depends on terminal size - the traceback occurs when there's
an option long enough so the usage line doesn't fit the terminal width.
Option order is also important for reproducibility.

Excluding empty groups (with all options suppressed) from inserts
fixes the problem.
(cherry picked from commit 5f7df88)

Co-authored-by: Daniel Mach <[email protected]>
@bedevere-app

bedevere-app Bot commented Feb 21, 2024

Copy link
Copy Markdown

GH-115767 is a backport of this pull request to the 3.12 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Feb 21, 2024
@dmach @miss-islington
pythongh-96310: Fix a traceback in argparse when all options in a mut…
0e71ea4
…ually exclusive group are suppressed (pythonGH-96311)

Reproducer depends on terminal size - the traceback occurs when there's
an option long enough so the usage line doesn't fit the terminal width.
Option order is also important for reproducibility.

Excluding empty groups (with all options suppressed) from inserts
fixes the problem.
(cherry picked from commit 5f7df88)

Co-authored-by: Daniel Mach <[email protected]>
@bedevere-app bedevere-app Bot removed the needs backport to 3.12 only security fixes label Feb 21, 2024
@bedevere-app

bedevere-app Bot commented Feb 21, 2024

Copy link
Copy Markdown

GH-115768 is a backport of this pull request to the 3.11 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.11 only security fixes label Feb 21, 2024
serhiy-storchaka pushed a commit that referenced this pull request Feb 21, 2024
@miss-islington @dmach
[3.11] gh-96310: Fix a traceback in argparse when all options in a mu…
a27c540
…tually exclusive group are suppressed (GH-96311) (GH-115768)

Reproducer depends on terminal size - the traceback occurs when there's
an option long enough so the usage line doesn't fit the terminal width.
Option order is also important for reproducibility.

Excluding empty groups (with all options suppressed) from inserts
fixes the problem.
(cherry picked from commit 5f7df88)

Co-authored-by: Daniel Mach <[email protected]>
serhiy-storchaka pushed a commit that referenced this pull request Feb 21, 2024
@miss-islington @dmach
[3.12] gh-96310: Fix a traceback in argparse when all options in a mu…
dae7341
…tually exclusive group are suppressed (GH-96311) (GH-115767)

Reproducer depends on terminal size - the traceback occurs when there's
an option long enough so the usage line doesn't fit the terminal width.
Option order is also important for reproducibility.

Excluding empty groups (with all options suppressed) from inserts
fixes the problem.
(cherry picked from commit 5f7df88)

Co-authored-by: Daniel Mach <[email protected]>
woodruffw pushed a commit to woodruffw-forks/cpython that referenced this pull request Mar 4, 2024
@dmach @woodruffw
pythongh-96310: Fix a traceback in argparse when all options in a mut…
7c42ba2
…ually exclusive group are suppressed (pythonGH-96311)

Reproducer depends on terminal size - the traceback occurs when there's
an option long enough so the usage line doesn't fit the terminal width.
Option order is also important for reproducibility.

Excluding empty groups (with all options suppressed) from inserts
fixes the problem.
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 17, 2024
@dmach @diegorusso
pythongh-96310: Fix a traceback in argparse when all options in a mut…
97c6c57
…ually exclusive group are suppressed (pythonGH-96311)

Reproducer depends on terminal size - the traceback occurs when there's
an option long enough so the usage line doesn't fit the terminal width.
Option order is also important for reproducibility.

Excluding empty groups (with all options suppressed) from inserts
fixes the problem.
encukou pushed a commit that referenced this pull request May 7, 2024
@hamdanal
gh-62090: Simplify argparse usage formatting (GH-105039)
de1428f
Rationale
=========

argparse performs a complex formatting of the usage for argument grouping
and for line wrapping to fit the terminal width. This formatting has been
a constant source of bugs for at least 10 years (see linked issues below)
where defensive assertion errors are triggered or brackets and paranthesis
are not properly handeled.

Problem
=======

The current implementation of argparse usage formatting relies on regular
expressions to group arguments usage only to separate them again later
with another set of regular expressions. This is a complex and error prone
approach that caused all the issues linked below. Special casing certain
argument formats has not solved the problem. The following are some of
the most common issues:
- empty `metavar`
- mutually exclusive groups with `SUPPRESS`ed arguments
- metavars with whitespace
- metavars with brackets or paranthesis

Solution
========

The following two comments summarize the solution:
- #82091 (comment)
- #77048 (comment)

Mainly, the solution is to rewrite the usage formatting to avoid the
group-then-separate approach. Instead, the usage parts are kept separate
and only joined together at the end. This allows for a much simpler
implementation that is easier to understand and maintain. It avoids the
regular expressions approach and fixes the corresponding issues.

This closes the following GitHub issues:
-  #62090
-  #62549
-  #77048
-  #82091
-  #89743
-  #96310
-  #98666

These PRs become obsolete:
-  #15372
-  #96311
@encukou encukou mentioned this pull request May 7, 2024
argparse: mutually exclusive groups full of help-suppressed args can cause AssertionErrors #62090
Closed
SonicField pushed a commit to SonicField/cpython that referenced this pull request May 8, 2024
@hamdanal @SonicField
pythongh-62090: Simplify argparse usage formatting (pythonGH-105039)
8047c0b
Rationale
=========

argparse performs a complex formatting of the usage for argument grouping
and for line wrapping to fit the terminal width. This formatting has been
a constant source of bugs for at least 10 years (see linked issues below)
where defensive assertion errors are triggered or brackets and paranthesis
are not properly handeled.

Problem
=======

The current implementation of argparse usage formatting relies on regular
expressions to group arguments usage only to separate them again later
with another set of regular expressions. This is a complex and error prone
approach that caused all the issues linked below. Special casing certain
argument formats has not solved the problem. The following are some of
the most common issues:
- empty `metavar`
- mutually exclusive groups with `SUPPRESS`ed arguments
- metavars with whitespace
- metavars with brackets or paranthesis

Solution
========

The following two comments summarize the solution:
- python#82091 (comment)
- python#77048 (comment)

Mainly, the solution is to rewrite the usage formatting to avoid the
group-then-separate approach. Instead, the usage parts are kept separate
and only joined together at the end. This allows for a much simpler
implementation that is easier to understand and maintain. It avoids the
regular expressions approach and fixes the corresponding issues.

This closes the following GitHub issues:
-  python#62090
-  python#62549
-  python#77048
-  python#82091
-  python#89743
-  python#96310
-  python#98666

These PRs become obsolete:
-  python#15372
-  python#96311
LukasWoodtli pushed a commit to LukasWoodtli/cpython that referenced this pull request Jan 22, 2025
@dmach @LukasWoodtli
pythongh-96310: Fix a traceback in argparse when all options in a mut…
7ebe28b
…ually exclusive group are suppressed (pythonGH-96311)

Reproducer depends on terminal size - the traceback occurs when there's
an option long enough so the usage line doesn't fit the terminal width.
Option order is also important for reproducibility.

Excluding empty groups (with all options suppressed) from inserts
fixes the problem.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@serhiy-storchaka serhiy-storchaka serhiy-storchaka approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@dmach @bedevere-bot @serhiy-storchaka
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.