Enable some ruff checks for performance and readability (#39383)

* Fix inefficient sequence tests

Signed-off-by: cyy <cyyever@outlook.com>

* Enable PERF102

Signed-off-by: cyy <cyyever@outlook.com>

* Enable PLC1802

Signed-off-by: cyy <cyyever@outlook.com>

* Enable PLC0208

Signed-off-by: cyy <cyyever@outlook.com>

---------

Signed-off-by: cyy <cyyever@outlook.com>
This commit is contained in:
Yuanyuan Chen
2025-07-17 21:21:59 +08:00
committed by GitHub
parent fc700c2a26
commit 60b5471da3
31 changed files with 40 additions and 40 deletions

View File

@@ -125,7 +125,7 @@ class AutoModelTest(unittest.TestCase):
self.assertIsNotNone(model)
self.assertIsInstance(model, BertForPreTraining)
# Only one value should not be initialized and in the missing keys.
for key, value in loading_info.items():
for value in loading_info.values():
self.assertEqual(len(value), 0)
@slow

View File

@@ -70,7 +70,7 @@ class AutoTokenizerTest(unittest.TestCase):
@slow
def test_tokenizer_from_pretrained(self):
for model_name in {"google-bert/bert-base-uncased", "google-bert/bert-base-cased"}:
for model_name in ("google-bert/bert-base-uncased", "google-bert/bert-base-cased"):
tokenizer = AutoTokenizer.from_pretrained(model_name)
self.assertIsNotNone(tokenizer)
self.assertIsInstance(tokenizer, (BertTokenizer, BertTokenizerFast))

View File

@@ -897,7 +897,7 @@ class LukeModelIntegrationTests(unittest.TestCase):
encoding = tokenizer(text, entity_spans=[span], add_prefix_space=True, return_tensors="pt")
# move all values to device
for key, value in encoding.items():
for key in encoding.keys():
encoding[key] = encoding[key].to(torch_device)
outputs = model(**encoding)
@@ -932,7 +932,7 @@ class LukeModelIntegrationTests(unittest.TestCase):
encoding = tokenizer(text, entity_spans=[span], add_prefix_space=True, return_tensors="pt")
# move all values to device
for key, value in encoding.items():
for key in encoding.keys():
encoding[key] = encoding[key].to(torch_device)
outputs = model(**encoding)

View File

@@ -757,7 +757,7 @@ class PeftIntegrationTester(unittest.TestCase, PeftTesterMixin):
model.load_adapter(tmpdirname, is_trainable=True)
for name, module in model.named_modules():
if len(list(module.children())):
if list(module.children()):
# only check leaf modules
continue

View File

@@ -2535,7 +2535,7 @@ class ModelTesterMixin:
shared_ptrs = {k: v for k, v in ptrs.items() if len(v) > 1}
for _, shared_names in shared_ptrs.items():
for shared_names in shared_ptrs.values():
reloaded_ptrs = {reloaded_state[k].data_ptr() for k in shared_names}
self.assertEqual(
len(reloaded_ptrs),

View File

@@ -139,7 +139,7 @@ task_to_pipeline_and_spec_mapping = {
"zero-shot-image-classification": (ZeroShotImageClassificationPipeline, ZeroShotImageClassificationInput),
}
for task, task_info in pipeline_test_mapping.items():
for task_info in pipeline_test_mapping.values():
test = task_info["test"]
task_info["mapping"] = {
"pt": getattr(test, "model_mapping", None),

View File

@@ -96,7 +96,7 @@ class TestImportStructures(unittest.TestCase):
with self.subTest(f"Testing arch {architecture}"):
import_structure = define_import_structure(self.models_path / architecture)
backend_agnostic_import_structure = {}
for requirement, module_object_mapping in import_structure.items():
for module_object_mapping in import_structure.values():
for module, objects in module_object_mapping.items():
if module not in backend_agnostic_import_structure:
backend_agnostic_import_structure[module] = []