Skip to content

Element.__init__ tag parameter has positional-only inconsistency #144846

@NekoAsakura

Description

@NekoAsakura

Bug report

Bug description:

Element.__init__ has the same Python/C signature inconsistency as SubElement (gh-144270).

Python fallback:

def __init__(self, tag, attrib={}, **extra):

tag accepts keyword arguments: Element(tag="foo") works.

C accelerator:

if (!PyArg_ParseTuple(args, "O|O!:Element", &tag, &PyDict_Type, &attrib))
return -1;

PyArg_ParseTuple only parses positional args, so tag is positional-only. Element(tag="foo") raises TypeError.

Proposed fix

-    def __init__(self, tag, attrib={}, **extra):
+    def __init__(self, tag, /, attrib={}, **extra):

Same pattern as the SubElement fix in gh-144270.

Reproduce

import sys, importlib
import xml.etree.ElementTree as ET

root = ET.Element("root")

try:
    ET.Element(tag="foo")
except TypeError:
    print("C rejects Element(tag=...)")

sys.modules['_elementtree'] = None
importlib.reload(ET)

e = ET.Element(tag="foo")
print(f"pure-python accepted it: {e.tag}") 

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-XMLtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions