gh-145821: properly raise OptionError in Option._check_callback when callback_kwargs is invalid#145822
Conversation
…wargs %r % tuple unpacks the tuple as multiple format args, raising TypeError instead of OptionError. Wrap in a 1-tuple: % (value,).
…parse_callback_kwargs_typeerror
picnixz
left a comment
There was a problem hiding this comment.
Please add a regression test.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
OptionError when callback_kwargs is invalid
OptionError when callback_kwargs is invalidOptionError when callback_kwargs is invalid
OptionError when callback_kwargs is invalidOptionError in Option._check_callback when callback_kwargs is invalid
…WSIjR.rst Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
… OptionError A tuple callback_kwargs should raise OptionError with the repr of the tuple, not TypeError from string formatting unpacking the tuple.
…ub.com/stefanzetzsche/cpython into fix/optparse_callback_kwargs_typeerror
|
This PR is stale because it has been open for 30 days with no activity. |
picnixz
left a comment
There was a problem hiding this comment.
Don't you also have the same issue when supplying a tuple for a non-callback action? (L713)
| "callback_kwargs, if supplied, must be a dict: not %r" | ||
| % self.callback_kwargs, self) | ||
| % (self.callback_kwargs,), self) |
There was a problem hiding this comment.
An alterantive would be to use an f-string so that we don't have this issue anymore.
Issue
Option._check_callbackformats its error message with%r % self.callback_kwargs. Whencallback_kwargsis a tuple, Python's%operator unpacks it as multiple arguments, causingTypeErrorinstead of the expectedOptionError.Reproducer
Fix
In
Lib/optparse.py, wrap the value in a tuple for the format call:Tests
Added
test_callback_kwargs_tuple_not_dicttoTestOptionChecksinLib/test/test_optparse.py.