如何键入提示返回函数的函数?

Nat*_*ord 3 python type-hinting python-3.x

假设我有以下代码:

def validator(blacklist: list=['heck', 'muffins']):

    def f(questionable_word: str) -> bool:
        return questionable_word in blacklist

    return f

validator_british = validator(['pish'])
validator_british('pish')  # returns True
validator_british('heck')  # returns False
Run Code Online (Sandbox Code Playgroud)

我的问题是我该如何键入提示该validator函数,以使其指示返回了一个函数,特别是需要a str并返回a 的函数bool?该f函数的签名是:

def f(questionable_word: str) -> bool
Run Code Online (Sandbox Code Playgroud)

我放什么???地方validator

validator(blacklist: list=['heck', 'muffins']) -> ???:
Run Code Online (Sandbox Code Playgroud)

Mos*_*oye 5

typing.Callable 是您想要的:

validator(blacklist: list=['heck', 'muffins']) -> Callable[[str], bool]:
Run Code Online (Sandbox Code Playgroud)