nedbat / coveragepy

The code coverage tool for Python
https://coverage.readthedocs.io
Apache License 2.0
3.03k stars 435 forks source link

Adding Branch Coverage to API #1888

Open eleanorjboyd opened 2 weeks ago

eleanorjboyd commented 2 weeks ago

Hi! Eleanor from the Python for VS Code extension following up on API updates I was hoping to get which the VS Code python extension needs to support branch coverage! As you know, right now there is no way to get branch coverage from the API so Im looking to implement this but wanted to start the discussion as an issue to make sure I have the right approach.

What I think is the easiest access point to the branch coverage values I am looking for is in the Analysis class. Here there is the function _total_branches() which would provide one value I need and then I would need either covered branches or missing branches. n_missing_branches is defined in the Analysis class but not accessible, so maybe a getter for this value would be sufficient. (it is my understanding that branches are different from arcs and therefore it is not sufficient to display arc values but if I am mistaken please let me know)

Next is getting the Analysis class instance for a given file. From what I can tell, it seems like this is returned in two places, _analyze or analysis_from_file_reporter, (which makes sense given that _analyze calls analysis_from_file_reporter). It seems like _analyze under the Coverage class is probably easiest, given the comment "Private for now." in its docstring this might have been something already considered.

To summarize, what I am suggesting is:

  1. making the Analysis class part of the API
  2. adding the _analyze function in Coverage to the API
  3. adding a function to Analysis to surface either covered branches or missing branches

These are quite a few asks so if you have a suggestion that might add less to the API (and less to maintain) please let me know. It is not my intent to bloat the API only get these branch numbers so we could also attempt edits to surface them without the Analysis data class but I am unsure.

For context, here is where we collect and transform coverage in the extension. Here was my previous idea on how to get branch coverage before I realized it was not part of the API.

Thank you!