Coveragepy: The stub implementations of a protocol are treated as testable code

Created on 23 Nov 2020  路  5Comments  路  Source: nedbat/coveragepy

Describe the bug
coverage.py is reporting the stub implementations (...) of methods in a protocol as red, when in reality they're not real methods that should be executed

To Reproduce
The issue can be reproduced by running coverage with this file:

# protocol_bug.py
from typing import Protocol

class MyProtocol(Protocol):
    def foo(self: int) -> str:
        ...

print("Hello, world!")
$ python -m coverage run protocol_bug.py && python -m coverage html

the ... line is reported "red' (not ran).

Expected behavior
I think it would make more sense for protocol definitions to be ignored

bug

Most helpful comment

I added a simpler example to the docs in 551949f426b8ef292df5e6d4caf9d9d038a92634:

class .*\bProtocol\):

All 5 comments

Fixed with a regular expression filter, no worries :+1:

@decorator-factory Do you mind sharing the specific regex and setting that worked for you?

This worked for me:

class \w+\((.*?, )?(t(yping)?\.)?Protocol(, (t(yping)?\.)?Generic\[.*?\])?\):

it doesn't account for edge cases like awkward spacing, multiline parents lists etc., but should cover all usual protocols, including generic ones.

I added a simpler example to the docs in 551949f426b8ef292df5e6d4caf9d9d038a92634:

class .*\bProtocol\):

FYI for those who come across it, for pyproject.toml config I needed to double escape, e.g., "class .*\\bProtocol\\):".

Was this page helpful?
0 / 5 - 0 ratings