astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
28.78k stars 932 forks source link

`ARG002` false positive with `kwargs`-consuming decorators #8568

Open rassie opened 8 months ago

rassie commented 8 months ago

My code goes something like this:

@wrapt.decorator
def my_request(wrapped, instance, args, kwargs):
    filtered_kwargs = {key: value for key, value in kwargs.items() if value is not None}
    url = wrapped(*args, **filtered_kwargs)
    response = requests.get(url, filtered_kwargs)
    return response.content

class MyClass:
    @my_request
    def some_data(self, additional_data=None):
        return "https://myurl.example.com"

The idea is to define available query parameters through non-None keyword arguments, so if I call obj.some_data() I expect the contents of "https://myurl.example.com" to be returned and on obj.some_data(additional_data="data"), "https://myurl.example.com?additional_data=data" should be called.

Now the problem is that ARG002 is triggered on additional_data, which is of course used, but only indirectly via the decorator. I don't know whether looking up what decorators are doing is in scope for ruff at all so I don't have high expectations that this is worth fixing, but would still appreciate it deeply.

charliermarsh commented 8 months ago

This will be pretty tough to fix, my honest suggestion would be to try and restructure the code to avoid this kind of interception-via-decorator, but I know you didn't come here for those kinds of suggestions :)