diff --git a/Lib/optparse.py b/Lib/optparse.py index de1082442ef7f2..3d242681270f22 100644 --- a/Lib/optparse.py +++ b/Lib/optparse.py @@ -706,7 +706,7 @@ def _check_callback(self): not isinstance(self.callback_kwargs, dict)): raise OptionError( "callback_kwargs, if supplied, must be a dict: not %r" - % self.callback_kwargs, self) + % (self.callback_kwargs,), self) else: if self.callback is not None: raise OptionError( diff --git a/Lib/test/test_optparse.py b/Lib/test/test_optparse.py index fc8ef9520b3c0f..dd05e3e4e438a7 100644 --- a/Lib/test/test_optparse.py +++ b/Lib/test/test_optparse.py @@ -301,6 +301,14 @@ def test_callback_kwargs_no_dict(self): 'callback': self.dummy, 'callback_kwargs': 'foo'}) + def test_callback_kwargs_tuple_not_dict(self): + self.assertOptionError( + "option -b: callback_kwargs, if supplied, " + "must be a dict: not (1, 2)", + ["-b"], {'action': 'callback', + 'callback': self.dummy, + 'callback_kwargs': (1, 2)}) + def test_no_callback_for_action(self): self.assertOptionError( "option -b: callback supplied ('foo') for non-callback option", diff --git a/Misc/NEWS.d/next/Library/2026-03-11-14-14-27.gh-issue-145821.xWSIjR.rst b/Misc/NEWS.d/next/Library/2026-03-11-14-14-27.gh-issue-145821.xWSIjR.rst new file mode 100644 index 00000000000000..654ea8ae6a7277 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-03-11-14-14-27.gh-issue-145821.xWSIjR.rst @@ -0,0 +1,2 @@ +:mod:`optparse`: raise :exc:`~optparse.OptionError` instead of :exc:`TypeError` +when ``callback_kwargs`` is not a dictionary.