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

会有一股神秘感。 提交于 2020-04-11 05:29:12

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/48132786/why-is-this-warning-expected-type-int-matched-generic-type-t-got-dict

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!