23

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?

5
  • 1
    not on VS 2017 python 3.6 not in pyfiddle.io , maybe a pycharm speciality? Commented Jan 6, 2018 at 23:16
  • I agree. I have no warnings either in Visual Studio Code. Must be PyCharm. Commented Jan 6, 2018 at 23:17
  • 1
    Possible duplicate of What does this warning in PyCharm mean? Commented Jan 6, 2018 at 23:18
  • Please watch carefully the question and carefully the answers of what you think is a duplicate, and you'll change your mind Commented Jan 6, 2018 at 23:39
  • I am not suggesting anything. I use VS2017 sometimes and for simple stuff even pyfiddle.io. I do not endorse anything. Use whatever suits you - I merely observed that for the same code blocks you postet I get no warning in either IDE I use so they might be IDE-Dependent. I will review the other post I thought a duplicate, gimme a sec. Commented Jan 6, 2018 at 23:45

1 Answer 1

22

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.

2
  • 18
    I prefer to add a "# noinspection PyTypeChecker" above the line in causing the warning, this makes it explicit for the next person reading the code, that this is known and should be ignored.
    – chjortlund
    Commented Jun 5, 2020 at 7:42
  • 10
    You can also resolve it by adding a hint like game_data: dict[str, Any]
    – laike9m
    Commented Jan 29, 2021 at 7:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.