Skip to content
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

monkeypatch.setitem type hints do not support TypedDict #10999

Closed
adamjstewart opened this issue May 13, 2023 · 0 comments · Fixed by #11000
Closed

monkeypatch.setitem type hints do not support TypedDict #10999

adamjstewart opened this issue May 13, 2023 · 0 comments · Fixed by #11000

Comments

@adamjstewart
Copy link
Contributor

Python 3.8 introduced the typing.TypedDict type to properly annotate dicts with multiple types. It appears that this type is not compatible with the type annotations for monkeypatch.setitem.

The following code runs fine with pytest:

from typing import TypedDict

from _pytest.monkeypatch import MonkeyPatch

Foo = TypedDict('Foo', {'x': int, 'y': float})

a: Foo = {'x': 1, 'y': 3.14}
b = {'x': 1, 'y': 3.14}

def test(monkeypatch: MonkeyPatch) -> None:
    monkeypatch.setitem(a, 'x', 2)
    monkeypatch.setitem(b, 'x', 2)

However, it fails with mypy:

$ mypy test.py
test.py:11: error: Argument 1 to "setitem" of "MonkeyPatch" has incompatible type "Foo"; expected "MutableMapping[str, int]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

Versions

  • Python 3.10.10
  • pytest 7.2.1
  • macOS 13.3.1 (a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant