Delete attribute ``name`` from ``target``. If no ``name`` is specified and ``target`` is a string it will be interpreted as a dotted import path with the last part being the attribute name. Raises AttributeError it the attribute does not exist, unless ``rais
(
self,
target: object | str,
name: str | NotSetType = NOTSET,
raising: bool = True,
)
| 248 | self._setattr.append((target, name, oldval)) |
| 249 | |
| 250 | def delattr( |
| 251 | self, |
| 252 | target: object | str, |
| 253 | name: str | NotSetType = NOTSET, |
| 254 | raising: bool = True, |
| 255 | ) -> None: |
| 256 | """Delete attribute ``name`` from ``target``. |
| 257 | |
| 258 | If no ``name`` is specified and ``target`` is a string |
| 259 | it will be interpreted as a dotted import path with the |
| 260 | last part being the attribute name. |
| 261 | |
| 262 | Raises AttributeError it the attribute does not exist, unless |
| 263 | ``raising`` is set to False. |
| 264 | """ |
| 265 | __tracebackhide__ = True |
| 266 | import inspect |
| 267 | |
| 268 | if name is NOTSET: |
| 269 | if not isinstance(target, str): |
| 270 | raise TypeError( |
| 271 | "use delattr(target, name) or " |
| 272 | "delattr(target) with target being a dotted " |
| 273 | "import string" |
| 274 | ) |
| 275 | name, target = derive_importpath(target, raising) |
| 276 | |
| 277 | if not hasattr(target, name): |
| 278 | if raising: |
| 279 | raise AttributeError(name) |
| 280 | else: |
| 281 | oldval = getattr(target, name, NOTSET) |
| 282 | # Avoid class descriptors like staticmethod/classmethod. |
| 283 | if inspect.isclass(target): |
| 284 | oldval = target.__dict__.get(name, NOTSET) |
| 285 | self._setattr.append((target, name, oldval)) |
| 286 | delattr(target, name) |
| 287 | |
| 288 | def setitem(self, dic: Mapping[K, V], name: K, value: V) -> None: |
| 289 | """Set dictionary entry ``name`` to value.""" |