1717from typing import TypeVar
1818import warnings
1919
20+ from _pytest .compat import NOTSET
21+ from _pytest .compat import NotSetType
2022from _pytest .deprecated import MONKEYPATCH_LEGACY_NAMESPACE_PACKAGES
2123from _pytest .fixtures import fixture
2224from _pytest .warning_types import PytestWarning
@@ -107,14 +109,6 @@ def derive_importpath(import_path: str, raising: bool) -> tuple[str, object]:
107109 return attr , target
108110
109111
110- class Notset :
111- def __repr__ (self ) -> str :
112- return "<notset>"
113-
114-
115- notset = Notset ()
116-
117-
118112@final
119113class MonkeyPatch :
120114 """Helper to conveniently monkeypatch attributes/items/environment
@@ -167,7 +161,7 @@ def setattr(
167161 self ,
168162 target : str ,
169163 name : object ,
170- value : Notset = ...,
164+ value : NotSetType = ...,
171165 raising : bool = ...,
172166 ) -> None : ...
173167
@@ -184,7 +178,7 @@ def setattr(
184178 self ,
185179 target : str | object ,
186180 name : object | str ,
187- value : object = notset ,
181+ value : object = NOTSET ,
188182 raising : bool = True ,
189183 ) -> None :
190184 """
@@ -225,7 +219,7 @@ def setattr(
225219 __tracebackhide__ = True
226220 import inspect
227221
228- if isinstance ( value , Notset ) :
222+ if value is NOTSET :
229223 if not isinstance (target , str ):
230224 raise TypeError (
231225 "use setattr(target, name, value) or "
@@ -242,20 +236,20 @@ def setattr(
242236 "import string"
243237 )
244238
245- oldval = getattr (target , name , notset )
246- if raising and oldval is notset :
239+ oldval = getattr (target , name , NOTSET )
240+ if raising and oldval is NOTSET :
247241 raise AttributeError (f"{ target !r} has no attribute { name !r} " )
248242
249243 # avoid class descriptors like staticmethod/classmethod
250244 if inspect .isclass (target ):
251- oldval = target .__dict__ .get (name , notset )
245+ oldval = target .__dict__ .get (name , NOTSET )
252246 self ._setattr .append ((target , name , oldval ))
253247 setattr (target , name , value )
254248
255249 def delattr (
256250 self ,
257251 target : object | str ,
258- name : str | Notset = notset ,
252+ name : str | NotSetType = NOTSET ,
259253 raising : bool = True ,
260254 ) -> None :
261255 """Delete attribute ``name`` from ``target``.
@@ -270,7 +264,7 @@ def delattr(
270264 __tracebackhide__ = True
271265 import inspect
272266
273- if isinstance ( name , Notset ) :
267+ if name is NOTSET :
274268 if not isinstance (target , str ):
275269 raise TypeError (
276270 "use delattr(target, name) or "
@@ -283,16 +277,16 @@ def delattr(
283277 if raising :
284278 raise AttributeError (name )
285279 else :
286- oldval = getattr (target , name , notset )
280+ oldval = getattr (target , name , NOTSET )
287281 # Avoid class descriptors like staticmethod/classmethod.
288282 if inspect .isclass (target ):
289- oldval = target .__dict__ .get (name , notset )
283+ oldval = target .__dict__ .get (name , NOTSET )
290284 self ._setattr .append ((target , name , oldval ))
291285 delattr (target , name )
292286
293287 def setitem (self , dic : Mapping [K , V ], name : K , value : V ) -> None :
294288 """Set dictionary entry ``name`` to value."""
295- self ._setitem .append ((dic , name , dic .get (name , notset )))
289+ self ._setitem .append ((dic , name , dic .get (name , NOTSET )))
296290 # Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict
297291 dic [name ] = value # type: ignore[index]
298292
@@ -306,7 +300,7 @@ def delitem(self, dic: Mapping[K, V], name: K, raising: bool = True) -> None:
306300 if raising :
307301 raise KeyError (name )
308302 else :
309- self ._setitem .append ((dic , name , dic .get (name , notset )))
303+ self ._setitem .append ((dic , name , dic .get (name , NOTSET )))
310304 # Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict
311305 del dic [name ] # type: ignore[attr-defined]
312306
@@ -410,13 +404,13 @@ def undo(self) -> None:
410404 Prefer to use :meth:`context() <pytest.MonkeyPatch.context>` instead.
411405 """
412406 for obj , name , value in reversed (self ._setattr ):
413- if value is not notset :
407+ if value is not NOTSET :
414408 setattr (obj , name , value )
415409 else :
416410 delattr (obj , name )
417411 self ._setattr [:] = []
418412 for dictionary , key , value in reversed (self ._setitem ):
419- if value is notset :
413+ if value is NOTSET :
420414 try :
421415 # Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict
422416 del dictionary [key ] # type: ignore[attr-defined]
0 commit comments