Skip to content

Commit d12f714

Browse files
committed
Clarify TokenInfo field semantics and positions
1 parent 7e7b3b7 commit d12f714

1 file changed

Lines changed: 43 additions & 10 deletions

File tree

‎Doc/library/tokenize.rst‎

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,34 +61,67 @@ The primary entry point is a :term:`generator`:
6161

6262
.. class:: TokenInfo(type, string, start, end, line)
6363

64-
A :term:`named tuple` with five fields describing a token:
64+
A :term:`named tuple` describing a token. It has five fields and an
65+
additional :attr:`exact_type` property.
6566

6667
.. attribute:: type
6768

68-
The token type.
69+
An integer identifying the token type, as defined by the constants in
70+
:mod:`token`, such as :data:`~token.NAME` and :data:`~token.STRING`.
71+
Operators and delimiters have type :data:`~token.OP`; use
72+
:attr:`exact_type` for their specific token type.
6973

7074
.. attribute:: string
7175

72-
The token string.
76+
The token text as a :class:`str`, usually as it appears in the source.
77+
For :data:`~token.STRING` tokens, this includes the quotes, any prefix,
78+
and escape sequences, without evaluating them.
79+
80+
For an :data:`~token.ENCODING` token, this is the encoding name.
81+
Tokens inserted without corresponding source text can have an empty
82+
string. In :data:`~token.FSTRING_MIDDLE` and
83+
:data:`~token.TSTRING_MIDDLE` tokens, doubled braces are replaced by
84+
single braces.
7385

7486
.. attribute:: start
7587

76-
A 2-tuple ``(srow, scol)`` of ints specifying the row and column where
77-
the token begins in the source.
88+
A 2-tuple ``(srow, scol)`` of ints specifying the inclusive start
89+
position of the token in the source. Rows are numbered from 1 and
90+
columns from 0. Columns count Unicode code points; a tab counts as
91+
one column, without expanding it to a tab stop.
92+
93+
For an :data:`~token.ENCODING` token, this is ``(0, 0)``.
7894

7995
.. attribute:: end
8096

81-
A 2-tuple ``(erow, ecol)`` of ints specifying the row and column where
82-
the token ends in the source.
97+
A 2-tuple ``(erow, ecol)`` of ints specifying the exclusive end
98+
position of the token, using the same coordinate conventions as
99+
:attr:`start`. For a token spanning multiple lines, *erow* is the
100+
final row and *ecol* is the column on that row.
101+
102+
For example, the name ``spam`` at the beginning of the first line
103+
has :attr:`start` equal to ``(1, 0)`` and :attr:`end` equal to ``(1, 4)``.
104+
105+
For an :data:`~token.ENCODING` token, this is ``(0, 0)``.
106+
A :data:`~token.NEWLINE` or :data:`~token.NL` token inserted at the end
107+
of input without a final newline can have an empty :attr:`string`
108+
and an end column one beyond the source text.
83109

84110
.. attribute:: line
85111

86-
The physical line or lines on which the token was found.
112+
A :class:`str` containing the full physical source line on which
113+
the token was found, including surrounding text and the line terminator
114+
if present. For a token spanning multiple lines, this can contain
115+
several physical lines. It is empty for :data:`~token.ENCODING` and
116+
:data:`~token.ENDMARKER` tokens.
87117

88118
.. attribute:: exact_type
89119

90-
The exact operator type for :data:`~token.OP` tokens. For all other
91-
token types, this equals :attr:`type`.
120+
An integer identifying the specific operator type for
121+
:data:`~token.OP` tokens, as defined by the constants in :mod:`token`.
122+
For all other token types, this equals :attr:`type`.
123+
For example, a ``+`` token has :attr:`type` equal to :data:`~token.OP`
124+
and :attr:`exact_type` equal to :data:`~token.PLUS`.
92125

93126
.. versionadded:: 3.3
94127

0 commit comments

Comments
 (0)