Skip to content

Inconsistent coverage data for docstring between JSON report and other reports like html or lcov #2105

@silverzhaojr

Description

@silverzhaojr

Describe the bug

the coverage data of docstring in coverage.json is different with other reports.

To Reproduce

  1. Run test and generate different reports
# cat test.py
01
02 '''
03 global docstring
04 '''
05
06 def add(a, b):
07     '''
08     this is docstring for "add"
09     '''
10     return a + b
11
12 print(add(2,7))

# coverage run test.py

# coverage json
# coverage html
# coverage lcov
# coverage xml
  1. Compare the reports

=== coverage.json

the executed lines are like:

"executed_lines": [2, 6, 10, 12 ]

which marks line 2 (global docstring) as executed. Notice line 7 is not marked as executed though it's also a docstring.

=== html report

It doesn't mark line 2 as executed.

Image

=== coverage.lcov

It doesn't mark line 2 as executed.

# cat coverage.lcov 
   1 SF:test.py
   2 DA:6,1
   3 DA:10,1
   4 DA:12,1
   5 LF:3
   6 LH:3
   7 FN:6,10,add
   8 FNDA:1,add
   9 FNF:1
  10 FNH:1
  11 end_of_record

=== coverage.xml

It doesn't mark line 2 as executed.

# cat coverage.xml
...
<lines>
        <line number="6" hits="1"/>
        <line number="10" hits="1"/>
        <line number="12" hits="1"/>
</lines>
...

Answer the questions below:

  1. What version of Python are you using?
    Python 3.14.2
  2. What version of coverage.py shows the problem? The output of coverage debug sys is helpful.
    coverage_version: 7.13.0

Expected behavior

The coverage data of executed_lines should be consistent between JSON report and other reports.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions