jendrikseipp / vulture

Find dead Python code
MIT License
3.45k stars 152 forks source link

Skip functions decorated with abc.abstractmethod #317

Open xmnlab opened 1 year ago

xmnlab commented 1 year ago

hey everyone!

I am new to vulture, and maybe there is a nice way to do that already, but something that I saw that maybe could be done automatically is to skip methods that is decorated by abc.abstractmethod.

for now I am just using noqa to skip that, Although I saw that is preferred to use whitelist .. but at least for now, I think that noqa would be simpler than adding a new file to the repo.

thank you so much!

jendrikseipp commented 1 year ago

You can use --ignore-decorators @abc.abstractmethod for this purpose.

xmnlab commented 1 year ago

thanks for your response @jendrikseipp !

for some reason it is not working:

$ vulture --min-confidence 80 --ignore-decorators @abc.abstractmethod src/arxir/builders/base.py 
src/arxir/builders/base.py:59: unused variable 'output_file' (100% confidence)
 58     @abc.abstractmethod
 59     def build(self, expr: ast.AST, output_file: str) -> None:
 60         ...
 61 

I am using the latest version:

$ vulture --version
vulture 2.9.1

any thoughts?

jendrikseipp commented 1 year ago

Currently, --ignore-decorators @abc.abstractmethod ignores the build method, but not it's arguments. If you make a case for ignoring the arguments as well, we could think about how to implement this.

xmnlab commented 1 year ago

not sure if I understood i correctly, but my case is very simple, abc.abstractmethod defines a method that will be overridden, that means that that function doesn't need to have any code inside, so any argument don't need to be used inside the function. In my example above, it just uses ... to define an empty body.

so, in this case, the vulture will alert that that variable (argument) is not used with 100% of confidence.

it would be nice to have some way to skip any argument of functions decorated by abc.abstractmethod .. or at least a generic way to do that where I can configure to skip any functions and their arguments decorated by a specific decorator (but not the functions that override the original ones).

jendrikseipp commented 1 year ago

I don't know enough about the abc module, but from a quick glance at its docs, it seems that ignoring all arguments of all methods that have only ... (ellipsis) in their body should solve your problem, right? And if the method itself is not used anywhere, this should still be reported as dead code, right?

dpinol commented 1 week ago

I understand that --ignore-decorators @abc.abstractmethod is not a valid wowkaround here because what we need is a way to skip errors in interface method declarations, not in implementations of ABC's methods. Eg.

class Logger(ABC):
    @abstractmethod
    def log(self, message: str, arg1: int) -> None:
        raise NotImplementedError()

It should not complain about unused arg1 usage because this is not an implementation of a function, just an interface definition to be overridden by derived classes.