What does this warning in PyCharm mean?

匿名 (未验证) 提交于 2019-12-03 00:56:02

问题:

I'm writing a Uno game in Python and I'm currently setting up a Uno deck.

_VALID_FACES = ['skip', 'draw2', 'reverse', 'wild', 'wild4'] + range(10) 

I think this should be just fine and dandy, no problem. However, PyCharm insists on this error:

Expected type list[str] (matched generic type 'list[T]'), got 'list[int]' instead 

Now I'm not entirely sure what this means. Any ideas? The code runs, but the warning is still there in PyCharm.

回答1:

Though you can have list of strings and ints in python, it's preferable to keep list elements' types consistent. In your example you can convert all elements to strings:

_VALID_FACES = ['skip', 'draw2', 'reverse', 'wild', 'wild4'] + map(str, range(10)) 


回答2:

PyCharm reads your code and tries to guess what you're doing, then if you do something contrary to what it thinks you should be doing, it will warn you. This is useful when you have a large codebase and you accidentally do something stupid, but can be annoying when you know exactly what you're doing.

In this case, you've got a list full of strings, and you're adding a list of integers to it. PyCharm is surprised at this, thinking you'd only be having strings in your list, not a mixture of strings and integers.

You should be able to safely ignore it.



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