8000 refactor: change impossible branches to asserts in parser.py · shenanigansd/coveragepy@39114d5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 39114d5

Browse files
committed
refactor: change impossible branches to asserts in parser.py
1 parent 60a5d6f commit 39114d5

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ Unreleased
2727
statements changed how they traced. This affected whether people saw the
2828
fix for `issue 1880`_.
2929

30+
- refactor: some code unreachable code paths in parser.py were changed to
31+
asserts. If you encounter any of these, please let me know!
32+
3033

3134
.. start-releases
3235

coverage/parser.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ def _analyze_ast(self) -> None:
296296
297297
"""
298298
assert self._ast_root is not None
299-
aaa = AstArcAnalyzer(self._ast_root, self.raw_statements, self._multiline)
299+
aaa = AstArcAnalyzer(self.filename, self._ast_root, self.raw_statements, self._multiline)
300300
aaa.analyze()
301301
self._with_jump_fixers = aaa.with_jump_fixers()
302302

@@ -337,9 +337,7 @@ def exit_counts(self) -> dict[TLineNo, int]:
337337
"""
338338
exit_counts: dict[TLineNo, int] = collections.defaultdict(int)
339339
for l1, l2 in self.arcs():
340-
if l1 < 0:
341-
# Don't ever report -1 as a line number
342-
continue
340+
assert l1 > 0, f"{l1=} should be greater than zero in {self.filename}"
343341
if l1 in self.excluded:
344342
# Don't report excluded lines as line numbers.
345343
continue
@@ -437,15 +435,15 @@ def _line_numbers(self) -> Iterable[TLineNo]:
437435
byte_num = 0
438436
for byte_incr, line_incr in zip(byte_increments, line_increments):
439437
if byte_incr:
440-
if line_num != last_line_num:
441-
yield line_num
442-
last_line_num = line_num
438+
assert line_num != last_line_num, f"Oops, {byte_incr = }, {line_incr = }"
439+
yield line_num
440+
last_line_num = line_num
443441
byte_num += byte_incr
444442
if line_incr >= 0x80:
445443
line_incr -= 0x100
446444
line_num += line_incr
447-
if line_num != last_line_num:
448-
yield line_num
445+
assert line_num != last_line_num
446+
yield line_num
449447

450448
def _find_statements(self) -> Iterable[TLineNo]:
451449
"""Find the statements in `self.code`.
@@ -643,10 +641,12 @@ class AstArcAnalyzer:
643641

644642
def __init__(
645643
self,
644+
filename: str,
646645
root_node: ast.AST,
647646
statements: set[TLineNo],
648647
multiline: dict[TLineNo, TLineNo],
649648
) -> None:
649+
self.filename = filename
650650
self.root_node = root_node
651651
# TODO: I think this is happening in too many places.
652652
self.statements = {multiline.get(l, l) for l in statements}
@@ -1076,9 +1076,9 @@ def _handle_decorated(self, node: ast.FunctionDef) -> set[ArcStart]:
10761076
# in `self.statements`. For some constructs, `line_for_node` is
10771077
# not what we'd think of as the first line in the statement, so map
10781078
# it to the first one.
1079-
if node.body:
1080-
body_start = self.line_for_node(node.body[0])
1081-
body_start = self.multiline.get(body_start, body_start)
1079+
assert node.body, f"Oops: {node.body = } in {self.filename}@{node.lineno}"
1080+
body_start = self.line_for_node(node.body[0])
1081+
body_start = self.multiline.get(body_start, body_start)
10821082
# The body is handled in collect_arcs.
10831083
assert last is not None
10841084
return {ArcStart(last)}

0 commit comments

Comments
 (0)
0