Skip to content

no-method-argument (PLE0211)

Preview (since 0.17.5) · Category: suspicious · Related issues · View source

Derived from the Pylint linter.

This rule is unstable and in preview. The --preview flag is required for use.

What it does

Checks for methods that have no parameters.

Methods with decorators are ignored because decorators can change how a method is called.

Why is this bad?

Calling a method through an instance passes the instance as an implicit positional argument. Some special methods, such as __init_subclass__, receive the class instead. A method without any parameters cannot accept this argument and raises a TypeError when called this way.

Add an instance or class parameter, conventionally named self or cls. If the function does not need either, use @staticmethod instead.

Example

class Example:
    def method():
        return 1

Use instead:

class Example:
    def method(self):
        return 1

Known problems

An ordinary function defined in a class can be called directly through the class, without passing an instance: Example.method() works in the first example, while Example().method() raises a TypeError. This rule flags the definition regardless of how it is called. Use @staticmethod, or suppress the diagnostic if class-only access is intentional.

References