【发布时间】:2021-01-25 14:42:08
【问题描述】:
我想对没有参数的函数使用类型提示
from typing import Callable
def no_parameters_returns_int() -> int:
return 7
def get_int_returns_int(a: int) -> int:
return a
def call_function(next_method: Callable[[], int]):
print(next_method())
call_function(no_parameters_returns_int) # no indication of error from IDE expected
call_function(get_int_returns_int) # expected an indication of error from IDE
当我传递一个带有参数的函数时,我希望 PyCharm 能够标记该行。
还尝试了Callabale[[None], int] 和Callabale[[...], int]。但是第一个提示传递的函数接收None 类型参数,第二个提示传递的函数至少接收一个参数。
是否可以暗示传递的函数不接收参数?
【问题讨论】:
-
MyPy 检测正确:mypy-play.net/…
-
@jonrsharpe 谢谢,我切换到 Callable[..., int] 并没有收到提示错误,但是我预计运行时会出错
-
运行时是什么意思? Python 不会在运行时进行类型检查,我假设您询问的是 PyCharm 未能发出警告。
-
@jonrsharpe 我的意思是,当 python 解释器执行 hof(one_arg) 行时,它应该会导致错误。但是我在 mypy 上打开了您的代码并将 Callable[[], int] 更改为 Callable[..., int] 现在没有错误。 mypy-play 只是测试类型检查吗?
标签: python python-3.x pycharm type-hinting nonetype