Skip to content

gh-145821: properly raise OptionError in Option._check_callback when callback_kwargs is invalid#145822

Open
stefanzetzsche wants to merge 6 commits intopython:mainfrom
stefanzetzsche:fix/optparse_callback_kwargs_typeerror
Open

gh-145821: properly raise OptionError in Option._check_callback when callback_kwargs is invalid#145822
stefanzetzsche wants to merge 6 commits intopython:mainfrom
stefanzetzsche:fix/optparse_callback_kwargs_typeerror

Conversation

@stefanzetzsche
Copy link
Contributor

@stefanzetzsche stefanzetzsche commented Mar 11, 2026

Issue

Option._check_callback formats its error message with %r % self.callback_kwargs. When callback_kwargs is a tuple, Python's % operator unpacks it as multiple arguments, causing TypeError instead of the expected OptionError.

Reproducer

from optparse import Option
Option('--foo', action='callback', callback=lambda *a: None, callback_kwargs=(1, 2))
# TypeError: not all arguments converted during string formatting
# Expected: OptionError

Fix

In Lib/optparse.py, wrap the value in a tuple for the format call:

% (self.callback_kwargs,), self)

Tests

Added test_callback_kwargs_tuple_not_dict to TestOptionChecks in Lib/test/test_optparse.py.

stefanzetzsche and others added 2 commits February 22, 2026 14:54
…wargs

%r % tuple unpacks the tuple as multiple format args, raising TypeError
instead of OptionError. Wrap in a 1-tuple: % (value,).
Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

Please add a regression test.

@bedevere-app
Copy link

bedevere-app bot commented Mar 11, 2026

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 I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@picnixz picnixz changed the title gh-145821: Fix/optparse callback kwargs typeerror gh-145821: Properly OptionError when callback_kwargs is invalid Mar 11, 2026
@picnixz picnixz changed the title gh-145821: Properly OptionError when callback_kwargs is invalid gh-145821: Properly raise OptionError when callback_kwargs is invalid Mar 11, 2026
@picnixz picnixz changed the title gh-145821: Properly raise OptionError when callback_kwargs is invalid gh-145821: properly raise OptionError in Option._check_callback when callback_kwargs is invalid Mar 11, 2026
…WSIjR.rst

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@python-cla-bot
Copy link

python-cla-bot bot commented Mar 11, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

… OptionError

A tuple callback_kwargs should raise OptionError with the repr of the
tuple, not TypeError from string formatting unpacking the tuple.
@stefanzetzsche stefanzetzsche requested a review from picnixz March 11, 2026 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants