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.
This commit is contained in:
Jacob Walls 2024-06-06 23:18:30 -04:00 committed by GitHub
parent 14e1506a6d
commit 6b606522ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 21 additions and 13 deletions

View File

@ -1058,9 +1058,10 @@ Element Objects
:meth:`~object.__getitem__`, :meth:`~object.__setitem__`, :meth:`~object.__getitem__`, :meth:`~object.__setitem__`,
:meth:`~object.__len__`. :meth:`~object.__len__`.
Caution: Elements with no subelements will test as ``False``. Testing the Caution: Elements with no subelements will test as ``False``. In a future
truth value of an Element is deprecated and will raise an exception in release of Python, all elements will test as ``True`` regardless of whether
Python 3.14. Use specific ``len(elem)`` or ``elem is None`` test instead.:: subelements exist. Instead, prefer explicit ``len(elem)`` or
``elem is not None`` tests.::
element = root.find('foo') element = root.find('foo')

View File

@ -1440,8 +1440,6 @@ and will be removed in Python 3.14.
* :mod:`typing`: :class:`!typing.ByteString` * :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 ``__package__`` and ``__cached__`` attributes on module objects.
* The :attr:`~codeobject.co_lnotab` attribute of code 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`) * :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, * Currently Python accepts numeric literals immediately followed by keywords,
for example ``0in x``, ``1or x``, ``0if 1else 2``. It allows confusing for example ``0in x``, ``1or x``, ``0if 1else 2``. It allows confusing
and ambiguous expressions like ``[0x1for x in y]`` (which can be and ambiguous expressions like ``[0x1for x in y]`` (which can be

View File

@ -1728,11 +1728,6 @@ Pending Removal in Python 3.14
public API. public API.
(Contributed by Gregory P. Smith in :gh:`88168`.) (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 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 * :mod:`wsgiref`: ``SimpleHandler.stdout.write()`` should not do partial
writes. 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: * :meth:`zipimport.zipimporter.load_module` is deprecated:
use :meth:`~zipimport.zipimporter.exec_module` instead. use :meth:`~zipimport.zipimporter.exec_module` instead.

View File

@ -4088,7 +4088,7 @@ class BoolTest(unittest.TestCase):
def test_warning(self): def test_warning(self):
e = ET.fromstring('<a style="new"></a>') e = ET.fromstring('<a style="new"></a>')
msg = ( 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"future versions. "
r"Use specific 'len\(elem\)' or 'elem is not None' test instead.") r"Use specific 'len\(elem\)' or 'elem is not None' test instead.")
with self.assertWarnsRegex(DeprecationWarning, msg): with self.assertWarnsRegex(DeprecationWarning, msg):

View File

@ -201,7 +201,7 @@ class Element:
def __bool__(self): def __bool__(self):
warnings.warn( 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. " "future versions. "
"Use specific 'len(elem)' or 'elem is not None' test instead.", "Use specific 'len(elem)' or 'elem is not None' test instead.",
DeprecationWarning, stacklevel=2 DeprecationWarning, stacklevel=2

View File

@ -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.

View File

@ -1502,7 +1502,7 @@ element_bool(PyObject* self_)
{ {
ElementObject* self = (ElementObject*) self_; ElementObject* self = (ElementObject*) self_;
if (PyErr_WarnEx(PyExc_DeprecationWarning, 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 " "in future versions. Use specific 'len(elem)' or "
"'elem is not None' test instead.", "'elem is not None' test instead.",
1) < 0) { 1) < 0) {