summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-06-07 03:43:17 (GMT)
committerGitHub <noreply@github.com>2024-06-07 03:43:17 (GMT)
commit56a7e0483436d1ebd2af97c02defe0e67c4bb495 (patch)
tree04b0c8ad8d4db46be7c8da2668301538345ee994
parentd4566c0cfcc2f3db8fa61b35f99260c184416ba4 (diff)
downloadcpython-56a7e0483436d1ebd2af97c02defe0e67c4bb495.zip
cpython-56a7e0483436d1ebd2af97c02defe0e67c4bb495.tar.gz
cpython-56a7e0483436d1ebd2af97c02defe0e67c4bb495.tar.bz2
[3.13] gh-119577: Adjust DeprecationWarning when testing element truth values in ElementTree (GH-119762) (GH-120189)
gh-119577: Adjust DeprecationWarning when testing element truth values in ElementTree (GH-119762) 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. (cherry picked from commit 6b606522ca97488aad6fe2f193d4511e7a8f8334) Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
-rw-r--r--Doc/library/xml.etree.elementtree.rst7
-rw-r--r--Doc/whatsnew/3.12.rst7
-rw-r--r--Doc/whatsnew/3.13.rst10
-rw-r--r--Lib/test/test_xml_etree.py2
-rw-r--r--Lib/xml/etree/ElementTree.py2
-rw-r--r--Misc/NEWS.d/next/Library/2024-05-29-21-50-05.gh-issue-119577.S3BlKJ.rst4
-rw-r--r--Modules/_elementtree.c2
7 files changed, 21 insertions, 13 deletions
diff --git a/Doc/library/xml.etree.elementtree.rst b/Doc/library/xml.etree.elementtree.rst
index a6a9eb8..e591902 100644
--- a/Doc/library/xml.etree.elementtree.rst
+++ b/Doc/library/xml.etree.elementtree.rst
@@ -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')
diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst
index d9a314e..f3cabb7 100644
--- a/Doc/whatsnew/3.12.rst
+++ b/Doc/whatsnew/3.12.rst
@@ -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
diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst
index b4b7cb9..19727b9 100644
--- a/Doc/whatsnew/3.13.rst
+++ b/Doc/whatsnew/3.13.rst
@@ -1716,11 +1716,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
------------------------------
@@ -1925,6 +1920,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.
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py
index bae61f7..3d9141f 100644
--- a/Lib/test/test_xml_etree.py
+++ b/Lib/test/test_xml_etree.py
@@ -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):
diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py
index 9e15d34..ce67d7d 100644
--- a/Lib/xml/etree/ElementTree.py
+++ b/Lib/xml/etree/ElementTree.py
@@ -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
index 0000000..bd2daf3
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-05-29-21-50-05.gh-issue-119577.S3BlKJ.rst
@@ -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.
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index b11983d..3818e20 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -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) {