Skip to content

Line coverage metrics do not add up for executed_lines and summary.covered_lines in generated coverage.json #1900

Open
@JohnPeng47

Description

@JohnPeng47

Describe the bug
Here below is an item in "files" generated from coverage.py:

"src\textual\widgets\_list_item.py": {
"executed_lines": [
1,
3,
5,
6,
7,
8,
11,
12,
19,
20,
22,
23,
25,
29,
32,
35,
36,
37
],
"summary": {
"covered_lines": 15,
"num_statements": 21,
"percent_covered": 71.42857142857143,
"percent_covered_display": "71",
"missing_lines": 6,
"excluded_lines": 0
},
"missing_lines": [
26,
27,
30,
33,
38,
39
],
"excluded_lines": [],

I would expect len(executed_lines) == summary.covered_lines but the first is 18 and the later is 15. Where does the extra 3 come from?

To Reproduce
I tried this with the following:
pytest . --tb short --color no --cov=src\textual --cov-report json --continue-on-collection-errors --disable-warnings
On the https://github.com/Textualize/textual repo.

Expected behavior
My expectation is:
covered_lines + executed_lines + excluded_lines == summary.num_statements, but this seems to not be the case?

Activity

nedbat

nedbat commented on Dec 3, 2024

@nedbat
Owner

I'd like to reproduce this, but I don't use poetry so I'm not sure how to get everything in order. Can you give the exact commands you used, starting with git clone?

nedbat

nedbat commented on Dec 3, 2024

@nedbat
Owner

I think the three lines are the docstrings, but I'm not sure why they are treated differently.

JohnPeng47

JohnPeng47 commented on Dec 3, 2024

@JohnPeng47
Author

Yeah so here's the repo: https://github.com/JohnPeng47/textual-neutered

  1. Clone
  2. Install poetry in your current python env
  3. cd into repo and run poetry install to get deps
  4. run poetry shell to activate project venv with deps installed from 3
  5. run pytest . --tb short --color no --cov=src/textual --cov-report json --continue-on-collection-errors --disable-warnings
  6. [OPTIONAL] I have a script test_coverage.py that basically checks that this conditions is true for all coverage files:
    missing_check = len(missing_lines) + len(executed_lines) + len(excluded_lines) == total_stmts
JohnPeng47

JohnPeng47 commented on Dec 3, 2024

@JohnPeng47
Author

Also a shorter example here:

"__init__.py": {
  "executed_lines": [
    0
  ],
  "summary": {
    "covered_lines": 0,
    "num_statements": 0,
    "percent_covered": 100,
    "percent_covered_display": "100",
    "missing_lines": 0,
    "excluded_lines": 0
  },
  "missing_lines": [],
  "excluded_lines": [],

In this example, both covered_lines and num_statements should be 1 right, not 0?

JohnPeng47

JohnPeng47 commented on Dec 4, 2024

@JohnPeng47
Author

Also another thing that I just noticed now, is that the coverage collected on the same repo is different on Windows vs. Linux. Is this just inherent to the OS differences in settrace impl?

nedbat

nedbat commented on Dec 4, 2024

@nedbat
Owner

There is no OS difference in the settrace implementation. Likely the difference is do to OS differences in textual repo. If you don't think that's the case, let's look at some more specific details.

JohnPeng47

JohnPeng47 commented on Dec 4, 2024

@JohnPeng47
Author

Ahh no I think that makes sense. But could you confirm:
len(missing_lines) + len(executed_lines) + len(excluded_lines) == total_stmts

Is invariant for every file under coverage?

JohnPeng47

JohnPeng47 commented on Dec 6, 2024

@JohnPeng47
Author

Hey sorry to keep pestering, but do you think you can just answer that one question above? My coverage diffing code is broken for some reason, and I just want to be sure that the above condition should hold

nedbat

nedbat commented on Jan 26, 2025

@nedbat
Owner

But could you confirm:
len(missing_lines) + len(executed_lines) + len(excluded_lines) == total_stmts

Is invariant for every file under coverage?

As I mentioned, docstrings complicate matters. I'm also not sure what number you mean by total_stmts. That's not a number in the JSON report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingquestionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @nedbat@JohnPeng47

        Issue actions

          Line coverage metrics do not add up for executed_lines and summary.covered_lines in generated coverage.json · Issue #1900 · nedbat/coveragepy