Skip to content

TCH004: false positive for import that is used only in type hints #7214

@WhyNotHugo

Description

@WhyNotHugo

My test.py:

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from sentry_sdk._types import Event
    from sentry_sdk._types import Hint

def before_send(event: Event, hint: Hint) -> Event | None:
    pass

Steps to reproduce:

> ruff --select TCH test.py
test.py:4:35: TCH004 [*] Move import `sentry_sdk._types.Event` out of type-checking block. Import is used for more than type hinting.
test.py:5:35: TCH004 [*] Move import `sentry_sdk._types.Hint` out of type-checking block. Import is used for more than type hinting.
Found 2 errors.
[*] 2 potentially fixable with the --fix option.

In this particular case, sentry_sdk only exports types if TYPE_CHECKING, so I cannot move this out of the type checking block.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-inferenceRequires more advanced type inference.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions