Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ def check_element(element):
self.assertRegex(repr(element), r"^<Element 't\xe4g' at 0x.*>$")
element = ET.Element("tag", key="value")

# Verify type checking for ElementTree constructor
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make a separate test method, say "test_constructor" and put that test before test_interface? TiA


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add also a test where only an Element-like class is tested (only with a tag attribute, nothing else, not even inheriting from xml.etree.Element).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I tried this in a boilerplate file:

class element impl:
    def __init__(self, tag):
        self.tag = tag

..but an instance of that wasn't enough to be able to pass as a root element to an ElementTree instance, call write, and have it work as normal (assuming I understood you properly). I ended up needing to add iter(), items(), __len__(), text, and tail before it would. Was that what you meant?

As a side thought, if ElementTree supports custom element implementations, should I remove the type check and change iselement to check for all of those attributes instead?

with self.assertRaises(TypeError):
tree = ET.ElementTree("")
with self.assertRaises(TypeError):
tree = ET.ElementTree(ET.ElementTree())

# Make sure all standard element methods exist.

def check_method(method):
Expand Down
8 changes: 6 additions & 2 deletions Lib/xml/etree/ElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,9 @@ class ElementTree:

"""
def __init__(self, element=None, file=None):
# assert element is None or iselement(element)
if element is not None and not iselement(element):
raise TypeError(f"element must be etree.Element, "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would appreciate either writing xml.etree.Element or Element only.

I also suspect that the isinstance() check was removed so that elements not inheriting from xml.Element still work, or elements that implement the XML interface without directly inheriting from it.

And the minimal element interface is "tag".

f"not {type(element).__name__}")
self._root = element # first node
if file:
self.parse(file)
Expand All @@ -543,7 +545,9 @@ def _setroot(self, element):
with the given element. Use with care!

"""
# assert iselement(element)
if not iselement(element):
raise TypeError(f"element must be etree.Element, "
f"not {type(element).__name__}")
self._root = element

def parse(self, source, parser=None):
Expand Down
Loading