8000 gh-119577: Adjust DeprecationWarning when testing element truth value… · python/cpython@6b60652 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6b60652

Browse files
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.
1 parent 14e1506 commit 6b60652

File tree

7 files changed

+21
-13
lines changed

7 files changed

+21
-13
lines changed

Doc/library/xml.etree.elementtree.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,9 +1058,10 @@ Element Objects
10581058
:meth:`~object.__getitem__`, :meth:`~object.__setitem__`,
10591059
:meth:`~object.__len__`.
10601060

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

10651066
element = root.find('foo')
10661067

Doc/whatsnew/3.12.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,8 +1440,6 @@ and will be removed in Python 3.14.
14401440

14411441
* :mod:`typing`: :class:`!typing.ByteString`
14421442

1443-
* :mod:`xml.etree.ElementTree`: Testing the truth value of an :class:`xml.etree.ElementTree.Element`.
1444-
14451443
* The ``__package__`` and ``__cached__`` attributes on module objects.
14461444

14471445
* The :attr:`~codeobject.co_lnotab` attribute of code objects.
@@ -1467,6 +1465,11 @@ although there is currently no date scheduled for their removal.
14671465

14681466
* :class:`typing.Text` (:gh:`92332`)
14691467

1468+
* :mod:`xml.etree.ElementTree`: Testing the truth value of an
1469+
:class:`xml.etree.ElementTree.Element` is deprecated. In a future release it
1470+
will always return True. Prefer explicit ``len(elem)`` or
1471+
``elem is not None`` tests instead.
1472+
14701473
* Currently Python accepts numeric literals immediately followed by keywords,
14711474
for example ``0in x``, ``1or x``, ``0if 1else 2``. It allows confusing
14721475
and ambiguous expressions like ``[0x1for x in y]`` (which can be

Doc/whatsnew/3.13.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,11 +1728,6 @@ Pending Removal in Python 3.14
17281728
public API.
17291729
(Contributed by Gregory P. Smith in :gh:`88168`.)
17301730

1731-
* :mod:`xml.etree.ElementTree`: Testing the truth value of an
1732-
:class:`~xml.etree.ElementTree.Element` is deprecated and will raise an
1733-
exception in Python 3.14.
1734-
1735-
17361731
Pending Removal in Python 3.15
17371732
------------------------------
17381733

@@ -1937,6 +1932,11 @@ although there is currently no date scheduled for their removal.
19371932
* :mod:`wsgiref`: ``SimpleHandler.stdout.write()`` should not do partial
19381933
writes.
19391934

1935+
* :mod:`xml.etree.ElementTree`: Testing the truth value of an
1936+
:class:`~xml.etree.ElementTree.Element` is deprecated. In a future release it
1937+
it will always return ``True``. Prefer explicit ``len(elem)`` or
1938+
``elem is not None`` tests instead.
1939+
19401940
* :meth:`zipimport.zipimporter.load_module` is deprecated:
19411941
use :meth:`~zipimport.zipimporter.exec_module` instead.
19421942

Lib/test/test_xml_etree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4088,7 +4088,7 @@ class BoolTest(unittest.TestCase):
40884088
def test_warning(self):
40894089
e = ET.fromstring('<a style="new"></a>')
40904090
msg = (
4091-
r"Testing an element's truth value will raise an exception in "
4091+
r"Testing an element's truth value will always return True in "
40924092
r"future versions. "
40934093
r"Use specific 'len\(elem\)' or 'elem is not None' test instead.")
40944094
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
@@ -1502,7 +1502,7 @@ element_bool(PyObject* self_)
15021502
{
15031503
ElementObject* self = (ElementObject*) self_;
15041504
if (PyErr_WarnEx(PyExc_DeprecationWarning,
1505-
"Testing an element's truth value will raise an exception "
1505+
"Testing an element's truth value will always return True "
15061506
"in future versions. Use specific 'len(elem)' or "
15071507
"'elem is not None' test instead.",
15081508
1) < 0) {

0 commit comments

Comments
 (0)
0