From 79f9f4575321fafc2ef770e3255f874db3d4b037 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Thu, 11 Nov 2021 06:54:06 -0500 Subject: [PATCH] refactor: we no longer need to treat 'class' lines specially Originally, this tokenizing code was paired with bytecode analysis. But now we use AST instead, so class lines don't need to be handled differently. --- coverage/parser.py | 15 --------------- lab/parser.py | 2 -- tests/test_parser.py | 6 +++--- 3 files changed, 3 insertions(+), 20 deletions(-) diff --git a/coverage/parser.py b/coverage/parser.py index b47fd12ef..d17d7c9be 100644 --- a/coverage/parser.py +++ b/coverage/parser.py @@ -67,9 +67,6 @@ def __init__(self, text=None, filename=None, exclude=None): # The raw line numbers of excluded lines of code, as marked by pragmas. self.raw_excluded = set() - # The line numbers of class definitions. - self.raw_classdefs = set() - # The line numbers of docstring lines. self.raw_docstrings = set() @@ -133,12 +130,6 @@ def _raw_parse(self): indent += 1 elif toktype == token.DEDENT: indent -= 1 - elif toktype == token.NAME: - if ttext == 'class': - # Class definitions look like branches in the bytecode, so - # we need to exclude them. The simplest way is to note the - # lines with the 'class' keyword. - self.raw_classdefs.add(slineno) elif toktype == token.OP: if ttext == ':' and nesting == 0: should_exclude = (elineno in self.raw_excluded) or excluding_decorators @@ -301,12 +292,6 @@ def exit_counts(self): continue exit_counts[l1] += 1 - # Class definitions have one extra exit, so remove one for each: - for l in self.raw_classdefs: - # Ensure key is there: class definitions can include excluded lines. - if l in exit_counts: - exit_counts[l] -= 1 - return exit_counts def missing_arc_description(self, start, end, executed_arcs=None): diff --git a/lab/parser.py b/lab/parser.py index 19cb94a86..50b435644 100644 --- a/lab/parser.py +++ b/lab/parser.py @@ -108,8 +108,6 @@ def one_file(self, options, filename): marks[2] = str(exits) if lineno in pyparser.raw_docstrings: marks[3] = '"' - if lineno in pyparser.raw_classdefs: - marks[3] = 'C' if lineno in pyparser.raw_excluded: marks[4] = 'x' diff --git a/tests/test_parser.py b/tests/test_parser.py index 303f2b553..538e25009 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -43,7 +43,7 @@ class Bar: pass """) assert parser.exit_counts() == { - 2:1, 3:1, 4:2, 5:1, 7:1, 9:1, 10:1 + 2:2, 3:1, 4:2, 5:1, 7:1, 9:2, 10:1 } def test_generator_exit_counts(self): @@ -89,7 +89,7 @@ class Bar: pass """) assert parser.exit_counts() == { - 1:0, 2:1, 3:1 + 1:1, 2:1, 3:1 } def test_missing_branch_to_excluded_code(self): @@ -457,7 +457,7 @@ def foo(self, a): class Bar: pass """ - counts = { 2:1, 3:1, 4:2, 5:1, 7:1, 9:1, 10:1 } + counts = { 2:2, 3:1, 4:2, 5:1, 7:1, 9:2, 10:1 } name_endings = (("unix", "\n"), ("dos", "\r\n"), ("mac", "\r")) for fname, newline in name_endings: fname = fname + ".py"