You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[mypyc] Support deleting attributes in __setattr__ wrapper (#19997)
The `__setattr__` wrapper that mypyc generates needs to handle deleting
attributes as well because `del` statements go through the same
`tp_setattro` pointer but with the value argument set to `NULL`.
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 of
`object.__delattr__` which results in `AttributeError` 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.
Copy file name to clipboardExpand all lines: mypyc/test-data/irbuild-classes.test
+156-5Lines changed: 156 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -2222,18 +2222,41 @@ class AllowsInterpreted:
2222
2222
def __setattr__(self, attr: str, val: object) -> None: # E: "__setattr__" not supported in class "AllowsInterpreted" because it allows interpreted subclasses
2223
2223
pass
2224
2224
2225
+
def __delattr__(self, attr: str) -> None:
2226
+
pass
2227
+
2225
2228
class InheritsInterpreted(dict):
2226
2229
def __setattr__(self, attr: str, val: object) -> None: # E: "__setattr__" not supported in class "InheritsInterpreted" because it inherits from a non-native class
def __setattr__(self, attr: str, val: object) -> None: # E: "__setattr__" not supported in class "InheritsNonNative" because it inherits from a non-native class
def __delattr__(self, attr: str) -> None: # E: "__delattr__" supported only in classes that also override "__setattr__", or inherit from a native class that overrides it.
0 commit comments