python / python/mypy

Coverage status reported for "not TYPE_CHECKING" blocks

Open
#6,101 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

priority-1-normal topic-usability
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

I'm not sure whether this qualifies as a bug, feature request or discussion.

I have a code base that I want to be able to run on Python 3.5 (the version in Debian stable at the moment), without introducing additional runtime dependencies (depending on typing_extensions while type checking is fine, but I don't want to force every end user to install it). Therefore, I made the following workaround for NoReturn:

# NoReturn was introduced in Python 3.6.5.
if TYPE_CHECKING:
	from typing_extensions import NoReturn
else:
	NoReturn = None

This works fine, both at runtime and when type checking. However, when I ask mypy to produce a coverage report, the NoReturn = None line shows up red.

Other code I put in the else body, for example a print statement, also shows up red. My guess is that mypy doesn't type check the else body because TYPE_CHECKING is True and then marks all code that it doesn't have information on as red.

I use the coverage report to see if there is anything regarding type hinting that can be improved in modules and these red lines from not TYPE_CHECKING blocks are noise that makes it harder to spot areas that could be improved. So for my use case, it would be better if blocks that are deliberately unchecked would be marked in either green or a new low-attention color like white or grey.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing mypy's coverage-report handling for TYPE_CHECKING branches and compare how the else body is classified. The work is done when deliberately unchecked code in not TYPE_CHECKING blocks no longer appears as actionable red coverage, while ordinary uncovered code remains distinguishable.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.