python - 诱使 PyCharm 在可调用对象中推断通用 Python 类型参数的技巧?
问题描述
我正在编写一些代码,其中函数被大量传递给其他函数,并且我试图让 PyCharm 帮助我在“编译时”(或“IDE 时间”或其他任何时候)正确获取所有参数类型称为)。不幸的是,我似乎无法走得很远。例如,PyCharm 对这段代码非常满意:
def plus_one(x: int) -> int:
return x+1
map(plus_one, [1])
map(plus_one, ["aaa"]) # The error here could be spotted at "IDE time"
即使我编写自己的版本map
并输入提示,PyCharm 仍然不会抱怨:
from typing import List, Callable, TypeVar
def plus_one(x: int) -> int:
return x+1
V = TypeVar("V")
W = TypeVar("W")
def map_list(f: Callable[[V], W], x: List[V]) -> List[W]:
return [f(y) for y in x]
map_list(plus_one, [1])
map_list(plus_one, ["aaa"])
有什么方法可以让 PyCharm 在此处更积极地标记类型错误?
解决方案
推荐阅读
- php - 如果 IP 匹配,则从 CSV 中删除一行
- php - 您可以(MySQL)按可选值和不同表中的值排序吗?
- windows - 加载 mod_wsgi 时的 Apache 权限问题
- php - PHP 将 CSV 转换为特定的 JSON 分布
- python - 在 RRS 提要中寻找不同的坏词,停留在变量列表中,如果有的话
- arangodb - 在 ArangoDB 中进行 EXISTS 查询的最佳方法是什么
- javascript - 为什么html2canvas产生模糊的pdf文件?
- css - 代号一的组件在 Eclipse 上失去了风格
- javascript - 异步 API 请求返回所有结果
- php - 从 API 响应中迭代 PHP Laravel 中的多维数组时出现问题