You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First of all thanks a lot for the awesome library! I just recently discovered it and it saved me so many headaches. Recently I was trying to do something of this sort, where either x or y has to be given to the __init__ method for this to be valid:
This however (as the docs state) fails because self.y would not be initialized during the call to y_plus_one. Even though the docs state this fails I wanted to try and fix the issue and managed to do so in a very hacky way. However in the process I thought up a non invasive way in which this may work.
If we first initialize those fields that were given a value through __init__ and then use the NOTHING sentinel to initialize the values for those attributes that were not initialized through __init__ but before calling the factories, the problem dissapears. The reordered __init__ method would look something like this (I'm not really sure how the method is autogenerated but its a rough idea):
@defineclassMyClass:
...
def__init__(self, x=NOTHING, y=NOTHING): # <- this is autogenerated# initialize all fields before calling factoriesifxisnotNOTHING:
self.x=xelifisinstance(self.__attrs_attrs__['x'].default, Factory):
self.x=NOTHINGelifself.__attrs_attrs__['x'].defaultisnotNone:
self.x=self.__attrs_attrs__['x'].defaultifyisnotNOTHING:
self.y=yelifisinstance(self.__attrs_attrs__['y'].default, Factory):
self.y=NOTHINGelifself.__attrs_attrs__['y'].defaultisnotNone:
self.y=self.__attrs_attrs__['y'].default# call the factories for all fields that were not given a value through initifself.xisNOTHINGandisinstance(self.__attrs_attrs__['x'].default, Factory):
self.x=self.__attrs_attrs__['x'].default.factory(self)
ifself.xisNOTHING:
raiseValueError("a factory may not return the NOTHING sentinel")
ifself.yisNOTHINGandisinstance(self.__attrs_attrs__['y'].default, Factory):
self.y=self.__attrs_attrs__['y'].default.factory(self)
ifself.yisNOTHING:
raiseValueError("a factory may not return the NOTHING sentinel")
...
This has the additional benefit that now I can throw more descriptive error messages during initialization, like:
...
@x.defaultdefy_plus_one(self):
ifself.yisNOTHING:
raiseValueError("either x or y has to be initialized")
returnself.y+1
...
The text was updated successfully, but these errors were encountered:
First of all thanks a lot for the awesome library! I just recently discovered it and it saved me so many headaches. Recently I was trying to do something of this sort, where either x or y has to be given to the
__init__
method for this to be valid:This however (as the docs state) fails because self.y would not be initialized during the call to y_plus_one. Even though the docs state this fails I wanted to try and fix the issue and managed to do so in a very hacky way. However in the process I thought up a non invasive way in which this may work.
If we first initialize those fields that were given a value through
__init__
and then use theNOTHING
sentinel to initialize the values for those attributes that were not initialized through__init__
but before calling the factories, the problem dissapears. The reordered__init__
method would look something like this (I'm not really sure how the method is autogenerated but its a rough idea):This has the additional benefit that now I can throw more descriptive error messages during initialization, like:
The text was updated successfully, but these errors were encountered: