Skip to content

Commit 5858284

Browse files
authored
Merge pull request #757 from opsmill/pog-avoid-nested-if
Avoid nested chained if statements
2 parents 2d1b586 + 76e9b68 commit 5858284

5 files changed

Lines changed: 27 additions & 21 deletions

File tree

infrahub_sdk/graphql/plugin.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ class FutureAnnotationPlugin(Plugin):
1414
def insert_future_annotation(module: ast.Module) -> ast.Module:
1515
# First check if the future annotation is already present
1616
for item in module.body:
17-
if isinstance(item, ast.ImportFrom) and item.module == "__future__":
18-
if any(alias.name == "annotations" for alias in item.names):
19-
return module
17+
if (
18+
isinstance(item, ast.ImportFrom)
19+
and item.module == "__future__"
20+
and any(alias.name == "annotations" for alias in item.names)
21+
):
22+
return module
2023

2124
module.body.insert(0, ast.ImportFrom(module="__future__", names=[ast.alias(name="annotations")], level=0))
2225
return module

infrahub_sdk/node/node.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,12 @@ def _strip_unmodified_dict(data: dict, original_data: dict, variables: dict, ite
313313
if item in original_data and isinstance(original_data[item], dict) and isinstance(data_item, dict):
314314
for item_key in original_data[item]:
315315
for property_name in PROPERTIES_OBJECT:
316-
if item_key == property_name and isinstance(original_data[item][property_name], dict):
317-
if original_data[item][property_name].get("id"):
318-
original_data[item][property_name] = original_data[item][property_name]["id"]
316+
if (
317+
item_key == property_name
318+
and isinstance(original_data[item][property_name], dict)
319+
and original_data[item][property_name].get("id")
320+
):
321+
original_data[item][property_name] = original_data[item][property_name]["id"]
319322
if item_key in data[item]:
320323
if item_key == "id" and len(data[item].keys()) > 1:
321324
# Related nodes typically require an ID. So the ID is only
@@ -365,9 +368,12 @@ def _strip_unmodified(self, data: dict, variables: dict) -> tuple[dict, dict]:
365368
for item in original_data:
366369
if item in data:
367370
if data[item] == original_data[item]:
368-
if attr := getattr(self, item, None): # this should never be None, just a safety default value
369-
if not isinstance(attr, Attribute) or not attr.value_has_been_mutated:
370-
data.pop(item)
371+
if (
372+
attr := getattr(self, item, None)
373+
) and ( # this should never be None, just a safety default value
374+
not isinstance(attr, Attribute) or not attr.value_has_been_mutated
375+
):
376+
data.pop(item)
371377
continue
372378
if isinstance(original_data[item], dict):
373379
self._strip_unmodified_dict(data=data, original_data=original_data, variables=variables, item=item)

infrahub_sdk/spec/object.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,14 +281,13 @@ async def validate_object(
281281
)
282282
)
283283

284-
if key in schema.attribute_names:
285-
if not isinstance(value, (str, int, float, bool, list, dict)):
286-
errors.append(
287-
ObjectValidationError(
288-
position=position + [key],
289-
message=f"{key} must be a string, int, float, bool, list, or dict",
290-
)
284+
if key in schema.attribute_names and not isinstance(value, (str, int, float, bool, list, dict)):
285+
errors.append(
286+
ObjectValidationError(
287+
position=position + [key],
288+
message=f"{key} must be a string, int, float, bool, list, or dict",
291289
)
290+
)
292291

293292
if key in schema.relationship_names:
294293
rel_info = await get_relationship_info(

infrahub_sdk/transfer/importer/json.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,9 @@ async def remove_and_store_optional_relationships(self) -> None:
115115
if relationship_value.peer_ids:
116116
self.optional_relationships_by_node[node.id][relationship_name] = relationship_value
117117
setattr(node, relationship_name, None)
118-
elif isinstance(relationship_value, RelatedNode):
119-
if relationship_value.id:
120-
self.optional_relationships_by_node[node.id][relationship_name] = relationship_value
121-
setattr(node, relationship_name, None)
118+
elif isinstance(relationship_value, RelatedNode) and relationship_value.id:
119+
self.optional_relationships_by_node[node.id][relationship_name] = relationship_value
120+
setattr(node, relationship_name, None)
122121

123122
async def update_optional_relationships(self) -> None:
124123
update_batch = await self.client.create_batch(return_exceptions=True)

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,6 @@ ignore = [
258258
"RUF029", # Function is declared `async`, but doesn't `await` or use `async` features.
259259
"S311", # Standard pseudo-random generators are not suitable for cryptographic purposes
260260
"S701", # By default, jinja2 sets `autoescape` to `False`. Consider using `autoescape=True`
261-
"SIM102", # Use a single `if` statement instead of nested `if` statements
262261
"SIM105", # Use `contextlib.suppress(KeyError)` instead of `try`-`except`-`pass`
263262
"SIM108", # Use ternary operator `key_str = f"{value[ALIAS_KEY]}: {key}" if ALIAS_KEY in value and value[ALIAS_KEY] else key` instead of `if`-`else`-block
264263
"SIM110", # Use `return any(getattr(item, resource_field) == resource_id for item in getattr(self, RESOURCE_MAP[resource_type]))` instead of `for` loop

0 commit comments

Comments
 (0)