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
{{ message }}
Repository navigation
Assume if TYPE_CHECKING: ... else: ... block is covered #831
fromtypingimportTYPE_CHECKINGifTYPE_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.
Reacted by David, ניר, Adeel Hassan, mbbyn, Augustin LAVILLE, Lars Reimann, Tuukka Mustonen, Daniel Schaffer, Micael Jarniac, Alex Racapé and 19 more
Since this is something that is configurable by the user, I would rather not change coverage.py to deal with it.
Reacted by Dobromir Marinov, earonesty, Peter Schutt, drakenation, Edoardo Facchinelli, Tadeu Manoel, Martin Thoma, Johannes Wienke, Stevan Milic, Julin S and 62 more
@nedbat - when would you expect code inside if TYPE_CHECKING blocks to be executed while code coverage measurement is in effect? How would you feel about making this part of the default exclude_lines?
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.
Reacted by Mika Mäki, Charles Tapley Hoyt, Thomas Berdy, Sebastian Brandt, Den Kasyanov, Vinícius Vargas, Jérome Perrin, Ilya Priven, Qijia Liu, David and 22 more
Reacted by Chris Withers, Sebastian Brandt, Vinícius Vargas, Maddy Guthridge, mbbyn and Robin
I guess I'm also a bit concerned that someone could have their own user-defined base class called Protocol, and I'd inadvertently exclude entire classes.
Can be addressed by r"class .+\(.*\bProtocol\b.*\):" (without taking into account multi-line cases).
I guess I'm also a bit concerned that someone could have their own user-defined base class called Protocol, and I'd inadvertently exclude entire classes.
Yeah, regexes can only get you so far; a proper solution would require AST parsing. Maybe just mention it in the docs so that users can apply it on their own?
@KSmanis - let's not let perfect be the enemy of good - your example is weird enough I'd be happy for the fix for it to be "please format your code differently" :-)
(if I ended up with a need for that layout, I'd be totally fine with a custom exclude or just comment exclude!)
Of course, I'm not advocating for it to be considered. IMO a regex for Protocols and overloads would cover most common cases, rather than trying to perfect a regex for Ellipsis.
I'm still concerned that matching the words "overload" or "Protocol" will have false positives. People can configure those extra regexes themselves. Am I wrong to be concerned?
Activity
nedbat commentedon Aug 2, 2019
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.
feat(kitty#Reasons to migrate from urxvt to kitty): Reasons to migrat…
cjw296 commentedon Jan 24, 2022
@nedbat - when would you expect code inside
if TYPE_CHECKINGblocks to be executed while code coverage measurement is in effect? How would you feel about making this part of the defaultexclude_lines?nedbat commentedon 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.
ikonst commentedon Apr 6, 2022
Can we also add
...to the defaults while we're at it?cjw296 commentedon Apr 7, 2022
@ikonst - I'm not 100% sure that's a good idea, but also as I've painfully discovered, you really mean
\.\.\..ikonst commentedon Apr 7, 2022
Yeah, obviously in its escaped form. I think the sharp change in coverage would tell you :)
cjw296 commentedon Apr 7, 2022
It can be quite the footgun when your projects are starting from 100% line coverage...
Tell coveragepy to ignore type checking code and `...`
Tell coveragepy to ignore type checking code and `...`
Exclude "if TYPE_CHECKING" from code coverage
39 remaining items
nedbat commentedon Jul 17, 2025
I guess I'm also a bit concerned that someone could have their own user-defined base class called Protocol, and I'd inadvertently exclude entire classes.
KSmanis commentedon Jul 17, 2025
Random example in the wild: https://mypy.readthedocs.io/en/stable/protocols.html#defining-subprotocols-and-subclassing-protocols
Can be addressed by
r"class .+\(.*\bProtocol\b.*\):"(without taking into account multi-line cases).Yeah, regexes can only get you so far; a proper solution would require AST parsing. Maybe just mention it in the docs so that users can apply it on their own?
nedbat commentedon Jul 22, 2025
I just learned that ruff will reformat code so that this:
becomes:
which changes my perspective on the inline ellipsis.
Perhaps the ellipsis regex should be:
KSmanis commentedon Jul 22, 2025
Off the top of my head:
Some false negatives:
A false positive:
nedbat commentedon Jul 22, 2025
The false negatives are great examples, thanks. The false positive wouldn't match because of the comma, right?
KSmanis commentedon Jul 22, 2025
True, the false positive could be only triggered by an unformatted (and quite odd):
cjw296 commentedon Jul 22, 2025
@KSmanis - let's not let perfect be the enemy of good - your example is weird enough I'd be happy for the fix for it to be "please format your code differently" :-)
(if I ended up with a need for that layout, I'd be totally fine with a custom exclude or just comment exclude!)
KSmanis commentedon Jul 22, 2025
Of course, I'm not advocating for it to be considered. IMO a regex for Protocols and overloads would cover most common cases, rather than trying to perfect a regex for Ellipsis.
cjw296 commentedon Jul 22, 2025
Separate refex in the defaults to cover those two is a great suggestion!
nedbat commentedon Jul 22, 2025
I'm still concerned that matching the words "overload" or "Protocol" will have false positives. People can configure those extra regexes themselves. Am I wrong to be concerned?
fix: better ellipsis exclusion regex. #831
nedbat commentedon Jul 23, 2025
In commit 4d4ddd8 I made the ellipsis pattern (much) more elaborate:
r"^\s*(((async )?def .*?)?\)(\s*->.*?)?:\s*)?\.\.\.\s*(#|$)",This works for all the real cases we've identified, but still has a false positive:
I think it strikes a good balance.
nedbat commentedon Jul 24, 2025
This is now released as part of coverage 7.10.0.
nedbat commentedon Jul 26, 2025
Oops, the
if TYPE_CHECKING:exclusion was wrong, fixed in commit 1e2f41anedbat commentedon Jul 27, 2025
This is now released as part of coverage 7.10.1.