-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-135640: Adds type checking to ElementTree.ElementTree constructor #135643
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I tried this in a boilerplate file:
..but an instance of that wasn't enough to be able to pass as a root element to an ElementTree instance, call As a side thought, if ElementTree supports custom element implementations, should I remove the type check and change |
||
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): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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): | ||
|
There was a problem hiding this comment.
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