MCPcopy Create free account
hub / github.com/pytest-dev/pytest / delattr

Method delattr

src/_pytest/monkeypatch.py:226–262  ·  view source on GitHub ↗

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: Union[object, str],
        name: Union[str, Notset] = notset,
        raising: bool = True,
    )

Source from the content-addressed store, hash-verified

224 setattr(target, name, value)
225
226 def delattr(
227 self,
228 target: Union[object, str],
229 name: Union[str, Notset] = notset,
230 raising: bool = True,
231 ) -> None:
232 """Delete attribute ``name`` from ``target``.
233
234 If no ``name`` is specified and ``target`` is a string
235 it will be interpreted as a dotted import path with the
236 last part being the attribute name.
237
238 Raises AttributeError it the attribute does not exist, unless
239 ``raising`` is set to False.
240 """
241 __tracebackhide__ = True
242 import inspect
243
244 if isinstance(name, Notset):
245 if not isinstance(target, str):
246 raise TypeError(
247 "use delattr(target, name) or "
248 "delattr(target) with target being a dotted "
249 "import string"
250 )
251 name, target = derive_importpath(target, raising)
252
253 if not hasattr(target, name):
254 if raising:
255 raise AttributeError(name)
256 else:
257 oldval = getattr(target, name, notset)
258 # Avoid class descriptors like staticmethod/classmethod.
259 if inspect.isclass(target):
260 oldval = target.__dict__.get(name, notset)
261 self._setattr.append((target, name, oldval))
262 delattr(target, name)
263
264 def setitem(self, dic: MutableMapping[K, V], name: K, value: V) -> None:
265 """Set dictionary entry ``name`` to value."""

Calls 3

derive_importpathFunction · 0.85
appendMethod · 0.80
getMethod · 0.45