Skip to content

monkeypatch: add support for TypedDict #11000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Abdeali JK
Abdelrahman Elbehery
Abhijeet Kasurde
Adam Johnson
Adam Stewart
Adam Uhlir
Ahn Ki-Wook
Akiomi Kamakura
Expand Down
1 change: 1 addition & 0 deletions changelog/10999.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed bug where monkeypatch setitem/delitem type annotations didn't support typing.TypedDict.
6 changes: 3 additions & 3 deletions src/_pytest/monkeypatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class MonkeyPatch:

def __init__(self) -> None:
self._setattr: List[Tuple[object, str, object]] = []
self._setitem: List[Tuple[MutableMapping[Any, Any], object, object]] = []
self._setitem: List[Tuple[Union[MutableMapping[Any, Any], "TypedDict[Any, Any]", object, object]] = []
self._cwd: Optional[str] = None
self._savesyspath: Optional[List[str]] = None

Expand Down Expand Up @@ -290,12 +290,12 @@ def delattr(
self._setattr.append((target, name, oldval))
delattr(target, name)

def setitem(self, dic: MutableMapping[K, V], name: K, value: V) -> None:
def setitem(self, dic: Union[MutableMapping[K, V], "TypedDict[K, V]"], name: K, value: V) -> None:
"""Set dictionary entry ``name`` to value."""
self._setitem.append((dic, name, dic.get(name, notset)))
dic[name] = value

def delitem(self, dic: MutableMapping[K, V], name: K, raising: bool = True) -> None:
def delitem(self, dic: Union[MutableMapping[K, V], "TypedDict[K, V"], name: K, raising: bool = True) -> None:
"""Delete ``name`` from dict.

Raises ``KeyError`` if it doesn't exist, unless ``raising`` is set to
Expand Down