]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-119577: Adjust DeprecationWarning when testing element truth values in ElementTree...
authorJacob Walls <jacobtylerwalls@gmail.com>
Fri, 7 Jun 2024 03:18:30 +0000 (23:18 -0400)
committerGitHub <noreply@github.com>
Fri, 7 Jun 2024 03:18:30 +0000 (20:18 -0700)
Adjust DeprecationWarning when testing element truth values in ElementTree, we're planning to go with the more natural True return rather than a disruptive harder to code around exception raise, and are deferring the behavior change for a few more releases.

Doc/library/xml.etree.elementtree.rst
Doc/whatsnew/3.12.rst
Doc/whatsnew/3.13.rst
Lib/test/test_xml_etree.py
Lib/xml/etree/ElementTree.py
Misc/NEWS.d/next/Library/2024-05-29-21-50-05.gh-issue-119577.S3BlKJ.rst [new file with mode: 0644]
Modules/_elementtree.c

index a6a9eb87f56d8866e9f4f504f54f4d251c650da5..e5919029c62c93dc8433b9a662e84b9c124ada0b 100644 (file)
@@ -1058,9 +1058,10 @@ Element Objects
    :meth:`~object.__getitem__`, :meth:`~object.__setitem__`,
    :meth:`~object.__len__`.
 
-   Caution: Elements with no subelements will test as ``False``.  Testing the
-   truth value of an Element is deprecated and will raise an exception in
-   Python 3.14.  Use specific ``len(elem)`` or ``elem is None`` test instead.::
+   Caution: Elements with no subelements will test as ``False``.  In a future
+   release of Python, all elements will test as ``True`` regardless of whether
+   subelements exist.  Instead, prefer explicit ``len(elem)`` or
+   ``elem is not None`` tests.::
 
      element = root.find('foo')
 
index f99489fb53db745d975dbaa9ab0961cedd8d8058..28b28e9ce50e11c53c64e09ae5eb5f49d77d856c 100644 (file)
@@ -1440,8 +1440,6 @@ and will be removed in Python 3.14.
 
 * :mod:`typing`: :class:`!typing.ByteString`
 
-* :mod:`xml.etree.ElementTree`: Testing the truth value of an :class:`xml.etree.ElementTree.Element`.
-
 * The ``__package__`` and ``__cached__`` attributes on module objects.
 
 * The :attr:`~codeobject.co_lnotab` attribute of code objects.
@@ -1467,6 +1465,11 @@ although there is currently no date scheduled for their removal.
 
 * :class:`typing.Text` (:gh:`92332`)
 
+* :mod:`xml.etree.ElementTree`: Testing the truth value of an
+  :class:`xml.etree.ElementTree.Element` is deprecated. In a future release it
+  will always return True. Prefer explicit ``len(elem)`` or
+  ``elem is not None`` tests instead.
+
 * Currently Python accepts numeric literals immediately followed by keywords,
   for example ``0in x``, ``1or x``, ``0if 1else 2``.  It allows confusing
   and ambiguous expressions like ``[0x1for x in y]`` (which can be
index a1d2a0d84e758118157ce1ff18344c17656fb24e..81daaabdb889d7b29e180eead896cb3e3bee80b0 100644 (file)
@@ -1728,11 +1728,6 @@ Pending Removal in Python 3.14
   public API.
   (Contributed by Gregory P. Smith in :gh:`88168`.)
 
-* :mod:`xml.etree.ElementTree`: Testing the truth value of an
-  :class:`~xml.etree.ElementTree.Element` is deprecated and will raise an
-  exception in Python 3.14.
-
-
 Pending Removal in Python 3.15
 ------------------------------
 
@@ -1937,6 +1932,11 @@ although there is currently no date scheduled for their removal.
 * :mod:`wsgiref`: ``SimpleHandler.stdout.write()`` should not do partial
   writes.
 
+* :mod:`xml.etree.ElementTree`: Testing the truth value of an
+  :class:`~xml.etree.ElementTree.Element` is deprecated. In a future release it
+  it will always return ``True``. Prefer explicit ``len(elem)`` or
+  ``elem is not None`` tests instead.
+
 * :meth:`zipimport.zipimporter.load_module` is deprecated:
   use :meth:`~zipimport.zipimporter.exec_module` instead.
 
index bae61f754e75f5965a1b9d9cd0bfc364ebbac5a2..3d9141fea1ef3e43540b6be245545fc3c44321a6 100644 (file)
@@ -4088,7 +4088,7 @@ class BoolTest(unittest.TestCase):
     def test_warning(self):
         e = ET.fromstring('<a style="new"></a>')
         msg = (
-            r"Testing an element's truth value will raise an exception in "
+            r"Testing an element's truth value will always return True in "
             r"future versions.  "
             r"Use specific 'len\(elem\)' or 'elem is not None' test instead.")
         with self.assertWarnsRegex(DeprecationWarning, msg):
index 9e15d34d22aa6c44fd617f11448069e12f32d75a..ce67d7d7d54748c98a66e8101bae3c14c4242a93 100644 (file)
@@ -201,7 +201,7 @@ class Element:
 
     def __bool__(self):
         warnings.warn(
-            "Testing an element's truth value will raise an exception in "
+            "Testing an element's truth value will always return True in "
             "future versions.  "
             "Use specific 'len(elem)' or 'elem is not None' test instead.",
             DeprecationWarning, stacklevel=2
diff --git a/Misc/NEWS.d/next/Library/2024-05-29-21-50-05.gh-issue-119577.S3BlKJ.rst b/Misc/NEWS.d/next/Library/2024-05-29-21-50-05.gh-issue-119577.S3BlKJ.rst
new file mode 100644 (file)
index 0000000..bd2daf3
--- /dev/null
@@ -0,0 +1,4 @@
+The :exc:`DeprecationWarning` emitted when testing the truth value of an
+:class:`xml.etree.ElementTree.Element` now describes unconditionally
+returning ``True`` in a future version rather than raising an exception in
+Python 3.14.
index b11983d2caa2d1c82ae3c2ca6a60f5d91bdbf85e..3818e20b4f0f28428547b159ecae3ff2e1f8568d 100644 (file)
@@ -1502,7 +1502,7 @@ element_bool(PyObject* self_)
 {
     ElementObject* self = (ElementObject*) self_;
     if (PyErr_WarnEx(PyExc_DeprecationWarning,
-                     "Testing an element's truth value will raise an exception "
+                     "Testing an element's truth value will always return True "
                      "in future versions.  Use specific 'len(elem)' or "
                      "'elem is not None' test instead.",
                      1) < 0) {