Skip to content

Commit 62fcd75

Browse files
authored
Merge pull request #814 from opsmill/pog-SIM108
Use ternary operator instead of `if`-`else`-block
2 parents 84682b1 + 9e42f1f commit 62fcd75

8 files changed

Lines changed: 21 additions & 53 deletions

File tree

infrahub_sdk/client.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,10 +1868,11 @@ async def convert_object_type(
18681868
for more information.
18691869
"""
18701870

1871-
if fields_mapping is None:
1872-
mapping_dict = {}
1873-
else:
1874-
mapping_dict = {field_name: model.model_dump(mode="json") for field_name, model in fields_mapping.items()}
1871+
mapping_dict = (
1872+
{}
1873+
if fields_mapping is None
1874+
else {field_name: model.model_dump(mode="json") for field_name, model in fields_mapping.items()}
1875+
)
18751876

18761877
branch_name = branch or self.default_branch
18771878
response = await self.execute_graphql(
@@ -3425,10 +3426,11 @@ def convert_object_type(
34253426
for more information.
34263427
"""
34273428

3428-
if fields_mapping is None:
3429-
mapping_dict = {}
3430-
else:
3431-
mapping_dict = {field_name: model.model_dump(mode="json") for field_name, model in fields_mapping.items()}
3429+
mapping_dict = (
3430+
{}
3431+
if fields_mapping is None
3432+
else {field_name: model.model_dump(mode="json") for field_name, model in fields_mapping.items()}
3433+
)
34323434

34333435
branch_name = branch or self.default_branch
34343436
response = self.execute_graphql(

infrahub_sdk/ctl/cli_commands.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,7 @@ def transform(
350350
# Run Transform
351351
result = asyncio.run(transform.run(data=data))
352352

353-
if isinstance(result, str):
354-
json_string = result
355-
else:
356-
json_string = ujson.dumps(result, indent=2, sort_keys=True)
353+
json_string = result if isinstance(result, str) else ujson.dumps(result, indent=2, sort_keys=True)
357354

358355
if out:
359356
write_to_file(Path(out), json_string)

infrahub_sdk/graphql/renderers.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,7 @@ def render_query_block(data: dict, offset: int = 4, indentation: int = 4, conver
149149
elif isinstance(value, dict) and len(value) == 1 and alias_key in value and value[alias_key]:
150150
lines.append(f"{offset_str}{value[alias_key]}: {key}")
151151
elif isinstance(value, dict):
152-
if value.get(alias_key):
153-
key_str = f"{value[alias_key]}: {key}"
154-
else:
155-
key_str = key
152+
key_str = f"{value[alias_key]}: {key}" if value.get(alias_key) else key
156153

157154
if value.get(filters_key):
158155
filters_str = ", ".join(

infrahub_sdk/node/node.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -648,10 +648,7 @@ def _init_relationships(self, data: dict | RelatedNode | None = None) -> None:
648648
)
649649
if value is not None
650650
}
651-
if peer_id_data:
652-
rel_data = peer_id_data
653-
else:
654-
rel_data = None
651+
rel_data = peer_id_data or None
655652
self._relationship_cardinality_one_data[rel_schema.name] = RelatedNode(
656653
name=rel_schema.name, branch=self._branch, client=self._client, schema=rel_schema, data=rel_data
657654
)
@@ -1538,10 +1535,7 @@ def _init_relationships(self, data: dict | None = None) -> None:
15381535
)
15391536
if value is not None
15401537
}
1541-
if peer_id_data:
1542-
rel_data = peer_id_data
1543-
else:
1544-
rel_data = None
1538+
rel_data = peer_id_data or None
15451539
self._relationship_cardinality_one_data[rel_schema.name] = RelatedNodeSync(
15461540
name=rel_schema.name, branch=self._branch, client=self._client, schema=rel_schema, data=rel_data
15471541
)

infrahub_sdk/template/__init__.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ def get_template(self) -> jinja2.Template:
6464
return self._template_definition
6565

6666
try:
67-
if self.is_string_based:
68-
template = self._get_string_based_template()
69-
else:
70-
template = self._get_file_based_template()
67+
template = self._get_string_based_template() if self.is_string_based else self._get_file_based_template()
7168
except jinja2.TemplateSyntaxError as exc:
7269
self._raise_template_syntax_error(error=exc)
7370
except jinja2.TemplateNotFound as exc:
@@ -127,10 +124,7 @@ async def render(self, variables: dict[str, Any]) -> str:
127124
errors = _identify_faulty_jinja_code(traceback=traceback)
128125
raise JinjaTemplateUndefinedError(message=exc.message, errors=errors)
129126
except Exception as exc:
130-
if error_message := getattr(exc, "message", None):
131-
message = error_message
132-
else:
133-
message = str(exc)
127+
message = error_message if (error_message := getattr(exc, "message", None)) else str(exc)
134128
raise JinjaTemplateError(message=message or "Unknown template error")
135129

136130
return output
@@ -195,10 +189,7 @@ def _identify_faulty_jinja_code(traceback: Traceback, nbr_context_lines: int = 3
195189
# Extract only the Jinja related exception
196190
for frame in [frame for frame in traceback.trace.stacks[0].frames if not frame.filename.endswith(".py")]:
197191
code = "".join(linecache.getlines(frame.filename))
198-
if frame.filename == "<template>":
199-
lexer_name = "text"
200-
else:
201-
lexer_name = Traceback._guess_lexer(frame.filename, code)
192+
lexer_name = "text" if frame.filename == "<template>" else Traceback._guess_lexer(frame.filename, code)
202193
syntax = Syntax(
203194
code,
204195
lexer_name,

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,6 @@ ignore = [
248248
"RUF029", # Function is declared `async`, but doesn't `await` or use `async` features.
249249
"RUF067", # `__init__` module should only contain docstrings and re-exports
250250
"S311", # Standard pseudo-random generators are not suitable for cryptographic purposes
251-
"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
252251
"TC003", # Move standard library import `collections.abc.Iterable` into a type-checking block
253252
"UP031", # Use format specifiers instead of percent format
254253
]

tests/unit/sdk/test_file_handler.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,7 @@ async def test_file_handler_build_url_with_branch(client_type: str, clients: Bot
288288
"""Test URL building with branch parameter."""
289289
client = getattr(clients, client_type)
290290

291-
if client_type == "standard":
292-
handler = FileHandler(client=client)
293-
else:
294-
handler = FileHandlerSync(client=client)
291+
handler = FileHandler(client=client) if client_type == "standard" else FileHandlerSync(client=client)
295292

296293
url = handler._build_url(node_id="node-123", branch="feature-branch")
297294
assert url == "http://mock/api/storage/files/node-123?branch=feature-branch"
@@ -302,10 +299,7 @@ async def test_file_handler_build_url_without_branch(client_type: str, clients:
302299
"""Test URL building without branch parameter."""
303300
client = getattr(clients, client_type)
304301

305-
if client_type == "standard":
306-
handler = FileHandler(client=client)
307-
else:
308-
handler = FileHandlerSync(client=client)
302+
handler = FileHandler(client=client) if client_type == "standard" else FileHandlerSync(client=client)
309303

310304
url = handler._build_url(node_id="node-456", branch=None)
311305
assert url == "http://mock/api/storage/files/node-456"

tests/unit/sdk/test_schema.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,7 @@ async def test_schema_wait_happy_path(clients: BothClients, client_type: list[st
243243

244244
@pytest.mark.parametrize("client_type", client_types)
245245
async def test_schema_set_cache_dict(clients: BothClients, client_type: list[str], schema_query_01_data: dict) -> None:
246-
if client_type == "standard":
247-
client = clients.standard
248-
else:
249-
client = clients.sync
246+
client = clients.standard if client_type == "standard" else clients.sync
250247

251248
client.schema.set_cache(schema_query_01_data, branch="branch1")
252249
assert "branch1" in client.schema.cache
@@ -257,10 +254,7 @@ async def test_schema_set_cache_dict(clients: BothClients, client_type: list[str
257254
async def test_schema_set_cache_branch_schema(
258255
clients: BothClients, client_type: list[str], schema_query_01_data: dict
259256
) -> None:
260-
if client_type == "standard":
261-
client = clients.standard
262-
else:
263-
client = clients.sync
257+
client = clients.standard if client_type == "standard" else clients.sync
264258

265259
schema = BranchSchema.from_api_response(schema_query_01_data)
266260

0 commit comments

Comments
 (0)