From 080d704af11d8e0cff7fc1156166b7912b198d36 Mon Sep 17 00:00:00 2001 From: Yuanyuan Chen Date: Fri, 17 Oct 2025 21:09:42 +0800 Subject: [PATCH] Fix Pylint warnings (#41644) * Fix pylint warnings Signed-off-by: Yuanyuan Chen * More fixes Signed-off-by: Yuanyuan Chen * Raise with an exception Signed-off-by: Yuanyuan Chen --------- Signed-off-by: Yuanyuan Chen --- examples/legacy/seq2seq/run_distributed_eval.py | 3 +-- src/transformers/models/edgetam/configuration_edgetam.py | 2 -- src/transformers/models/edgetam/modular_edgetam.py | 2 -- .../models/qwen2_vl/video_processing_qwen2_vl.py | 2 +- .../models/qwen3_vl/video_processing_qwen3_vl.py | 2 +- .../switch_transformers/modeling_switch_transformers.py | 1 - .../switch_transformers/modular_switch_transformers.py | 1 - .../video_llama_3/video_processing_video_llama_3.py | 2 +- src/transformers/pipelines/question_answering.py | 8 ++++---- tests/models/bart/test_modeling_bart.py | 2 +- tests/models/blenderbot/test_modeling_blenderbot.py | 2 +- .../blenderbot_small/test_modeling_blenderbot_small.py | 2 +- tests/models/fsmt/test_modeling_fsmt.py | 2 +- tests/models/led/test_modeling_led.py | 2 +- tests/models/marian/test_modeling_marian.py | 2 +- tests/models/mbart/test_modeling_mbart.py | 2 +- tests/models/mvp/test_modeling_mvp.py | 2 +- tests/models/opt/test_modeling_opt.py | 2 +- tests/models/pegasus/test_modeling_pegasus.py | 2 +- tests/models/pegasus_x/test_modeling_pegasus_x.py | 2 +- tests/models/plbart/test_modeling_plbart.py | 2 +- tests/models/rag/test_modeling_rag.py | 2 +- 22 files changed, 21 insertions(+), 28 deletions(-) diff --git a/examples/legacy/seq2seq/run_distributed_eval.py b/examples/legacy/seq2seq/run_distributed_eval.py index c491b01af94..795da2efe4b 100755 --- a/examples/legacy/seq2seq/run_distributed_eval.py +++ b/examples/legacy/seq2seq/run_distributed_eval.py @@ -252,8 +252,7 @@ def gather_results_from_each_node(num_replicas, save_dir, timeout) -> list[dict[ return json_data except JSONDecodeError: continue - else: - raise TimeoutError("Rank 0 gave up on waiting for other processes") + raise TimeoutError("Rank 0 gave up on waiting for other processes") # Unreachable diff --git a/src/transformers/models/edgetam/configuration_edgetam.py b/src/transformers/models/edgetam/configuration_edgetam.py index 2c4ef6e1d43..a7654780957 100644 --- a/src/transformers/models/edgetam/configuration_edgetam.py +++ b/src/transformers/models/edgetam/configuration_edgetam.py @@ -93,8 +93,6 @@ class EdgeTamVisionConfig(PreTrainedConfig): if isinstance(backbone_config, dict): backbone_config["model_type"] = backbone_config.get("model_type", "timm_wrapper") backbone_config = CONFIG_MAPPING[backbone_config["model_type"]](**backbone_config) - elif isinstance(backbone_config, AutoConfig): - backbone_config = backbone_config elif backbone_config is None: backbone_config = AutoConfig.from_pretrained( "timm/repvit_m1.dist_in1k", diff --git a/src/transformers/models/edgetam/modular_edgetam.py b/src/transformers/models/edgetam/modular_edgetam.py index b32d71ec0a5..d432a725b02 100644 --- a/src/transformers/models/edgetam/modular_edgetam.py +++ b/src/transformers/models/edgetam/modular_edgetam.py @@ -116,8 +116,6 @@ class EdgeTamVisionConfig(PreTrainedConfig): if isinstance(backbone_config, dict): backbone_config["model_type"] = backbone_config.get("model_type", "timm_wrapper") backbone_config = CONFIG_MAPPING[backbone_config["model_type"]](**backbone_config) - elif isinstance(backbone_config, AutoConfig): - backbone_config = backbone_config elif backbone_config is None: backbone_config = AutoConfig.from_pretrained( "timm/repvit_m1.dist_in1k", diff --git a/src/transformers/models/qwen2_vl/video_processing_qwen2_vl.py b/src/transformers/models/qwen2_vl/video_processing_qwen2_vl.py index 11b5ff80dad..7153154048b 100644 --- a/src/transformers/models/qwen2_vl/video_processing_qwen2_vl.py +++ b/src/transformers/models/qwen2_vl/video_processing_qwen2_vl.py @@ -162,7 +162,7 @@ class Qwen2VLVideoProcessor(BaseVideoProcessor): ) max_frames = math.floor(min(max_frames, total_num_frames) / temporal_patch_size) * temporal_patch_size num_frames = total_num_frames / metadata.fps * fps - num_frames = min(min(max(num_frames, min_frames), max_frames), total_num_frames) + num_frames = min(max(num_frames, min_frames), max_frames, total_num_frames) num_frames = math.floor(num_frames / temporal_patch_size) * temporal_patch_size if num_frames > total_num_frames: diff --git a/src/transformers/models/qwen3_vl/video_processing_qwen3_vl.py b/src/transformers/models/qwen3_vl/video_processing_qwen3_vl.py index e74f55b642d..8a70a1a6858 100644 --- a/src/transformers/models/qwen3_vl/video_processing_qwen3_vl.py +++ b/src/transformers/models/qwen3_vl/video_processing_qwen3_vl.py @@ -164,7 +164,7 @@ class Qwen3VLVideoProcessor(BaseVideoProcessor): "Defaulting to `fps=24`. Please provide `video_metadata` for more accurate results." ) num_frames = int(total_num_frames / metadata.fps * fps) - num_frames = min(min(max(num_frames, self.min_frames), self.max_frames), total_num_frames) + num_frames = min(max(num_frames, self.min_frames), self.max_frames, total_num_frames) if num_frames is None: num_frames = min(max(total_num_frames, self.min_frames), self.max_frames) diff --git a/src/transformers/models/switch_transformers/modeling_switch_transformers.py b/src/transformers/models/switch_transformers/modeling_switch_transformers.py index 8df2f2297d9..008cd020f52 100644 --- a/src/transformers/models/switch_transformers/modeling_switch_transformers.py +++ b/src/transformers/models/switch_transformers/modeling_switch_transformers.py @@ -230,7 +230,6 @@ class SwitchTransformersLayerFF(nn.Module): def forward(self, hidden_states, **kwargs): forwarded_states = self.layer_norm(hidden_states) forwarded_states = self.mlp(forwarded_states) - forwarded_states = forwarded_states output = hidden_states + self.dropout(forwarded_states) return output diff --git a/src/transformers/models/switch_transformers/modular_switch_transformers.py b/src/transformers/models/switch_transformers/modular_switch_transformers.py index 5b26ea6b849..ccb1fe739bd 100644 --- a/src/transformers/models/switch_transformers/modular_switch_transformers.py +++ b/src/transformers/models/switch_transformers/modular_switch_transformers.py @@ -250,7 +250,6 @@ class SwitchTransformersLayerFF(nn.Module): def forward(self, hidden_states, **kwargs): forwarded_states = self.layer_norm(hidden_states) forwarded_states = self.mlp(forwarded_states) - forwarded_states = forwarded_states output = hidden_states + self.dropout(forwarded_states) return output diff --git a/src/transformers/models/video_llama_3/video_processing_video_llama_3.py b/src/transformers/models/video_llama_3/video_processing_video_llama_3.py index 981d4f39f6e..d55b2ab6656 100644 --- a/src/transformers/models/video_llama_3/video_processing_video_llama_3.py +++ b/src/transformers/models/video_llama_3/video_processing_video_llama_3.py @@ -163,7 +163,7 @@ class VideoLlama3VideoProcessor(BaseVideoProcessor): ) max_frames = math.floor(min(max_frames, total_num_frames) / temporal_patch_size) * temporal_patch_size num_frames = total_num_frames / metadata.fps * fps - num_frames = min(min(max(num_frames, min_frames), max_frames), total_num_frames) + num_frames = min(max(num_frames, min_frames), max_frames, total_num_frames) num_frames = math.floor(num_frames / temporal_patch_size) * temporal_patch_size if num_frames > total_num_frames: diff --git a/src/transformers/pipelines/question_answering.py b/src/transformers/pipelines/question_answering.py index e94604b6469..51d4203fcd5 100644 --- a/src/transformers/pipelines/question_answering.py +++ b/src/transformers/pipelines/question_answering.py @@ -86,10 +86,10 @@ def decode_spans( def select_starts_ends( - start, - end, - p_mask, - attention_mask, + start: np.ndarray, + end: np.ndarray, + p_mask: np.ndarray, + attention_mask: np.ndarray, min_null_score=1000000, top_k=1, handle_impossible_answer=False, diff --git a/tests/models/bart/test_modeling_bart.py b/tests/models/bart/test_modeling_bart.py index fad475cc73a..811b9c178e9 100644 --- a/tests/models/bart/test_modeling_bart.py +++ b/tests/models/bart/test_modeling_bart.py @@ -514,7 +514,7 @@ def assert_tensors_close(a, b, atol=1e-12, prefix=""): try: if torch.allclose(a, b, atol=atol): return True - raise + raise Exception except Exception: pct_different = (torch.gt((a - b).abs(), atol)).float().mean().item() if a.numel() > 100: diff --git a/tests/models/blenderbot/test_modeling_blenderbot.py b/tests/models/blenderbot/test_modeling_blenderbot.py index 39dd2ee8161..fbc2febb8af 100644 --- a/tests/models/blenderbot/test_modeling_blenderbot.py +++ b/tests/models/blenderbot/test_modeling_blenderbot.py @@ -270,7 +270,7 @@ def assert_tensors_close(a, b, atol=1e-12, prefix=""): try: if torch.allclose(a, b, atol=atol): return True - raise + raise Exception except Exception: pct_different = (torch.gt((a - b).abs(), atol)).float().mean().item() if a.numel() > 100: diff --git a/tests/models/blenderbot_small/test_modeling_blenderbot_small.py b/tests/models/blenderbot_small/test_modeling_blenderbot_small.py index ad18e8714aa..f107cbefcdd 100644 --- a/tests/models/blenderbot_small/test_modeling_blenderbot_small.py +++ b/tests/models/blenderbot_small/test_modeling_blenderbot_small.py @@ -275,7 +275,7 @@ def assert_tensors_close(a, b, atol=1e-12, prefix=""): try: if torch.allclose(a, b, atol=atol): return True - raise + raise Exception except Exception: pct_different = (torch.gt((a - b).abs(), atol)).float().mean().item() if a.numel() > 100: diff --git a/tests/models/fsmt/test_modeling_fsmt.py b/tests/models/fsmt/test_modeling_fsmt.py index 5a4014e6096..095b9128657 100644 --- a/tests/models/fsmt/test_modeling_fsmt.py +++ b/tests/models/fsmt/test_modeling_fsmt.py @@ -414,7 +414,7 @@ def _assert_tensors_equal(a, b, atol=1e-12, prefix=""): try: if torch.allclose(a, b, atol=atol): return True - raise + raise Exception except Exception: if len(prefix) > 0: prefix = f"{prefix}: " diff --git a/tests/models/led/test_modeling_led.py b/tests/models/led/test_modeling_led.py index 97ad98035af..2a17cb4d8a4 100644 --- a/tests/models/led/test_modeling_led.py +++ b/tests/models/led/test_modeling_led.py @@ -476,7 +476,7 @@ def assert_tensors_close(a, b, atol=1e-12, prefix=""): try: if torch.allclose(a, b, atol=atol): return True - raise + raise Exception except Exception: pct_different = (torch.gt((a - b).abs(), atol)).float().mean().item() if a.numel() > 100: diff --git a/tests/models/marian/test_modeling_marian.py b/tests/models/marian/test_modeling_marian.py index 5d4b92a9805..0ec3f6e6100 100644 --- a/tests/models/marian/test_modeling_marian.py +++ b/tests/models/marian/test_modeling_marian.py @@ -346,7 +346,7 @@ def assert_tensors_close(a, b, atol=1e-12, prefix=""): try: if torch.allclose(a, b, atol=atol): return True - raise + raise Exception except Exception: pct_different = (torch.gt((a - b).abs(), atol)).float().mean().item() if a.numel() > 100: diff --git a/tests/models/mbart/test_modeling_mbart.py b/tests/models/mbart/test_modeling_mbart.py index f9da9fc0864..797ecda798e 100644 --- a/tests/models/mbart/test_modeling_mbart.py +++ b/tests/models/mbart/test_modeling_mbart.py @@ -379,7 +379,7 @@ def assert_tensors_close(a, b, atol=1e-12, prefix=""): try: if torch.allclose(a, b, atol=atol): return True - raise + raise Exception except Exception: pct_different = (torch.gt((a - b).abs(), atol)).float().mean().item() if a.numel() > 100: diff --git a/tests/models/mvp/test_modeling_mvp.py b/tests/models/mvp/test_modeling_mvp.py index f7d3506f844..53bcc3e7716 100644 --- a/tests/models/mvp/test_modeling_mvp.py +++ b/tests/models/mvp/test_modeling_mvp.py @@ -521,7 +521,7 @@ def assert_tensors_close(a, b, atol=1e-12, prefix=""): try: if torch.allclose(a, b, atol=atol): return True - raise + raise Exception except Exception: pct_different = (torch.gt((a - b).abs(), atol)).float().mean().item() if a.numel() > 100: diff --git a/tests/models/opt/test_modeling_opt.py b/tests/models/opt/test_modeling_opt.py index d4265348cd0..607e6c19124 100644 --- a/tests/models/opt/test_modeling_opt.py +++ b/tests/models/opt/test_modeling_opt.py @@ -341,7 +341,7 @@ def assert_tensors_close(a, b, atol=1e-12, prefix=""): try: if torch.allclose(a, b, atol=atol): return True - raise + raise Exception except Exception: pct_different = (torch.gt((a - b).abs(), atol)).float().mean().item() if a.numel() > 100: diff --git a/tests/models/pegasus/test_modeling_pegasus.py b/tests/models/pegasus/test_modeling_pegasus.py index 09e76aa7dd3..327e55f3894 100644 --- a/tests/models/pegasus/test_modeling_pegasus.py +++ b/tests/models/pegasus/test_modeling_pegasus.py @@ -300,7 +300,7 @@ def assert_tensors_close(a, b, atol=1e-12, prefix=""): try: if torch.allclose(a, b, atol=atol): return True - raise + raise Exception except Exception: pct_different = (torch.gt((a - b).abs(), atol)).float().mean().item() if a.numel() > 100: diff --git a/tests/models/pegasus_x/test_modeling_pegasus_x.py b/tests/models/pegasus_x/test_modeling_pegasus_x.py index fb44c919e1c..34e0828fd6d 100644 --- a/tests/models/pegasus_x/test_modeling_pegasus_x.py +++ b/tests/models/pegasus_x/test_modeling_pegasus_x.py @@ -545,7 +545,7 @@ def assert_tensors_close(a, b, atol=1e-12, prefix=""): try: if torch.allclose(a, b, atol=atol): return True - raise + raise Exception except Exception: pct_different = (torch.gt((a - b).abs(), atol)).float().mean().item() if a.numel() > 100: diff --git a/tests/models/plbart/test_modeling_plbart.py b/tests/models/plbart/test_modeling_plbart.py index 43dc7151202..e76b5e02e63 100644 --- a/tests/models/plbart/test_modeling_plbart.py +++ b/tests/models/plbart/test_modeling_plbart.py @@ -330,7 +330,7 @@ def assert_tensors_close(a, b, atol=1e-12, prefix=""): try: if torch.allclose(a, b, atol=atol): return True - raise + raise Exception except Exception: pct_different = (torch.gt((a - b).abs(), atol)).float().mean().item() if a.numel() > 100: diff --git a/tests/models/rag/test_modeling_rag.py b/tests/models/rag/test_modeling_rag.py index ad0160a42a6..3349fc0dc9f 100644 --- a/tests/models/rag/test_modeling_rag.py +++ b/tests/models/rag/test_modeling_rag.py @@ -75,7 +75,7 @@ def _assert_tensors_equal(a, b, atol=1e-12, prefix=""): try: if torch.allclose(a, b, atol=atol): return True - raise + raise Exception except Exception: msg = f"{a} != {b}" if prefix: