Repository navigation
TRF060: Check for absolute imports - #77
Merged
Merged
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tarekziade
force-pushed
the
check-absolute-imports
branch
from
October 28, 2026 06:50
0934008 to
c19e684
Compare
tarekziade
added a commit
to guarin/transformers-mlinter
that referenced
this pull request
Oct 28, 2026
TRF060 landed in huggingface#77 and PRs are merged in date order, so this rule takes the next free id. The rule prose and violation message now say what goes wrong: the modular converter keeps a same-file base in the generated modeling file. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tarekziade
added a commit
to hmellor/transformers-mlinter
that referenced
this pull request
Oct 28, 2026
TRF060, TRF061 and TRF062 landed first (huggingface#77, huggingface#81, huggingface#82) and PRs merge in date order. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tarekziade
added a commit
that referenced
this pull request
Oct 28, 2026
## Why [huggingface/transformers#49748](huggingface/transformers#49748): `from transformers import *` crashes on an install with Pillow but no torch. `Gemma4Processor` / `Gemma4UnifiedProcessor` are `@requires(backends=("vision",))`, but under `if is_vision_available():` they import `.image_processing_gemma4[_unified]`, which does `import torch` / torchvision at top level. The lazy module imports the processor file as soon as vision is installed, and the sibling import crashes. TRF009 doesn't catch this because it only bans imports from *other* models; imports within a model folder are fine. CI didn't catch it either, because the runtime "PIL only" import step was running without PIL (fixed in huggingface/transformers-ci#341). This rule catches the pattern at lint time. ## What TRF064 checks For every relative import that runs at module import time: - **Declared** backends = the file's base requirements (mirror of `BASE_FILE_REQUIREMENTS` in transformers' `import_utils.py`) βͺ the *weakest* `@requires(backends=...)` among its `__all__` objects, since the lazy loader imports the file as soon as any one exported object's backends are present. - **Needed** backends = what the imported model-local files' unguarded top-level imports pull in (`import torch` β torch, `from PIL` β vision, β¦), followed transitively, with implied backends (torchvision β torch). - Flags when needed β declared βͺ enclosing `is_<backend>_available()` guards. - Skips function/class bodies, `TYPE_CHECKING`, and `try`/`except ImportError`. Core deps (`tokenizers`, `numpy`, β¦) are never backends. - `modular_*.py` files are checked through the files they generate, since generated files aren't linted. The violation anchors on the matching import or exported class in the modular file, so `# trf-ignore: TRF064` works there. - The message shows the full import chain. The lint cache now hashes every sibling file in the model directory, so editing an imported file re-checks its importers. ## Results on transformers | Tree | TRF064 hits | |---|---| | `main` @ `fb3e4f2828` | 2: `gemma4/processing_gemma4.py:L28` and `gemma4_unified/modular_gemma4_unified.py:L340` | | PR #49748 head | 0 | These are exactly the two classes that fail `from transformers import *` at runtime in the `huggingface/transformers-consistency` image with only Pillow installed. There are no other hits (an earlier draft treated `tokenizers` as optional and produced 3 false positives; it is a core dependency). `make lint`, `make test` (454 passed) and `make typecheck` all pass. ## Limitations / follow-ups - Base requirements and the package β backend map are hand-mirrored from transformers and can drift. - Direct third-party imports (e.g. top-level `import torch` in a `processing_*.py` with only `@requires("vision")`) are not flagged yet: the rule is scoped to model-local imports to keep this first version precise. Worth a follow-up after a false-positive pass. - Adopting it in transformers needs a TRF064 entry in `utils/rules.toml`. - TRF060β063 are taken by open PRs (#77, #79, #81, #82, #85), hence 064. π€ Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
tarekziade
added a commit
to guarin/transformers-mlinter
that referenced
this pull request
Oct 28, 2026
TRF060-TRF063 landed first (huggingface#77, huggingface#81, huggingface#82, huggingface#86) and PRs merge in date order. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Check that all imports in
transformers/modelsare relative