coveragepy icon indicating copy to clipboard operation
coveragepy copied to clipboard

Option to record and report classes and imports not used at runtime

Open Zeckie opened this issue 1 year ago • 1 comments

A project I'm working on is quite large - has a lot of modules (so lots of imports) and a lot of dataclasses. The dataclasses (or other classes that do not contain their own methods) and their fields show as 100% covered if the module containing them was loaded, whether the classes / fields are used or not.

This can also happen with imports - the import is run, and the target module loaded, so coverage records them as covered. However, nothing actually used the module / functions / classes that were imported.

Describe the solution you'd like

  • A new option, similar to branch coverage, that collects the extra details at runtime (whether the class is instantiated, and which fields are accessed, which imports are used)
  • Highlight unused items in reports, similar to how missing branches are shown

Describe alternatives you've considered Static checkers can sometimes find unused code, such as imports, but they usually assume that all functions / methods / branches are called, and do not report classes as unused if they are public and so could be used by another module.

Functions / methods could be included too, but I don't think that is necessary as the coverage of code within them is recorded.

Additional context There are other things in python that behave in a similar way (are defined, given a name, and later accessed by that name) such as global variables.

Zeckie avatar Mar 25 '24 02:03 Zeckie

I can see why this would be useful, but I don't see a way to collect that information.

nedbat avatar Mar 26 '24 11:03 nedbat