Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link
@NekoAsakura

Description

@NekoAsakura
Issue body actions

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

Linked PRs

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-XMLtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.