diff --git a/Doc/library/xml.etree.elementtree.rst b/Doc/library/xml.etree.elementtree.rst index 177be9ff4bad25..f1e0921a1f144d 100644 --- a/Doc/library/xml.etree.elementtree.rst +++ b/Doc/library/xml.etree.elementtree.rst @@ -879,7 +879,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. @@ -889,6 +889,9 @@ Element Objects an optional dictionary, containing element attributes. *extra* contains additional attributes, given as keyword arguments. + .. versionchanged:: 3.15 + *tag* is now a positional-only parameter. + .. attribute:: tag diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index 93162f52ba0344..d2bdaed999f257 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -381,6 +381,22 @@ def test_simpleops(self): self.serialize_check(element, '') + 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') + + # 'attrib' can be passed as keyword + e = ET.Element('e', attrib={'key': 'value'}) + self.assertEqual(e.get('key'), 'value') + + # 'tag' as kwarg becomes an XML attribute, not the element tag + e = ET.Element('e', tag='foo') + self.assertEqual(e.tag, 'e') + self.assertEqual(e.get('tag'), 'foo') + def test_cdata(self): # Test CDATA handling (etc). diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py index e3d81a2c4560d9..193f9bc665772e 100644 --- a/Lib/xml/etree/ElementTree.py +++ b/Lib/xml/etree/ElementTree.py @@ -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__,)) diff --git a/Misc/NEWS.d/next/Library/2026-02-16-10-32-44.gh-issue-144846.4eda4bd2.rst b/Misc/NEWS.d/next/Library/2026-02-16-10-32-44.gh-issue-144846.4eda4bd2.rst new file mode 100644 index 00000000000000..c709b4aee0e702 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-02-16-10-32-44.gh-issue-144846.4eda4bd2.rst @@ -0,0 +1,2 @@ +Made the *tag* parameter of :class:`xml.etree.ElementTree.Element` +positional-only, matching the behavior of the C accelerator.