Skip to content
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

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

Merged
merged 4 commits into from
Jan 17, 2025

Conversation

advait-dixit
Copy link
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.

Copy link
Collaborator

@JukkaL JukkaL left a comment

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.

@@ -381,17 +381,30 @@ CPyDataclass_SleightOfHand(PyObject *dataclass_dec, PyObject *tp,
if (!res) {
goto fail;
}
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
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
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
Copy link
Collaborator

@JukkaL JukkaL left a comment

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
12 checks passed
@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