python / python/mypy

Add a way for mypy coverage reports to omit certain lines or functions from precision checking

Open
#9,106 0 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

When looking at a coverage report which shows the precision of the types on my code, I very frequently find that the same handful of functions are responsible for lines being marked "imprecise".

In general:

  • library or stdlib functions which really consume Any, like Logger.debug
  • library or stdlib functions which produce Any, like sqlalchemy.orm.query.Query.scalar
  • functions in my own code where the type is Any to avoid other issues (e.g. absence of unsafe unions)

I would like to be able to tell mypy to exempt certain usages from its reporting.
Several tools support this via comments (# pragma: no cover, # fmt: off, # noqa), but coverage has another interface which may be better suited to mypy: you can specify expressions which are skipped. (tool.coverage.report.exclude_lines in pyproject.toml).

Being able to stipulate something like:

mypy-coverage-precision-exclude =
    logging.Logger.debug
    logging.Logger.error
    sqlalchemy.orm.query.Query.scalar
    mymodule.myfunc

would make the coverage reporting much more useful, because it would eliminate "false negatives" on lines that really ought to have imprecise types.

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

The issue names no files or tests. Start by locating mypy’s coverage-precision reporting and configuration parsing, then trace how reported usages are identified. Done means users can configure exclusions for specified functions or usages and the coverage report omits those entries without affecting normal type checking.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
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.