如何让 mypy 接受解压的字典?

How to get mypy to accept an unpacked dict?

我的 mypy 有问题。

我有这个代码:

func(arg1, arg2, arg3=0.0, arg4=0.0)
# type: (float, float, float, float) -> float
# do something and return float.

dict_with_other_arguments = {arg3: 0.5, arg4: 1.4}
a = func(arg1, arg2, **dict_with_other_arguments)

问题是 mypy 不检查字典中的类型,相反,我得到这样的错误:

error: Argument 3 to "func" has incompatible type "**Dict[str, float]"; expected "float"

知道如何在不更改代码的情况下解决此问题吗?

Mypy 可以正确标记您的函数调用。以下代码说明了原因:

def func(str_arg='x', float_arg=3.0):
  # type: (str, float) -> None
  print(str_arg, float_arg)

kwargs1 = {'float_arg': 8.0}
kwargs2 = {'str_arg': 13.0}  # whoops

func(float_arg=5.0)  # prints "x 5.0" -- good
func(**kwargs1)      # prints "x 13.0" -- good but flagged by Mypy
func(**kwargs2)      # prints "13.0 3.0" -- bad

在这个例子中,kwargs1kwargs2 都是 Dict[str, float] 类型。类型检查器不考虑键的内容,只考虑它们的类型,因此对 func 的第二次和第三次调用看起来与 Mypy 相同。它们必须要么都是错误要么都是可接受的,并且它们不能都是可接受的,因为第三次调用违反了类型系统。

类型检查器可以确保您没有在字典中传递错误类型的唯一方法是所有未明确传递的参数是否共享字典值的类型。但是请注意,mypy 不会保护您免受因在 dict 中重新指定关键字参数而导致的错误:

# This works fine:
func('x', **kwargs1)
# This is technically type safe and accepted by mypy, but at runtime raises
# `TypeError: func() got multiple values for argument 'str_arg'`:
func('x', **kwargs2)

这里有一些关于这个问题的进一步讨论:https://github.com/python/mypy/issues/1969