Apply several ruff SIM rules (#37283)
* Apply ruff SIM118 fix Signed-off-by: cyy <cyyever@outlook.com> * Apply ruff SIM910 fix Signed-off-by: cyy <cyyever@outlook.com> * Apply ruff SIM101 fix Signed-off-by: cyy <cyyever@outlook.com> * Format code Signed-off-by: cyy <cyyever@outlook.com> * More fixes Signed-off-by: cyy <cyyever@outlook.com> --------- Signed-off-by: cyy <cyyever@outlook.com>
This commit is contained in:
@@ -37,7 +37,7 @@ from tests.test_pipeline_mixin import pipeline_test_mapping
|
||||
|
||||
|
||||
PIPELINE_TEST_MAPPING = {}
|
||||
for task in pipeline_test_mapping.keys():
|
||||
for task in pipeline_test_mapping:
|
||||
PIPELINE_TEST_MAPPING[task] = {"pt": None, "tf": None}
|
||||
|
||||
|
||||
|
||||
@@ -731,8 +731,8 @@ def is_copy_consistent(
|
||||
|
||||
# Below, we change some names in `theoretical_code_blocks` and `observed_code_blocks`. These mappings map the
|
||||
# original names to the modified names: this is used to restore the original order of the code blocks.
|
||||
name_mappings_1 = {k: k for k in theoretical_code_blocks.keys()}
|
||||
name_mappings_2 = {k: k for k in observed_code_blocks.keys()}
|
||||
name_mappings_1 = {k: k for k in theoretical_code_blocks}
|
||||
name_mappings_2 = {k: k for k in observed_code_blocks}
|
||||
|
||||
# Update code blocks' name and content:
|
||||
# If `"# Ignore copy"` is found in a block of the observed code:
|
||||
|
||||
@@ -913,7 +913,7 @@ def match_docstring_with_signature(obj: Any) -> Optional[tuple[str, str]]:
|
||||
missing = set(signature.keys()) - set(old_arguments)
|
||||
new_param_docs.extend([arguments[name] for name in missing if len(arguments[name]) > 0])
|
||||
else:
|
||||
new_param_docs = [arguments[name] for name in signature.keys() if len(arguments[name]) > 0]
|
||||
new_param_docs = [arguments[name] for name in signature if len(arguments[name]) > 0]
|
||||
new_doc_arg = "\n".join(new_param_docs)
|
||||
|
||||
return old_doc_arg, new_doc_arg
|
||||
|
||||
@@ -199,8 +199,7 @@ def check_dummies(overwrite: bool = False):
|
||||
# Locate actual dummy modules and read their content.
|
||||
path = os.path.join(PATH_TO_TRANSFORMERS, "utils")
|
||||
dummy_file_paths = {
|
||||
backend: os.path.join(path, f"dummy_{short_names.get(backend, backend)}_objects.py")
|
||||
for backend in dummy_files.keys()
|
||||
backend: os.path.join(path, f"dummy_{short_names.get(backend, backend)}_objects.py") for backend in dummy_files
|
||||
}
|
||||
|
||||
actual_dummies = {}
|
||||
@@ -212,7 +211,7 @@ def check_dummies(overwrite: bool = False):
|
||||
actual_dummies[backend] = ""
|
||||
|
||||
# Compare actual with what they should be.
|
||||
for backend in dummy_files.keys():
|
||||
for backend in dummy_files:
|
||||
if dummy_files[backend] != actual_dummies[backend]:
|
||||
if overwrite:
|
||||
print(
|
||||
|
||||
@@ -257,7 +257,7 @@ def analyze_results(import_dict_objects: dict[str, list[str]], type_hint_objects
|
||||
|
||||
errors = []
|
||||
# Find all errors.
|
||||
for key in import_dict_objects.keys():
|
||||
for key in import_dict_objects:
|
||||
# Duplicate imports in any half.
|
||||
duplicate_imports = find_duplicates(import_dict_objects[key])
|
||||
if duplicate_imports:
|
||||
|
||||
@@ -71,7 +71,7 @@ def compare_files(modular_file_path, show_diff=True):
|
||||
# Generate the expected modeling content
|
||||
generated_modeling_content = convert_modular_file(modular_file_path)
|
||||
diff = 0
|
||||
for file_type in generated_modeling_content.keys():
|
||||
for file_type in generated_modeling_content:
|
||||
diff += process_file(modular_file_path, generated_modeling_content, file_type, show_diff)
|
||||
return diff
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ def topological_sort(dependencies: dict) -> list[list[str]]:
|
||||
"""
|
||||
|
||||
# Nodes are the name of the models to convert (we only add those to the graph)
|
||||
nodes = {node.rsplit("modular_", 1)[1].replace(".py", "") for node in dependencies.keys()}
|
||||
nodes = {node.rsplit("modular_", 1)[1].replace(".py", "") for node in dependencies}
|
||||
# This will be a graph from models to convert, to models to convert that should be converted before (as they are a dependency)
|
||||
graph = {}
|
||||
name_mapping = {}
|
||||
|
||||
@@ -424,7 +424,7 @@ def get_tiny_config(config_class, model_class=None, **model_tester_kwargs):
|
||||
# `text_model_tester` via `text_kwargs`. The same trick is also necessary for `Flava`.
|
||||
|
||||
if "vocab_size" in model_tester_kwargs:
|
||||
if "text_kwargs" in inspect.signature(model_tester_class.__init__).parameters.keys():
|
||||
if "text_kwargs" in inspect.signature(model_tester_class.__init__).parameters:
|
||||
vocab_size = model_tester_kwargs.pop("vocab_size")
|
||||
model_tester_kwargs["text_kwargs"] = {"vocab_size": vocab_size}
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ def get_module_source_from_name(module_name: str) -> str:
|
||||
|
||||
def preserve_case_replace(text, patterns: dict, default_name: str):
|
||||
# Create a regex pattern to match all variations
|
||||
regex_pattern = "|".join(re.escape(key) for key in patterns.keys())
|
||||
regex_pattern = "|".join(re.escape(key) for key in patterns)
|
||||
compiled_regex = re.compile(f"(?<![a-z0-9])({regex_pattern})(.|$)", re.IGNORECASE | re.DOTALL)
|
||||
|
||||
def replace(match):
|
||||
@@ -445,7 +445,7 @@ def find_all_dependencies(
|
||||
# Add the dependencies
|
||||
all_dependencies.add(current)
|
||||
all_dependencies_with_parent += [(current, parents[current])]
|
||||
if current in dependency_mapping.keys():
|
||||
if current in dependency_mapping:
|
||||
# Update dependency queue
|
||||
dependency_queue.extend(dependency_mapping[current])
|
||||
parents.update(dict.fromkeys(dependency_mapping[current], current))
|
||||
@@ -670,7 +670,7 @@ class ModuleMapper(CSTVisitor, ABC):
|
||||
the recursive mapping, i.e. `recursive_dependencies = {"test": {"bar", "foo"}, "bar": {"foo}}`.
|
||||
"""
|
||||
recursive_dependencies = {}
|
||||
for object_name in self.object_dependency_mapping.keys():
|
||||
for object_name in self.object_dependency_mapping:
|
||||
all_dependencies = find_all_dependencies(self.object_dependency_mapping, start_entity=object_name)
|
||||
recursive_dependencies[object_name] = all_dependencies
|
||||
return recursive_dependencies
|
||||
@@ -682,7 +682,7 @@ class ModuleMapper(CSTVisitor, ABC):
|
||||
new_dependencies = dependencies.copy()
|
||||
# Go through the set of dependencies
|
||||
for dep in tuple(dependencies):
|
||||
if dep in self.object_recursive_dependency_mapping.keys():
|
||||
if dep in self.object_recursive_dependency_mapping:
|
||||
new_dependencies.update(self.object_recursive_dependency_mapping[dep])
|
||||
return new_dependencies
|
||||
|
||||
@@ -790,9 +790,7 @@ class ModelFileMapper(ModuleMapper):
|
||||
"""
|
||||
# Add/overwrite all needed function nodes and dependencies
|
||||
self.functions.update(functions)
|
||||
self.object_dependency_mapping.update(
|
||||
{obj: dep for obj, dep in object_mapping.items() if obj in functions.keys()}
|
||||
)
|
||||
self.object_dependency_mapping.update({obj: dep for obj, dep in object_mapping.items() if obj in functions})
|
||||
# Add them to global nodes
|
||||
self.global_nodes.update(self.functions)
|
||||
|
||||
@@ -1150,7 +1148,7 @@ def get_needed_imports(body: dict[str, dict], all_imports: list[cst.CSTNode]) ->
|
||||
import_ref_count[name] = max(ref_count, import_ref_count[name])
|
||||
# Similar imports may be redefined, and only used between their 1st and 2nd definition so if we already have
|
||||
# a ref count > 0 at any point, the imports is actually used
|
||||
unused_imports = {name for name, count in import_ref_count.items() if count <= 0 or name in body.keys()}
|
||||
unused_imports = {name for name, count in import_ref_count.items() if count <= 0 or name in body}
|
||||
|
||||
imports_to_keep = []
|
||||
# We need to keep track of which names were already imported, because some import may be duplicated from multiple sources
|
||||
@@ -1494,8 +1492,8 @@ class ModularFileMapper(ModuleMapper):
|
||||
final_name_mapping[file] = get_lowercase_name(final_name)
|
||||
|
||||
# Check we are not missing imported files
|
||||
for file in self.model_specific_modules.keys():
|
||||
if file not in final_name_mapping.keys():
|
||||
for file in self.model_specific_modules:
|
||||
if file not in final_name_mapping:
|
||||
final_name_mapping[file] = self.model_name
|
||||
|
||||
return final_name_mapping
|
||||
@@ -1614,7 +1612,7 @@ def get_class_node_and_dependencies(
|
||||
nodes_to_add = {
|
||||
dep: (relative_dependency_order[dep], modular_mapper.global_nodes[dep])
|
||||
for dep in all_dependencies_to_add
|
||||
if dep not in file_to_update.keys()
|
||||
if dep not in file_to_update
|
||||
}
|
||||
|
||||
# Add the class node itself to the nodes to add
|
||||
@@ -1710,7 +1708,7 @@ def convert_modular_file(modular_file):
|
||||
|
||||
|
||||
def save_modeling_file(modular_file, converted_file):
|
||||
for file_type in converted_file.keys():
|
||||
for file_type in converted_file:
|
||||
file_name_prefix = file_type.split("*")[0]
|
||||
file_name_suffix = file_type.split("*")[-1] if "*" in file_type else ""
|
||||
new_file_name = modular_file.replace("modular_", f"{file_name_prefix}_").replace(
|
||||
|
||||
@@ -1199,7 +1199,7 @@ if __name__ == "__main__":
|
||||
|
||||
unclassified_model_failures = []
|
||||
|
||||
for matrix_name in matrix_job_results.keys():
|
||||
for matrix_name in matrix_job_results:
|
||||
for artifact_path_dict in available_artifacts[f"{report_name_prefix}_{matrix_name}_test_reports"].paths:
|
||||
path = artifact_path_dict["path"]
|
||||
artifact_gpu = artifact_path_dict["gpu"]
|
||||
@@ -1323,10 +1323,10 @@ if __name__ == "__main__":
|
||||
"failures": {},
|
||||
"job_link": {},
|
||||
}
|
||||
for key in additional_files.keys()
|
||||
for key in additional_files
|
||||
}
|
||||
|
||||
for key in additional_results.keys():
|
||||
for key in additional_results:
|
||||
# If a whole suite of test fails, the artifact isn't available.
|
||||
if additional_files[key] not in available_artifacts:
|
||||
additional_results[key]["error"] = True
|
||||
|
||||
@@ -69,7 +69,7 @@ if __name__ == "__main__":
|
||||
raw_test_collection_map[file_dir].append(file)
|
||||
|
||||
refined_test_collection_map = {}
|
||||
for file_dir in raw_test_collection_map.keys():
|
||||
for file_dir in raw_test_collection_map:
|
||||
if file_dir in ["docs/source/en/model_doc", "docs/source/en/tasks"]:
|
||||
for file in raw_test_collection_map[file_dir]:
|
||||
refined_test_collection_map[file] = file
|
||||
|
||||
Reference in New Issue
Block a user