Skip to content

TC004 diagnostics/docs should clarify that type hints can be executed at run-time #16490

@nathanjmcdougall

Description

@nathanjmcdougall

Summary

For me, TC004 fairly consistently gives false positives when the guarded import is used for return types.

For example:

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    import abc


def bar() -> abc.ABC | None:
    return None

Running ruff check --isolated --select TC004 gives the following output:

main.py:4:12: TC004 Move import `abc` out of type-checking block. Import is used for more than type hinting.
  |
3 | if TYPE_CHECKING:
4 |     import abc
  |            ^^^ TC004
  |
  = help: Move out of type-checking block

Found 1 error.
No fixes available (1 hidden fix can be enabled with the `--unsafe-fixes` option).

Version

ruff 0.9.9 (091d0af 2025-02-28)

Metadata

Metadata

Assignees

No one assigned

    Labels

    diagnosticsRelated to reporting of diagnostics.help wantedContributions especially welcome

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions