Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this warning "Expected type 'int' (matched generic type '_T'), got 'Dict[str, None]' instead"?

Tags:

python

pycharm

Please watch carefully the question and carefully the answers of this and you'll see it's not a duplicate, especially because they dont answer my question.

Try to make a new empty project, and add this code. It works fine without warnings:

game_data = {'boats': [], }
game_data['boats'].append({'name': None})

Now change it to:

game_data = {'boats': [], 'width': None, 'height': None, }
game_data['boats'].append({'name': None})

Still no warnings. And change again to:

w = 12
game_data = {'boats': [], 'width': None, 'height': w, }
game_data['boats'].append({'name': None})

And now you'll get:

Expected type 'int' (matched generic type '_T'), got 'Dict[str, None]' instead

Am I the only one to have this? And why is this? Is there a solution to make this warning go away?

like image 428
Olivier Pons Avatar asked Jan 06 '18 23:01

Olivier Pons


1 Answers

My guess would be the analytics that give this warning are not sharp enough.

The value type for

game_data = {'boats': [], 'width': None, 'height': None} 

can not be determined.

The first "real" value you put in is an int:

w = 12
game_data = {'boats': [], 'width': None, 'height': w}

So PyCharm assumes that this is a dict(string->int).

Then you add a inner dict as value to your empty list:

game_data['boats'].append({'name': None})

So now it has a dict(string->int) that suddenly gets to be a mixed thing and warns you.

Thats about the same as what What does this warning in PyCharm mean? is about: adding int into a list of strings using pycharm as IDE.

As to how to get rid of the warning: Jetbrains Resharper is very configurable, I guess pycharm will be as well. This documentation https://www.jetbrains.com/help/pycharm/configuring-inspection-severities.html#severity might help you configure the severity down - if not I am sure the support of Jetbrains is eager to help you out - they were whenever I had problems using resharper.

like image 58
Patrick Artner Avatar answered Nov 15 '22 17:11

Patrick Artner