astral-sh / ruff

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

ARG003 does not seem to respect Protocols #13678

Open harahu opened 1 day ago

harahu commented 1 day ago

The ruff ruleARG003: unused-class-method-argument does not seem to understand that unused arguments sometimes are necessary in order to implement a protocol. See example below:

from abc import abstractmethod
from typing import Protocol

class A(Protocol):
    @classmethod
    @abstractmethod
    def foo(cls, bar: str) -> str:
        raise NotImplementedError

class B(A):
    @classmethod
    def foo(cls, bar: object) -> str:
        return "foo"

https://play.ruff.rs/204c57b4-a35e-48d5-8f92-ca2e109dd2cd

This problem appears in ruff 0.6.9

MichaReiser commented 23 hours ago

All Ruff rules don't understand Protocols, or class inheritance (with few hardcoded exceptions) because Ruff doesn't support multifile analysis (it doesn't understand the Protocol type nor can it resolve the class A if it is defined in another file).

For now, I suggest that you add a @typing.override decorator to B.foo. It let's Ruff know that this is an overridden method and that extra arguments are intentional.

harahu commented 22 hours ago

I appreciate your clear explanation of the current limitations of ruff. Also appreciate the advice on @typing.override. Thanks!

I understand that there is a significant backlog of features hinging on type inference and multi-file analysis, so I wish you guys all the best in working towards those. Ruff is already really great, but would undoubtedly be even better with those capabilities implemented.