There seem to be two ways to implement this:
- The easy way: simply add the checks to
ABCMeta, so that they will be called whensuper().__new__andsuper().__init__are called. (currently,@abstractmethodare checked duringsuper().__new__).
This however has a disadvantage: It required the user to make sure to call super().__new__ and super().__init__ only after adding the methods/attributes. This can be problematic, because sometimes super().__new__ and super().__init__ are required to be called early to set up infrastructure (torch.nn.Module comes to mind).
- The ideal way: Perform the checks after
__new__/__init__. This would give the user maximum flexibility with how they write custom__new__/__init__. However, I am not sure if it is doable with the current class-creation setup.
I found this other post with the idea of adding a general __post__ hook for class creation . This would be ideal for ABCs. We already have __post_init__ in dataclasses. If there were generic hooks __post_init__ and __post_new__ then it would allow ABCs to perform class / instance validation there, when it is guaranteed that __new__ and __init__ have finished. Packages like pydantic would probably greatly benefit from this.