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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion 5 Doc/library/xml.etree.elementtree.rst
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ Element Objects
:noindex:
:no-index:

.. class:: Element(tag, attrib={}, **extra)
.. class:: Element(tag, /, attrib={}, **extra)

Element class. This class defines the Element interface, and provides a
reference implementation of this interface.
Expand All @@ -889,6 +889,9 @@ Element Objects
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

*tag* is now a positional-only parameter.


.. attribute:: tag

Expand Down
16 changes: 16 additions & 0 deletions 16 Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,22 @@ def test_simpleops(self):
self.serialize_check(element,
'<tag key="value"><subtag /><subtag /></tag>')

def test_positional_only_parameter(self):
# Test Element positional-only parameters (gh-144846).

# 'tag' is positional-only
with self.assertRaises(TypeError):
ET.Element(tag='fail')

# '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'?


# 'tag' as kwarg becomes an XML attribute, not the element tag
e = ET.Element('e', tag='foo')
self.assertEqual(e.tag, 'e')
self.assertEqual(e.get('tag'), 'foo')

def test_cdata(self):
# Test CDATA handling (etc).

Expand Down
2 changes: 1 addition & 1 deletion 2 Lib/xml/etree/ElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class Element:

"""

def __init__(self, tag, attrib={}, **extra):
def __init__(self, tag, /, attrib={}, **extra):
if not isinstance(attrib, dict):
raise TypeError("attrib must be dict, not %s" % (
attrib.__class__.__name__,))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Made the *tag* parameter of :class:`xml.etree.ElementTree.Element`
positional-only, matching the behavior of the C accelerator.
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.