Skip to content

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

Merged
serhiy-storchaka merged 2 commits into
python:mainfrom
NekoAsakura:gh-144270-subelement-positional-only
Mar 30, 2026
Merged

serhiy-storchaka merged 2 commits into
python:mainfrom
NekoAsakura:gh-144270-subelement-positional-only

Conversation

@NekoAsakura

@NekoAsakura NekoAsakura commented Feb 15, 2026 •

Copy link
Copy Markdown
Member

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 tag → def 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

python-cla-bot Bot commented Feb 15, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app

bedevere-app Bot commented Feb 15, 2026

Copy link
Copy Markdown

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.

Comment thread Lib/test/test_xml_etree.py Resolved

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. 👍

Comment thread Doc/library/xml.etree.elementtree.rst Resolved
Comment thread Lib/test/test_xml_etree.py Resolved
@serhiy-storchaka

Copy link
Copy Markdown
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
Copy Markdown
Member 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.

@serhiy-storchaka
serhiy-storchaka merged commit afd8113 into python:main Mar 30, 2026
49 checks passed
@NekoAsakura
NekoAsakura deleted the gh-144270-subelement-positional-only branch March 30, 2026 16:42
@serhiy-storchaka

Copy link
Copy Markdown
Member

Merged. Thank you for your contribution, @NekoAsakura.

maurycy added a commit to maurycy/cpython that referenced this pull request Mar 30, 2026
…ktor

* upstream/main: (119 commits)
  pythongh-144270: Make SubElement parent and tag positional-only (pythonGH-144845)
  pythongh-146558: JIT optimize dict access for objects with known hash (python#146559)
  pythongh-139922: always run MSVC 64-bit tail-calling CI (pythonGH-146570)
  pythongh-126835: Fix _PY_IS_SMALL_INT() macro (python#146631)
  pythongh-146587: fix type slot assignment incase of multiple slots for same name (python#146593)
  pythongh-138122: Add differential flame graph (python#145785)
  pythongh-146416: Emscripten: Improve standard stream handling in node_entry.mjs (python#146417)
  pythongh-146444: Don't package as part of iOS 'build hosts' target (python#146628)
  pythongh-138850: Add --disable-epoll to configure (pythonGH-145768)
  pythongh-146444: Make Platforms/Apple/ compatible with Python 3.9 (python#146624)
  pythongh-138577: Fix keyboard shortcuts in getpass with echo_char (python#141597)
  pythongh-146556: Fix infinite loop in annotationlib.get_annotations() on circular __wrapped__ (python#146557)
  pythongh-146579: _zstd: Fix decompression options dict error message (python#146577)
  pythongh-146083: Upgrade bundled Expat to 2.7.5 (python#146085)
  pythongh-146080: fix a crash in SNI callbacks when the SSL object is gone (python#146573)
  pythongh-146090: fix memory management of internal `sqlite3` callback contexts (python#146569)
  pythongh-145876: Do not mask KeyErrors raised during dictionary unpacking in call (pythonGH-146472)
  pythongh-146004: fix test_args_from_interpreter_flags on windows (python#146580)
  pythongh-139003: Use frozenset for module level attributes in _pyrepl.utils (python#139004)
  pythonGH-146527: Add more data to GC statistics and add it to PyDebugOffsets (python#146532)
  ...
clin1234 pushed a commit to clin1234/cpython that referenced this pull request Apr 16, 2026
…onGH-144845)

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.
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
…onGH-144845)

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants