Finding unneeded pragmas
Briefly

A proof-of-concept program identifies lines excluded by coverage.py exclusion pragmas that were actually executed during tests. The program is a standalone file in the coverage.py repository and is unsupported. Users can copy warn_executed.py and supply a TOML file listing only the exclusion regexes to check. The tool prints lines matched by those patterns that ran according to coverage data, helping determine whether exclusions are still necessary. The tool requires Python 3.11+, and does not assume existing coverage settings or default regexes. The tool has limitations, such as flagging executed if-lines used to exclude TYPE_CHECKING bodies.
To answer a long-standing coverage.py feature request, I threw together an experiment: a tool to identify lines that have been excluded from coverage, but which were actually executed. The program is a standalone file in the coverage.py repo. It is unsupported. I'd like people to try it to see what they think of the idea. Later we can decide what to do with it.
To try it: copy warn_executed.py from GitHub. Create a .toml file that looks something like this: These are exclusion regexes that you've used in your coverage runs. The program will print out any line identified by a pattern and that ran during your tests. It might be that you don't need to exclude the line, because it ran. In this file, none of your coverage settings or the default regexes are assumed: you need to explicitly specify all the patterns you want flagged.
The reason for a new list of patterns instead of just reading the existing coverage settings is that some exclusions are 'don't care' rather than 'this will never happen.' For example, I exclude 'def __repr__' because some __repr__'s are just to make my debugging easier. I don't care if the test suite runs them or not. It might run them, so I don't want it to be a warning that they actually ran.
Read at Nedbatchelder
[
|
]