TRF047

Image/video processors are stateless: preprocess/_preprocess/post_process_* must not write self attributes.

   
Default Enabled
Scope All models
Source mlinter/trf047.py
Show in terminal mlinter --rule TRF047

What it does

Checks preprocess, _preprocess, __call__, and post_process* methods in image_processing_*.py and video_processing_*.py for assignments to self attributes.

Why is this bad?

A processor that carries state between calls breaks preprocess-many-then-postprocess batching: the second preprocess overwrites the state the first postprocess needs. Return the value or pass it through the method chain.

Example

 def _preprocess(self, images, **kwargs):
-    self.original_sizes = [image.shape[-2:] for image in images]
+    original_sizes = [image.shape[-2:] for image in images]
     ...
+    return BatchFeature(data={"pixel_values": pixel_values, "original_sizes": original_sizes})

Suppressing this rule

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