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

When testing element truth values, emit a DeprecationWarning in all implementations.

This had emitted a FutureWarning in the rarely used python-only implementation since ~2.7 and has always been documented as a behavior not to rely on.

Matching an element in a tree search but having it test False can be unexpected. Raising the warning enables making the choice to finally raise an exception for this ambiguous behavior in the future.
This commit is contained in:
Jacob Walls 2023-01-22 20:16:48 -05:00 committed by GitHub
parent 997073c28b
commit d717be04dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 59 additions and 5 deletions

View file

@ -200,9 +200,10 @@ class Element:
def __bool__(self):
warnings.warn(
"The behavior 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