Skip to content

Commit fc115c9

Browse files
scrummyinblurb-it[bot]ambv
authored
bpo-41086: Add exception for uninstantiated interpolation (configparser) (GH-21062)
* Add exception for uninstantiated interpolation (configparser) The current feedback when users try to pass an uninstantiated interpolation into a ConfigParser is an error message that does not help users solve the problem. This current error of `TypeError: before_set() missing 1 required positional argument: 'value'` does not display until the parser is used, which usually results in the assumption that instantiation of the parser was done correctly. The new exception of InterpolationTypeError, will be raised on the line where the ConfigParser is instantiated. This will result in users see the line that has the error in their backtrace for faster debugging. There have been a number of bugs created in the issue tracker, which could have been addressed by: https://bugs.python.org/issue26831 and https://bugs.python.org/issue26469 * 📜🤖 Added by blurb_it. * Replace custom Error with TypeError Per feedback from @iritkatriel, the custom InterpolationTypeError has been dropped in favour of a TypeError with a custom message, and the unittests have been expanded. * More verbose message Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Łukasz Langa <[email protected]>
1 parent 5bc4327 commit fc115c9

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

Lib/configparser.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,11 @@ def __init__(self, defaults=None, dict_type=_default_dict,
633633
self._interpolation = self._DEFAULT_INTERPOLATION
634634
if self._interpolation is None:
635635
self._interpolation = Interpolation()
636+
if not isinstance(self._interpolation, Interpolation):
637+
raise TypeError(
638+
f"interpolation= must be None or an instance of Interpolation;"
639+
f" got an object of type {type(self._interpolation)}"
640+
)
636641
if converters is not _UNSET:
637642
self._converters.update(converters)
638643
if defaults:

Lib/test/test_configparser.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,14 @@ def test_set_malformatted_interpolation(self):
10481048
self.assertEqual(cf.get("sect", "option2"), "foo%%bar")
10491049

10501050

1051+
class ConfigParserTestCaseInvalidInterpolationType(unittest.TestCase):
1052+
def test_error_on_wrong_type_for_interpolation(self):
1053+
for value in [configparser.ExtendedInterpolation, 42, "a string"]:
1054+
with self.subTest(value=value):
1055+
with self.assertRaises(TypeError):
1056+
configparser.ConfigParser(interpolation=value)
1057+
1058+
10511059
class ConfigParserTestCaseNonStandardDelimiters(ConfigParserTestCase):
10521060
delimiters = (':=', '$')
10531061
comment_prefixes = ('//', '"')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make the :class:`configparser.ConfigParser` constructor raise :exc:`TypeError` if the ``interpolation`` parameter is not of type :class:`configparser.Interpolation`

0 commit comments

Comments
 (0)