question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Assume `if TYPE_CHECKING: ... else: ...` block is covered

See original GitHub issue

if typing.TYPE_CHECKING blocks are never run but are still counted as needing test coverage. ( https://docs.python.org/3.7/library/typing.html#typing.TYPE_CHECKING )

Example

from typing import TYPE_CHECKING
if TYPE_CHECKING:
    # coverage.py thinks this is not covered because TYPE_CHECKING is True at runtime
    ...
else:
    ....

You have to add # pragma: no_cover to the if block here because coverage.py assumes TYPE_CHECKING to be False, and therefore assumes one of the blocks does not have a test case. It would be good if you didn’t have to, and blocks requiring TYPE_CHECKING to be True are assumed to not need test cases.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

28reactions
nedbatcommented, Aug 2, 2019

You can solve this yourself today by adding this to your .coveragerc file:

[report]
exclude_lines = 
    pragma: no cover
    if TYPE_CHECKING:

More details about this setting are here: Advanced exclusion.

Since this is something that is configurable by the user, I would rather not change coverage.py to deal with it.

14reactions
nedbatcommented, Jan 24, 2022

A few years later, I think you are right that it should be part of the default. If the user wants to measure those lines, they can.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python Type Checking (Guide) - Real Python
This is a comprehensive guide that will cover a lot of ground. If you want to just get a quick glimpse of how...
Read more >
Common issues and solutions - mypy 0.991 documentation
Common issues and solutions#. This section has examples of cases when you need to update your code to use static typing, and ideas...
Read more >
Exhaustive Type Checking with TypeScript!
We're now ready to discuss exhaustiveness checks. Exhaustive Type Checking. TypeScript supports literal values as types. Suppose we have a ...
Read more >
Documentation - Narrowing - TypeScript
Much like how TypeScript analyzes runtime values using static types, it overlays type analysis on JavaScript's runtime control flow constructs like if/else ......
Read more >
If-Else Statements - Python Numerical Methods
A branching statement, If-Else Statement, or If-Statement for short, ... want a condition that covers any other case, then you can use an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found