diff --git a/Lib/configparser.py b/Lib/configparser.py index f28aba7c3db7b6..3a3b6d26f28fee 100644 --- a/Lib/configparser.py +++ b/Lib/configparser.py @@ -991,6 +991,8 @@ def items(self, section=_UNSET, raw=False, vars=None): # Update with the entry specific variables if vars: for key, value in vars.items(): + if value is not None: + value = str(value) d[self.optionxform(key)] = value value_getter = lambda option: self._interpolation.before_get( self, section, option, d[option], d diff --git a/Lib/test/test_configparser.py b/Lib/test/test_configparser.py index f24d7b869991c2..394ffb5ad4aee7 100644 --- a/Lib/test/test_configparser.py +++ b/Lib/test/test_configparser.py @@ -904,6 +904,21 @@ def check_items_config(self, expected): with self.assertRaises(configparser.NoSectionError): cf.items("no such section") + def test_items_non_string_vars(self): + cf = self.newconfig(defaults={'default': 'original'}) + cf.read_dict({'section': {'value': 'original'}}) + for value in (42, 0, False): + for raw in (False, True): + with self.subTest(value=value, raw=raw): + self.assertEqual( + dict(cf.items('section', raw=raw, vars={ + 'default': value, + 'VALUE': value, + 'extra': value, + })), + {'default': str(value), 'value': str(value)}, + ) + def test_popitem(self): cf = self.fromstring( f""" diff --git a/Misc/NEWS.d/next/Library/2026-10-26-06-10-00.gh-issue-160684.vArs42.rst b/Misc/NEWS.d/next/Library/2026-10-26-06-10-00.gh-issue-160684.vArs42.rst new file mode 100644 index 00000000000000..d4a57c66aea3c9 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-26-06-10-00.gh-issue-160684.vArs42.rst @@ -0,0 +1,2 @@ +Fix :meth:`configparser.ConfigParser.items` to convert non-``None`` values +passed in *vars* to strings, consistently with :meth:`~configparser.ConfigParser.get`.