[mypyc] Fixing __init__ for classes with @attr.s(slots=True).#18447
Merged
JukkaL merged 4 commits intopython:masterfrom Jan 17, 2025
Merged
[mypyc] Fixing __init__ for classes with @attr.s(slots=True).#18447JukkaL merged 4 commits intopython:masterfrom
JukkaL merged 4 commits intopython:masterfrom
Conversation
JukkaL
reviewed
Jan 14, 2025
Collaborator
JukkaL
left a comment
There was a problem hiding this comment.
Thanks for the PR! I have one suggestion.
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__", ""}; |
Collaborator
There was a problem hiding this comment.
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.
Contributor
Author
There was a problem hiding this comment.
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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes mypyc/mypyc#1079.
@attr.sgenerates a__init__function which was getting lost inCPyDataclass_SleightOfHand. This change copies the generated__init__function and a couple of others ones to maintain consistency with CPython.