8000 gh-83122: Deprecate testing element truth values in `ElementTree` by jacobtylerwalls · Pull Request #31149 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-83122: Deprecate testing element truth values in ElementTree #31149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Doc/library/xml.etree.elementtree.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1045,9 +1045,9 @@ Element Objects
:meth:`~object.__getitem__`, :meth:`~object.__setitem__`,
:meth:`~object.__len__`.

Caution: Elements with no subelements will test as ``False``. This behavior
will change in future versions. Use specific ``len(elem)`` or ``elem is
None`` test instead. ::
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.::

element = root.find('foo')

Expand All @@ -1057,6 +1057,9 @@ Element Objects
if element is None:
print("element not found")

.. versionchanged:: 3.12
Testing the truth value of an Element emits :exc:`DeprecationWarning`.

Prior to Python 3.8, the serialisation order of the XML attributes of
elements was artificially made predictable by sorting the attributes by
their name. Based on the now guaranteed ordering of dicts, this arbitrary
Expand Down
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,11 @@ Deprecated
is no current event loop set and it decides to create one.
(Contributed by Serhiy Storchaka and Guido van Rossum in :gh:`100160`.)

* The :mod:`xml.etree.ElementTree` module now emits :exc:`DeprecationWarning`
when testing the truth value of an :class:`xml.etree.ElementTree.Element`.
Before, the Python implementation emitted :exc:`FutureWarning`, and the C
implementation emitted nothing.


Pending Removal in Python 3.13
------------------------------
Expand Down Expand Up @@ -475,6 +480,9 @@ Pending Removal in Python 3.14
* ``__package__`` and ``__cached__`` will cease to be set or taken
into consideration by the import system (:gh:`97879`).

* 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 Future Versions
----------------------------------
Expand Down
20 changes: 20 additions & 0 deletions Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -3957,6 +3957,25 @@ def test_correct_import_pyET(self):
self.assertIsInstance(pyET.Element.__init__, types.FunctionType)
self.assertIsInstance(pyET.XMLParser.__init__, types.FunctionType)

# --------------------------------------------------------------------

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"future versions. "
r"Use specific 'len\(elem\)' or 'elem is not None' test instead.")
with self.assertWarnsRegex(DeprecationWarning, msg):
result = bool(e)
# Emulate prior behavior for now
self.assertIs(result, False)

# Element with children
ET.SubElement(e, 'b')
with self.assertWarnsRegex(DeprecationWarning, msg):
new_result = bool(e)
self.assertIs(new_result, True)

# --------------------------------------------------------------------

Expand Down Expand Up @@ -4223,6 +4242,7 @@ def test_main(module=None):
XMLPullParserTest,
BugsTest,
KeywordArgsTest,
BoolTest,
C14NTest,
]

Expand Down
5 changes: 3 additions & 2 deletions Lib/xml/etree/ElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,10 @@ def __len__(self):

def __bool__(self):
warnings.warn(
"The behav E864 ior of this method will change in future versions. "
"Testing an element's truth value will raise an exception in "
"future versions. "
"Use specific 'len(elem)' or 'elem is not None' test instead.",
FutureWarning, stacklevel=2
DeprecationWarning, stacklevel=2
)
return len(self._children) != 0 # emulate old behaviour, for now

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
The :mod:`xml.etree.ElementTree` module now emits :exc:`DeprecationWarning`
when testing the truth value of an :class:`xml.etree.ElementTree.Element`.
Before, the Python implementation emitted :exc:`FutureWarning`, and the C
implementation emitted nothing.
18 changes: 18 additions & 0 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,23 @@ element_getitem(PyObject* self_, Py_ssize_t index)
return Py_NewRef(self->extra->children[index]);
}

static int
element_bool(PyObject* self_)
{
ElementObject* self = (ElementObject*) self_;
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"Testing an element's truth value will raise an exception "
"in future versions. Use specific 'len(elem)' or "
"'elem is not None' test instead.",
1) < 0) {
return -1;
};
if (self->extra ? self->extra->length : 0) {
return 1;
}
return 0;
Comment on lines +1463 to +1466
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be simply

return self->extra && self->extra->length;

}

/*[clinic input]
_elementtree.Element.insert

Expand Down Expand Up @@ -4156,6 +4173,7 @@ static PyType_Slot element_slots[] = {
{Py_sq_length, element_length},
{Py_sq_item, element_getitem},
{Py_sq_ass_item, element_setitem},
{Py_nb_bool, element_bool},
{Py_mp_length, element_length},
{Py_mp_subscript, element_subscr},
{Py_mp_ass_subscript, element_ass_subscr},
Expand Down
0