zarr-python icon indicating copy to clipboard operation
zarr-python copied to clipboard

Child classes do not call the `__init__` method of the base class

Open DimitriPapadopoulos opened this issue 1 year ago • 2 comments

Zarr version

v3

Description

I think this could and should be fixed in most cases:

Do you agree?

Steps to reproduce

Full list of occurrences available here:

  • https://app.deepsource.com/gh/DimitriPapadopoulos/zarr-python/issue/PYL-W0231/occurrences

DimitriPapadopoulos avatar Oct 15 '24 16:10 DimitriPapadopoulos

storage.logging.LoggingStore.init does not call abc.store.Store.init

This is the expected behavior. The LoggingStore wraps another Store object and the assumption is that this store called super().__init__(). I don't think it would make sense to call it again.

jhamman avatar Oct 15 '24 23:10 jhamman

While the child class does not have to call super.__init__(), it is usually expected to.

In this specific case, LoggingStore inherits Store. If possible, LoggingStore.__init__ should call Store.__init__. That does seem possible and preferable here, as the base class initialises some private variables in Store.__init__:

        self._is_open = False
        self._mode = AccessMode.from_literal(mode)

which the child class does not initialise in LoggingStore.__init__:

        self._store = store
        self.counter = defaultdict(int)
        self.log_level = log_level
        self.log_handler = log_handler

        self._configure_logger(log_level, log_handler)

Shouldn't the variables of the base class be initialised when creating a child class? I may be missing something, but then it could mean that the code is overly complex, breaking usual assumptions without any visible explanation.

Said otherwise, why have a non-empty __init__ in an abstract base class, if it is not called by child classes?

DimitriPapadopoulos avatar Oct 16 '24 05:10 DimitriPapadopoulos