微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么这个 Python 类型提示在 Pyright 中产生错误而不是在 MyPy 中产生错误?

如何解决为什么这个 Python 类型提示在 Pyright 中产生错误而不是在 MyPy 中产生错误?

我有以下一段代码

from typing import TypeVar,Callable,Any

T = TypeVar("T")

def test(t: T,s: Callable[[T],Any] = (lambda x: x)) -> None:
    print(s(t))

test(1)

运行 pyright test.py 会产生以下错误

  8:1 - error: Argument of type "(x: T@test) -> T@test" cannot be assigned to parameter "s" of type "(_p0: T@test) -> Any" in function "test"
    Type "(x: T@test) -> T@test" cannot be assigned to type "(_p0: T@test) -> Any"
      Parameter 1: type "T@test" cannot be assigned to type "T@test"
        Type "Literal[1]" cannot be assigned to type "T@test" (reportGeneralTypeIssues)

使用 mypy 时不会出现同样的错误

奇怪的是,如果我明确地将 s 传递给 test错误不会发生:

test(1,lambda x: x)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。