7
7
from typing import Any
8
8
from typing import Generator
9
9
from typing import List
10
+ from typing import Mapping
10
11
from typing import MutableMapping
11
12
from typing import Optional
12
13
from typing import overload
@@ -129,7 +130,7 @@ class MonkeyPatch:
129
130
130
131
def __init__ (self ) -> None :
131
132
self ._setattr : List [Tuple [object , str , object ]] = []
132
- self ._setitem : List [Tuple [MutableMapping [Any , Any ], object , object ]] = []
133
+ self ._setitem : List [Tuple [Mapping [Any , Any ], object , object ]] = []
133
134
self ._cwd : Optional [str ] = None
134
135
self ._savesyspath : Optional [List [str ]] = None
135
136
@@ -290,12 +291,13 @@ def delattr(
290
291
self ._setattr .append ((target , name , oldval ))
291
292
delattr (target , name )
292
293
293
- def setitem (self , dic : MutableMapping [K , V ], name : K , value : V ) -> None :
294
+ def setitem (self , dic : Mapping [K , V ], name : K , value : V ) -> None :
294
295
"""Set dictionary entry ``name`` to value."""
295
296
self ._setitem .append ((dic , name , dic .get (name , notset )))
296
- dic [name ] = value
297
+ # Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict
298
+ dic [name ] = value # type: ignore[index]
297
299
298
- def delitem (self , dic : MutableMapping [K , V ], name : K , raising : bool = True ) -> None :
300
+ def delitem (self , dic : Mapping [K , V ], name : K , raising : bool = True ) -> None :
299
301
"""Delete ``name`` from dict.
300
302
301
303
Raises ``KeyError`` if it doesn't exist, unless ``raising`` is set to
@@ -306,7 +308,8 @@ def delitem(self, dic: MutableMapping[K, V], name: K, raising: bool = True) -> N
306
308
raise KeyError (name )
307
309
else :
308
310
self ._setitem .append ((dic , name , dic .get (name , notset )))
309
- del dic [name ]
311
+ # Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict
312
+ del dic [name ] # type: ignore[attr-defined]
310
313
311
314
def setenv (self , name : str , value : str , prepend : Optional [str ] = None ) -> None :
312
315
"""Set environment variable ``name`` to ``value``.
@@ -401,11 +404,13 @@ def undo(self) -> None:
401
404
for dictionary , key , value in reversed (self ._setitem ):
402
405
if value is notset :
403
406
try :
404
- del dictionary [key ]
407
+ # Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict
408
+ del dictionary [key ] # type: ignore[attr-defined]
405
409
except KeyError :
406
410
pass # Was already deleted, so we have the desired state.
407
411
else :
408
- dictionary [key ] = value
412
+ # Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict
413
+ dictionary [key ] = value # type: ignore[index]
409
414
self ._setitem [:] = []
410
415
if self ._savesyspath is not None :
411
416
sys .path [:] = self ._savesyspath
0 commit comments