[Python-Dev] Raising assertions on wrong element types in ElementTree

Eli Bendersky eliben at gmail.com
Fri Mar 16 08:38:49 CET 2012


Hi,

[Terry suggested in http://bugs.python.org/issue13782 to raise this
dilemma to python-dev. I concur.]

The Element class in ElementTree
(http://docs.python.org/py3k/library/xml.etree.elementtree.html) has
some methods for adding new children: append, insert and extend.
Currently the documentation states that extend raises AssertionError
when something that's not an Element is being passed to it, and the
others don't mention mention this case.

There are a number of problems with this:

1. The behavior of append, insert and extend should be similar in this respect
2. AssertionError is not the customary error in such case - TypeError
is much more suitable
3. The C implementation of ElementTree actually raises TypeError in
all these methods, by virtue of using PyArg_ParseTuple
4. The Python implementation (at least in 3.2) actually doesn't raise
even AssertionError in extend - this was commented out

The suggestion for 3.3 (where compatibility between the C and Python
implementations gets even more important, since the C one is now being
imported by default when available) is to raise TypeError in all 3
methods in the Python implementation, to match the C implementation,
and to modify the documentation accordingly.

There may appear to be a backwards compatibility here, since the doc
of extend mentions raising AssertionError - but as said above, the doc
is wrong, so no regressions in the code are be expected.

Does that sound reasonable (for 3.3)?

Does it make sense to also fix this in 3.2/2.7? Or fix only the
documentation? Or not touch them at all?

Thanks in advance,
Eli


More information about the Python-Dev mailing list