The error "__init__ method from the 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 peelint says

__init__ method from base class 'A' is not called

Obviously i don't want to do

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

(I tried ABC and got

Undefined variable 'abstractmethod'

from the peel, so this is also not an option).

+8
source share
3 answers

Usage abcworks 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 abcor parent __init__not 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)

What it's worth, I'm with @holdenweb on this. Sometimes you know better than pylint.

+4
source

pylint. , . , , . - , .

+6

__init__ .

A ABC (AB stract C lass) @abstractmethod , .

from abc import ABC, abstractmethod

class A(ABC):
    @abstractmethod
    def cool_method(self):
        raise NotImplemented

, A, . __init__ __init__ A , :

def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)

In addition, it has the advantage that it respects mro, if you later want to use Amultiple inheritance in the configuration, you will not have problems.

0
source

All Articles