Skip to content

gh-144270: make SubElement parent and tag positional-only#144845

Open
NekoAsakura wants to merge 2 commits intopython:mainfrom
NekoAsakura:gh-144270-subelement-positional-only
Open

gh-144270: make SubElement parent and tag positional-only#144845
NekoAsakura wants to merge 2 commits intopython:mainfrom
NekoAsakura:gh-144270-subelement-positional-only

Conversation

@NekoAsakura
Copy link

@NekoAsakura NekoAsakura commented Feb 15, 2026

Issue: #144270

Make parent and tag positional-only in xml.etree.ElementTree.SubElement to match the C accelerator.

The C implementation (_elementtree.SubElement) uses PyArg_ParseTuple, which inherently makes parent and tag positional-only. The Python fallback used def SubElement(parent, tag, attrib={}, **extra): which allowed parent and tag as keyword arguments. This inconsistency meant SubElement(parent=e, tag="x") worked only when the C accelerator was unavailable.

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

Changes

  • Lib/xml/etree/ElementTree.py: add / to SubElement signature
  • Lib/test/test_xml_etree.py: add test_subelement 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

Note

Element.__init__ has the same Python/C signature mismatch — tag is positional-only in the C version (PyArg_ParseTuple at _elementtree.c:419) but accepts keywords in the Python
fallback (ElementTree.py:167). I will create a separate issue for that to keep this PR focused.

def __init__(self, tag, attrib={}, **extra):

if (!PyArg_ParseTuple(args, "O|O!:Element", &tag, &PyDict_Type, &attrib))
return -1;


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

@python-cla-bot
Copy link

python-cla-bot bot commented Feb 15, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Feb 15, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

The C accelerator in _elementtree.c uses PyArg_ParseTuple, which
makes parent and tag positional-only. The Python fallback in
ElementTree.py allowed them as keyword arguments, creating an
inconsistency. Most users never noticed because the C version
overrides the Python version via "from _elementtree import *".

Add "/" after tag in the Python signature to match the C behavior.
The attrib parameter remains keyword-capable since the C version
accepts it via get_attrib_from_keywords().
@NekoAsakura NekoAsakura force-pushed the gh-144270-subelement-positional-only branch from d956c30 to 1551ff7 Compare February 16, 2026 15:41
Co-authored-by: bkap123 <97006829+bkap123@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants