From 474a3639cd73d5a9498ae3ae93ee78a6621d5a0a Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Thu, 10 Oct 2024 18:30:21 -0400 Subject: [PATCH] fix: didn't-ectomy. #1873 --- CHANGES.rst | 5 ++++- coverage/parser.py | 2 +- tests/test_arcs.py | 13 +++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 803b6dd46..2cd75262a 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -23,7 +23,10 @@ upgrading your version of coverage.py. Unreleased ---------- -Nothing yet. +- Fix: the missing branch message about not exiting a module had an extra + "didn't," as described in `issue 1873`_. This is now fixed. + +.. _issue 1873: https://github.com/nedbat/coveragepy/issues/1873 .. start-releases diff --git a/coverage/parser.py b/coverage/parser.py index 842190a3d..cf454eba1 100644 --- a/coverage/parser.py +++ b/coverage/parser.py @@ -719,7 +719,7 @@ def _code_object__Module(self, node: ast.Module) -> None: if node.body: exits = self.process_body(node.body) for xit in exits: - self.add_arc(xit.lineno, -start, xit.cause, "didn't exit the module") + self.add_arc(xit.lineno, -start, xit.cause, "exit the module") else: # Empty module. self.add_arc(start, -start) diff --git a/tests/test_arcs.py b/tests/test_arcs.py index a8abcab8b..969619667 100644 --- a/tests/test_arcs.py +++ b/tests/test_arcs.py @@ -323,6 +323,19 @@ def f(x): expected = "line 3 didn't jump to the function exit" assert self.get_missing_arc_description(cov, 3, -2) == expected + def test_leaving_module(self) -> None: + cov = self.check_coverage("""\ + print(a := 1) + if a == 1: + print(3) + """, + branchz="2. 23", + branchz_missing="2.", + ) + assert self.stdout() == "1\n3\n" + expected = "line 2 didn't exit the module because the condition on line 2 was always true" + assert self.get_missing_arc_description(cov, 2, -1) == expected + def test_with_with_lambda(self) -> None: self.check_coverage("""\ from contextlib import nullcontext