错误:无类型装饰器(@typeguard)使函数“add_two”无类型[misc]

问题描述 投票:0回答:0

上下文

在带有mypy的项目上使用

typeguard
时,我遇到了错误:

src/pythontemplate/adder.py:6: error: Untyped decorator makes function "add_two" untyped  [misc]

在以下示例代码中:

"""Example python file with a function."""

from typeguard import typechecked


@typechecked
def add_two(*, x: int) -> int:
    """Adds a value to an incoming number."""
    return x + 2

看完this答案后,似乎可以通过为

@typechecked
函数/装饰器手动添加类型来解决此错误消息。

问题

  1. 我不太确定
    @typeguard
    装饰器的类型。
  2. 这个装饰器在项目的每个
    python
    文件中,因此在每个文件中添加该装饰器的类型,或者甚至导入到具有该类型的单独文件,将是相当多的重复代码和/或样板代码.
  3. 这个装饰器专门用于类型检查,所以我希望可能有更优雅的方式来解决这个问题。

问题

如何确保

@typeguard
装饰器被键入?

python mypy typing typeguards
© www.soinside.com 2019 - 2024. All rights reserved.