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

gh-144270: make SubElement parent and tag positional-only by NekoAsakura · Pull Request #144845 · python/cpython · GitHub

/ cpython Public
Merged
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
10 changes: 8 additions & 2 deletions 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 @@ -691,7 +691,7 @@ Functions
.. versionadded:: 3.2


.. function:: SubElement(parent, tag, attrib={}, **extra)
.. function:: SubElement(parent, tag, /, attrib={}, **extra)

Subelement factory. This function creates an element instance, and appends
it to an existing element.
Expand All @@ -705,6 +705,9 @@ Functions
.. versionchanged:: 3.15
*attrib* can now be a :class:`frozendict`.

.. versionchanged:: next
*parent* and *tag* are now positional-only parameters.


.. function:: tostring(element, encoding="us-ascii", method="xml", *, \
xml_declaration=None, default_namespace=None, \
Expand Down Expand Up @@ -880,7 +883,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 @@ -893,6 +896,9 @@ Element Objects
.. versionchanged:: 3.15
*attrib* can now be a :class:`frozendict`.

.. versionchanged:: next
*tag* is now a positional-only parameter.


.. attribute:: tag

Expand Down
35 changes: 35 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,19 @@ 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')

# 'tag' and 'attrib' as kwarg/attribute names
e = ET.Element('e', attrib={'attrib': 'foo'}, tag='bar')
self.assertEqual(e.tag, 'e')
self.assertEqual(e.get('attrib'), 'foo')
self.assertEqual(e.get('tag'), 'bar')

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

Expand Down Expand Up @@ -484,6 +497,28 @@ def test_attrib(self):
self.assertEqual(ET.tostring(elem),
b'<test a="&#13;" b="&#13;&#10;" c="&#09;&#10;&#13; " d="&#10;&#10;&#13;&#13;&#09;&#09; " />')

def test_subelement_positional_only_parameter(self):
# Test SubElement positional-only parameters (gh-144270).
parent = ET.Element('parent')

# 'parent' and 'tag' are positional-only
with self.assertRaises(TypeError):
ET.SubElement(parent=parent, tag='fail')
with self.assertRaises(TypeError):
ET.SubElement(parent, tag='fail')

# 'attrib' can be passed as keyword
sub1 = ET.SubElement(parent, 'sub1', attrib={'key': 'value'})
self.assertEqual(sub1.get('key'), 'value')
Comment thread
NekoAsakura marked this conversation as resolved.

# 'tag' and 'parent' as kwargs become XML attributes, not func params
sub2 = ET.SubElement(parent, 'sub2', attrib={'attrib': 'foo'},
tag='bar', parent='baz')
self.assertEqual(sub2.tag, 'sub2')
self.assertEqual(sub2.get('attrib'), 'foo')
self.assertEqual(sub2.get('tag'), 'bar')
self.assertEqual(sub2.get('parent'), 'baz')

def test_makeelement(self):
# Test makeelement handling.

Expand Down
4 changes: 2 additions & 2 deletions 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, frozendict)):
raise TypeError("attrib must be dict or frozendict, not %s" % (
attrib.__class__.__name__,))
Expand Down Expand Up @@ -416,7 +416,7 @@ def itertext(self):
yield t


def SubElement(parent, tag, attrib={}, **extra):
def SubElement(parent, tag, /, attrib={}, **extra):
"""Subelement factory which creates an element instance, and appends it
to an existing parent.

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,3 @@
Made the *tag* parameter of :class:`xml.etree.ElementTree.Element` and the
*parent* and *tag* parameters of :func:`xml.etree.ElementTree.SubElement`
positional-only, matching the behavior of the C accelerator.
Loading

Back | FazBrowse Home | New Git URL