Deprecate bool(Element)

Bug #2071312 reported by masklinn
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
lxml
Triaged
Medium
scoder

Bug Description

Currently, using an Element in a boolean context e.g.

    if Element('a'):

triggers a FutureWarning, matching elementtree, sort-of: in the standard library the warning was emitted by the Python implementation but not the accelerated C implementation.

https://github.com/python/cpython/issues/83122 proposed to add the warning to the C implementation, ultimately https://github.com/python/cpython/pull/31149 also *upgraded it to a DeprecationWarning*.

Maybe lxml could be updated to do the same?

I noticed that you'd participated in the original discussion and had explained the issue at the root of the warning, but I don't know if you followed closely, 5.2 still uses the FutureWarning, the 5.3 changelog makes no mention of it, and I found no mention of this change on either here or github.

Revision history for this message
masklinn (masklinn) wrote (last edit ):

Also discussions on the future of Element.__bool__ (wether to return a constant `True` or raise) are at https://github.com/python/cpython/issues/119577

I would assume your input would be appreciated as I believe lxml to be a pretty massively used provider of the ElementTree API.

Revision history for this message
scoder (scoder) wrote :

Thanks for the notification.

> upgraded it to a DeprecationWarning

Is that an "upgrade"? A DeprecationWarning is not even visible by default, whereas lxml's FutureWarning always was. Users of lxml either never walked the "if element:" path or ignored the warning almost forever, so making it invisible now would suggest that the change actually won't happen. But it probably will.

I'll keep the FutureWarning in for now and let CPython take the first step. I think it's reasonable to change the behaviour to "always true".

Changed in lxml:
importance: Undecided → Medium
status: New → Triaged
assignee: nobody → scoder (scoder)
Revision history for this message
masklinn (masklinn) wrote :

> Is that an "upgrade"? A DeprecationWarning is not even visible by default, whereas lxml's FutureWarning always was.

Yeah you're definitely not wrong on that, the software I generally work on has had enabled it for so long I kinda forgot it wasn't that by default.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.