jendrikseipp / vulture

Find dead Python code
MIT License
3.38k stars 148 forks source link

Don't report unused variables #269

Closed MarcoGorelli closed 2 years ago

MarcoGorelli commented 2 years ago

Hi @jendrikseipp ,

Well done on vulture, it's awesome!

Would you consider an option to make vulture not report unused variables, and hence only report on unreachable code?

Happy to make a PR if so

RJ722 commented 2 years ago

@MarcoGorelli I'm not sure about an option within vulture. But in the mean time, since in addition to unreachable code, only unused function arguments are reported with 100% confidence, you can weed them out using grep:

vulture code/ --min-confidence 100 | grep -v 'unused'
MarcoGorelli commented 2 years ago

Thanks @RJ722 for your reply!

True, I can do that, but then the return code would be affected

Anyway, if this isn't welcome within Vulture, I'll find a workaround - thanks!

MarcoGorelli commented 2 years ago

Furthermore, I don't think that would work within a pre-commit hook

RJ722 commented 2 years ago

@MarcoGorelli correct, thanks for your insights!

@jendrikseipp do you think that this is the right time to publicize issue codes? I was thinking if we could provide an option to filter using issue codes, it would be elegant.

MarcoGorelli commented 2 years ago

Thanks! Here's a workaround I'm using https://github.com/pandas-dev/pandas/pull/45173

RJ722 commented 2 years ago

I see that a Python script is invoking Vulture in a subprocess. Since Vulture also has a clean Python API, I'm curious if you'd want to try it out instead (following is untested – It's almost 4 AM here, and I really should go get some sleep):

import sys

from vulture import Vulture

v = Vulture()
v.scavenge(files)
for item in v.get_unused_code(min_confidence=100):
    if item.typ == "unreachable_code":
        print(item.get_report())

sys.exit(v.found_dead_code_or_error)
MarcoGorelli commented 2 years ago

Ah, I didn't realise there was an API! I'll use that then, thanks, no need for a flag

jendrikseipp commented 2 years ago

Nice idea @RJ722 !

MarcoGorelli commented 2 years ago

Yup, thanks!

I had to change it to

import argparse
import subprocess
import sys

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("files", nargs="*")
    args = parser.parse_args()

    from vulture import Vulture

    v = Vulture()
    v.scavenge(args.files)
    ret = 0
    for item in v.get_unused_code(min_confidence=100):
        if item.typ == "unreachable_code":
            print(item.get_report())
            ret = 1

    sys.exit(ret)

though, v.found_dead_code_or_error is False in my example

RJ722 commented 2 years ago

Do I spot an unused import? 👀

MarcoGorelli commented 2 years ago

Yeah, I just didn't think that'd affect how vulture would run

shaperilio commented 1 year ago

I just started exploring vulture; it looks really useful.

I too wanted to disable reporting unused variables. We use other tools for that, and vulture is reporting too many cases that we don't care about - for example, when overriding __new__ and also having an __init__ with arguments.

I think a command-line flag would be useful for this. I would like to be able to set that in the config and just run vulture, I think that's better than having to write a script or to filter results somehow.