设置默认参数的mypy call-arg错误

发布于 2025-01-30 03:46:12 字数 500 浏览 2 评论 0原文

我的代码看起来如下:

from typing import Callable


def decorate(func: Callable[[str], None]) -> Callable[[str], None]:
    return func


@decorate
def do_something(some_str: str = 'Hello world') -> None:
    print(some_str)


if __name__ == '__main__':
    do_something()

运行mypy时,它报告了最后一行的以下错误:

错误:“ do_something”的论点太少[call-arg]

如何修复此错误(而不将dectorate的返回类型更改为 callable [...,none])?

I have code that looks like the following:

from typing import Callable


def decorate(func: Callable[[str], None]) -> Callable[[str], None]:
    return func


@decorate
def do_something(some_str: str = 'Hello world') -> None:
    print(some_str)


if __name__ == '__main__':
    do_something()

When running mypy, it reports the following error for the last line:

error: Too few arguments for "do_something" [call-arg]

How can I fix this error (without changing the return type of decorate to Callable[..., None])?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

油焖大侠 2025-02-06 03:46:12

使用typeVar表示输入和返回类型是 2ORES ,并由callable收到的定义。由于可呼叫类型包括整个签名,因此类型的检查器也可以推论位置/可选/关键字参数。

from typing import Callable, TypeVar

# A type variable that can represent any Callable type
C = TypeVar("C", bound=Callable)

# The input Callable type defines the output Callable type as well
def decorate(func: C) -> C:
    return func

如果装饰只能与可能采用字符串的可可配合使用,请相应地调整BONDEND

# A type variable that can represent any Callable type that takes a string
C = TypeVar("C", bound=Callable[[str], None])

值得注意的是,服用str 或使用默认值仍然满足callable [[str],none],因为它可以使用a <调用代码> str 参数。

Use a TypeVar to indicate that the input and return type are equivalent and defined by the Callable received. Since Callable types include the entire signature, this lets the type checker infer positional/optional/keyword arguments as well.

from typing import Callable, TypeVar

# A type variable that can represent any Callable type
C = TypeVar("C", bound=Callable)

# The input Callable type defines the output Callable type as well
def decorate(func: C) -> C:
    return func

If decorate can only work with callables that may take a string, adjust the bound accordingly.

# A type variable that can represent any Callable type that takes a string
C = TypeVar("C", bound=Callable[[str], None])

Notably, a callable taking a str or using a default still satisfies Callable[[str], None] since it can be called with a str argument.

寄居者 2025-02-06 03:46:12

协议__呼叫__在表达功能类型时,给出了很多(某种冗长的)功率,

class Foo(Protocol):
    def __call__(self, some_str: str = 'Hello world') -> None:
        ...

def decorate(func: Foo) -> Foo:
    return func

@decorate
def do_something(some_str: str = 'Hello world') -> None:
    print(some_str)

if __name__ == '__main__':
    do_something()

不确定如何令人满意地避免避免“ Hello World!”复制。

Protocol and __call__ give a lot of (somewhat verbose) power when expressing function types

class Foo(Protocol):
    def __call__(self, some_str: str = 'Hello world') -> None:
        ...

def decorate(func: Foo) -> Foo:
    return func

@decorate
def do_something(some_str: str = 'Hello world') -> None:
    print(some_str)

if __name__ == '__main__':
    do_something()

Not sure how to satisfyingly avoid the "Hello World!" duplication.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文