Open
Description
The documentation says:
If
__attrs_pre_init__
takes more than theself
argument, the attrs-generated__init__
will call it with the same arguments it received itself.
However, if those attributes are specified with default values (and not specified as keyword only), then their default value is passed into __attrs_pre_init__
even when a user has given them init values:
@attrs.define
class Example:
val1: int
val2: int = 100
val3: dict = attrs.field(factory=dict)
val4: int = attrs.field(kw_only=True)
val5: int = attrs.field(default=100, kw_only=True)
def __attrs_pre_init__(self, val1, val2, val3, val4, val5):
print("val1", val1)
print("val2", val2)
print("val3", val3)
print("val4", val4)
print("val5", val5)
e = Example(val1=200, val2=200, val3={"blah": "blah"}, val4=200, val5=200)
val1 200
val2 100 # <-- Incorrect
val3 _Nothing.NOTHING # <-- Incorrect
val4 200
val5 200
If we inspect the generated init method:
import inspect
print(inspect.getsource(Example.__init__))
We get:
def __init__(self, val1, val2=attr_dict['val2'].default, val3=NOTHING, *, val4, val5=attr_dict['val5'].default):
self.__attrs_pre_init__(val1, val2=attr_dict['val2'].default, val3=NOTHING, val4=val4, val5=val5)
...
When I would expect either:
def __init__(self, val1, val2=attr_dict['val2'].default, val3=NOTHING, *, val4, val5=attr_dict['val5'].default):
self.__attrs_pre_init__(val1, val2, val3, val4=val4, val5=val5)
# Or:
self.__attrs_pre_init__(val1, val2=val2, val3=val3, val4=val4, val5=val5)
...
Assuming this is a bug, this is probably a simple fix.
Attrs version: 25.3.0
Metadata
Metadata
Assignees
Labels
No labels