By default, the dataclass decorator generates an __init__ method which does the following (among other things):
- Accepts input arguments based on the field definitions in the current and parent classes
- Initializes the attributes of
self from input arguments
- Calls
self.__post_init__
Python's inheritance works such that if a parent class has an attribute, the child inherits it unless told otherwise. That's sort of the point.
To disable inheritance, you must override it somehow. The pythoic approach to disabling an inherited method entirely is to set it to None in the child class:
@dataclass
class B(A):
__post_init__ = None
You can find this technique in the documentation of NotImplementedError, which you should not be using here:
Note: It [NotImplementedError] should not be used to indicate that an operator or method is not meant to be supported at all – in that case either leave the operator / method undefined or, if a subclass, set it to None.
In fact, any time the child class overrides a method, the parent implementation won't be called unless explicitly told to do so.
In other words, setting the child's method to None is roughly equivalent to
@dataclass
class B(A):
def __post_init__(self):
pass
While leaving the name out of the child class is very roughly equivalent to
@dataclass
class B(A):
def __post_init__(self):
super().__post_init__()
By not calling super().__post_init__() from within the child implementation, you prevent the parent implementation from ever being executed.