Pyright: Invalid type definition on WeakValueDictionary

Created on 25 Dec 2019  路  3Comments  路  Source: microsoft/pyright

Describe the bug
Type definitions of methods of weakref.WeakValueDictionary seems wrong.

To Reproduce
Save the code in Screenshots or Code section to test.py then

$ pyright test.py
Searching for source files
Found 1 source file
/Users/alisue/.ghq/github.com/lambdalisue/typeshed/test.py
  21:17 - error: Cannot access member 'say' for type 'int'
  Member 'say' is unknown
1 error, 0 warnings
Completed in 1.231sec

Note that mypy works correctly for same file.

$ mypy test.py
Success: no issue found in 1 source file

Expected behavior
v should be Optional[Object] instead of Optional[int]

Screenshots or Code
If applicable, add screenshots or the text of the code (surrounded by triple back ticks) to help explain your problem.

from __future__ import annotations

import weakref


class Object:
    def say(self) -> str:
        return "Hello"


a = Object()
b = Object()

d: weakref.WeakValueDictionary[int, Object] = weakref.WeakValueDictionary()
d[id(a)] = a
d[id(b)] = b

for r in d.valuerefs():
    v = r()
    if v is not None:
        print(v.say())

VS Code extension or command-line
Command line

Additional context
It seems typeshed definition cause this issue. I think the deifinition should be List[ref[_VT]]. However, while mypy does not argue this, I was not sure if it is an issue of typeshed or pyright. That's why I made this issue report.

addressed in next version bug

All 3 comments

Thanks for the bug report. I'm able to repro the problem, and I understand the cause. A fix will be forthcoming.

This will be fixed in the next release.

This is now fixed in 1.1.15, which I just published.

Was this page helpful?
0 / 5 - 0 ratings