Method Could Be Static

ID

python.method_could_be_static

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Best Practice

Language

Python

Tags

best-practice, code-style

Description

Reports instance methods whose body never references self. Such methods do not depend on any instance state and can be marked @staticmethod, which makes the lack of dependency explicit, lets callers invoke them via Cls.method(…​) without instantiating, and slightly reduces per-call overhead.

class Foo:
    def helper(self, x):       # FLAW — body never uses self
        return x * 2

    def method(self):
        return self.helper(self.value)

Rationale

A method that takes self but never reads or writes through it is a method the class structure does not justify. Promoting it to @staticmethod documents the truth and prevents future maintenance from accidentally introducing a self.X reference that ties the helper to the class state.

The rule is conservative: it skips methods decorated with @staticmethod, @classmethod, @property, or any of the @abstractXxx family, and it skips dunder methods (init, eq, repr, …​) since the data model often requires them to be instance methods even when their implementation ignores self.

Remediation

Add the @staticmethod decorator and drop the self parameter:

class Foo:
    @staticmethod
    def helper(x):
        return x * 2

If the method really should depend on instance state, the fix is the other direction — make it use self.