TRF063
Model-local imports must not need backends beyond those the importing module’s exported objects declare.
| Default | Enabled |
| Scope | All models |
| Source | mlinter/trf063.py |
| Show in terminal | mlinter --rule TRF063 |
What it does
For every relative import that runs when a model file is imported, follows the imported model files and collects the backends their unguarded top-level imports need (import torch needs torch, from PIL import Image needs vision, and so on). It flags the import when one of those backends is not covered by the file’s base requirements, the weakest @requires(backends=...) among its __all__ objects, or an enclosing if is_<backend>_available(): guard. A modular_*.py file is checked through the files it generates.
Why is this bad?
Transformers’ lazy module imports a file as soon as the backends one of its exported objects declares are installed. If that file pulls in a sibling which needs more, from transformers import * crashes in that environment: a processor marked @requires(backends=("vision",)) that imports a torchvision image processor breaks every install that has Pillow but not torch. CI rarely runs that combination, so the crash reaches users.
Example
if is_vision_available():
- from .image_processing_acme import AcmeImageProcessorKwargs
+ from .image_processing_pil_acme import AcmeImageProcessorKwargs
@requires(backends=("vision",))
class AcmeProcessor(ProcessorMixin):
Suppressing this rule
Add a # trf-ignore: TRF063 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.
