FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-144846: make Element tag positional-only by NekoAsakura · Pull Request #144876 · python/cpython · GitHub

/ cpython Public

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 commented Feb 16, 2026
edited by github-actions Bot
Loading

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 tag → def __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 thread Lib/test/test_xml_etree.py Outdated

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

Copy link
Copy Markdown
Member 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 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 force-pushed the gh-144846-element-init-positional-only branch from 6f67486 to aa2871b Compare February 16, 2026 15:43

Copy link
Copy Markdown
Member Author

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

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-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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Maybe test also the attribute name 'attrib'?

Copy link
Copy Markdown
Member Author

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

NekoAsakura deleted the gh-144846-element-init-positional-only branch March 30, 2026 15:33
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

4 participants


Back | FazBrowse Home | New Git URL