Why is this an issue?
"Class-Private" methods that are never executed inside their enclosing class are dead code: unnecessary, inoperative code that should be removed.
Cleaning out dead code decreases the size of the maintained codebase, making it easier to understand the program and preventing bugs from being
Python has no real private methods. Every method is accessible. There are however two conventions indicating that a method is not meant to be
- methods with a name starting with a single underscore (ex:
_mymethod) should be seen as non-public and might change without prior
notice. They should not be used by third-party libraries or software. It is ok to use those methods inside the library defining them but it should
be done with caution.
- "class-private" methods have a name which starts with at least two underscores and ends with at most one underscore. These methods' names will
be automatically mangled to avoid collision with subclasses' methods. For example
__mymethod will be renamed as
classname is the method’s class name without its leading underscore(s). These methods
shouldn’t be used outside of their enclosing class.
This rule raises an issue when a class-private method (two leading underscores, max one underscore at the end) is never called inside the class.
Class methods, static methods and instance methods will all raise an issue.
Noncompliant code example
def __mangled_class_method(cls): # Noncompliant
def __mangled_static_method(): # Noncompliant
def __mangled_instance_method(self): # Noncompliant