TRF060

Files in a model directory must import from transformers with relative imports, not from transformers....

   
Default Enabled
Scope Models added on or after 2026-06-20
Source mlinter/trf060.py
Show in terminal mlinter --rule TRF060

What it does

Flags from transformers... import X and import transformers... in every file under src/transformers/models/ and suggests the relative spelling for the file’s location (from ...modeling_utils import X, from .configuration_acme import X). convert_*.py scripts are skipped (they run as standalone scripts, where a relative import cannot resolve).

Why is this bad?

Relative imports are the convention across src/transformers. An absolute from transformers import X inside the package goes through the lazy top-level transformers/__init__.py, which can raise a circular import while the package is still initializing, and in a checkout with several installs it can resolve to a different copy of transformers than the one the file lives in.

Example

-from transformers.modeling_utils import PreTrainedModel
-from transformers.models.acme.configuration_acme import AcmeConfig
-from transformers import initialization as init
+from ...modeling_utils import PreTrainedModel
+from .configuration_acme import AcmeConfig
+from ... import initialization as init

Suppressing this rule

Add a # trf-ignore: TRF060 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.

Allowlisted models

1 model are exempt from TRF060 in mlinter/rules.toml, because they predate the convention and cannot be changed without breaking backward compatibility.

Show the 1 allowlisted model
  • auto