Skip to content

fix: don't drop same-named functions and classes from the JSON report - #2353

Merged
nedbat merged 4 commits into
coveragepy:mainfrom
nk-batman21:fix-json-duplicate-region-names
Nov 11, 2026
Merged

nedbat merged 4 commits into
coveragepy:mainfrom
nk-batman21:fix-json-duplicate-region-names

Conversation

@nk-batman21

Copy link
Copy Markdown
Contributor

The JSON report keys each file's functions and classes by name, but Python code often has more than one function or class with the same name. A property getter and setter are both C.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:

class C:
    @property
    def x(self):
        return 1

    @x.setter
    def x(self, value):
        if value:
            pass


c = C()
c.x = 0

After coverage run and coverage json, the file's missing_lines are [4, 9], but functions has only "C.x" (the setter, start_line 7, missing [9]) and "". The getter and its missing line 4 are gone. coverage lcov on the same file writes FN:3,4,C.x and FN: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 @overload the 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 both functions and classes, 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 --strict and the doc8, sphinx-lint and sphinx-build steps of tox -e doc report nothing new compared with main.

@ericdelorefice

Copy link
Copy Markdown
Contributor

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 except and also in else or finally.

try:
    import no_such_module_xyz
except ImportError:
    def f():
        return 5
else:
    def f():
        return 8
f()

coverage lcov gives f (line 4) and f@7. coverage json on this branch gives f (line 7) and f@4. So plain f is the one that ran in LCOV and the one that didn't in JSON. code_regions visits else and finally before the handlers, and LCOV sorts its functions by line before naming them. get_windows_console_features in rich's _windows.py hits this, and it was the only name mismatch in the 368 files.

If you want them to match, sorting regions by r.start when noun_regions is built did it here and for rich, and tests/test_json.py still passed, though it has no case like this. Leaving it is fine by me too.

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.
@nk-batman21
nk-batman21 force-pushed the fix-json-duplicate-region-names branch from 15efe12 to 82a6f57 Compare November 7, 2026 10:11
@nk-batman21

Copy link
Copy Markdown
Contributor Author

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.

@nedbat
nedbat merged commit 1a06304 into coveragepy:main Nov 11, 2026
97 of 108 checks passed
@nedbat

nedbat commented Nov 12, 2026

Copy link
Copy Markdown
Member

This is now released as part of coverage 7.17.3.

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.

3 participants