I have a simple code snippet where I set dictionary values as empty lists:
new_dict = {}
for i in range(1, 13):
new_dict[i] = []
Now, if inside the loop on the next line I would type new_dict[i]
and add a dot, I expect PyCharm to show me a list of methods available for a list
, but PyCharm fails to recognize the dictionary value type in this simple case:
Why is it happening and what can I do? Using PyCharm 2016.1.2, Python 2.7.10.
As a workaround, I can explicitly add a type hint, letting PyCharm know that new_dict
is a dictionary where keys are integers and values are lists by adding a # type: dict[int, list]
inline comment:
Lets consider a slightly more complex scenario with a variable:
In this case the last line will give us all the methods of
int
s andstr
s because PyCharm is able to detect thatx
will either be anint
or astr
Now replace all the occurences of
x
withmy_dict[i]
:All of the same rules apply as above, we know (and PyCharm would be able to figure out) that
my_dict[i]
is either going to be anint
or astr
.However what would you expect to happen if you were not initializing the
dict
?In this case there is no way to know what the values of the dict are other then adding an explicit annotation just like you would in your example case:
This really reinforces some lines from The Zen of Python:
(I include readability because the explicit type hint is a lot easier to read then a potentially buried assignment)
I think you are expecting PyCharm to be able to infer too much from the code.
Providing the type hint annotation gives PyCharm the information it needs to show the code completion options. In this case you are explicitly telling PyCharm what type the value of each dictionary element is going to be.
Without the type hint annotation you are expecting PyCharm to be able to know what the dictionary's element value is going to be when you are assigning it inside of a loop. That is way too much assumption for PyCharm to make. As Tadhg McDonald-Jensen mentioned in the comments, this is just too dynamic of a situation for PyCharm to figure it out reliably. The code completion would be wrong too often.