Uh oh!
There was an error while loading. Please reload this page.
gh-133653: Fix subclassing HelpFormatter - #133668
Conversation
| color=self.color, | ||
| ) | ||
| else: | ||
| except TypeError: |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
That doesn't work: FAILED (failures=2, errors=156)
There was a problem hiding this comment.
Wait, it does, obviously need to replace y and z :)
serhiy-storchaka
left a comment
There was a problem hiding this comment.
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.
hugovk
commented
May 8, 2025
Would you prefer the |
serhiy-storchaka
commented
May 8, 2025
No, I think the user code should be changed. They use undocumented feature. |
iritkatriel
commented
May 8, 2025
We can do my original suggestion - make Already two external libraries were broken by this in the first alpha testing. Leaving this as it is will break user code for sure. |
serhiy-storchaka
commented
May 8, 2025
This would not help. The problem is that the user code does not accept any arguments besides |
iritkatriel
commented
May 8, 2025
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. |
iritkatriel
commented
May 8, 2025
Another option is to set these fields through a setter method and not via |
serhiy-storchaka
commented
May 10, 2025
See alternative PR #133813. |
The problem was with this code:
We want to set the extra arguments for
HelpFormatterandargparse's own subclasses, which don't have their own__init__s.But many third-party subclasses do have an
__init__and don't pass onkwargs. So we can't just check for a subclass ofHelpFormatter, so let'stry/exceptinstead.