Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Branches shown where there are none #1701

Open
jaltmayerpizzorno opened this issue Nov 21, 2023 · 0 comments
Open

Branches shown where there are none #1701

jaltmayerpizzorno opened this issue Nov 21, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@jaltmayerpizzorno
Copy link

jaltmayerpizzorno commented Nov 21, 2023

Describe the bug
coverage.py detects (executed and/or missing) branches where really there are none.

To Reproduce
coverage.py shows branches for the excerpts below where there are none; I can reproduce these issues with various version of Python (with some differences), including Python 3.11 and 3.12. I'm using coverage.py v7.3.2.

  • comprehensions/generators:
a = [n for n in range(10)]
b = {n for n in range(10)}
c = {n:n for n in range(10)}
z = sum(n for n in range(10))

yields:

Name               Stmts   Miss Branch BrPart  Cover   Missing
--------------------------------------------------------------
comprehension.py       4      0      8      0   100%
--------------------------------------------------------------
TOTAL                  4      0      8      0   100%
  • decorated def:
def foo(x):
    return x

@foo
def bar(y):
    return y

yields:

Name               Stmts   Miss Branch BrPart  Cover   Missing
--------------------------------------------------------------
decorated-def.py       5      1      2      0    86%   6
--------------------------------------------------------------
TOTAL                  5      1      2      0    86%
  • with statement:
with open("with.py") as f:
    x = len(f.readlines())

y = 0

yields:

Name      Stmts   Miss Branch BrPart  Cover   Missing
-----------------------------------------------------
with.py       3      0      2      0   100%
-----------------------------------------------------
TOTAL         3      0      2      0   100%
  • multiline if:
def foo(x):
    if (x >= 10
        and x > 0
    ):
        return True

foo(10)

yields (in the JSON):

            "executed_branches": [
                [
                    2,
                    2
                ],
                [
                    2,
                    5
                ]
            ],
            "missing_branches": [
                [
                    2,
                    -1
                ]
            ]
  • exception handling:
try:
    raise RuntimeError("oops")

except RuntimeError:
    # nothing here
    x = 42

except Exception:
    x = 666

yields (in the JSON):

            "executed_branches": [
                [
                    4,
                    6
                ]
            ],
            "missing_branches": [
                [
                    4,
                    8
                ]
            ]

Each of these was generated with:

P="python3.11"
for f in *.py; do
    $P -m coverage run --branch $f
    $P -m coverage report -m
    $P -m coverage json --pretty-print -o `basename -s .py $f`.json
done

with.json
multiline-if.json
exception.json
decorated-def.json
comprehension.json

@jaltmayerpizzorno jaltmayerpizzorno added bug Something isn't working needs triage labels Nov 21, 2023
patman15 added a commit to patman15/BMS_BLE-HA that referenced this issue Sep 18, 2024
* full branch coverage

* full branch coverage

* detailed error message

* full branch coverage

* increased branch coverage

* fixed branch coverage for OGT

https://coverage.readthedocs.io/en/7.5.4/branch.html#generator-expressions
nedbat/coveragepy#1701

* cleaned debug messages

* added pack to test uncovered branch

* update pytest settings for branch coverage

* removed redundant debug message

* code formatting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants