Skip to content

Comments

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

Open
NekoAsakura wants to merge 1 commit intopython:mainfrom
NekoAsakura:gh-144270-subelement-positional-only
Open

gh-144270: make SubElement parent and tag positional-only#144845
NekoAsakura wants to merge 1 commit 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.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. 👍

@serhiy-storchaka
Copy link
Member

You could also merge two PRs in one.

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.
@NekoAsakura NekoAsakura force-pushed the gh-144270-subelement-positional-only branch from b9cacad to 38af9cf Compare February 19, 2026 21:46
@NekoAsakura
Copy link
Author

You could also merge two PRs in one.

Great. I've squashed the Element change from #144876 into this PR, you can close that one.

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.

3 participants