python / python/cpython

optparse Option._check_callback raises TypeError instead of OptionError for tuple callback_kwargs

Open
#145,821 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

Issue

When callback_kwargs is a tuple, Option._check_callback raises TypeError instead of OptionError. The error message is formatted with %r % self.callback_kwargs. When callback_kwargs is a tuple, Python's % operator unpacks it as multiple format arguments, causing TypeError: not all arguments converted during string formatting.

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
CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs
  • gh-145822

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at optparse.Option._check_callback and reproduce the tuple callback_kwargs case from the issue. Confirm that the invalid callback_kwargs path reports OptionError rather than a formatting TypeError; linked PR gh-145822 indicates work is already underway.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.