跳到内容

quoted-annotation (UP037)

源自 pyupgrade linter。

修复总是可用的。

作用

检查类型注解中是否存在不必要的引号。

为什么这不好?

在 Python 中,可以引用类型注解以避免前向引用。

但是,如果存在 from __future__ import annotations,Python 将始终以延迟方式评估类型注解,从而使引号变得不必要。

类似地,如果注解位于仅类型上下文中,并且不会在运行时被 Python 评估,则引号也将被认为是不必要的。例如,Python 不会评估函数体中赋值的类型注解。

示例

给定

from __future__ import annotations


def foo(bar: "Bar") -> "Bar": ...

建议改为

from __future__ import annotations


def foo(bar: Bar) -> Bar: ...

给定

def foo() -> None:
    bar: "Bar"

建议改为

def foo() -> None:
    bar: Bar

预览

当启用 预览 时,如果 lint.future-annotations 设置为 true,则如果这样做可以取消注解的引用,将添加 from __future__ import annotations

修复安全性

规则的修复被标记为安全,除非启用 预览 和 [lint.future_annotations][lint.future_annotations] 并且添加了 from __future__ import annotations 导入。这样的导入可能会改变文件中所有注解的行为。

Options (选项)

另请参阅

参考