You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is often the case that a feature will be added in one commit, but there isn't any client code that calls a particular method yet. As such, the method is marked
pragma: no cover
coverage passes and everyone continues along their merry way.
Later, when client code is added that exercises that code path, it would be nice if coverage.py could complain saying something like
"WARNING: Block marked 'no cover' actually covered. You may want to remove the pragma."
That way, we could continually make sure that the minimum amount of code is under 'no cover' pragmas.
Hmm, I'm not sure I approve of pragma'ing away code that you intend to cover eventually, but it's an interesting idea to alert the user to unneeded pragmas.
The only complexity is whether this alert is only added for the default #pragma: no cover, in which case you would want to add another default which doesnt trigger the alert, and/or a way to turn off the warning (but imo the ecosystem is improved if the warnings cant be easily disabled, as it is important that coders limit when the block isnt covered to specific conditions).
Original comment byToshio Kuratomi (Bitbucket: toshio, GitHub: toshio)
Looking at the code handling exclusions, it appears that exclusions are recorded and tracked all the way up to Coverage.Analysis but the excluded lines are being removed from executed statements at a much lower level, when we parse the python file. To implement this, I think we need to keep excluded lines in the executed statements until much later. I think Analysis makes sense for that as it is where other statistics get combined and calculated before the Reporter phase.
Original comment byToshio Kuratomi (Bitbucket: toshio, GitHub: toshio)
(Just FYI: I am using exclude_lines similar to jayvdb, I want to exclude lines that can only run on Python2 or only run on Python3 when the Python interpreter running them is the opposite version. These lines are essentially dead code on that version of Python. I want coverage to warn me if that code is being executed on that Python version as it means either my code is wrong or my marking of it as dead code on python2/3 is wrong.)
Hi @nedbat
No problem, thanks for the answer, it was just to be sure I did not miss something.
I am quite surprise that this feature is not much requested while it seems very important to me :)
Thinking about this more, there's a subtlety here. Let's say I have some code that is only for Windows and I only run my tests on Linux. I might put this in my configuration file:
[report]
exclude_lines =
if WINDOWS:
This has the advantage that the entire clause beneath the if is excluded:
blah_bla() # line 1ifWINDOWS: # line 3windows_thing() # line 4another_windows_thing() # line 5more_blah() # line 7
Here, lines 3,4,5 will be excluded. But line 3 is executed, when the test for WINDOWS happens, and is false.
Simply reporting on the excluded lines that were executed would report line 3, which is not interesting.
Do other people use exclude_lines like this? Ideally, this should report that no excluded lines were executed.
As mentioned in #1482, my idea to help resolve this is to have a configuration option that behaves 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 an analogous warn_unused_exclude option (defaults to false) 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. Ideally, this warning would cause Coverage to return a non-zero exit code, but I could live with that not being the case.
The main complication is that for users who have customized exclude_lines, setting warn_unused_exclude to 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_exclude may produce false-positive warnings in the former case.
To resolve this complication, @nedbat suggested that warn_unused_exclude could be a list of regexes which are the exclusions to warn about. I like this idea, though to keep things simple it could also be added later under a separate configuration option if users turn out to need it.
The way I'm thinking about this option is that it should cause both coverage of the given lines to be treated as excluded, and a warning if they are covered. The idea being that, the lines from this option and the lines from exclude_lines could be mutually exclusive. That is, at some point, the default for exclude_lines could be [], and the default for this new option could be ['pragma: no cover']. If this is how the option is to work, then the issue with the warn_if_run_lines name is that it suggests that a warning will be produced if the lines are covered, but not that the lines will be marked as excluded. In this case, no_cover_lines seems like a more appropriate name.
If instead the option should only cause the given lines to produce a warning when covered, then warn_if_run_lines would be an apt choice of name. The result being that warn_if_run_lines and exclude_lines would control discrete behaviors - warn_if_run_lines controls warnings on line coverage, and exclude_lines causes lines to be marked as excluded in reports. However, I can't think of a case where you'd want to specify a line in warn_if_run_lines but not in exclude_lines. Thus my instinct to have a no_cover_lines-like option that causes both coverage warnings and exclusion markings in reports.
But the warning is also quite distinct from any reporting those commands do...
The --fail-under option, which is present for of all the reporting commands, seems somewhat similar in that the desire would be for these warnings to cause a failure either optionally or by default, as was mentioned in this comment.
@rmorshea I must respectfully disagree with your suggestion to exclude these lines from reports (although this could be an optional behaviour for those who desire it). It's especially valuable that the annotate format has exactly the same number of lines as the source file, and can be scroll-bound with the original source (to use the VIm term).
I've amended the language of my comment to more accurately reflect my intended meaning. Instead of saying that lines should be "excluded from reports" I now say that they should be "marked as excluded" or "treated as excluded".
Thinking about how to do this some more... First, I wish there were a good word to mean, "lines I thought wouldn't execute, but did," like we have "unexecuted" for "lines I thought would execute, but didn't." In this comment, I'll call them surprises.
Reporting on surprises is not like reporting fail-under, because there a good deal of text to output for surprises. It will be like report --missing: we need to list files, and for each file, some line numbers that executed unexpectedly. I don't want the every report command to also output a text table of surprises.
So i think at first this should be limited to the coverage report command. We can add --show-surprises to parallel --show-missing. Surprise line numbers can be indicated with an asterisk or exclamation point. The --skip-covered option should become --skip-ok, meaning, don't show files that have nothing wrong with them.
IMO it has to be tough. We are speaking about lines that really have no reason to be executed, so if they are it means that there is an issue somewhere.
I would even go for something tougher, like illegally-executed.
I suggest "poseur". I like what it implies and I think it'd be my choice. Plus, it's nice and short.
Other possibilities are stowaway, overavid, impostor, or cosplayer.
Or borrow from some other language. (Besides French. Insert joke about the French here. ;-) Something like "sasaeng" (Korean), meaning: "obsessive fan who stalks or engages in other behavior constituting an invasion of the privacy of Korean idols". (From wikipedia.)
Other words that might shake something useful out of the trees are: stalker, gaslighter, erotomaniac (ha!), yandere (Japanese), Zersetzungist (from the German Zersetzung),.
Didn't try it yet, and did not read through this thread. Judging by the length of it, above util probably doesn't cover it all. But maybe it's sufficient for a simple case.
Would ofc prefer a coveragepy integrated option...
We have some Jinja2 files in our packages, so I had to explicitly exclude them from coverage (ie. add "**/*.jinja" to omit). Else, the script was trying to parse them as Python files, failing.
Afterwards, the script executed successfully though it's a bit slow, 2-3s on my machine / my project. Not a problem.
The real problem is exactly what you mention at the end of the post:
For example, I exclude “if TYPE_CHECKING:” because I want that entire clause excluded. But the if-line itself is actually run. If I include that pattern in the warn-executed list, it will flag all of those lines. Maybe I’m forgetting a way to do this: it would be good to have a way to exclude the body of the if clause while understanding that the if-line itself is executed.
99% of the hits in my case are due to this.
Something like # pragma: no cover nested would be needed...
Activity
nedbat commentedon Aug 1, 2013
Hmm, I'm not sure I approve of pragma'ing away code that you intend to cover eventually, but it's an interesting idea to alert the user to unneeded pragmas.
nedbat commentedon Feb 9, 2017
Original comment by John Vandenberg (Bitbucket: jayvdb, GitHub: jayvdb)
In my experience,
#pragma: no coveris most often used for OS specific paths.These proposed alerts would be a good way to advertise that platform specific code can be properly required when the tests are being run on the respective hosts using env vars https://bitbucket.org/ned/coveragepy/issues/563
The only complexity is whether this alert is only added for the default
#pragma: no cover, in which case you would want to add another default which doesnt trigger the alert, and/or a way to turn off the warning (but imo the ecosystem is improved if the warnings cant be easily disabled, as it is important that coders limit when the block isnt covered to specific conditions).nedbat commentedon Feb 24, 2017
Original comment by Andrei Fokau (Bitbucket: andreif, GitHub: andreif)
Wonderful idea, all
exclude_linesnumber should be reported.Maybe even omitted
*.pyfiles as well.nedbat commentedon Nov 25, 2017
Issue #619 was marked as a duplicate of this issue.
nedbat commentedon Nov 26, 2017
Original comment by Toshio Kuratomi (Bitbucket: toshio, GitHub: toshio)
Looking at the code handling exclusions, it appears that exclusions are recorded and tracked all the way up to Coverage.Analysis but the excluded lines are being removed from executed statements at a much lower level, when we parse the python file. To implement this, I think we need to keep excluded lines in the executed statements until much later. I think Analysis makes sense for that as it is where other statistics get combined and calculated before the Reporter phase.
nedbat commentedon Nov 26, 2017
Original comment by Toshio Kuratomi (Bitbucket: toshio, GitHub: toshio)
(Just FYI: I am using exclude_lines similar to jayvdb, I want to exclude lines that can only run on Python2 or only run on Python3 when the Python interpreter running them is the opposite version. These lines are essentially dead code on that version of Python. I want coverage to warn me if that code is being executed on that Python version as it means either my code is wrong or my marking of it as dead code on python2/3 is wrong.)
adrien-berchet commentedon Apr 28, 2021
Did anything move forward on this topic? I would really like this feature for the exact same reason as the one originally reported here.
nedbat commentedon Apr 28, 2021
Sorry, nothing new has happened.
adrien-berchet commentedon Apr 29, 2021
Hi @nedbat
No problem, thanks for the answer, it was just to be sure I did not miss something.
I am quite surprise that this feature is not much requested while it seems very important to me :)
nedbat commentedon Nov 17, 2021
Thinking about this more, there's a subtlety here. Let's say I have some code that is only for Windows and I only run my tests on Linux. I might put this in my configuration file:
This has the advantage that the entire clause beneath the if is excluded:
Here, lines 3,4,5 will be excluded. But line 3 is executed, when the test for WINDOWS happens, and is false.
Simply reporting on the excluded lines that were executed would report line 3, which is not interesting.
Do other people use exclude_lines like this? Ideally, this should report that no excluded lines were executed.
rmorshea commentedon Oct 31, 2022
As mentioned in #1482, my idea to help resolve this is to have a configuration option that behaves similarly to MyPy's
warn_unused_ignorewhich, if true, emits a warning if a line or file contains atype: ignorecomment even though the line contains no typing errors.In the case of Coverage, the behavior of an analogous
warn_unused_excludeoption (defaults tofalse) 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. Ideally, this warning would cause Coverage to return a non-zero exit code, but I could live with that not being the case.The main complication is that 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.To resolve this complication, @nedbat suggested that
warn_unused_excludecould be a list of regexes which are the exclusions to warn about. I like this idea, though to keep things simple it could also be added later under a separate configuration option if users turn out to need it.10 remaining items
rmorshea commentedon Nov 23, 2022
The way I'm thinking about this option is that it should cause both coverage of the given lines to be treated as excluded, and a warning if they are covered. The idea being that, the lines from this option and the lines from
exclude_linescould be mutually exclusive. That is, at some point, the default forexclude_linescould be[], and the default for this new option could be['pragma: no cover']. If this is how the option is to work, then the issue with thewarn_if_run_linesname is that it suggests that a warning will be produced if the lines are covered, but not that the lines will be marked as excluded. In this case,no_cover_linesseems like a more appropriate name.If instead the option should only cause the given lines to produce a warning when covered, then
warn_if_run_lineswould be an apt choice of name. The result being thatwarn_if_run_linesandexclude_lineswould control discrete behaviors -warn_if_run_linescontrols warnings on line coverage, andexclude_linescauses lines to be marked as excluded in reports. However, I can't think of a case where you'd want to specify a line inwarn_if_run_linesbut not inexclude_lines. Thus my instinct to have ano_cover_lines-like option that causes both coverage warnings and exclusion markings in reports.rmorshea commentedon Nov 23, 2022
The
--fail-underoption, which is present for of all the reporting commands, seems somewhat similar in that the desire would be for these warnings to cause a failure either optionally or by default, as was mentioned in this comment.finite-state-machine commentedon Nov 25, 2022
@rmorshea I must respectfully disagree with your suggestion to exclude these lines from reports (although this could be an optional behaviour for those who desire it). It's especially valuable that the
annotateformat has exactly the same number of lines as the source file, and can be scroll-bound with the original source (to use the VIm term).rmorshea commentedon Nov 26, 2022
I've amended the language of my comment to more accurately reflect my intended meaning. Instead of saying that lines should be "excluded from reports" I now say that they should be "marked as excluded" or "treated as excluded".
finite-state-machine commentedon Nov 27, 2022
I apologize for misunderstanding. Thanks for the clarification!
nedbat commentedon Nov 30, 2022
Thinking about how to do this some more... First, I wish there were a good word to mean, "lines I thought wouldn't execute, but did," like we have "unexecuted" for "lines I thought would execute, but didn't." In this comment, I'll call them surprises.
Reporting on surprises is not like reporting fail-under, because there a good deal of text to output for surprises. It will be like
report --missing: we need to list files, and for each file, some line numbers that executed unexpectedly. I don't want the every report command to also output a text table of surprises.So i think at first this should be limited to the
coverage reportcommand. We can add--show-surprisesto parallel--show-missing. Surprise line numbers can be indicated with an asterisk or exclamation point. The--skip-coveredoption should become--skip-ok, meaning, don't show files that have nothing wrong with them.Thoughts?
rcriii42 commentedon Dec 1, 2022
How about overexecuted?
labroid commentedon Dec 1, 2022
That's tough. Perhaps: unpredicted, unexpected, unforecast, unforeseen, or unanticipated?
adrien-berchet commentedon Dec 2, 2022
IMO it has to be tough. We are speaking about lines that really have no reason to be executed, so if they are it means that there is an issue somewhere.
I would even go for something tougher, like
illegally-executed.kpinc commentedon Dec 3, 2022
I suggest "poseur". I like what it implies and I think it'd be my choice. Plus, it's nice and short.
Other possibilities are stowaway, overavid, impostor, or cosplayer.
Or borrow from some other language. (Besides French. Insert joke about the French here. ;-) Something like "sasaeng" (Korean), meaning: "obsessive fan who stalks or engages in other behavior constituting an invasion of the privacy of Korean idols". (From wikipedia.)
Other words that might shake something useful out of the trees are: stalker, gaslighter, erotomaniac (ha!), yandere (Japanese), Zersetzungist (from the German Zersetzung),.
mtvx commentedon Aug 22, 2025
There seems to be https://github.com/pydantic/strict-no-cover
Didn't try it yet, and did not read through this thread. Judging by the length of it, above util probably doesn't cover it all. But maybe it's sufficient for a simple case.
Would ofc prefer a
coveragepyintegrated option...nedbat commentedon Aug 24, 2025
I threw together a proof-of-concept: https://nedbatchelder.com/blog/202508/finding_unneeded_pragmas.html Let me know if you try it.
mtvx commentedon Aug 25, 2025
👍🏻
We have some Jinja2 files in our packages, so I had to explicitly exclude them from coverage (ie. add
"**/*.jinja"toomit). Else, the script was trying to parse them as Python files, failing.Afterwards, the script executed successfully though it's a bit slow, 2-3s on my machine / my project. Not a problem.
The real problem is exactly what you mention at the end of the post:
99% of the hits in my case are due to this.
Something like
# pragma: no cover nestedwould be needed...nickdrozd commentedon Sep 13, 2025
I tried the warn-unused script. Results:
Overall, warning script approach is not perfect, but seems to be on the right track.
This is a feature I would love to see!