[Python-checkins] bpo-41086: Add exception for uninstantiated interpolation (configparser) (GH-21062)

ambv webhook-mailer at python.org
Thu Feb 17 07:18:14 EST 2022


https://github.com/python/cpython/commit/fc115c9bde52a58b4fb9be2b80c3d6be8a4d2454
commit: fc115c9bde52a58b4fb9be2b80c3d6be8a4d2454
branch: main
author: Brian Faherty <anothergenericuser at gmail.com>
committer: ambv <lukasz at langa.pl>
date: 2022-02-17T13:17:43+01:00
summary:

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 <lukasz at langa.pl>

files:
A Misc/NEWS.d/next/Library/2020-06-23-01-50-24.bpo-41086.YnOvpS.rst
M Lib/configparser.py
M Lib/test/test_configparser.py

diff --git a/Lib/configparser.py b/Lib/configparser.py
index 3470624e63f61..f5666f518f121 100644
--- a/Lib/configparser.py
+++ b/Lib/configparser.py
@@ -633,6 +633,11 @@ def __init__(self, defaults=None, dict_type=_default_dict,
             self._interpolation = self._DEFAULT_INTERPOLATION
         if self._interpolation is None:
             self._interpolation = Interpolation()
+        if not isinstance(self._interpolation, Interpolation):
+            raise TypeError(
+                f"interpolation= must be None or an instance of Interpolation;"
+                f" got an object of type {type(self._interpolation)}"
+            )
         if converters is not _UNSET:
             self._converters.update(converters)
         if defaults:
diff --git a/Lib/test/test_configparser.py b/Lib/test/test_configparser.py
index e9b03e6c62ef1..569959c3ed919 100644
--- a/Lib/test/test_configparser.py
+++ b/Lib/test/test_configparser.py
@@ -1048,6 +1048,14 @@ def test_set_malformatted_interpolation(self):
         self.assertEqual(cf.get("sect", "option2"), "foo%%bar")
 
 
+class ConfigParserTestCaseInvalidInterpolationType(unittest.TestCase):
+    def test_error_on_wrong_type_for_interpolation(self):
+        for value in [configparser.ExtendedInterpolation,  42,  "a string"]:
+            with self.subTest(value=value):
+                with self.assertRaises(TypeError):
+                    configparser.ConfigParser(interpolation=value)
+
+
 class ConfigParserTestCaseNonStandardDelimiters(ConfigParserTestCase):
     delimiters = (':=', '$')
     comment_prefixes = ('//', '"')
diff --git a/Misc/NEWS.d/next/Library/2020-06-23-01-50-24.bpo-41086.YnOvpS.rst b/Misc/NEWS.d/next/Library/2020-06-23-01-50-24.bpo-41086.YnOvpS.rst
new file mode 100644
index 0000000000000..1041c0490fab7
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-06-23-01-50-24.bpo-41086.YnOvpS.rst
@@ -0,0 +1 @@
+Make the :class:`configparser.ConfigParser` constructor raise :exc:`TypeError` if the ``interpolation`` parameter is not of type :class:`configparser.Interpolation`



More information about the Python-checkins mailing list