python - 警告 : Expected type [Class Name], 改为 'Dict[str, int]'

标签 python pycharm

我正在构建一个类,其方法采用字典作为输入,但 Pycharm 显示警告。

''' 预期类型“TestClass”,得到“Dict[str, int]”而不是 less...(⌘F1) 检查信息:此检查检测函数调用表达式中的类型错误。由于动态调度和鸭子类型,这在有限但有用的情况下是可能的。函数参数的类型可以在文档字符串或 Python 3 函数注释中指定 '''

class TestClass:
    def __getitem__(self, index):
        return self[index]

    def get_keys(self):
        return list(self.keys())


dict_input = {'a':123, 'b':456}
TestClass.get_keys(dict_input)

所以我在这里收到警告:

TestClass.get_keys(dict_input)

此警告是什么意思?解决方法是什么?

最佳答案

像您编写的这样的方法称为“实例方法”。

self(接收者)应该是 TestClass 的实例(否则,很多事情可能会出错,例如 super)。

您可以将 get_keys 定义为静态方法,或使用简单的函数(无需将其放入类中)。

class TestClass:
    @staticmethod
    def get_keys(s):
        return list(s.keys())

您可能想阅读Python documentation about classes了解更多详情。

关于python - 警告 : Expected type [Class Name], 改为 'Dict[str, int]',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58358374/

相关文章:

python - tweepy/twitter 从位置 : 获取所有推文

python - 如何构建热图?

python 3.7 : pycurl installation on Windows 10

python - 如何通过设置conda环境在PyCharm中添加conda的所有包?

python - PyCharm 中用于 Python 编译扩展的自动完成功能

python - 导入错误 : cannot import name '_remove_dead_weakref'

python - 在前一个文件之上读取文件

python - 从多处理启动 celery Worker

Python - "No module named my_module"在终端中,但在 PyCharm 中不存在

pycharm - 如何多次运行同一个脚本而不等待其完成