Skip to content

[mypyc] Fixing __init__ for classes with @attr.s(slots=True). - #18447

Merged
JukkaL merged 4 commits into
python:masterfrom
advait-dixit:fix_attr_slots
Jan 17, 2025
Merged

[mypyc] Fixing __init__ for classes with @attr.s(slots=True).#18447
JukkaL merged 4 commits into
python:masterfrom
advait-dixit:fix_attr_slots

Conversation

@advait-dixit

Copy link
Copy Markdown
Contributor

Fixes mypyc/mypyc#1079.

@attr.s generates a __init__ function which was getting lost in CPyDataclass_SleightOfHand. This change copies the generated __init__ function and a couple of others ones to maintain consistency with CPython.

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! I have one suggestion.

Comment thread mypyc/lib-rt/misc_ops.c Outdated
}
Py_DECREF(res);
// These attributes are added or modified by @attr.s(slots=True).
const char * const keys[] = {"__attrs_attrs__", "__attrs_own_setattr__", "__init__", ""};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about doing this only when using @attr.s, but not for a regular dataclass? E.g. pass a flag to the function indicating whether we are dealing with an attrs class.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If my understanding is correct, CPython actually uses res but CPyDataclass_SleightOfHand(...) is throwing it away. So, copying attributes will probably reduce errors. Anyway, I have added a condition to copy attributes only for attr and attr-auto dataclasses. Please take a look.

@advait-dixit advait-dixit changed the title Fixing __init__ for classes with @attr.s(slots=True). [mypyc] Fixing __init__ for classes with @attr.s(slots=True). Jan 16, 2025

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update.

@JukkaL
JukkaL merged commit c61bce4 into python:master Jan 17, 2025
@advait-dixit
advait-dixit deleted the fix_attr_slots branch January 21, 2025 22:01
x612skm pushed a commit to x612skm/mypy-dev that referenced this pull request Feb 24, 2025
…#18447)

Fixes mypyc/mypyc#1079.

`@attr.s` generates a `__init__` function which was getting lost in
`CPyDataclass_SleightOfHand`. This change copies the generated
`__init__` function and a couple of others ones to maintain consistency
with CPython.
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 this pull request may close these issues.

AttributeError with attr.s(slots=True)

2 participants