`trace.CoverageResults.__init__` does not copy the counts dict
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Issue
CoverageResults.__init__ copies calledfuncs and callers via .copy() but assigns counts directly. This means self.counts is an alias to the caller's dict, and calling update() mutates the original dict passed to the constructor.
Reproducer
from trace import CoverageResults
# counts is NOT copied — original is mutated:
counts = {}
cr = CoverageResults(counts=counts)
cr.update(CoverageResults(counts={('file.py', 1): 5}))
print(counts) # {('file.py', 1): 5} — mutated
# calledfuncs IS copied — original is not mutated:
calledfuncs = {}
cr = CoverageResults(calledfuncs=calledfuncs)
cr.update(CoverageResults(calledfuncs={('file.py', 'mod', 'func'): 1}))
print(calledfuncs) # {} — not mutated
Impact
The documentation states that CoverageResults "should not be created directly by the user," so direct external impact is limited. However, the inconsistency also affects internal usage: Trace.results() passes its internal self.counts directly to the constructor, so calling update() on the returned CoverageResults will mutate the Trace object's internal state.
CPython versions tested on:
CPython main branch
Operating systems tested on:
No response
Linked PRs
- gh-146176
- gh-146183
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in Lib/trace.py at CoverageResults.init and Trace.results(), where the issue says the counts dictionary is passed through. Check the existing update behavior and add coverage for preserving the caller's dictionary; done means updating a CoverageResults no longer mutates the dictionary supplied by the constructor or the Trace object's internal state.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 25/100