Skip to content

Class definitions interpreted as branches #1449

@arttuperala

Description

@arttuperala

Describe the bug
Using coverage run --branch reports an unexpected number of branches in a file with class definitions when using coverage >= 6.2.

To Reproduce

class MyClass:
    def my_method(self):
        return True

    @property
    def my_property(self):
        return True


my_class = MyClass()
my_class.my_method()
my_class.my_property
$ coverage --version
Coverage.py, version 6.4.4 with C extension
Full documentation is at https://coverage.readthedocs.io
$ coverage run --branch test.py && coverage report
Name      Stmts   Miss Branch BrPart  Cover
-------------------------------------------
test.py       9      0      2      0   100%
-------------------------------------------
TOTAL         9      0      2      0   100%

Generating the result XML file shows that the branch statement comes from the first line in the test file.

<class name="test.py" filename="test.py" complexity="0" line-rate="1" branch-rate="1">
    <methods/>
    <lines>
        <line number="1" hits="1" branch="true" condition-coverage="100% (2/2)"/>
        <line number="2" hits="1"/>
        <line number="3" hits="1"/>
        <line number="5" hits="1"/>
        <line number="6" hits="1"/>
        <line number="7" hits="1"/>
        <line number="10" hits="1"/>
        <line number="11" hits="1"/>
        <line number="12" hits="1"/>
    </lines>
</class>

Downgrading to 6.1.2 yields a different (but expected) result.

$ coverage --version
Coverage.py, version 6.1.2 with C extension
Full documentation is at https://coverage.readthedocs.io
$ coverage run --branch test.py && coverage report
Name      Stmts   Miss Branch BrPart  Cover
-------------------------------------------
test.py       9      0      0      0   100%
-------------------------------------------
TOTAL         9      0      0      0   100%

6.2 is the earliest version where I have found the unexpected behaviour.

$ coverage --version
Coverage.py, version 6.2 with C extension
Full documentation is at https://coverage.readthedocs.io
$ coverage run --branch test.py && coverage report
Name      Stmts   Miss Branch BrPart  Cover
-------------------------------------------
test.py       9      0      2      0   100%
-------------------------------------------
TOTAL         9      0      2      0   100%

Tested with Python 3.8.14, 3.9.13, 3.10.6 on macOS 12.6 (x86) with a minimal virtualenv only containing the tested version of coverage.

Expected behavior
No branch statements are reported in a file with a class definition but no branch statements (coverage 6.1.2 behaviour).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions