相关疑难解决方法(0)

mypy 设置字典键/接口

假设我有一个将字典作为参数的函数:

def f(d: dict) -> None:
    x = d["x"]
    print(x)
Run Code Online (Sandbox Code Playgroud)

我可以指定这本字典必须"x"mypy的键吗?我正在寻找类似于typescript 的 interface 的东西,而无需更改d为类。

我不想更改d为类的原因是因为我正在修改一个大型现有代码库以添加mypy类型检查并且该字典在许多地方使用。如果我必须将所有实例更改为d["x"],我将不得不修改大量代码d.x

python type-hinting python-3.x mypy

4
推荐指数
1
解决办法
1210
查看次数

标签 统计

mypy ×1

python ×1

python-3.x ×1

type-hinting ×1