Skip to content

Enable TCH rules: Enforce correct TYPE_CHECKING usage #8231

@HyeockJinKim

Description

@HyeockJinKim

Rules

TCH category includes:

  • TCH001: Move import into TYPE_CHECKING block (used only for annotations)
  • TCH002: Move import out of TYPE_CHECKING block (needed at runtime)
  • TCH003: Move stdlib import into TYPE_CHECKING block

Purpose

Ensure correct usage of TYPE_CHECKING blocks:

  1. Imports only used for type annotations should be in TYPE_CHECKING
  2. Imports needed at runtime should NOT be in TYPE_CHECKING
  3. Properly separates annotation-only imports from runtime imports

Benefits

  • Faster import times (annotation-only imports not loaded at runtime)
  • Accurate transitive dependency detection in lint/check/test
  • Prevents runtime errors from imports that should be in TYPE_CHECKING
  • Catches runtime-evaluated values incorrectly placed in TYPE_CHECKING

Auto-fix

✅ Available

Notes

  • Critical for correct pants --changed-dependents=transitive behavior
  • Helps with circular import prevention

JIRA Issue: BA-4023

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions