gpt4 book ai didi

python - 如何使用任意参数列表(vararg)键入提示可调用

转载 作者:行者123 更新时间:2023-12-04 07:32:43 25 4
gpt4 key购买 nike

我正在尝试键入一个看起来像这样的函数:

def delete_file_if_exists():
"""Fixture providing a function that deletes a list of files of given filename it it exists
"""
def deletion_function(*args: AnyPath) -> None:
"""A function deleting the file of given path if it exists
:param args:The list of path to delete if file exists
"""
for arg in args:
if os.path.isfile(arg):
os.remove(arg)
return deletion_function
我想知道如何准确输入:
1.
def delete_file_if_exists() -> Callable[..., None]:
有效但未指定变量参数的类型
2.
def delete_file_if_exists() -> Callable[[List[AnyPath]], None]:
不工作,但 mypy在它上面运行会出现以下异常: Incompatible return value type (got "Callable[[VarArg(Union[str, bytes, _PathLike[str], _PathLike[bytes]])], None]", expected "Callable[[List[Union[str, bytes, _PathLike[str], _PathLike[bytes]]]], None]")所以我想知道我是否可以用这个 VarArg(我无法导入)做一些事情,或者我是否被一个省略号打字卡住了。

最佳答案

您可以使用 mypy-extension 导入类型 VarArg .

关于python - 如何使用任意参数列表(vararg)键入提示可调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67873865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com