You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Adjust deprecation warning re: testing truth value of xml.etree.ElementTree.Element #119577
In favor of raising an error: #83122 (comment)
In favor of DeprecationWarning being the choice for an exception vs. FutureWarning being the choice to for a behavior change: #83122 (comment)
cc/ @gpshead who reviewed #31149, which changed FutureWarning to DeprecationWarning and documented that an exception would be raised.
Raising an exception in __bool__() is extremely uncommon behavior. For now, there is only one special case -- NotImplemented, and it is a very special case. On other hand, there is a precedence of datetime.time -- the midnight had the false boolean value, and now it has lost its special meaning and has the same boolean value like all other datetime.time values.
We did document that it would raise an exception in the future. But I agree that that is unusual.
Lets be conservative and delay the __bool__ behavior change on Element for a few more releases until the 3.16 timeframe to let the DeprecationWarning be more widely seen and acted upon in existing code. In 3.16+ we can (edit updated:) make its __bool__ method return true. Thus making all instances truthy as most class instances are.
What that means is that we need a PR to update the docs and the DeprecationWarning error message to reflect the new plan.
bool raising an exception does have valid uses: Such as when a class that is commonly mistaken for an actual boolean value in some API designs is easily misused in the wrong context. Element doesn't seem like one of those.
We cannot just remove __bool__. Element has __len__, and this is the reason why childless elements had the false boolean value, why this deprecation started. It should be a method that always returns True. Yes, inconsistency between __len__ and __bool__ is also unusual, but this considered lesser evil than having the false boolean value for otherwise normal elements.
Reacted by Gregory P. Smith, Jacob Walls, Alex Waygood and xmo-odoo
changed the title [-]Make testing truth value of `xml.etree.ElementTree.Element` raise an exception[/-][+]Adjust deprecation warning re: testing truth value of `xml.etree.ElementTree.Element`[/+]on May 30, 2024
Activity
pythongh-119577: Raise RuntimeError when testing element truth values…
pythongh-119577: Raise RuntimeError when testing element truth values…
serhiy-storchaka commentedon May 27, 2024
AFAIK, the plan was to return True instead of raising an exception.
jacobtylerwalls commentedon May 28, 2024
In favor of raising an error: #83122 (comment)
In favor of DeprecationWarning being the choice for an exception vs. FutureWarning being the choice to for a behavior change: #83122 (comment)
cc/ @gpshead who reviewed #31149, which changed FutureWarning to DeprecationWarning and documented that an exception would be raised.
serhiy-storchaka commentedon May 28, 2024
Raising an exception in
__bool__()is extremely uncommon behavior. For now, there is only one special case --NotImplemented, and it is a very special case. On other hand, there is a precedence ofdatetime.time-- the midnight had the false boolean value, and now it has lost its special meaning and has the same boolean value like all otherdatetime.timevalues.gpshead commentedon May 28, 2024
We did document that it would raise an exception in the future. But I agree that that is unusual.
Lets be conservative and delay the
__bool__behavior change onElementfor a few more releases until the 3.16 timeframe to let the DeprecationWarning be more widely seen and acted upon in existing code. In 3.16+ we can (edit updated:) make its__bool__method return true. Thus making all instances truthy as most class instances are.What that means is that we need a PR to update the docs and the DeprecationWarning error message to reflect the new plan.
bool raising an exception does have valid uses: Such as when a class that is commonly mistaken for an actual boolean value in some API designs is easily misused in the wrong context. Element doesn't seem like one of those.
serhiy-storchaka commentedon May 28, 2024
We cannot just remove
__bool__.Elementhas__len__, and this is the reason why childless elements had the false boolean value, why this deprecation started. It should be a method that always returnsTrue. Yes, inconsistency between__len__and__bool__is also unusual, but this considered lesser evil than having the false boolean value for otherwise normal elements.[-]Make testing truth value of `xml.etree.ElementTree.Element` raise an exception[/-][+]Adjust deprecation warning re: testing truth value of `xml.etree.ElementTree.Element`[/+]pythongh-119577: Adjust DeprecationWarning when testing element truth…
pythongh-119577: Adjust DeprecationWarning when testing element truth…
jacobtylerwalls commentedon Jun 6, 2024
New PR: #119762
gh-119577: Adjust DeprecationWarning when testing element truth value…
pythongh-119577: Adjust DeprecationWarning when testing element truth…
pythongh-119577: Adjust DeprecationWarning when testing element truth…
[3.13] gh-119577: Adjust DeprecationWarning when testing element trut…
pythongh-119577: Adjust DeprecationWarning when testing element truth…
pythongh-119577: Adjust DeprecationWarning when testing element truth…
pythongh-119577: Adjust DeprecationWarning when testing element truth…
[3.12] gh-119577: Adjust DeprecationWarning when testing element trut…
hugovk commentedon Aug 8, 2024
Triage: closing because the PR has been merged, please re-open if still needed. Thanks!
Testing an element's truth value is deprecated