Skip to content

Commit 47fbe38

Browse files
serhiy-storchakamiss-islington
authored andcommitted
gh-156765: Fix inaccuracies in the ElementTree documentation (GH-156766)
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 e9585e2 commit 47fbe38

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
@@ -551,10 +551,9 @@ Functions
551551
.. function:: Comment(text=None)
552552

553553
Comment element factory. This factory function creates a special element
554-
that will be serialized as an XML comment by the standard serializer. The
555-
comment string can be either a bytestring or a Unicode string. *text* is a
556-
string containing the comment string. Returns an element instance
557-
representing a comment.
554+
that will be serialized as an XML comment by the standard serializer.
555+
*text* is a string containing the comment string.
556+
Returns an element instance representing a comment.
558557

559558
Note that :class:`XMLParser` skips over comments in the input
560559
instead of creating comment objects for them. An :class:`ElementTree` will
@@ -621,10 +620,11 @@ Functions
621620
``"pi"``, ``"start-ns"`` and ``"end-ns"``
622621
(the "ns" events are used to get detailed namespace
623622
information). If *events* is omitted, only ``"end"`` events are reported.
624-
*parser* is an optional parser instance. If not given, the standard
625-
:class:`XMLParser` parser is used. *parser* must be a subclass of
626-
:class:`XMLParser` and can only use the default :class:`TreeBuilder` as a
627-
target. Returns an :term:`iterator` providing ``(event, elem)`` pairs;
623+
*parser* is an optional parser instance.
624+
If not given, the standard :class:`XMLParser` parser is used.
625+
*parser* must be an instance of :class:`XMLParser` or its subclass
626+
and can only use the default :class:`TreeBuilder` as a target.
627+
Returns an :term:`iterator` providing ``(event, elem)`` pairs;
628628
it has a ``root`` attribute that references the root element of the
629629
resulting XML tree once *source* is fully read.
630630
The iterator has the :meth:`!close` method that closes the internal
@@ -696,8 +696,7 @@ Functions
696696
Subelement factory. This function creates an element instance, and appends
697697
it to an existing element.
698698

699-
The element name, attribute names, and attribute values can be either
700-
bytestrings or Unicode strings. *parent* is the parent element. *tag* is
699+
*parent* is the parent element. *tag* is
701700
the subelement name. *attrib* is an optional dictionary, containing element
702701
attributes. *extra* contains additional attributes, given as keyword
703702
arguments. Returns an element instance.
@@ -888,11 +887,22 @@ Element Objects
888887
Element class. This class defines the Element interface, and provides a
889888
reference implementation of this interface.
890889

891-
The element name, attribute names, and attribute values can be either
892-
bytestrings or Unicode strings. *tag* is the element name. *attrib* is
890+
*tag* is the element name. *attrib* is
893891
an optional dictionary, containing element attributes. *extra* contains
894892
additional attributes, given as keyword arguments.
895893

894+
The element name and the attribute names and values are strings or
895+
:class:`QName` instances, and the text and the tail are strings or
896+
``None``.
897+
The element name can also be :func:`Comment` or
898+
:func:`ProcessingInstruction`, which are used for special elements.
899+
If it is ``None``, the element itself is not serialized: only its text
900+
and its children are written, and its attributes are ignored.
901+
This can be used for a fragment which contains several elements.
902+
With ``method="html"`` the attribute value can also be ``None``,
903+
which produces an empty attribute (such as ``checked``).
904+
Other objects can be stored in the tree, but they cannot be serialized.
905+
896906
.. versionchanged:: 3.15
897907
*attrib* can now be a :class:`frozendict`.
898908

@@ -958,14 +968,12 @@ Element Objects
958968

959969
.. method:: items()
960970

961-
Returns the element attributes as a sequence of (name, value) pairs. The
962-
attributes are returned in an arbitrary order.
971+
Returns the element attributes as (name, value) pairs.
963972

964973

965974
.. method:: keys()
966975

967-
Returns the elements attribute names as a list. The names are returned
968-
in an arbitrary order.
976+
Returns the element attribute names.
969977

970978

971979
.. method:: set(key, value)
@@ -1296,8 +1304,7 @@ TreeBuilder Objects
12961304

12971305
.. method:: data(data)
12981306

1299-
Adds text to the current element. *data* is a string. This should be
1300-
either a bytestring, or a Unicode string.
1307+
Adds text to the current element. *data* is a string.
13011308

13021309

13031310
.. method:: end(tag)
@@ -1400,7 +1407,8 @@ XMLParser Objects
14001407

14011408
.. method:: feed(data)
14021409

1403-
Feeds data to the parser. *data* is encoded data.
1410+
Feeds data to the parser. *data* is a string
1411+
or encoded data (:class:`bytes` or a :term:`bytes-like object`).
14041412

14051413

14061414
.. method:: flush()
@@ -1479,7 +1487,8 @@ XMLPullParser Objects
14791487

14801488
.. method:: feed(data)
14811489

1482-
Feed the given bytes data to the parser.
1490+
Feed the given data to the parser. *data* is a string
1491+
or encoded data (:class:`bytes` or a :term:`bytes-like object`).
14831492

14841493
.. method:: flush()
14851494

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)