From acbed6605678674a0db52f7328c8443415b525b6 Mon Sep 17 00:00:00 2001 From: Patrick Ogenstad Date: Tue, 18 Nov 2025 15:23:06 +0100 Subject: [PATCH] Simplify dictionary lookups --- infrahub_sdk/config.py | 2 +- infrahub_sdk/ctl/graphql.py | 2 +- infrahub_sdk/node/attribute.py | 2 +- infrahub_sdk/node/node.py | 8 ++++---- infrahub_sdk/operation.py | 2 +- infrahub_sdk/schema/__init__.py | 2 +- infrahub_sdk/spec/object.py | 2 +- infrahub_sdk/transfer/importer/json.py | 2 +- pyproject.toml | 1 - tests/integration/test_infrahub_client.py | 4 ++-- 10 files changed, 13 insertions(+), 14 deletions(-) diff --git a/infrahub_sdk/config.py b/infrahub_sdk/config.py index 9a27df82..ac6dbe69 100644 --- a/infrahub_sdk/config.py +++ b/infrahub_sdk/config.py @@ -194,7 +194,7 @@ def clone(self, branch: str | None = None) -> Config: "log": self.log, } covered_keys = list(config.keys()) - for field in Config.model_fields.keys(): + for field in Config.model_fields: if field not in covered_keys: config[field] = deepcopy(getattr(self, field)) diff --git a/infrahub_sdk/ctl/graphql.py b/infrahub_sdk/ctl/graphql.py index 91b90473..363faf59 100644 --- a/infrahub_sdk/ctl/graphql.py +++ b/infrahub_sdk/ctl/graphql.py @@ -180,5 +180,5 @@ async def generate_return_types( generate_result_types(directory=directory, package=package_generator, fragment=module_fragment) - for file_name in package_generator._result_types_files.keys(): + for file_name in package_generator._result_types_files: console.print(f"[green]Generated {file_name} in {directory}") diff --git a/infrahub_sdk/node/attribute.py b/infrahub_sdk/node/attribute.py index ec292bcd..c1dd3111 100644 --- a/infrahub_sdk/node/attribute.py +++ b/infrahub_sdk/node/attribute.py @@ -26,7 +26,7 @@ def __init__(self, name: str, schema: AttributeSchemaAPI, data: Any | dict) -> N self.name = name self._schema = schema - if not isinstance(data, dict) or "value" not in data.keys(): + if not isinstance(data, dict) or "value" not in data: data = {"value": data} self._properties_flag = PROPERTIES_FLAG diff --git a/infrahub_sdk/node/node.py b/infrahub_sdk/node/node.py index 72624467..1b415818 100644 --- a/infrahub_sdk/node/node.py +++ b/infrahub_sdk/node/node.py @@ -284,12 +284,12 @@ def _generate_input_data( # noqa: C901 def _strip_unmodified_dict(data: dict, original_data: dict, variables: dict, item: str) -> None: data_item = data.get(item) if item in original_data and isinstance(original_data[item], dict) and isinstance(data_item, dict): - for item_key in original_data[item].keys(): + for item_key in original_data[item]: for property_name in PROPERTIES_OBJECT: if item_key == property_name and isinstance(original_data[item][property_name], dict): if original_data[item][property_name].get("id"): original_data[item][property_name] = original_data[item][property_name]["id"] - if item_key in data[item].keys(): + if item_key in data[item]: if item_key == "id" and len(data[item].keys()) > 1: # Related nodes typically require an ID. So the ID is only # removed if it's the last key in the current context @@ -335,8 +335,8 @@ def _strip_unmodified(self, data: dict, variables: dict) -> tuple[dict, dict]: elif isinstance(relationship_property, RelationshipManagerBase) and not relationship_property.has_update: data.pop(relationship) - for item in original_data.keys(): - if item in data.keys(): + for item in original_data: + if item in data: if data[item] == original_data[item]: if attr := getattr(self, item, None): # this should never be None, just a safety default value if not isinstance(attr, Attribute) or not attr.value_has_been_mutated: diff --git a/infrahub_sdk/operation.py b/infrahub_sdk/operation.py index 4f431a5e..428c8536 100644 --- a/infrahub_sdk/operation.py +++ b/infrahub_sdk/operation.py @@ -65,7 +65,7 @@ async def process_nodes(self, data: dict) -> None: await self._init_client.schema.all(branch=self.branch_name) for kind in data: - if kind in self._init_client.schema.cache[self.branch_name].nodes.keys(): + if kind in self._init_client.schema.cache[self.branch_name].nodes: for result in data[kind].get("edges", []): node = await self.infrahub_node.from_graphql( client=self._init_client, branch=self.branch_name, data=result diff --git a/infrahub_sdk/schema/__init__.py b/infrahub_sdk/schema/__init__.py index b1360a08..b12dd337 100644 --- a/infrahub_sdk/schema/__init__.py +++ b/infrahub_sdk/schema/__init__.py @@ -122,7 +122,7 @@ def validate(self, data: dict[str, Any]) -> None: SchemaRoot(**data) def validate_data_against_schema(self, schema: MainSchemaTypesAPI, data: dict) -> None: - for key in data.keys(): + for key in data: if key not in schema.relationship_names + schema.attribute_names: identifier = f"{schema.kind}" raise ValidationError( diff --git a/infrahub_sdk/spec/object.py b/infrahub_sdk/spec/object.py index 77a0babf..633d3dee 100644 --- a/infrahub_sdk/spec/object.py +++ b/infrahub_sdk/spec/object.py @@ -230,7 +230,7 @@ async def validate_object( # First validate if all mandatory fields are present for element in schema.mandatory_input_names: - if not any([element in data.keys(), element in context.keys()]): + if not any([element in data, element in context]): errors.append(ObjectValidationError(position=position + [element], message=f"{element} is mandatory")) # Validate if all attributes are valid diff --git a/infrahub_sdk/transfer/importer/json.py b/infrahub_sdk/transfer/importer/json.py index ccd9f4a8..d9e6ad13 100644 --- a/infrahub_sdk/transfer/importer/json.py +++ b/infrahub_sdk/transfer/importer/json.py @@ -109,7 +109,7 @@ async def remove_and_store_optional_relationships(self) -> None: relationship_schema ) - for relationship_name in self.optional_relationships_schemas_by_node_kind[node_kind].keys(): + for relationship_name in self.optional_relationships_schemas_by_node_kind[node_kind]: relationship_value = getattr(node, relationship_name) if isinstance(relationship_value, RelationshipManager): if relationship_value.peer_ids: diff --git a/pyproject.toml b/pyproject.toml index 4a67fbf0..2813e63a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -216,7 +216,6 @@ ignore = [ "SIM110", # Use `return any(getattr(item, resource_field) == resource_id for item in getattr(self, RESOURCE_MAP[resource_type]))` instead of `for` loop "SIM114", # Combine `if` branches using logical `or` operator "SIM117", # Use a single `with` statement with multiple contexts instead of nested `with` statements - "SIM118", # Use `key in dict` instead of `key in dict.keys) "TC003", # Move standard library import `collections.abc.Iterable` into a type-checking block "UP031", # Use format specifiers instead of percent format "UP045", # Use `X | None` for type annotations diff --git a/tests/integration/test_infrahub_client.py b/tests/integration/test_infrahub_client.py index 8c972d0a..f4d1a245 100644 --- a/tests/integration/test_infrahub_client.py +++ b/tests/integration/test_infrahub_client.py @@ -56,8 +56,8 @@ async def test_branch_delete(self, client: InfrahubClient, base_dataset) -> None pre_delete = await client.branch.all() await client.branch.delete(async_branch) post_delete = await client.branch.all() - assert async_branch in pre_delete.keys() - assert async_branch not in post_delete.keys() + assert async_branch in pre_delete + assert async_branch not in post_delete async def test_get_all(self, client: InfrahubClient, base_dataset) -> None: nodes = await client.all(kind=TESTING_CAT)