Back to Ruff

Invalid Syntax In Forward Annotation

crates/ty_python_semantic/resources/lint_docs/invalid-syntax-in-forward-annotation.md

0.15.18948 B
Original Source

What it does

Checks for string-literal annotations where the string cannot be parsed as a Python expression.

Why is this bad?

Type annotations are expected to be Python expressions that describe the expected type of a variable, parameter, attribute or return statement.

Type annotations are permitted to be string-literal expressions, in order to enable forward references to names not yet defined. However, it must be possible to parse the contents of that string literal as a normal Python expression.

Example

python
def foo() -> "intstance of C":  # error
    return 42


class C: ...

Use instead:

python
def foo() -> "C":
    return C()


class C: ...

References