Skip to content

Commit e2524cf

Browse files
[3.15] gh-156765: Fix inaccuracies in the ElementTree documentation (GH-156766) (GH-156848)
Corrected: * the parser argument of iterparse() is an instance of XMLParser or its subclass, not a subclass; * feed() accepts a string as well as encoded data; * keys() and items() do not return a list in the Python implementation, and the attributes are no longer returned in an arbitrary order. Removed the claims that the element tag, the attribute names and values, the text of a comment and the data of TreeBuilder.data() can be bytes. It is a Python 2 leftover. Documented instead what they can be. (cherry picked from commit 09ff4d4) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 7ff1378 commit e2524cf

2 files changed

Lines changed: 33 additions & 31 deletions

File tree

‎Doc/library/xml.etree.elementtree.rst‎

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -580,10 +580,9 @@ Functions
580580
.. function:: Comment(text=None)
581581

582582
Comment element factory. This factory function creates a special element
583-
that will be serialized as an XML comment by the standard serializer. The
584-
comment string can be either a bytestring or a Unicode string. *text* is a
585-
string containing the comment string. Returns an element instance
586-
representing a comment.
583+
that will be serialized as an XML comment by the standard serializer.
584+
*text* is a string containing the comment string.
585+
Returns an element instance representing a comment.
587586

588587
Note that :class:`XMLParser` skips over comments in the input
589588
instead of creating comment objects for them. An :class:`ElementTree` will
@@ -650,10 +649,11 @@ Functions
650649
``"pi"``, ``"start-ns"`` and ``"end-ns"``
651650
(the "ns" events are used to get detailed namespace
652651
information). If *events* is omitted, only ``"end"`` events are reported.
653-
*parser* is an optional parser instance. If not given, the standard
654-
:class:`XMLParser` parser is used. *parser* must be a subclass of
655-
:class:`XMLParser` and can only use the default :class:`TreeBuilder` as a
656-
target. Returns an :term:`iterator` providing ``(event, elem)`` pairs;
652+
*parser* is an optional parser instance.
653+
If not given, the standard :class:`XMLParser` parser is used.
654+
*parser* must be an instance of :class:`XMLParser` or its subclass
655+
and can only use the default :class:`TreeBuilder` as a target.
656+
Returns an :term:`iterator` providing ``(event, elem)`` pairs;
657657
it has a ``root`` attribute that references the root element of the
658658
resulting XML tree once *source* is fully read.
659659
The iterator has the :meth:`!close` method that closes the internal
@@ -729,8 +729,7 @@ Functions
729729
Subelement factory. This function creates an element instance, and appends
730730
it to an existing element.
731731

732-
The element name, attribute names, and attribute values can be either
733-
bytestrings or Unicode strings. *parent* is the parent element. *tag* is
732+
*parent* is the parent element. *tag* is
734733
the subelement name. *attrib* is an optional dictionary, containing element
735734
attributes. *extra* contains additional attributes, given as keyword
736735
arguments. Returns an element instance.
@@ -921,11 +920,22 @@ Element Objects
921920
Element class. This class defines the Element interface, and provides a
922921
reference implementation of this interface.
923922

924-
The element name, attribute names, and attribute values can be either
925-
bytestrings or Unicode strings. *tag* is the element name. *attrib* is
923+
*tag* is the element name. *attrib* is
926924
an optional dictionary, containing element attributes. *extra* contains
927925
additional attributes, given as keyword arguments.
928926

927+
The element name and the attribute names and values are strings or
928+
:class:`QName` instances, and the text and the tail are strings or
929+
``None``.
930+
The element name can also be :func:`Comment` or
931+
:func:`ProcessingInstruction`, which are used for special elements.
932+
If it is ``None``, the element itself is not serialized: only its text
933+
and its children are written, and its attributes are ignored.
934+
This can be used for a fragment which contains several elements.
935+
With ``method="html"`` the attribute value can also be ``None``,
936+
which produces an empty attribute (such as ``checked``).
937+
Other objects can be stored in the tree, but they cannot be serialized.
938+
929939
.. versionchanged:: 3.15
930940
*attrib* can now be a :class:`frozendict`.
931941

@@ -991,14 +1001,12 @@ Element Objects
9911001

9921002
.. method:: items()
9931003

994-
Returns the element attributes as a sequence of (name, value) pairs. The
995-
attributes are returned in an arbitrary order.
1004+
Returns the element attributes as (name, value) pairs.
9961005

9971006

9981007
.. method:: keys()
9991008

1000-
Returns the elements attribute names as a list. The names are returned
1001-
in an arbitrary order.
1009+
Returns the element attribute names.
10021010

10031011

10041012
.. method:: set(key, value)
@@ -1329,8 +1337,7 @@ TreeBuilder Objects
13291337

13301338
.. method:: data(data)
13311339

1332-
Adds text to the current element. *data* is a string. This should be
1333-
either a bytestring, or a Unicode string.
1340+
Adds text to the current element. *data* is a string.
13341341

13351342

13361343
.. method:: end(tag)
@@ -1433,7 +1440,8 @@ XMLParser Objects
14331440

14341441
.. method:: feed(data)
14351442

1436-
Feeds data to the parser. *data* is encoded data.
1443+
Feeds data to the parser. *data* is a string
1444+
or encoded data (:class:`bytes` or a :term:`bytes-like object`).
14371445

14381446

14391447
.. method:: flush()
@@ -1512,7 +1520,8 @@ XMLPullParser Objects
15121520

15131521
.. method:: feed(data)
15141522

1515-
Feed the given bytes data to the parser.
1523+
Feed the given data to the parser. *data* is a string
1524+
or encoded data (:class:`bytes` or a :term:`bytes-like object`).
15161525

15171526
.. method:: flush()
15181527

‎Lib/xml/etree/ElementTree.py‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,6 @@ class Element:
129129
want to check if an element is truly empty, you should check BOTH
130130
its length AND its text attribute.
131131
132-
The element tag, attribute names, and attribute values can be either
133-
bytes or strings.
134-
135132
*tag* is the element name. *attrib* is an optional dictionary containing
136133
element attributes. *extra* are additional element attributes given as
137134
keyword arguments.
@@ -359,21 +356,17 @@ def set(self, key, value):
359356
self.attrib[key] = value
360357

361358
def keys(self):
362-
"""Get list of attribute names.
359+
"""Get attribute names.
363360
364-
Names are returned in an arbitrary order, just like an ordinary
365-
Python dict. Equivalent to attrib.keys()
361+
Equivalent to attrib.keys()
366362
367363
"""
368364
return self.attrib.keys()
369365

370366
def items(self):
371-
"""Get element attributes as a sequence.
372-
373-
The attributes are returned in arbitrary order. Equivalent to
374-
attrib.items().
367+
"""Get element attributes as (name, value) pairs.
375368
376-
Return a list of (name, value) tuples.
369+
Equivalent to attrib.items().
377370
378371
"""
379372
return self.attrib.items()

0 commit comments

Comments
 (0)