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-133653: Fix subclassing `HelpFormatter` by hugovk · Pull Request #133668 · python/cpython

Conversation

@hugovk

@hugovk hugovk commented May 8, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

The problem was with this code:

        if isinstance(self.formatter_class, type) and issubclass(
            self.formatter_class, HelpFormatter
        ):
            return self.formatter_class(
                prog=self.prog,
                prefix_chars=self.prefix_chars,
                color=self.color,
            )
        else:
            return self.formatter_class(prog=self.prog)

We want to set the extra arguments for HelpFormatter and argparse's own subclasses, which don't have their own __init__s.

But many third-party subclasses do have an __init__ and don't pass on kwargs. So we can't just check for a subclass of HelpFormatter, so let's try/except instead.

  • Issue: argparse.HelpFormatter subclasses may be broken by 3.14 changes #133653

Sorry, something went wrong.

Uh oh!

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

All reactions
@hugovk
Fix subclassing HelpFormatter
b1c0c60
@hugovk
hugovk requested a review from savannahostrowski as a code owner May 8, 2025 13:32
@hugovk hugovk added the needs backport to 3.14 bugs and security fixes label May 8, 2025
@bedevere-app bedevere-app Bot mentioned this pull request May 8, 2025
argparse.HelpFormatter subclasses may be broken by 3.14 changes #133653
Closed
@bedevere-app bedevere-app Bot added the awaiting core review label May 8, 2025
@hugovk
Fix Sphinx warning
fa4d044
iritkatriel
iritkatriel reviewed May 8, 2025
View reviewed changes
Comment thread Lib/argparse.py Outdated
color=self.color,
)
else:
except TypeError:

@iritkatriel iritkatriel May 8, 2025

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.

TypeError seems a bit generic (you're calling user code).

What do you think about this?

len([v.kind for (k, v) in inspect.signature(self.formatter_class).parameters.items() if k in ('y', 'z') and v.kind in (inspect._ParameterKind.POSITIONAL_OR_KEYWORD, inspect._ParameterKind.KEYWORD_ONLY)]) == 2

Sorry, something went wrong.

Uh oh!

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

All reactions

@hugovk hugovk May 8, 2025

Copy link
Copy Markdown
Member Author

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.

That doesn't work: FAILED (failures=2, errors=156)

Sorry, something went wrong.

Uh oh!

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

All reactions

@hugovk hugovk May 8, 2025

Copy link
Copy Markdown
Member Author

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.

Wait, it does, obviously need to replace y and z :)

Sorry, something went wrong.

Uh oh!

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

iritkatriel reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@hugovk
Big if
91c1f4f
iritkatriel
iritkatriel approved these changes May 8, 2025
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels May 8, 2025
serhiy-storchaka
serhiy-storchaka reviewed May 8, 2025
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.

I would not like to use inspect.signature() here. It is not reliable. If it fails (or return incorrect result), you cannot do anything with this. It can only be used in interactive introspection, when errors can be ignored.

Sorry, something went wrong.

Uh oh!

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

All reactions
@hugovk

hugovk commented May 8, 2025

Copy link
Copy Markdown
Member Author

Would you prefer the try/except or another approach?

All reactions

Sorry, something went wrong.

Uh oh!

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

@serhiy-storchaka

serhiy-storchaka commented May 8, 2025

Copy link
Copy Markdown
Member

No, I think the user code should be changed. They use undocumented feature.

All reactions

Sorry, something went wrong.

Uh oh!

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

@iritkatriel

iritkatriel commented May 8, 2025

Copy link
Copy Markdown
Member

We can do my original suggestion - make __init__ accept **kwargs and extract the new args with this.

Already two external libraries were broken by this in the first alpha testing. Leaving this as it is will break user code for sure.

All reactions

Sorry, something went wrong.

Uh oh!

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

@serhiy-storchaka

serhiy-storchaka commented May 8, 2025

Copy link
Copy Markdown
Member

This would not help. The problem is that the user code does not accept any arguments besides prog. And they do this because they use undocumented feature.

All reactions

Sorry, something went wrong.

Uh oh!

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

@iritkatriel

iritkatriel commented May 8, 2025

Copy link
Copy Markdown
Member

What's the concern with inspect? That it won't work for some weird function? In that case we can just use the default values for these arguments.

All reactions

Sorry, something went wrong.

Uh oh!

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

@iritkatriel

iritkatriel commented May 8, 2025

Copy link
Copy Markdown
Member

Another option is to set these fields through a setter method and not via __init__.

All reactions

Sorry, something went wrong.

Uh oh!

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

@serhiy-storchaka

serhiy-storchaka commented May 10, 2025

Copy link
Copy Markdown
Member

See alternative PR #133813.

hugovk reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@hugovk hugovk closed this May 10, 2025
@hugovk
hugovk deleted the 3.14-fix-subclassing-helpformatter branch May 13, 2025 21:10
@AA-Turner AA-Turner removed the needs backport to 3.14 bugs and security fixes label Aug 14, 2025
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 left review comments
@iritkatriel iritkatriel iritkatriel approved these changes
@savannahostrowski savannahostrowski Awaiting requested review from savannahostrowski savannahostrowski is a code owner

Uh oh!

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

Assignees

No one assigned

Labels

awaiting merge

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.

4 participants

@hugovk @serhiy-storchaka @iritkatriel @AA-Turner
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.