Skip to content

quoted-annotation (UP037)

Derived from the pyupgrade linter.

Fix is always available.

What it does

Checks for the presence of unnecessary quotes in type annotations.

Why is this bad?

In Python, type annotations can be quoted to avoid forward references.

However, if from __future__ import annotations is present, Python will always evaluate type annotations in a deferred manner, making the quotes unnecessary.

Similarly, if the annotation is located in a typing-only context and won't be evaluated by Python at runtime, the quotes will also be considered unnecessary. For example, Python does not evaluate type annotations on assignments in function bodies.

Example

Given:

from __future__ import annotations


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

Use instead:

from __future__ import annotations


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

Given:

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

Use instead:

def foo() -> None:
    bar: Bar

Preview

When preview is enabled, if lint.future-annotations is set to true, from __future__ import annotations will be added if doing so would allow an annotation to be unquoted.

Fix safety

The rule's fix is marked as safe, unless preview and [lint.future_annotations][lint.future_annotations] are enabled and a from __future__ import annotations import is added. Such an import may change the behavior of all annotations in the file.

Options

See also

References