[mypyc] Support deleting attributes in __setattr__ wrapper#19997
Merged
JukkaL merged 4 commits intopython:masterfrom Oct 6, 2025
Merged
[mypyc] Support deleting attributes in __setattr__ wrapper#19997JukkaL merged 4 commits intopython:masterfrom
JukkaL merged 4 commits intopython:masterfrom
Conversation
JukkaL
reviewed
Oct 6, 2025
|
|
||
| # Doesn't work because there's no __delattr__. | ||
| with assertRaises(AttributeError): | ||
| del i.four |
Collaborator
There was a problem hiding this comment.
Maybe mypy could catch this? If four is not a declared attribute and there is no __delattr__, this is expected to fail. If you agree, can you create a mypy issue about this (unless one already exists)?
Collaborator
Author
There was a problem hiding this comment.
I don't think mypy can catch this because this is a quirk of native classes. For interpreted classes that define __setattr__ deleting an attribute works without __delattr__ because it just removes it from __dict__. It's only native classes that might need an explicit __delattr__. So I think the error would be specific to mypyc.
JukkaL
approved these changes
Oct 6, 2025
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.
The
__setattr__wrapper that mypyc generates needs to handle deleting attributes as well becausedelstatements go through the sametp_setattropointer but with the value argument set toNULL.The wrapper calls
__delattr__in this case if it's overridden in the native class (or its parent). Handling of dynamic attributes is different without__dict__which makes a custom__delattr__required if the dynamic attributes are stored in a custom dictionary.If
__delattr__is not overridden it calls the implementation ofobject.__delattr__which results inAttributeErrorbecause there's no__dict__.If it's defined without
__setattr__, mypyc reports an error. It's possible to support just__delattr__but since it shares a slot with__setattr__, the wrapper generation would be more complicated. It seems like an unlikely use case to only need__delattr__so I think it makes sense to leave it for later.