文章内容

2022/1/28 18:45:14,作 者: 黄兵

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

最近在编写 Python 代码的时候使用 PyCharm 出现了如下警告:

Expected type 'Dict[str, Any]' (matched generic type '_T'), got 'Generator[Dict[str, Any], Any, None]' instead
 Inspection info:
Reports type errors in function call expressions, targets, and return values. In a dynamically typed language, this is possible in a limited number of cases.
Types of function parameters can be specified in docstrings or in Python 3 function annotations.
Example:
def foo() -> int:
    return "abc" # Expected int, got str


a: str
a = foo() # Expected str, got int
With the quick-fix, you can modify the problematic types:
def foo() -> str:
    return "abc"


a: str
a = foo()


出现这个问题的原因:

由于编辑器推断数据类型是 Dict[str, Any] 类型,但是后面对 List append 操作,使其变成了一个枚举类型。

但是经过程序运行并没有什么问题。

解决方案:

在出现警告的代码上面加上如下注释:

# noinspection PyTypeChecker

即可解决问题,同时也防止后面其他人误操作。


参考资料:

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


黄兵个人博客原创。

转载请注明出处:黄兵个人博客 - Expected type 'Dict[str, Any]' (matched generic type '_T'), got 'Generator[Dict[str, Any], Any, None]' instead 

分享到:

发表评论

评论列表