Skip to content

fix: a negative --precision exits with a traceback - #2261

Merged
nedbat merged 2 commits into
coveragepy:mainfrom
VXNCXNX:fix/negative-precision
Aug 15, 2026
Merged

nedbat merged 2 commits into
coveragepy:mainfrom
VXNCXNX:fix/negative-precision

Conversation

@VXNCXNX

@VXNCXNX VXNCXNX commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

A negative --precision exits with a raw traceback instead of a configuration error.

$ coverage report --precision=-2
Traceback (most recent call last):
  ...
  File "coverage/results.py", line 422, in display_covered
    return f"{pc:.{precision}f}"
ValueError: Format specifier missing precision

After:

precision=-2 is invalid. Must not be negative.

The same applies to coverage html --precision=-2 and to precision = -1 in a .coveragerc, so it is reachable from a config file and not just a typo on the command line.

should_fail_under, in this same file, already raises ConfigError for an out-of-range fail_under:

if not (0 <= precision < 10):
    raise ConfigError(f"Invalid precision: {precision}")

so this is the missing parallel guard rather than a new convention. ConfigError is caught by cmdline.main() and printed as a plain message.

Normal use is unaffected: --precision=2 still gives TOTAL 3 0 100.00%.

Testing

test_display_covered_negative_precision in tests/test_results.py, parametrised over -1 and -2.

Reverting only the condition, precision < 0 to precision < -99, so the test still imports cleanly and fails as a test rather than an error:

FAILED tests/test_results.py::NumbersTest::test_display_covered_negative_precision[-1]
E   ValueError: Format specifier missing precision

pytest tests/test_results.py is 47 passed. mypy coverage/results.py is Success: no issues found. ruff-format is clean.

Two pre-existing items I left alone, both verified identical on a stashed tree: a RUF015 finding at results.py:44, and 8 failures in test_report.py that need the C extension and a subprocess PATH this environment does not provide.

Disclosure: written with AI assistance (Claude Code). I reproduced the traceback, checked all three entry points (CLI report, CLI html, config file), and ran the revert check myself.

@nedbat
nedbat merged commit 08c7fd6 into coveragepy:main Aug 15, 2026
72 checks passed
@nedbat

nedbat commented Aug 28, 2026

Copy link
Copy Markdown
Member

This is now released as part of coverage 7.16.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants