Skip to content

Commit 24df7e9

Browse files
committed
refactor: tweak up and type-hint the token parsing
1 parent 07b76b2 commit 24df7e9

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

coverage/parser.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,25 @@ def _raw_parse(self) -> None:
127127

128128
# Tokenize, to find excluded suites, to find docstrings, and to find
129129
# multi-line statements.
130-
indent = 0
131-
exclude_indent = 0
132-
excluding = False
133-
excluding_decorators = False
134-
prev_toktype = token.INDENT
135-
first_line = None
136-
empty = True
137-
first_on_line = True
138-
nesting = 0
139-
prev_ttext = None
130+
131+
# The last token seen. Start with INDENT to get module docstrings
132+
prev_toktype: int = token.INDENT
133+
# The current number of indents.
134+
indent: int = 0
135+
# An exclusion comment will exclude an entire clause at this indent.
136+
exclude_indent: int = 0
137+
# Are we currently excluding lines?
138+
excluding: bool = False
139+
# Are we excluding decorators now?
140+
excluding_decorators: bool = False
141+
# The line number of the first line in a multi-line statement.
142+
first_line: int = 0
143+
# Is the file empty?
144+
empty: bool = True
145+
# Is this the first token on a line?
146+
first_on_line: bool = True
147+
# Parenthesis (and bracket) nesting level.
148+
nesting: int = 0
140149

141150
assert self.text is not None
142151
tokgen = generate_tokens(self.text)
@@ -180,26 +189,26 @@ def _raw_parse(self) -> None:
180189
nesting += 1
181190
elif ttext in ")]}":
182191
nesting -= 1
183-
elif toktype == token.STRING and prev_toktype == token.INDENT:
184-
# Strings that are first on an indented line are docstrings.
185-
# (a trick from trace.py in the stdlib.) This works for
186-
# 99.9999% of cases. For the rest (!) see:
187-
# http://stackoverflow.com/questions/1769332/x/1769794#1769794
188-
self.raw_docstrings.update(range(slineno, elineno+1))
192+
elif toktype == token.STRING:
193+
if prev_toktype == token.INDENT:
194+
# Strings that are first on an indented line are docstrings.
195+
# (a trick from trace.py in the stdlib.) This works for
196+
# 99.9999% of cases.
197+
self.raw_docstrings.update(range(slineno, elineno+1))
189198
elif toktype == token.NEWLINE:
190-
if first_line is not None and elineno != first_line: # type: ignore[unreachable]
199+
if first_line and elineno != first_line:
191200
# We're at the end of a line, and we've ended on a
192201
# different line than the first line of the statement,
193202
# so record a multi-line range.
194-
for l in range(first_line, elineno+1): # type: ignore[unreachable]
203+
for l in range(first_line, elineno+1):
195204
self._multiline[l] = first_line
196-
first_line = None
205+
first_line = 0
197206
first_on_line = True
198207

199208
if ttext.strip() and toktype != tokenize.COMMENT:
200209
# A non-white-space token.
201210
empty = False
202-
if first_line is None:
211+
if not first_line:
203212
# The token is not white space, and is the first in a statement.
204213
first_line = slineno
205214
# Check whether to end an excluded suite.

0 commit comments

Comments
 (0)