i have come across some weird behavior when using ABC library in Python 3.8.10.
from abc import ABC, abstractproperty, abstractstaticmethod, abstractmethod
from dataclasses import dataclass
@dataclass
class Message(ABC):
_type: str = None
_error: str = None
_error_msg: str = None
def error(self):
return self._error is not None
def error_msg(self):
return self._error_msg
@abstractstaticmethod
def from_payload(payload: dict):
"""Static initiator of the Message class."""
@abstractproperty
def json(self):
"""Returns json string suitable for websocket communication."""
class ActionsLogDetail(Message):
"""Represents detail for one action log from db."""
def __init__(self, id):
super().__init__(_type='ActionsLogDetail')
self.id = id
ActionsLogDetail.from_payload({'x':1})
None # This should raise error and not return None
Well i did some digging in the documentation and found this:
But, if I change the code appropriately:
@staticmethod
@abstractmethod
def from_payload(payload: dict):
"""Static initiator of the Message class."""
ActionsLogDetail.from_payload({'x':1})
None # Still returns None!
Well now I am thinking whether it could be maybe due to dataclass, but don't think so. This kind of defeats the purpose and kind of forces me to go back to raise NotImplementedError
in the parent class, but that lets me initiate, which I want to avoid.