Skip to content

Commit 7e7b3b7

Browse files
committed
Document tokenize.TokenInfo and its attributes
1 parent f5bd077 commit 7e7b3b7

1 file changed

Lines changed: 38 additions & 19 deletions

File tree

‎Doc/library/tokenize.rst‎

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ colorizers for on-screen displays.
1616
To simplify token stream handling, all :ref:`operator <operators>` and
1717
:ref:`delimiter <delimiters>` tokens and :data:`Ellipsis` are returned using
1818
the generic :data:`~token.OP` token type. The exact
19-
type can be determined by checking the ``exact_type`` property on the
20-
:term:`named tuple` returned from :func:`tokenize.tokenize`.
19+
type can be determined by checking the :attr:`TokenInfo.exact_type` property on
20+
the :class:`TokenInfo` returned from :func:`tokenize.tokenize`.
2121

2222

2323
.. warning::
@@ -40,26 +40,11 @@ The primary entry point is a :term:`generator`:
4040
:meth:`io.IOBase.readline` method of file objects. Each call to the
4141
function should return one line of input as bytes.
4242

43-
The generator produces 5-tuples with these members: the token type; the
44-
token string; a 2-tuple ``(srow, scol)`` of ints specifying the row and
45-
column where the token begins in the source; a 2-tuple ``(erow, ecol)`` of
46-
ints specifying the row and column where the token ends in the source; and
47-
the line on which the token was found. The line passed (the last tuple item)
48-
is the *physical* line. The 5 tuple is returned as a :term:`named tuple`
49-
with the field names:
50-
``type string start end line``.
51-
52-
The returned :term:`named tuple` has an additional property named
53-
``exact_type`` that contains the exact operator type for
54-
:data:`~token.OP` tokens. For all other token types ``exact_type``
55-
equals the named tuple ``type`` field.
43+
The generator produces :class:`TokenInfo` objects.
5644

5745
.. versionchanged:: 3.1
5846
Added support for named tuples.
5947

60-
.. versionchanged:: 3.3
61-
Added support for ``exact_type``.
62-
6348
:func:`.tokenize` determines the source encoding of the file by looking for a
6449
UTF-8 BOM or encoding cookie, according to :pep:`263`.
6550

@@ -71,9 +56,43 @@ The primary entry point is a :term:`generator`:
7156
a single line of input. However, :func:`generate_tokens` expects *readline*
7257
to return a str object rather than bytes.
7358

74-
The result is an iterator yielding named tuples, exactly like
59+
The result is an iterator yielding :class:`TokenInfo` objects, exactly like
7560
:func:`.tokenize`. It does not yield an :data:`~token.ENCODING` token.
7661

62+
.. class:: TokenInfo(type, string, start, end, line)
63+
64+
A :term:`named tuple` with five fields describing a token:
65+
66+
.. attribute:: type
67+
68+
The token type.
69+
70+
.. attribute:: string
71+
72+
The token string.
73+
74+
.. attribute:: start
75+
76+
A 2-tuple ``(srow, scol)`` of ints specifying the row and column where
77+
the token begins in the source.
78+
79+
.. attribute:: end
80+
81+
A 2-tuple ``(erow, ecol)`` of ints specifying the row and column where
82+
the token ends in the source.
83+
84+
.. attribute:: line
85+
86+
The physical line or lines on which the token was found.
87+
88+
.. attribute:: exact_type
89+
90+
The exact operator type for :data:`~token.OP` tokens. For all other
91+
token types, this equals :attr:`type`.
92+
93+
.. versionadded:: 3.3
94+
95+
7796
All constants from the :mod:`token` module are also exported from
7897
:mod:`!tokenize`.
7998

0 commit comments

Comments
 (0)