Skip to content

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

Open
stefanzetzsche wants to merge 7 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 7 commits intopython:mainfrom
stefanzetzsche:fix/optparse_callback_kwargs_typeerror

Conversation

@stefanzetzsche
Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread Misc/NEWS.d/next/Library/2026-03-11-14-14-27.gh-issue-145821.xWSIjR.rst Outdated
@bedevere-app
Copy link
Copy Markdown

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
Copy Markdown

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
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 8, 2026

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label May 8, 2026
Copy link
Copy Markdown
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.

Don't you also have the same issue when supplying a tuple for a non-callback action? (L713)

Comment thread Lib/optparse.py
Comment on lines 708 to +709
"callback_kwargs, if supplied, must be a dict: not %r"
% self.callback_kwargs, self)
% (self.callback_kwargs,), self)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

An alterantive would be to use an f-string so that we don't have this issue anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants