Skip to content

Commit

Permalink
Fix crash when __pre_init__, kw_only, and defaults come together
Browse files Browse the repository at this point in the history
Fixes #1284
  • Loading branch information
hynek committed Aug 2, 2024
1 parent f7e4e91 commit 0094089
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/attr/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -2219,15 +2219,17 @@ def _attrs_to_init_script(
# leading comma & kw_only args
args += f"{', ' if args else ''}*, {', '.join(kw_only_args)}"
pre_init_kw_only_args = ", ".join(
[f"{kw_arg}={kw_arg}" for kw_arg in kw_only_args]
[
f"{kw_arg_name}={kw_arg_name}"
# We need to remove the defaults from the kw_only_args.
for kw_arg_name in (kwa.split("=")[0] for kwa in kw_only_args)
]
)
pre_init_args += (
", " if pre_init_args else ""
) # handle only kwargs and no regular args
pre_init_args += ", " if pre_init_args else ""
pre_init_args += pre_init_kw_only_args

if call_pre_init and pre_init_has_args:
# If pre init method has arguments, pass same arguments as `__init__`
# If pre init method has arguments, pass same arguments as `__init__`.
lines[0] = f"self.__attrs_pre_init__({pre_init_args})"

# Python 3.7 doesn't allow backslashes in f strings.
Expand Down
13 changes: 13 additions & 0 deletions tests/test_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,19 @@ def __attrs_pre_init__(self2, y):

assert 12 == getattr(c, "z", None)

@pytest.mark.usefixtures("with_and_without_validation")
def test_pre_init_kw_only_work_with_defaults(self):
"""
Default values together with kw_only don't break __attrs__pre_init__.
"""

@attr.define
class KWOnlyAndDefault:
kw_and_default: int = attr.field(kw_only=True, default=3)

def __attrs_pre_init__(self, _):
pass

@pytest.mark.usefixtures("with_and_without_validation")
def test_post_init(self):
"""
Expand Down

0 comments on commit 0094089

Please sign in to comment.