Repository navigation
fix: don't drop same-named functions and classes from the JSON report - #2353
Conversation
|
Thanks for fixing the lost regions. I ran this PR on rich, click, attrs and sqlalchemy, 368 files. Without it, 55 files had regions that didn't add up to the file's lines. With it none do, and the file totals didn't change. I found one case where the JSON names don't match LCOV: a function defined in try:
import no_such_module_xyz
except ImportError:
def f():
return 5
else:
def f():
return 8
f()
If you want them to match, sorting Also, 7.17.1 and 7.17.2 went out after this branched, so a clean merge puts the CHANGES entry under 7.17.1 rather than Unreleased. |
The JSON report stores functions and classes in dicts keyed by name, but names can repeat, for example a property getter and setter, or a function or class defined in both branches of an if/else. Each later region overwrote the earlier one, so its lines were missing from the region data. Repeated names now get an @<line number> suffix, as the LCOV report does since coveragepy#2335. As in LCOV, the first same-named region with statements keeps the plain name, so an overload implementation keeps its name and the statement-less stubs get the suffix.
code_regions() lists the regions of an else or finally clause before those of the except handlers, so when a function was defined in both an except handler and the else clause, the JSON report gave the plain name to the later one and LCOV gave it to the earlier one. Sorting the regions by line before naming them makes the two reports agree.
15efe12 to
82a6f57
Compare
|
Thanks @ericdelorefice, good catch. 82a6f57 sorts the regions by line before naming them, so the JSON names now match LCOV in the try/except/else case, with a test that checks both reports. I also rebased on main, so the CHANGES entry is back under Unreleased. |
|
This is now released as part of coverage 7.17.3. |
The JSON report keys each file's
functionsandclassesby name, but Python code often has more than one function or class with the same name. A property getter and setter are bothC.x, and a function or class defined in both branches of an if/else repeats its name too. In those cases the later one overwrites the earlier one in the dict, so the earlier one's lines aren't in any region. It's the same duplication as #2335 for LCOV, but here the data is silently lost.For example:
After
coverage runandcoverage json, the file'smissing_linesare[4, 9], butfunctionshas only"C.x"(the setter,start_line7, missing[9]) and"". The getter and its missing line 4 are gone.coverage lcovon the same file writesFN:3,4,C.xandFN:7,9,C.x@7.This uses the same rule as the LCOV fix: a repeated name gets an
@<line number>suffix, so the example now has"C.x"(start 3, missing[4]) and"C.x@7"(start 7, missing[9]). Also like LCOV, the first same-named region with statements keeps the plain name, so with@overloadthe implementation stays"f"and the stubs, which have no statements, get the suffix. Names that don't repeat are unchanged. The new test covers a getter/setter pair, a class redefined in an if/else, and overloads, for bothfunctionsandclasses, and fails on main.Ran: the new test on Python 3.11 to 3.14 with the Python tracer and sys.monitoring cores, the report test modules on the same versions and cores, and the full suite on 3.14 (sys.monitoring) and 3.11 (Python tracer), with the same results as main apart from the new test.
Checked:
ruff format --check, pylint,mypy --strictand the doc8, sphinx-lint and sphinx-build steps oftox -e docreport nothing new compared with main.