Skip to content

Report covered 'no-cover' lines #251

Open

Description

@nedbat

Originally reported by Anonymous


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.


Activity

  1. nedbat commented on Aug 1, 2013

    @nedbat
    MemberAuthor

    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.

  2. nedbat commented on Feb 9, 2017

    @nedbat
    MemberAuthor

    Original comment by John Vandenberg (Bitbucket: jayvdb, GitHub: jayvdb)


    In my experience, #pragma: no cover is 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).

  3. nedbat commented on Feb 24, 2017

    @nedbat
    MemberAuthor

    Original comment by Andrei Fokau (Bitbucket: andreif, GitHub: andreif)


    Wonderful idea, all exclude_lines number should be reported.
    Maybe even omitted *.py files as well.

  4. nedbat commented on Nov 25, 2017

    @nedbat
    MemberAuthor

    Issue #619 was marked as a duplicate of this issue.

  5. nedbat commented on Nov 26, 2017

    @nedbat
    MemberAuthor

    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.

  6. nedbat commented on Nov 26, 2017

    @nedbat
    MemberAuthor

    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.)

  7. added
    enhancement
    on Jun 23, 2018
  8. adrien-berchet commented on Apr 28, 2021

    @adrien-berchet

    Did anything move forward on this topic? I would really like this feature for the exact same reason as the one originally reported here.

  9. nedbat commented on Apr 28, 2021

    @nedbat
    MemberAuthor

    Sorry, nothing new has happened.

  10. adrien-berchet commented on Apr 29, 2021

    @adrien-berchet

    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 :)

  11. nedbat commented on Nov 17, 2021

    @nedbat
    MemberAuthor

    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 1
    
        if WINDOWS:                     # line 3
            windows_thing()             # line 4
            another_windows_thing()     # line 5
    
        more_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.

  12. rmorshea commented on Oct 31, 2022

    @rmorshea

    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.

  13. 10 remaining items

  14. rmorshea commented on Nov 23, 2022

    @rmorshea

    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.

  15. rmorshea commented on Nov 23, 2022

    @rmorshea

    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.

  16. finite-state-machine commented on Nov 25, 2022

    @finite-state-machine

    @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).

  17. rmorshea commented on Nov 26, 2022

    @rmorshea

    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".

  18. finite-state-machine commented on Nov 27, 2022

    @finite-state-machine

    I've amended the language of my comment...

    I apologize for misunderstanding. Thanks for the clarification!

  19. nedbat commented on Nov 30, 2022

    @nedbat
    MemberAuthor

    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.

    Thoughts?

  20. rcriii42 commented on Dec 1, 2022

    @rcriii42

    How about overexecuted?

  21. labroid commented on Dec 1, 2022

    @labroid

    That's tough. Perhaps: unpredicted, unexpected, unforecast, unforeseen, or unanticipated?

  22. adrien-berchet commented on Dec 2, 2022

    @adrien-berchet

    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.

  23. kpinc commented on Dec 3, 2022

    @kpinc

    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),.

  24. mtvx commented on Aug 22, 2025

    @mtvx

    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 coveragepy integrated option...

  25. nedbat commented on Aug 24, 2025

    @nedbat
    MemberAuthor
  26. mtvx commented on Aug 25, 2025

    @mtvx

    I threw together a proof-of-concept: https://nedbatchelder.com/blog/202508/finding_unneeded_pragmas.html Let me know if you try it.

    👍🏻

    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...

  27. nickdrozd commented on Sep 13, 2025

    @nickdrozd
    Contributor

    I tried the warn-unused script. Results:

    • Code has 48 no-cover pragmas.
    • Script flags 36 of them as unused.
    • 12 of those were true positives, 12 or so false positives, the rest I didn't check. IOW, 1/3 good, 1/3 bad, 1/3 unknown.
    • I am assuming no false negatives, although not totally sure.

    Overall, warning script approach is not perfect, but seems to be on the right track.

    This is a feature I would love to see!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementopinions needed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions