[mypyc] Support deleting attributes in __setattr__ wrapper #19997
+333
−8
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 becausedel
statements go through the sametp_setattro
pointer 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 inAttributeError
because 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.