Skip to content

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

Closed
NekoAsakura wants to merge 1 commit into
python:mainfrom
NekoAsakura:gh-144846-element-init-positional-only
Closed

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

Conversation

@NekoAsakura

@NekoAsakuraNekoAsakura commented Feb 16, 2026

Copy link
Copy Markdown
Member

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/

Comment threadLib/test/test_xml_etree.py Outdated
@bedevere-app

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.

@NekoAsakura

Copy link
Copy Markdown
MemberAuthor

@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

aisk commented Feb 16, 2026

Copy link
Copy Markdown
Member

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
NekoAsakuraforce-pushed the gh-144846-element-init-positional-only branch from 6f67486 to aa2871bCompareFebruary 16, 2026 15:43
@NekoAsakura

Copy link
Copy Markdown
MemberAuthor

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

@brijkapadia

Copy link
Copy Markdown
Contributor

@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

@serhiy-storchakaserhiy-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. 👍

an optional dictionary, containing element attributes. *extra* contains
additional attributes, given as keyword arguments.

.. versionchanged:: 3.15

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.

Suggested change
.. versionchanged:: 3.15
.. versionchanged:: next


# 'attrib' can be passed as keyword
e = ET.Element('e', attrib={'key': 'value'})
self.assertEqual(e.get('key'), 'value')

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.

Maybe test also the attribute name 'attrib'?

@NekoAsakura

Copy link
Copy Markdown
MemberAuthor

Closing in favour of #144845, which already includes this change.

@NekoAsakura
NekoAsakura deleted the gh-144846-element-init-positional-only branch March 30, 2026 15:33
Sign up for freeto 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.

4 participants

@NekoAsakura@aisk@brijkapadia@serhiy-storchaka