I'm trying to create Model class with attrs module. In my script CollectionModel
class is inherited in User
class. So logically all the attributes in the CollectionModel
should be available in the User
. But while trying to create a new instance of User from dictionary (it is possible with attrs) it shows that attributes of CollectionModel
are not present in the User
My Script:
from bson import ObjectId
from attrs import asdict, define, field, validators, Factory
import time
@define
class CollectionModel:
""" Base Class For All Collection Schema"""
_id : str = field(converter=str, default=Factory(ObjectId))
_timestamp : float = field(default=Factory(time.time))
def get_dict(self):
return asdict(self)
def update(self):
self._timestamp = time.time()
@define
class User(CollectionModel):
username : str = field(factory = str, validator=validators.instance_of(str))
userType : str = field(factory = str, validator=validators.instance_of(str))
password : str = field(factory = str, validator=validators.instance_of(str))
user_object = User()
new_user_object = User(**asdict(user_object))
Here, I'm trying to create a new User object from the user_object
. It shows the following error.
TypeError: User.__init__() got an unexpected keyword argument '_id'
I guessed that the parent class is not initiated at first. So I tried to initiate it with the super()
function and according to attrs documentation it should be done with __attrs_pre_init__
. From the documentation:
The sole reason for the existence of
__attrs_pre_init__
is to give users the chance to callsuper().__init__()
, because some subclassing-based APIs require that.
So the modified child class becomes like this.
@define
class User(CollectionModel):
username : str = field(factory = str, validator=validators.instance_of(str))
userType : str = field(factory = str, validator=validators.instance_of(str))
password : str = field(factory = str, validator=validators.instance_of(str))
def __attrs_pre_init__(self):
super().__init__()
But the problem still remains. Am I doing the OOP in the wrong way? Or is it just a bug of attrs module?