Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions infrahub_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1868,10 +1868,11 @@ async def convert_object_type(
for more information.
"""

if fields_mapping is None:
mapping_dict = {}
else:
mapping_dict = {field_name: model.model_dump(mode="json") for field_name, model in fields_mapping.items()}
mapping_dict = (
{}
if fields_mapping is None
else {field_name: model.model_dump(mode="json") for field_name, model in fields_mapping.items()}
)

branch_name = branch or self.default_branch
response = await self.execute_graphql(
Expand Down Expand Up @@ -3425,10 +3426,11 @@ def convert_object_type(
for more information.
"""

if fields_mapping is None:
mapping_dict = {}
else:
mapping_dict = {field_name: model.model_dump(mode="json") for field_name, model in fields_mapping.items()}
mapping_dict = (
{}
if fields_mapping is None
else {field_name: model.model_dump(mode="json") for field_name, model in fields_mapping.items()}
)

branch_name = branch or self.default_branch
response = self.execute_graphql(
Expand Down
5 changes: 1 addition & 4 deletions infrahub_sdk/ctl/cli_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,7 @@ def transform(
# Run Transform
result = asyncio.run(transform.run(data=data))

if isinstance(result, str):
json_string = result
else:
json_string = ujson.dumps(result, indent=2, sort_keys=True)
json_string = result if isinstance(result, str) else ujson.dumps(result, indent=2, sort_keys=True)

if out:
write_to_file(Path(out), json_string)
Expand Down
5 changes: 1 addition & 4 deletions infrahub_sdk/graphql/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,7 @@ def render_query_block(data: dict, offset: int = 4, indentation: int = 4, conver
elif isinstance(value, dict) and len(value) == 1 and alias_key in value and value[alias_key]:
lines.append(f"{offset_str}{value[alias_key]}: {key}")
elif isinstance(value, dict):
if value.get(alias_key):
key_str = f"{value[alias_key]}: {key}"
else:
key_str = key
key_str = f"{value[alias_key]}: {key}" if value.get(alias_key) else key

if value.get(filters_key):
filters_str = ", ".join(
Expand Down
10 changes: 2 additions & 8 deletions infrahub_sdk/node/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,10 +648,7 @@ def _init_relationships(self, data: dict | RelatedNode | None = None) -> None:
)
if value is not None
}
if peer_id_data:
rel_data = peer_id_data
else:
rel_data = None
rel_data = peer_id_data or None
self._relationship_cardinality_one_data[rel_schema.name] = RelatedNode(
name=rel_schema.name, branch=self._branch, client=self._client, schema=rel_schema, data=rel_data
)
Expand Down Expand Up @@ -1538,10 +1535,7 @@ def _init_relationships(self, data: dict | None = None) -> None:
)
if value is not None
}
if peer_id_data:
rel_data = peer_id_data
else:
rel_data = None
rel_data = peer_id_data or None
self._relationship_cardinality_one_data[rel_schema.name] = RelatedNodeSync(
name=rel_schema.name, branch=self._branch, client=self._client, schema=rel_schema, data=rel_data
)
Expand Down
15 changes: 3 additions & 12 deletions infrahub_sdk/template/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ def get_template(self) -> jinja2.Template:
return self._template_definition

try:
if self.is_string_based:
template = self._get_string_based_template()
else:
template = self._get_file_based_template()
template = self._get_string_based_template() if self.is_string_based else self._get_file_based_template()
except jinja2.TemplateSyntaxError as exc:
self._raise_template_syntax_error(error=exc)
except jinja2.TemplateNotFound as exc:
Expand Down Expand Up @@ -120,10 +117,7 @@ async def render(self, variables: dict[str, Any]) -> str:
errors = _identify_faulty_jinja_code(traceback=traceback)
raise JinjaTemplateUndefinedError(message=exc.message, errors=errors)
except Exception as exc:
if error_message := getattr(exc, "message", None):
message = error_message
else:
message = str(exc)
message = error_message if (error_message := getattr(exc, "message", None)) else str(exc)
raise JinjaTemplateError(message=message or "Unknown template error")

return output
Expand Down Expand Up @@ -188,10 +182,7 @@ def _identify_faulty_jinja_code(traceback: Traceback, nbr_context_lines: int = 3
# Extract only the Jinja related exception
for frame in [frame for frame in traceback.trace.stacks[0].frames if not frame.filename.endswith(".py")]:
code = "".join(linecache.getlines(frame.filename))
if frame.filename == "<template>":
lexer_name = "text"
else:
lexer_name = Traceback._guess_lexer(frame.filename, code)
lexer_name = "text" if frame.filename == "<template>" else Traceback._guess_lexer(frame.filename, code)
syntax = Syntax(
code,
lexer_name,
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ ignore = [
"RUF005", # Consider `[*path, str(key)]` instead of concatenation
"RUF029", # Function is declared `async`, but doesn't `await` or use `async` features.
"S311", # Standard pseudo-random generators are not suitable for cryptographic purposes
"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
"TC003", # Move standard library import `collections.abc.Iterable` into a type-checking block
"UP031", # Use format specifiers instead of percent format
]
Expand Down
10 changes: 2 additions & 8 deletions tests/unit/sdk/test_file_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,7 @@ async def test_file_handler_build_url_with_branch(client_type: str, clients: Bot
"""Test URL building with branch parameter."""
client = getattr(clients, client_type)

if client_type == "standard":
handler = FileHandler(client=client)
else:
handler = FileHandlerSync(client=client)
handler = FileHandler(client=client) if client_type == "standard" else FileHandlerSync(client=client)

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

if client_type == "standard":
handler = FileHandler(client=client)
else:
handler = FileHandlerSync(client=client)
handler = FileHandler(client=client) if client_type == "standard" else FileHandlerSync(client=client)

url = handler._build_url(node_id="node-456", branch=None)
assert url == "http://mock/api/storage/files/node-456"
10 changes: 2 additions & 8 deletions tests/unit/sdk/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,7 @@ async def test_schema_wait_happy_path(clients: BothClients, client_type: list[st

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

client.schema.set_cache(schema_query_01_data, branch="branch1")
assert "branch1" in client.schema.cache
Expand All @@ -257,10 +254,7 @@ async def test_schema_set_cache_dict(clients: BothClients, client_type: list[str
async def test_schema_set_cache_branch_schema(
clients: BothClients, client_type: list[str], schema_query_01_data: dict
) -> None:
if client_type == "standard":
client = clients.standard
else:
client = clients.sync
client = clients.standard if client_type == "standard" else clients.sync

schema = BranchSchema.from_api_response(schema_query_01_data)

Expand Down