Skip to content

patch = subprocess seems not to work with --branch #2021

@jepler

Description

@jepler

Describe the bug
I want to use [run]patch = subprocess together with --branch but I'm getting unexpected results.

To Reproduce

  1. Python 3.13 on Debian Trixie x86_64
  2. Coverage.py, version 7.10.2 with C extension
  3. No other non-standard packages required
  4. Script content (subfunctions.py):
import subprocess
import argparse

def f1():
    print("function 1")
def f2():
    print("function 2")
def f3():
    print("function 3")

functions = [f1, f2, f3]

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("cases", nargs='+', type=int)
    args = parser.parse_args()

    if len(args.cases) > 1:
        for c in args.cases:
            subprocess.call(["python", __file__, f"{c}"])
    else:
        case = args.cases[0]
        functions[case]()

if __name__ == '__main__':
    main()
  1. .coveragerc content:
[run]
patch = subprocess
  1. coverage run --branch subfunctions.py 0 1 2 && coverage combine && coverage report

Expected behavior
The coverage combine step finishes and coverage report probably shows 100% coverage including branch coverage.

Instead, the step of coverage combine errors with a message similar to:

Combined data file .coverage.localhost.233908.XKcIJtPx
Can't combine statement coverage data with branch data

Additional context

This is a minimized reproducer. I ran into a problem in the context of an existing project when I tried to adapt it to use patch=subprocess.

My guess is that the --branch flag is not being propagated to the subprocesses. When the --branch flag is not specified, things seem to work properly.

My reproducer does appear to work properly if [run] branch=true is added to the applicable coveragerc instead of being specified on the commandline.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions