◐ Shell
clean mode source ↗

gh-144846: make Element tag positional-only by NekoAsakura · Pull Request #144876 · python/cpython

Make tag positional-only in xml.etree.ElementTree.Element.__init__ to match the C accelerator.

The C implementation (_elementtree.Element) uses PyArg_ParseTuple, which inherently makes tag positional-only. The Python fallback used def __init__(self, tag, attrib={}, **extra): which allowed tag as a keyword argument. This inconsistency meant Element(tag="foo") worked only when the C accelerator was unavailable.

Same pattern as gh-144270 (SubElement).

Fix: add / after tagdef __init__(self, tag, /, attrib={}, **extra):

Changes

  • Lib/xml/etree/ElementTree.py: add / to Element.__init__ signature
  • Lib/test/test_xml_etree.py: add test_element_init covering positional-only enforcement, keyword attrib, and kwargs-as-XML-attributes parity
  • Doc/library/xml.etree.elementtree.rst: update signature and add versionchanged:: 3.15

📚 Documentation preview 📚: https://cpython-previews--144876.org.readthedocs.build/