microsoft / pyright

Static Type Checker for Python
Other
12.7k stars 1.35k forks source link

False positive on magic methods in `MagicMock` #8306

Closed PerchunPak closed 1 week ago

PerchunPak commented 1 week ago

Describe the bug This is probably an extreme edge-case, but if I redefine return_value at MagicMock().__str__, pyright says Cannot assign to attribute "return_value" for class "function", even though it is valid code in runtime.

Code or Screenshots image

Code sample in pyright playground

VS Code extension or command-line Are you running pyright as a VS Code extension, a language server in another editor, integrated into Pylance, or the command-line tool? Which version?

command-line, 1.1.370

erictraut commented 1 week ago

Pyright is working correctly here, and this isn't a false positive.

The job of a type checker is to detect and report type violations, and your code has a type violation.