Python 使用Mypy存根文件不需要';t与函数定义中写入类型提示的工作方式相同

Python 使用Mypy存根文件不需要';t与函数定义中写入类型提示的工作方式相同,python,python-3.x,mypy,Python,Python 3.x,Mypy,我尝试在函数的定义中显式地编写类型提示 # test.py def annotated(x: int, y: str) -> bool: return x < y 没错。但是当我尝试使用存根文件时 # test.py def annotated(x, y): return x < y # test.pyi def annotated(x: int, y: str) -> bool: ... 没关系。我得到 error: Argument 2 to "

我尝试在函数的定义中显式地编写类型提示

# test.py
def annotated(x: int, y: str) -> bool:
    return x < y
没错。但是当我尝试使用存根文件时

# test.py
def annotated(x, y):
    return x < y

# test.pyi
def annotated(x: int, y: str) -> bool: ...
没关系。我得到

error: Argument 2 to "annotated" has incompatible type "int"; expected "str"
但是调用
注释(2,'s')
仍然一无所获。

那么,我应该如何用存根文件检查非法操作呢?谢谢

您可能想试着问这个问题——我怀疑这种行为可能是出于设计,但不一定记得,直接问核心开发人员可能会更快。据我所知,存根文件只为使用它的其他模块提供模块的“类型API”。换句话说,函数实现的主体不会根据存根文件进行检查。如果要更改此设置,则可以在mypy tracker上打开一个问题:在此处找到原因和解决方案:
from test import annotated

annotated(2, 3)
error: Argument 2 to "annotated" has incompatible type "int"; expected "str"