gh-83122: Deprecate testing element truth values in ElementTree by jacobtylerwalls · Pull Request #31149 · python/cpython (original) (raw)

Forgive me if this is stating the obvious, but as I see the status quo, users are tempted to write if node under the mistaken belief that all nodes are truthy, and then get bitten when the condition excludes childless nodes. I made this mistake a few times when learning ElementTree, and it's something I have to remember to look for in code review. I would have much preferred to see the warning! :-)

I'm guessing that the idea is that nodes will always be truthy, so you can use if node as a shorthand (in most contexts) for if node is not None right?

I might be misunderstanding you, but since nodes are not always truthy, I'm hoping for ElementTree to discourage this usage.

I'm not sure if it's worth requiring all elementtree applications to change.

The applications that need to change will be ones that desired if node to mean if node is not None and len(node), which seems like strange cases to commingle (especially in the negative, commingling None and childless nodes).

If the warning was only shown by the Python version, very few people have seen it, right, because (I presume) the C version is always used if it can be imported.

Right. Of course if the ultimate resolution here is a warning rather than an exception, we could just change FutureWarning to RuntimeWarning and be done with it. Then it could be something for linters to make an effort to detect and warn about.