[issue38941] xml.etree.ElementTree.Element inconsistent warning for bool

Philip Rowlands report at bugs.python.org
Fri Nov 29 08:21:05 EST 2019


New submission from Philip Rowlands <pythonbugs at dimebar.com>:

Steps to reproduce:

$ python3.7
Python 3.7.2 (default, May 13 2019, 13:52:56)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import xml.etree.ElementTree as ET
>>> bool(ET.fromstring("<a><b><c></c></b></a>").find(".//c"))
False


$ python3.7
Python 3.7.2 (default, May 13 2019, 13:52:56)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.modules['_elementtree'] = None
>>> import xml.etree.ElementTree as ET
>>> bool(ET.fromstring("<a><b><c></c></b></a>").find(".//c"))
__main__:1: FutureWarning: The behavior of this method will change in future versions.  Use specific 'len(elem)' or 'elem is not None' test instead.


This is (almost) the smallest test case, but in real code what I was trying to write was:
```
# check the result code is ok
if response.find("./result[@code='ok']"):
    return True
```

The unintuitive bool() behaviour was surprising, compared to other typical True / False determinations on objects, and I think what the FutureWarning is trying to avoid.

Please implement the same warning for bool(Element) in _elementtree.c as exists in ElementTree.py. bpo29204 was making similar alignments between the versions, but didn't consider this FutureWarning.

----------
components: Library (Lib)
messages: 357642
nosy: philiprowlands
priority: normal
severity: normal
status: open
title: xml.etree.ElementTree.Element inconsistent warning for bool
type: behavior

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38941>
_______________________________________


More information about the Python-bugs-list mailing list