TRF039
Imports guarded by is_*_available() must be removed once nothing in the file uses them.
| Default | Enabled |
| Scope | All models |
| Source | mlinter/trf039.py |
| Show in terminal | mlinter --rule TRF039 |
What it does
Finds if is_*_available(): import ... blocks (including combinations like is_vision_available() and is_torch_available()) and checks whether the imported name is referenced anywhere else in the file, including inside string type hints and __all__. Flags the import if it is not.
Why is this bad?
ruff’s unused-import check does not clean these up, because the import is reachable and ‘used’ as far as static analysis of the block alone is concerned. When code is refactored to no longer need PIL.Image, torch, etc., the guarded import is easy to forget and lingers as dead weight and a misleading signal about the file’s real dependencies.
Example
if is_vision_available():
- from PIL import Image
Suppressing this rule
Add a # trf-ignore: TRF039 comment on the flagged line or the line directly above it. See Suppressing rules for whole-file directives and when a suppression is the wrong answer.
