Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion Doc/library/xml.etree.elementtree.rst
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ Functions
.. versionadded:: 3.2


.. function:: SubElement(parent, tag, attrib={}, **extra)
.. function:: SubElement(parent, tag, /, attrib={}, **extra)

Subelement factory. This function creates an element instance, and appends
it to an existing element.
Expand All @@ -704,6 +704,9 @@ Functions
attributes. *extra* contains additional attributes, given as keyword
arguments. Returns an element instance.

.. versionchanged:: 3.15
*parent* and *tag* are now positional-only parameters.


.. function:: tostring(element, encoding="us-ascii", method="xml", *, \
xml_declaration=None, default_namespace=None, \
Expand Down
20 changes: 20 additions & 0 deletions Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,26 @@ def test_attrib(self):
self.assertEqual(ET.tostring(elem),
b'<test a="&#13;" b="&#13;&#10;" c="&#09;&#10;&#13; " d="&#10;&#10;&#13;&#13;&#09;&#09; " />')

def test_subelement_positional_only_parameters(self):
# Test SubElement positional-only parameters (gh-144270).
parent = ET.Element('parent')

# 'parent' and 'tag' are positional-only
with self.assertRaises(TypeError):
ET.SubElement(parent=parent, tag='fail')
with self.assertRaises(TypeError):
ET.SubElement(parent, tag='fail')

# 'attrib' can be passed as keyword
sub1 = ET.SubElement(parent, 'sub1', attrib={'key': 'value'})
self.assertEqual(sub1.get('key'), 'value')

# 'tag' and 'parent' as kwargs become XML attributes, not func params
sub2 = ET.SubElement(parent, 'sub2', tag='foo', parent='bar')
self.assertEqual(sub2.tag, 'sub2')
self.assertEqual(sub2.get('tag'), 'foo')
self.assertEqual(sub2.get('parent'), 'bar')

def test_makeelement(self):
# Test makeelement handling.

Expand Down
2 changes: 1 addition & 1 deletion Lib/xml/etree/ElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def itertext(self):
yield t


def SubElement(parent, tag, attrib={}, **extra):
def SubElement(parent, tag, /, attrib={}, **extra):
"""Subelement factory which creates an element instance, and appends it
to an existing parent.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Made the *parent* and *tag* parameters of
:func:`xml.etree.ElementTree.SubElement` positional-only, matching the
behavior of the C accelerator.
Loading