Skip to content

gh-144846: make Element tag positional-only#144876

Open
NekoAsakura wants to merge 1 commit intopython:mainfrom
NekoAsakura:gh-144846-element-init-positional-only
Open

gh-144846: make Element tag positional-only#144876
NekoAsakura wants to merge 1 commit intopython:mainfrom
NekoAsakura:gh-144846-element-init-positional-only

Conversation

@NekoAsakura
Copy link

@NekoAsakura NekoAsakura commented Feb 16, 2026

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/

@bedevere-app
Copy link

bedevere-app bot commented Feb 16, 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.

@NekoAsakura
Copy link
Author

@bkap123 cheers for the review. While I'm at it, do I need to make the same change over on #144845 (the SubElement one) as well, or is that alright as is?

@aisk
Copy link
Member

aisk commented Feb 16, 2026

This PR changes the behavior of the public APIs and may affect users' code, so a news entry is required. #144845 is the same.

@NekoAsakura NekoAsakura force-pushed the gh-144846-element-init-positional-only branch from 6f67486 to aa2871b Compare February 16, 2026 15:43
@NekoAsakura
Copy link
Author

Thanks for the reminder! I've added news entries to both PRs.

@bkap123
Copy link
Contributor

bkap123 commented Feb 16, 2026

@bkap123 cheers for the review. While I'm at it, do I need to make the same change over on #144845 (the SubElement one) as well, or is that alright as is?

Left small suggestion

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