diff --git a/Lib/test/test_trace.py b/Lib/test/test_trace.py index 241e841d732f5e..8ad5a4f09085db 100644 --- a/Lib/test/test_trace.py +++ b/Lib/test/test_trace.py @@ -487,6 +487,31 @@ def test_cover_files_written_no_highlight(self): " print('unreachable')\n" ) + def test_summary_without_missing(self): + for options in ([], ['--missing']): + with self.subTest(options=options): + _, stdout, _ = assert_python_ok( + '-m', 'trace', '--count', '--summary', *options, self.codefile) + self.assertIn(b'3 66.7%', stdout) + with open(self.coverfile, encoding='iso-8859-15') as f: + self.assertEqual('>>>>>>' in f.read(), bool(options)) + + def test_summary_preserves_results_file_override(self): + seen = [] + + class Results(trace.CoverageResults): + def write_results_file(self, path, lines, lnotab, lines_hit, encoding=None): + seen.append(bool(lnotab)) + return super().write_results_file(path, lines, lnotab, lines_hit, encoding) + + counts = {(self.codefile, 2): 1, (self.codefile, 3): 1} + results = Results(counts) + results.write_results() + with captured_stdout() as stdout: + results.write_results(show_missing=False, summary=True) + self.assertEqual(seen, [True, False]) + self.assertIn('3 66.7%', stdout.getvalue()) + def test_cover_files_written_with_highlight(self): argv = '-m trace --count --missing'.split() + [self.codefile] status, stdout, stderr = assert_python_ok(*argv) diff --git a/Lib/trace.py b/Lib/trace.py index 66471f45e1c004..c1688d2f3bce4a 100644 --- a/Lib/trace.py +++ b/Lib/trace.py @@ -279,6 +279,13 @@ def write_results(self, show_missing=True, summary=False, coverdir=None, *, n_hits, n_lines = self.write_results_file(coverpath, source, lnotab, count, encoding) if summary and n_lines: + if not show_missing: + executable = _find_executable_linenos(filename) + n_lines += sum( + lineno in executable and lineno not in count + and PRAGMA_NOCOVER not in line + for lineno, line in enumerate(source, 1) + ) sums[modulename] = n_lines, n_hits, modulename, filename if summary and sums: diff --git a/Misc/NEWS.d/next/Library/2026-11-26-14-30-00.gh-issue-162726.TrSm26.rst b/Misc/NEWS.d/next/Library/2026-11-26-14-30-00.gh-issue-162726.TrSm26.rst new file mode 100644 index 00000000000000..d649009d7ef485 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-26-14-30-00.gh-issue-162726.TrSm26.rst @@ -0,0 +1,2 @@ +Fix coverage percentages reported by :mod:`trace` when +:option:`!--summary` is used without :option:`!--missing`.