Skip to content

Defaults are passed to __attrs_pre_init__ instead of the init values themselves #1427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
redruin1 opened this issue Apr 18, 2025 · 0 comments · May be fixed by #1428
Open

Defaults are passed to __attrs_pre_init__ instead of the init values themselves #1427

redruin1 opened this issue Apr 18, 2025 · 0 comments · May be fixed by #1428

Comments

@redruin1
Copy link

The documentation says:

If __attrs_pre_init__ takes more than the self 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

redruin1 added a commit to redruin1/attrs that referenced this issue Apr 18, 2025
Values given to `__init__` should now always be propogated to `__attrs_pre_init__`, regardless of default configuration.
@redruin1 redruin1 linked a pull request Apr 18, 2025 that will close this issue
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant