在 pandas.DataFrame 的情况下,mypy 错过了不兼容的类型

mypy missed incompatible type in case of a pandas.DataFrame

当涉及到 DataFrame 时,我被 mypy 绊倒了,没有抱怨不兼容的类型。我可能是 mypy 的新手,所以请指出正确的方向。

我已将案例简化为以下代码。我的直觉告诉我,第 12 行和第 13 行都应该失败。但是第 13 行:func(DataFrame()) 由于某种原因通过了。

from pandas import DataFrame

class T1:
    pass

class T2:
    pass

def func(arg: T2):
    pass

func(T1())
func(DataFrame())
mypy mypy_sandbox.py 
mypy_sandbox.py:12: error: Argument 1 to "func" has incompatible type "T1"; expected "T2"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

好的,也许 pandas 目前(2021 年 12 月)缺少一些东西。 基于 answer given by @user2640045,我尝试安装 pandas-stubs,它解决了我的问题。

pip install pandas-stubs
Successfully installed pandas-stubs-1.2.0.39
mypy mypy_sandbox.py 
mypy_sandbox.py:12: error: Argument 1 to "func" has incompatible type "T1"; expected "T2"  [arg-type]
mypy_sandbox.py:13: error: Argument 1 to "func" has incompatible type "DataFrame"; expected "T2"  [arg-type]
Found 2 errors in 1 file (checked 1 source file)