Skip to content

Commit 5333939

Browse files
[3.15] gh-160036: Document tokenize.TokenInfo and its attributes (GH-160037) (#160112)
(cherry picked from commit 134a19e) Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
1 parent 0067af6 commit 5333939

1 file changed

Lines changed: 74 additions & 21 deletions

File tree

‎Doc/library/tokenize.rst‎

Lines changed: 74 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ colorizers for on-screen displays.
1515

1616
To simplify token stream handling, all :ref:`operator <operators>` and
1717
:ref:`delimiter <delimiters>` tokens and :data:`Ellipsis` are returned using
18-
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`.
18+
the generic :data:`~token.OP` token type.
19+
The :attr:`TokenInfo.exact_type` property gives the exact token type.
2120

2221

2322
.. warning::
@@ -40,25 +39,10 @@ The primary entry point is a :term:`generator`:
4039
:meth:`io.IOBase.readline` method of file objects. Each call to the
4140
function should return one line of input as bytes.
4241

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.
42+
The generator produces :class:`TokenInfo` objects.
5643

5744
.. versionchanged:: 3.1
58-
Added support for named tuples.
59-
60-
.. versionchanged:: 3.3
61-
Added support for ``exact_type``.
45+
Changed to yield :class:`TokenInfo` objects instead of plain tuples.
6246

6347
:func:`.tokenize` determines the source encoding of the file by looking for a
6448
UTF-8 BOM or encoding cookie, according to :pep:`263`.
@@ -71,9 +55,78 @@ The primary entry point is a :term:`generator`:
7155
a single line of input. However, :func:`generate_tokens` expects *readline*
7256
to return a str object rather than bytes.
7357

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

61+
.. class:: TokenInfo(type, string, start, end, line)
62+
63+
A :term:`named tuple` describing a token. It has five fields and an
64+
additional :attr:`exact_type` property.
65+
66+
.. versionadded:: 3.1
67+
68+
.. attribute:: type
69+
70+
An integer identifying the token type, as defined by the constants in
71+
:mod:`token`, such as :data:`~token.NAME` and :data:`~token.STRING`.
72+
Operators and delimiters have type :data:`~token.OP`; use
73+
:attr:`exact_type` for their specific token type.
74+
75+
.. attribute:: string
76+
77+
The token text as a :class:`str`.
78+
For :data:`~token.STRING` tokens, this includes the quotes, any prefix,
79+
and escape sequences, without evaluating them.
80+
81+
For an :data:`~token.ENCODING` token, this is the encoding name.
82+
Tokens inserted without corresponding source text can have an empty
83+
string. In :data:`~token.FSTRING_MIDDLE` and
84+
:data:`~token.TSTRING_MIDDLE` tokens, doubled braces are replaced by
85+
single braces.
86+
87+
.. attribute:: start
88+
89+
A 2-tuple ``(srow, scol)`` of ints specifying the inclusive start
90+
position of the token in the source. Rows are numbered from 1 and
91+
columns from 0. Columns count Unicode code points; a tab counts as
92+
one column, without expanding it to a tab stop.
93+
94+
For an :data:`~token.ENCODING` token, this is ``(0, 0)``.
95+
96+
.. attribute:: end
97+
98+
A 2-tuple ``(erow, ecol)`` of ints specifying the exclusive end
99+
position of the token. Rows are numbered from 1 and columns from 0.
100+
For a token spanning multiple lines, *erow* is the final row and
101+
*ecol* is the column on that row.
102+
103+
For example, the name ``spam`` at the beginning of the first line
104+
has :attr:`start` equal to ``(1, 0)`` and :attr:`end` equal to ``(1, 4)``.
105+
106+
For an :data:`~token.ENCODING` token, this is ``(0, 0)``.
107+
A :data:`~token.NEWLINE` or :data:`~token.NL` token inserted at the end
108+
of input without a final newline can have an end column one beyond
109+
the source text.
110+
111+
.. attribute:: line
112+
113+
A :class:`str` containing the full source line on which
114+
the token was found, including surrounding text and the line terminator
115+
if present. For a token spanning multiple lines, this can contain
116+
several source lines. It is empty for :data:`~token.ENCODING` and
117+
:data:`~token.ENDMARKER` tokens.
118+
119+
.. attribute:: exact_type
120+
121+
An integer identifying the specific operator type for
122+
:data:`~token.OP` tokens, as defined by the constants in :mod:`token`.
123+
For all other token types, this equals :attr:`type`.
124+
For example, a ``+`` token has :attr:`type` equal to :data:`~token.OP`
125+
and :attr:`exact_type` equal to :data:`~token.PLUS`.
126+
127+
.. versionadded:: 3.3
128+
129+
77130
All constants from the :mod:`token` module are also exported from
78131
:mod:`!tokenize`.
79132

0 commit comments

Comments
 (0)