让 Mypy 接受解压的 dict

Pap*_*K24 3 python dictionary mypy

我的 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)
Run Code Online (Sandbox Code Playgroud)

问题是 mypy 不检查字典中的类型,而是我得到如下错误: error: Argument 3 to "func" has incompatible type "**Dict[str, float]"; 预期的“浮动”

任何想法如何在不更改代码的情况下解决此问题?

小智 6

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
Run Code Online (Sandbox Code Playgroud)

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

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

# 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)
Run Code Online (Sandbox Code Playgroud)

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