Skip to content

Commit adea7c7

Browse files
committed
Use format specifiers instead of percent format
1 parent 72623d7 commit adea7c7

5 files changed

Lines changed: 46 additions & 29 deletions

File tree

infrahub_sdk/groups.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,18 @@ async def group_add_subscriber(
66
client: InfrahubClient, group: InfrahubNode, subscribers: list[str], branch: str
77
) -> dict:
88
subscribers_str = ["{ id: " + f'"{subscriber}"' + " }" for subscriber in subscribers]
9-
query = """
10-
mutation {
9+
query = f"""
10+
mutation {{
1111
RelationshipAdd(
12-
data: {
13-
id: "%s",
12+
data: {{
13+
id: "{group.id}",
1414
name: "subscribers",
15-
nodes: [ %s ]
16-
}
17-
) {
15+
nodes: [ {", ".join(subscribers_str)} ]
16+
}}
17+
) {{
1818
ok
19-
}
20-
}
21-
""" % (
22-
group.id,
23-
", ".join(subscribers_str),
24-
)
19+
}}
20+
}}
21+
"""
2522

2623
return await client.execute_graphql(query=query, branch_name=branch, tracker="mutation-relationshipadd")

infrahub_sdk/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def deep_merge_dict(dicta: dict, dictb: dict, path: list | None = None) -> dict:
156156
elif a_val == b_val or (a_val is not None and b_val is None):
157157
continue
158158
else:
159-
raise ValueError("Conflict at %s" % ".".join(path + [str(key)]))
159+
raise ValueError(f"Conflict at {'.'.join(path + [str(key)])}")
160160
else:
161161
dicta[key] = dictb[key]
162162
return dicta

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ ignore = [
262262
"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
263263
"SIM110", # Use `return any(getattr(item, resource_field) == resource_id for item in getattr(self, RESOURCE_MAP[resource_type]))` instead of `for` loop
264264
"TC003", # Move standard library import `collections.abc.Iterable` into a type-checking block
265-
"UP031", # Use format specifiers instead of percent format
266265
]
267266

268267

tests/unit/sdk/test_query_analyzer.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -152,22 +152,19 @@ async def test_get_variables(query_01: str, query_04: str, query_05: str, query_
152152
[("[ID]", False), ("[ID]!", True), ("[ID!]", False), ("[ID!]!", True)],
153153
)
154154
async def test_get_nested_variables(var_type: str, var_required: bool) -> None:
155-
query = (
156-
"""
157-
query ($ids: %s){
158-
TestPerson(ids: $ids) {
159-
edges {
160-
node {
161-
name {
155+
query = f"""
156+
query ($ids: {var_type}){{
157+
TestPerson(ids: $ids) {{
158+
edges {{
159+
node {{
160+
name {{
162161
value
163-
}
164-
}
165-
}
166-
}
167-
}
162+
}}
163+
}}
164+
}}
165+
}}
166+
}}
168167
"""
169-
% var_type
170-
)
171168

172169
gqa = GraphQLQueryAnalyzer(query=query)
173170
assert [var.model_dump() for var in gqa.variables] == [

tests/unit/sdk/test_utils.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,30 @@ def test_deep_merge_dict() -> None:
119119
assert deep_merge_dict(f, g) == {"keyA": "foo", "keyB": "bar"}
120120

121121

122+
def test_deep_merge_dict_conflict_scalar_values() -> None:
123+
"""Test that conflicting scalar values raise ValueError."""
124+
a = {"key": 1}
125+
b = {"key": 2}
126+
with pytest.raises(ValueError, match="Conflict at key"):
127+
deep_merge_dict(a, b)
128+
129+
130+
def test_deep_merge_dict_conflict_nested() -> None:
131+
"""Test that nested conflicts include full path in error message."""
132+
a = {"level1": {"level2": {"key": "value_a"}}}
133+
b = {"level1": {"level2": {"key": "value_b"}}}
134+
with pytest.raises(ValueError, match=r"Conflict at level1\.level2\.key"):
135+
deep_merge_dict(a, b)
136+
137+
138+
def test_deep_merge_dict_conflict_type_mismatch() -> None:
139+
"""Test that type mismatches (non-dict/list) raise ValueError."""
140+
a = {"key": "string"}
141+
b = {"key": 123}
142+
with pytest.raises(ValueError, match="Conflict at key"):
143+
deep_merge_dict(a, b)
144+
145+
122146
def test_str_to_bool() -> None:
123147
assert str_to_bool(True) is True
124148
assert str_to_bool(False) is False

0 commit comments

Comments
 (0)