Skip to content

Commit b317641

Browse files
[3.12] gh-119577: Adjust DeprecationWarning when testing element truth values in Element tree (GH-119762) (#120190)
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. Co-authored-by: Jacob Walls <[email protected]>
1 parent e970431 commit b317641

File tree

5 files changed

+11
-6
lines changed

5 files changed

+11
-6
lines changed

Doc/library/xml.etree.elementtree.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,9 +1054,10 @@ Element Objects
10541054
:meth:`~object.__getitem__`, :meth:`~object.__setitem__`,
10551055
:meth:`~object.__len__`.
10561056

1057-
Caution: Elements with no subelements will test as ``False``. Testing the
1058-
truth value of an Element is deprecated and will raise an exception in
1059-
Python 3.14. Use specific ``len(elem)`` or ``elem is None`` test instead.::
1057+
Caution: Elements with no subelements will test as ``False``. In a future
1058+
release of Python, all elements will test as ``True`` regardless of whether
1059+
subelements exist. Instead, prefer explicit ``len(elem)`` or
1060+
``elem is not None`` tests.::
10601061

10611062
element = root.find('foo')
10621063

Lib/test/test_xml_etree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4009,7 +4009,7 @@ class BoolTest(unittest.TestCase):
40094009
def test_warning(self):
40104010
e = ET.fromstring('<a style="new"></a>')
40114011
msg = (
4012-
r"Testing an element's truth value will raise an exception in "
4012+
r"Testing an element's truth value will always return True in "
40134013
r"future versions. "
40144014
r"Use specific 'len\(elem\)' or 'elem is not None' test instead.")
40154015
with self.assertWarnsRegex(DeprecationWarning, msg):

Lib/xml/etree/ElementTree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def __len__(self):
201201

202202
def __bool__(self):
203203
warnings.warn(
204-
"Testing an element's truth value will raise an exception in "
204+
"Testing an element's truth value will always return True in "
205205
"future versions. "
206206
"Use specific 'len(elem)' or 'elem is not None' test instead.",
207207
DeprecationWarning, stacklevel=2
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The :exc:`DeprecationWarning` emitted when testing the truth value of an
2+
:class:`xml.etree.ElementTree.Element` now describes unconditionally
3+
returning ``True`` in a future version rather than raising an exception in
4+
Python 3.14.

Modules/_elementtree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1504,7 +1504,7 @@ element_bool(PyObject* self_)
15041504
{
15051505
ElementObject* self = (ElementObject*) self_;
15061506
if (PyErr_WarnEx(PyExc_DeprecationWarning,
1507-
"Testing an element's truth value will raise an exception "
1507+
"Testing an element's truth value will always return True "
15081508
"in future versions. Use specific 'len(elem)' or "
15091509
"'elem is not None' test instead.",
15101510
1) < 0) {

0 commit comments

Comments
 (0)