嵌套字典上的参数过多或参数过多

问题描述

我正在尝试以dict类型添加类型提示,该类型提示具有多个将功能绑定到它们的字段。 例如

from typing import Dict,Callable,Any,Union

def fn():
  print("Hello World")

def fn2(name):
   print("goodbye world",name)

d = {
  "hello" : {
    "world": fn
  },"goodbye": {
    "world": fn2
  }
} # type: Dict[str,Dict[str,Union[Callable[[],None],Callable[[str],None]]]]

d["hello"]["world"]()
d["goodbye"]["world"]("john")

我要解决的问题是,每当我尝试运行mypy(v0.782)时都会引发错误:

test2.py:17: error: Too few arguments
test2.py:18: error: Too many arguments

很显然,从函数定义和类型提示可以看出,我已经传递了正确的参数。我显然丢失了一些东西,因为它会引发错误。

但是,以下方法可行,因此我怀疑它与类型提示中的Union类型有关。

from typing import Dict,Union


def fn():
    print("Hello World")


d = {"hello": {"world": fn}}  # type: Dict[str,Callable[[],None]]]

d["hello"]["world"]()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)