Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
gh-144270: make Element and SubElement parameters positional-only
The C accelerator implementations use PyArg_ParseTuple, which
inherently enforces positional-only parameters. The Python fallback
allowed these as keyword arguments, creating a behavioral mismatch.

Make the tag parameter of Element.__init__ and the parent and tag
parameters of SubElement positional-only to align with the C
accelerator.
  • Loading branch information
NekoAsakura committed Feb 19, 2026
commit 38af9cf03a6f7a036292167749e6baff8cd7285d
10 changes: 8 additions & 2 deletions 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:: next
*parent* and *tag* are now positional-only parameters.


.. function:: tostring(element, encoding="us-ascii", method="xml", *, \
xml_declaration=None, default_namespace=None, \
Expand Down Expand Up @@ -879,7 +882,7 @@ Element Objects
:noindex:
:no-index:

.. class:: Element(tag, attrib={}, **extra)
.. class:: Element(tag, /, attrib={}, **extra)

Element class. This class defines the Element interface, and provides a
reference implementation of this interface.
Expand All @@ -889,6 +892,9 @@ Element Objects
an optional dictionary, containing element attributes. *extra* contains
additional attributes, given as keyword arguments.

.. versionchanged:: next
*tag* is now a positional-only parameter.


.. attribute:: tag

Expand Down
35 changes: 35 additions & 0 deletions Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,19 @@ def test_simpleops(self):
self.serialize_check(element,
'<tag key="value"><subtag /><subtag /></tag>')

def test_positional_only_parameter(self):
# Test Element positional-only parameters (gh-144846).

# 'tag' is positional-only
with self.assertRaises(TypeError):
ET.Element(tag='fail')

# 'tag' and 'attrib' as kwarg/attribute names
e = ET.Element('e', attrib={'attrib': 'foo'}, tag='bar')
self.assertEqual(e.tag, 'e')
self.assertEqual(e.get('attrib'), 'foo')
self.assertEqual(e.get('tag'), 'bar')

def test_cdata(self):
# Test CDATA handling (etc).

Expand Down Expand Up @@ -484,6 +497,28 @@ 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_parameter(self):
Comment thread
NekoAsakura marked this conversation as resolved.
# 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')
Comment thread
NekoAsakura marked this conversation as resolved.

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

def test_makeelement(self):
# Test makeelement handling.

Expand Down
4 changes: 2 additions & 2 deletions Lib/xml/etree/ElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class Element:

"""

def __init__(self, tag, attrib={}, **extra):
def __init__(self, tag, /, attrib={}, **extra):
if not isinstance(attrib, dict):
raise TypeError("attrib must be dict, not %s" % (
attrib.__class__.__name__,))
Expand Down 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 *tag* parameter of :class:`xml.etree.ElementTree.Element` and the
*parent* and *tag* parameters of :func:`xml.etree.ElementTree.SubElement`
positional-only, matching the behavior of the C accelerator.
Loading