Repository navigation
Description
Is your feature request related to a problem? Please describe.
Consider a case where you exclude code from coverage reports because, for whatever reason, the code cannot be covered by your test suite. Then at some time in the future, you make changes that cause that same code to become covered. However, you forget that you've excluded this code from coverage and do not realize that you will not be notified if you later lose coverage in the effected area.
Describe the solution you'd like
It would be great to have a configuration option that behaved similarly to MyPy's warn_unused_ignore which, if true, emits a warning if a line or file contains a type: ignore comment even though the line contains no typing errors.
In the case of Coverage, the behavior of a warn_unused_exclude (defaults to false) options would be to, if true, emit a warning when a line or branch is excluded from coverage reports even though that line or branch is in fact fully covered.
Here are some complicating factors to consider...
- For users who have customized
exclude_lines, settingwarn_unused_excludeto true could produce false-positive warnings. That is, when users exclude code from coverage it is either because they don't care whether those sections of code have coverage, or because they would like those sections to be covered, but were unable to for some reason -warn_unused_excludemay produce false-positive warnings in the former case. I don't think this rules out the idea of addingwarn_unused_excludebecause, for users that might be impacted by these false-positive warnings, they can leavewarn_unused_exclude=falseor re-work theirexclude_linespatterns. - If
warn_unused_excludeis true, all lines and branches must be checked for coverage even ifpragma: no coveris declared so as to warn if those sections are covered despite the declared exclusion.
Describe alternatives you've considered
I don't believe there are any alternatives available that would solve the problem I described.
Activity
nedbat commentedon Oct 30, 2022
Thanks, this is an interesting idea.
You are right that some people exclude lines from coverage not because the lines won't be run, but because they don't care if they are run or not. So warning that the excluded lines were run might be annoying noise for them. I don't know how to strike that balance though. It leads me to thinking that
warn_unused_excludecould be a list of regexes which are the exclusions to warn about. Not many people would go to that much trouble, I expect.We don't have to worry about that, because exclusions don't affect how code is measured, only how it's reported. So all lines and branches are already checked for coverage no matter how many exclusions there are.
This feature has been requested before, in #251 and #619, and there's a related idea in #814. I'll close this as a duplicate, and we can continue the discussion on #251.
[-]Warn Unused Coverage Excusion[/-][+]Warn Unused Coverage Exclusion[/+]