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
I know, I know you...recommend against subclassing...but I was doing it in the hopes of reducing boilerplate. And I was really caught off guard by what happened when I mixed type annotations (which I now see the docs say you shouldn't do).
Specifically, I couldn't figure out why my child classes were using parent class default attributes. It took me a while to track it down: when I thought I was redefining the default value for an attribute for a child class, it was being ignored since I also had a field()-defined attribute without a type annotation.
MRE code
#%%fromattrsimportdefine, field# %%@defineclassParent:
x: int=0y: int=0@defineclassChildNoAnnotNoField(Parent):
x: int=1y=1@defineclassChildMixedAnnot(Parent):
# this x Attribute definition is ignored because# there is a field without a type annotationx: int=1y=field(default=1)
@defineclassChildAnnotField(Parent):
x: int=1y: int=field(default=1)
print(Parent()) # x=0, y=0print(ChildNoAnnotNoField()) # x=1, y=0print(ChildMixedAnnot()) # x=0, y=1print(ChildAnnotField()) # x=1, y=1
What I suggest is potentially raising a warning for cases like this to spare users from headaches. I had even read most of the docs in some detail, if not super closely, and this threw me for a loop.
Honestly, it wasn't until running into this that I realized you needed type annotations to define an Attribute either. The only place I see is on the Overview page: "Simply assign attrs.field() to the attributes instead of annotating them with types" which could be easily missed. So I'd also suggest making it a little more obvious (maybe on the 'By example' page?) that attributes are defined either by type annotations or by field.
The text was updated successfully, but these errors were encountered:
I know, I know you...recommend against subclassing...but I was doing it in the hopes of reducing boilerplate. And I was really caught off guard by what happened when I mixed type annotations (which I now see the docs say you shouldn't do).
Specifically, I couldn't figure out why my child classes were using parent class default attributes. It took me a while to track it down: when I thought I was redefining the default value for an attribute for a child class, it was being ignored since I also had a
field()
-defined attribute without a type annotation.MRE code
What I suggest is potentially raising a warning for cases like this to spare users from headaches. I had even read most of the docs in some detail, if not super closely, and this threw me for a loop.
Honestly, it wasn't until running into this that I realized you needed type annotations to define an
Attribute
either. The only place I see is on the Overview page: "Simply assign attrs.field() to the attributes instead of annotating them with types" which could be easily missed. So I'd also suggest making it a little more obvious (maybe on the 'By example' page?) that attributes are defined either by type annotations or byfield
.The text was updated successfully, but these errors were encountered: