guettli
guettli

Reputation: 27139

Abstract base class: raise NotImplementedError() in `__init__.py`?

PyCharm warns me Call to __init__ of super class is missed

class AbstractBase(object):
    def __init__(self):
        raise NotImplementedError()

class RealChild(AbstractBase):
    def __init__(self):
        #super(RealChild, self).__init__() ####
        print('do stuff')

child=RealChild()

But if I call it, the class AbstractBase will raise NotImplementedError.

I am a sheep and don't know how to proceed :-)

Upvotes: 8

Views: 5095

Answers (4)

oulenz
oulenz

Reputation: 1244

The answer by @Bryant correctly explains that you should use @abstractmethod instead of manually raising a NotImplementedError.

This allows you to call the super __init__, but it doesn't necessarily mean that you should, since it still doesn't do anything. It's a matter of personal taste, and Pycharm is wrong to issue a warning over it.

So my alternative answer is: ignore Pycharm and hope that they fix this some day.

Upvotes: 0

Bryant
Bryant

Reputation: 622

You might consider using the abc Abstract Base Class module to mark __init__ as abstract, and then go ahead and invoke the superclass __init__ from the subclass (and, as DorElias suggested, give the superclass __init__ a trivial implementation of pass):

from abc import ABCMeta, abstractmethod


class AbstractBase(object, metaclass=ABCMeta):
    @abstractmethod  # This method must be overridden...
    def __init__(self):
        print("...but can still be called via super by subclasses have shared construction logic")
        pass


class RealChild(AbstractBase):
    def __init__(self):
        super().__init__()  # Won't do anything, UNTIL the day you decide all subclasses of AbstractBase need shared logic
        print('do stuff')


child = RealChild()

If you try to instantiate via parent = AbstractBase() or parent = AbstractBase.__new__(AbstractBase), you'll get an error:

TypeError: Can't instantiate abstract class AbstractBase with abstract methods init

So you've got your uninstantiable abstract safety, but at the same time you are still well set up to alter all child class construction by altering base class construction, as is right and proper.

Upvotes: 8

poke
poke

Reputation: 388393

You could do something kind-of ugly and check the type of self in the abstract type’s initializer to make sure that it was subtyped:

class AbstractBase (object):
    def __init__ (self):
        if type(self) is AbstractBase:
            raise NotImplementedError

I think a more “normal” approach would be to simply not expose the abstract base type and expect users not to create it.

Upvotes: 4

DorElias
DorElias

Reputation: 2313

In the abstract class replace the exception in the init function with

pass

This exception is used to prevent you from initializing new instance of the abstract class (it is abstract, so you can't) So either use 'pass' or don't listen to pycharm and don't call super

Upvotes: 0

Related Questions