From 2aa86b80ebbbd2e0e7af316560ce18cbc39ec1b0 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Wed, 2 Feb 2022 09:05:53 +0200 Subject: [PATCH] bpo-46607: Add DeprecationWarning for LegacyInterpolation, deprecated in docs since 3.2 --- Doc/whatsnew/3.11.rst | 5 +++++ Lib/configparser.py | 9 +++++++++ Lib/test/test_configparser.py | 8 ++++++++ .../Library/2022-01-26-18-30-34.bpo-46607.xnhT4a.rst | 3 +++ 4 files changed, 25 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2022-01-26-18-30-34.bpo-46607.xnhT4a.rst diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index 18d4652fb42930..e89027e75767d8 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -473,6 +473,11 @@ Deprecated (Contributed by Hugo van Kemenade in :issue:`45173`.) +* :class:`configparser.LegacyInterpolation` has been deprecated in the docstring + since Python 3.2. It now emits a :exc:`DeprecationWarning` and will be removed + in Python 3.13. Use :class:`configparser.BasicInterpolation` or + :class:`configparser.ExtendedInterpolation instead. + (Contributed by Hugo van Kemenade in :issue:`46607`.) Removed ======= diff --git a/Lib/configparser.py b/Lib/configparser.py index 3470624e63f61b..cbf0f9c35268d3 100644 --- a/Lib/configparser.py +++ b/Lib/configparser.py @@ -525,6 +525,15 @@ class LegacyInterpolation(Interpolation): _KEYCRE = re.compile(r"%\(([^)]*)\)s|.") + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + warnings.warn( + "LegacyInterpolation has been deprecated since Python 3.2 " + "and will be removed from the configparser module in Python 3.13. " + "Use BasicInterpolation or ExtendedInterpolation instead.", + DeprecationWarning, stacklevel=2 + ) + def before_get(self, parser, section, option, value, vars): rawval = value depth = MAX_INTERPOLATION_DEPTH diff --git a/Lib/test/test_configparser.py b/Lib/test/test_configparser.py index e9b03e6c62ef14..f24e70ba1785fb 100644 --- a/Lib/test/test_configparser.py +++ b/Lib/test/test_configparser.py @@ -1658,6 +1658,14 @@ def test_safeconfigparser_deprecation(self): for warning in w: self.assertTrue(warning.category is DeprecationWarning) + def test_legacyinterpolation_deprecation(self): + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always", DeprecationWarning) + configparser.LegacyInterpolation() + self.assertGreaterEqual(len(w), 1) + for warning in w: + self.assertIs(warning.category, DeprecationWarning) + def test_sectionproxy_repr(self): parser = configparser.ConfigParser() parser.read_string(""" diff --git a/Misc/NEWS.d/next/Library/2022-01-26-18-30-34.bpo-46607.xnhT4a.rst b/Misc/NEWS.d/next/Library/2022-01-26-18-30-34.bpo-46607.xnhT4a.rst new file mode 100644 index 00000000000000..e0c7ed0531b575 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-01-26-18-30-34.bpo-46607.xnhT4a.rst @@ -0,0 +1,3 @@ +Add :exc:`DeprecationWarning` to :class:`LegacyInterpolation`, deprecated in +the docstring since Python 3.2. Will be removed in Python 3.13. Use +:class:`BasicInterpolation` or :class:`ExtendedInterpolation` instead.