jendrikseipp / vulture

Find dead Python code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wrong processing of typing.Protocol

akhundMurad opened this issue · comments

Suppose I have protocol class:

from typing import Protocol


class LoggerProtocol(Protocol):
    def log(
        self, level: int, msg: str, *args, extra: Mapping[str, object] | None = None
    ) -> None:
        ...

After execution of vulture I get the following output:

logging.py:24: unused variable 'args' (100% confidence)
logging.py:24: unused variable 'extra' (100% confidence)
logging.py:24: unused variable 'msg' (100% confidence)

In my opinion, vulture should ignore this type of "confidence", due to the nature of typing.Protocol

commented

Hi, can I work on this?

Can't you just add del args etc. in the method body @akhundMurad ?

@jendrikseipp It will make my code messy. Just imagine that each protocol method will have del statement in it...

Can you paste the content of the method to get a clearer picture for the use case?

Also, is it possible to change the method signature, i.e., to change msg to msg_ to signal that the argument is unused?

https://github.com/akhundMurad/diator/blob/main/src/diator/middlewares/logging.py

Here you can see the full code. I added # noqa comment as a temporary fix.

Thanks! And is it possible to change the method signature, i.e., to change msg to msg_ to signal that the argument is unused?

The purpose of the Protocol is to define the signature of class and its methods.
Therefore, I cannot change the argument name

OK, thanks. Then the best solution would be to detect that this class inherits from Protocol and ignore the unused method arguments. Doing this cleanly will probably require adding scope information to Vulture, see #304. Until then, I don't think it makes sense to tackle this issue.

Got it, thank you!