TRF027
Model files must raise explicit errors instead of using bare assert.
| Default | Enabled |
| Scope | Models added on or after 2026-06-20 |
| Source | mlinter/trf027.py |
| Show in terminal | mlinter --rule TRF027 |
What it does
Flags any assert statement in modeling_*.py, modular_*.py and configuration_*.py.
Why is this bad?
python -O strips assert statements, so a shape or config check written as an assert silently disappears in optimised runs. An assert also gives the user a bare AssertionError with no guidance, where a ValueError can name the offending value and what to do about it.
Example
def forward(self, hidden_states):
- assert hidden_states.dim() == 3
+ if hidden_states.dim() != 3:
+ raise ValueError(f"Expected a 3D tensor, got shape {tuple(hidden_states.shape)}.")
Suppressing this rule
Add a # trf-ignore: TRF027 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.
