Skip to content

quoted-type-alias (TC008)

Derived from the flake8-type-checking linter.

Fix is always available.

This rule is unstable and in preview. The --preview flag is required for use.

What it does

Checks for unnecessary quotes in PEP 613 explicit type aliases and PEP 695 type statements.

Why is this bad?

Unnecessary string forward references can lead to additional overhead in runtime libraries making use of type hints, as well as lead to bad interactions with other runtime uses like PEP 604 type unions.

For explicit type aliases the quotes are only considered redundant if the type expression contains no subscripts or attribute accesses this is because of stubs packages. Some types will only be subscriptable at type checking time, similarly there may be some module-level attributes like type aliases that are only available in the stubs.

Example

Given:

OptInt: TypeAlias = "int | None"

Use instead:

OptInt: TypeAlias = int | None

Given:

type OptInt = "int | None"

Use instead:

type OptInt = int | None

Fix safety

This rule's fix is marked as safe, unless the type annotation contains comments.

References