Skip to content

Adjust deprecation warning re: testing truth value of xml.etree.ElementTree.Element #119577

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

Closed
jacobtylerwalls opened this issue May 26, 2024 · 7 comments
Assignees
Labels
3.14 bugs and security fixes topic-XML type-feature A feature request or enhancement

Comments

jacobtylerwalls added a commit to jacobtylerwalls/cpython that referenced this issue May 26, 2024
jacobtylerwalls added a commit to jacobtylerwalls/cpython that referenced this issue May 26, 2024
@AlexWaygood AlexWaygood added type-feature A feature request or enhancement 3.14 bugs and security fixes labels May 26, 2024
@serhiy-storchaka
Copy link
Member

AFAIK, the plan was to return True instead of raising an exception.

@jacobtylerwalls
Copy link
Contributor Author

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
Copy link
Member

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.

@gpshead
Copy link
Member

gpshead commented 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 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.

@serhiy-storchaka
Copy link
Member

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.

@jacobtylerwalls jacobtylerwalls 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 May 30, 2024
jacobtylerwalls added a commit to jacobtylerwalls/cpython that referenced this issue May 30, 2024
jacobtylerwalls added a commit to jacobtylerwalls/cpython that referenced this issue May 30, 2024
@jacobtylerwalls
Copy link
Contributor Author

What that means is that we need a PR to update the docs and the DeprecationWarning error message to reflect the new plan.

New PR: #119762

gpshead pushed a commit that referenced this issue Jun 7, 2024
…s 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.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 7, 2024
… values in ElementTree (pythonGH-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.
(cherry picked from commit 6b60652)

Co-authored-by: Jacob Walls <[email protected]>
gpshead pushed a commit to gpshead/cpython that referenced this issue Jun 7, 2024
… values in ElementTree (pythonGH-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.
gpshead pushed a commit that referenced this issue Jun 7, 2024
…h values in ElementTree (GH-119762) (GH-120189)

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.
(cherry picked from commit 6b60652)

Co-authored-by: Jacob Walls <[email protected]>
pygeek pushed a commit to pygeek/cpython that referenced this issue Jun 7, 2024
… values in ElementTree (pythonGH-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.
noahbkim pushed a commit to hudson-trading/cpython that referenced this issue Jul 11, 2024
… values in ElementTree (pythonGH-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.
estyxx pushed a commit to estyxx/cpython that referenced this issue Jul 17, 2024
… values in ElementTree (pythonGH-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.
Yhg1s pushed a commit that referenced this issue Aug 6, 2024
…h values in Element tree (GH-119762) (#120190)

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.

Co-authored-by: Jacob Walls <[email protected]>
@hugovk
Copy link
Member

hugovk commented Aug 8, 2024

Triage: closing because the PR has been merged, please re-open if still needed. Thanks!

@hugovk hugovk closed this as completed Aug 8, 2024
millerdev added a commit to dimagi/commcare-hq that referenced this issue Apr 8, 2025
DeprecationWarning: Testing an element's truth value will always return
True in future versions. Use specific 'len(elem)' or 'elem is not None'
test instead.

python/cpython#119577
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.14 bugs and security fixes topic-XML type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

5 participants