Search code examples
pythonpylint

Error "__init__ method from base class is not called" for an abstract class


I have

class A(object):
    def __init__ (self): raise NotImplementedError("A")

class B(A):
    def __init__ (self):
        ....

and pylint says

__init__ method from base class 'A' is not called

Obviously, I do not want to do

super(B, self).__init__()
  • so what do I do?

(I tried abc and got

Undefined variable 'abstractmethod'

from pylint, so that is not an option either).


Solution

  • Using abc works for me:

    import abc
    
    class A(object):
        __metaclass__ = abc.ABCMeta
    
        @abc.abstractmethod
        def __init__(self):
            pass
    
    class B(A):
        def __init__(self):
            super(B, self).__init__()
    

    I get warnings, but nothing related to abc or the parent's __init__ not being called:

    C:  1, 0: Missing module docstring (missing-docstring)
    C:  3, 0: Invalid class name "A" (invalid-name)
    C:  3, 0: Missing class docstring (missing-docstring)
    R:  3, 0: Too few public methods (0/2) (too-few-public-methods)
    C:  9, 0: Invalid class name "B" (invalid-name)
    C:  9, 0: Missing class docstring (missing-docstring)
    R:  9, 0: Too few public methods (0/2) (too-few-public-methods)
    R:  3, 0: Abstract class is only referenced 1 times (abstract-class-little-used)
    

    For what its worth, I'm with @holdenweb on this one. Sometimes you know better than pylint.