Skip to content

Commit

Permalink
refactor: we no longer need to treat 'class' lines specially
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nedbat committed Nov 11, 2021
1 parent 0387bba commit 79f9f45
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 20 deletions.
15 changes: 0 additions & 15 deletions coverage/parser.py
Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 0 additions & 2 deletions lab/parser.py
Expand Up @@ -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'

Expand Down
6 changes: 3 additions & 3 deletions tests/test_parser.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 79f9f45

Please sign in to comment.