Use more precise context for TypedDict plugin errors by brianschubert · Pull Request #18293 · python/mypy (original) (raw)
Fixes #12271
Uses an applicable argument expression as the error context instead of the overall CallExpr.
Given:
flags: --pretty --show-column-number
from typing import TypedDict
class A(TypedDict): x: int
a: A x.setdefault("y", 123) x.setdefault("x", "bad")
Non-TypedDict case for reference
b: dict[str, int] b.setdefault("x", "bad")
Before:
main.py:8:1: error: TypedDict "A" has no key "y" [typeddict-item]
a.setdefault("y", 123)
^~~~~~~~~~~~~~~~~~~~~~
main.py:9:1: error: Argument 2 to "setdefault" of "TypedDict" has incompatible type "str"; expected "int" [typeddict-item]
a.setdefault("x", "bad")
^~~~~~~~~~~~~~~~~~~~~~~~
main.py:13:19: error: Argument 2 to "setdefault" of "MutableMapping" has incompatible type "str"; expected "int" [arg-type]
b.setdefault("x", "bad")
^~~~~
Found 3 errors in 1 file (checked 1 source file)
After:
main.py:8:14: error: TypedDict "A" has no key "y" [typeddict-item]
a.setdefault("y", 123)
^~~
main.py:9:19: error: Argument 2 to "setdefault" of "TypedDict" has incompatible type "str"; expected "int" [typeddict-item]
a.setdefault("x", "bad")
^~~~~
main.py:13:19: error: Argument 2 to "setdefault" of "MutableMapping" has incompatible type "str"; expected "int" [arg-type]
b.setdefault("x", "bad")
^~~~~
Found 3 errors in 1 file (checked 1 source file)