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

Filter by extension

Filter by extension .py  (2) .rst  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
5 changes: 4 additions & 1 deletion Doc/library/xml.etree.elementtree.rst
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
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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
.. versionchanged:: 3.15
.. versionchanged:: next

*tag* is now a positional-only parameter.


.. attribute:: tag

Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_xml_etree.py
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
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.

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

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 Lib/xml/etree/ElementTree.py
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
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
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
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

Back | FazBrowse Home | New Git URL