self.serialize_check(element, '<tag key="value"><subtag /></tag>') # 4
element.remove(subelement)
self.serialize_check(element, '<tag key="value" />') # 5
- with self.assertRaisesRegex(ValueError,
- r'Element\.remove\(.+\): element not found'):
+ with self.assertRaises(ValueError):
element.remove(subelement)
self.serialize_check(element, '<tag key="value" />') # 6
element[0:0] = [subelement, subelement, subelement]
self.assertEqual(e2.tag, 'group')
self.assertEqual(e2[0].tag, 'dogs')
+ def test_remove_errors(self):
+ e = ET.Element('tag')
+ with self.assertRaisesRegex(ValueError,
+ r"<Element 'subtag'.*> not in <Element 'tag'.*>"):
+ e.remove(ET.Element('subtag'))
+ with self.assertRaisesRegex(TypeError,
+ r".*\bElement, not type"):
+ e.remove(ET.Element)
+ with self.assertRaisesRegex(TypeError,
+ r".*\bElement, not int"):
+ e.remove(1)
class BadElementTest(ElementTestCase, unittest.TestCase):
ValueError is raised if a matching element could not be found.
"""
- # assert iselement(element)
try:
self._children.remove(subelement)
except ValueError:
+ # to align the error type with the C implementation
+ if isinstance(subelement, type) or not iselement(subelement):
+ raise TypeError('expected an Element, not %s' %
+ type(subelement).__name__) from None
# to align the error message with the C implementation
- raise ValueError("Element.remove(x): element not found") from None
+ raise ValueError(f"{subelement!r} not in {self!r}") from None
def find(self, path, namespaces=None):
"""Find first matching element by tag name or path.
}
if (rc == 0) {
- PyErr_SetString(PyExc_ValueError,
- "Element.remove(x): element not found");
+ PyErr_Format(PyExc_ValueError, "%R not in %R", subelement, self);
return NULL;
}