Assume `if TYPE_CHECKING: ... else: ...` block is covered
See original GitHub issueif 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:
- Created 4 years ago
- Reactions:1
- Comments:8 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You can solve this yourself today by adding this to your .coveragerc file:
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.
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.