Skip to content

Commit 9c282db

Browse files
[3.14] gh-160036: Document tokenize.TokenInfo and its attributes (GH-160037) (#160113)
(cherry picked from commit 134a19e) Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
1 parent 54f8aa8 commit 9c282db

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
@@ -18,9 +18,8 @@ colorizers for on-screen displays.
1818

1919
To simplify token stream handling, all :ref:`operator <operators>` and
2020
:ref:`delimiter <delimiters>` tokens and :data:`Ellipsis` are returned using
21-
the generic :data:`~token.OP` token type. The exact
22-
type can be determined by checking the ``exact_type`` property on the
23-
:term:`named tuple` returned from :func:`tokenize.tokenize`.
21+
the generic :data:`~token.OP` token type.
22+
The :attr:`TokenInfo.exact_type` property gives the exact token type.
2423

2524

2625
.. warning::
@@ -43,25 +42,10 @@ The primary entry point is a :term:`generator`:
4342
:meth:`io.IOBase.readline` method of file objects. Each call to the
4443
function should return one line of input as bytes.
4544

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

6047
.. versionchanged:: 3.1
61-
Added support for named tuples.
62-
63-
.. versionchanged:: 3.3
64-
Added support for ``exact_type``.
48+
Changed to yield :class:`TokenInfo` objects instead of plain tuples.
6549

6650
:func:`.tokenize` determines the source encoding of the file by looking for a
6751
UTF-8 BOM or encoding cookie, according to :pep:`263`.
@@ -74,9 +58,78 @@ The primary entry point is a :term:`generator`:
7458
a single line of input. However, :func:`generate_tokens` expects *readline*
7559
to return a str object rather than bytes.
7660

77-
The result is an iterator yielding named tuples, exactly like
61+
The result is an iterator yielding :class:`TokenInfo` objects, exactly like
7862
:func:`.tokenize`. It does not yield an :data:`~token.ENCODING` token.
7963

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

0 commit comments

Comments
 (0)