Wrong type warning when working with dictionary

Answered

Consider this very simple python example:

def b():
"""
:rtype: dict[str, int]
"""
return {"b": 1}

a = b()

a["test"] = "test"

 

 

The last line gives me this error

Expected type 'int' (matched generic type '_VT'), got 'str' instead

which is not correct in my opinion. Is there any workaround for this. I don't like to specify ` :rtype: dict[str, int | str]` in function because it is incorrect. 

 

I use

PyCharm 2018.3.3 (Professional Edition)
Build #PY-183.5153.39, built on January 9, 2019
JRE: 1.8.0_152-release-1343-b26 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Linux 4.15.0-43-generic

 

3 comments
Comment actions Permalink

Hi,

It seems to be known issue https://youtrack.jetbrains.com/issue/PY-33201, please vote for it and follow for updates.

0
Comment actions Permalink

I get the same error on a very similar piece of code, but I'm not sure the YouTrack issue reflects this as I'm not using TypeVars

Line 3: Expected type 'None' (matched generic type '_VT'), got 'int' instead)

foo = {}
foo['a'] = {'b': None}
foo['a']['b'] = 1

Replacing None by a number resolves the issue.

 

PyCharm 2019.1.1 (Professional Edition)
Build #PY-191.6605.12, built on April 3, 2019
JRE: 11.0.2+9-b159.34 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0

0
Comment actions Permalink

@Hfm5

Seem worthy of a new ticket: https://youtrack.jetbrains.com/issue/PY-35618

Also see the related ticket there. There's quite a few of similar issues.

0

Please sign in to leave a comment.