Skip to content

Commit edb911e

Browse files
authored
Merge pull request #302 from opsmill/wvd-fix-prefetch-relationships
Set default value of populate_store argument on client query methods to True
2 parents f55276c + 90d211a commit edb911e

File tree

4 files changed

+72
-50
lines changed

4 files changed

+72
-50
lines changed

changelog/15.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The SDK client query methods (get, filters, all) default behaviour has changed. The query methods will store the retrieved nodes in the internal store by default, where previously this behaviour had to be enabled explicitly using the `populate_store` argument.

docs/docs/python-sdk/guides/store.mdx

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,39 @@ The store can also be used to store Nodes that we retrieve using the client quer
1414

1515
## Storing nodes in the store
1616

17-
Nodes retrieved from Infrahub using a query method, will be stored in the internal store when you set the `populate_store` argument to `True`.
18-
Nodes stored in the store using this method can be retrieved using their `id` as the key in the store.
17+
Nodes retrieved from Infrahub using a the SDK client's query methods, such as the `get` `filters` or `all` method, will be automatically stored in the internal store. Nodes stored in the store using this method can be retrieved using their `id` as the key in the store.
1918

2019
<Tabs groupId="async-sync">
2120
<TabItem value="Async" default>
2221

2322
```python
24-
tag = await client.get(kind="BuiltinTag", name__value="RED", populate_store=True)
23+
tag = await client.get(kind="BuiltinTag", name__value="RED")
24+
```
25+
26+
</TabItem>
27+
<TabItem value="Sync" default>
28+
29+
```python
30+
tag = client.get(kind="BuiltinTag", name__value="RED")
31+
```
32+
33+
</TabItem>
34+
</Tabs>
35+
36+
This behaviour may not be desirable in all scenarios, therefor you can explicitly disable this behaviour by setting the `populate_store` argument to `False` when calling the query methods.
37+
38+
<Tabs groupId="async-sync">
39+
<TabItem value="Async" default>
40+
41+
```python
42+
tag = await client.get(kind="BuiltinTag", name__value="RED", populate_store=False)
2543
```
2644

2745
</TabItem>
2846
<TabItem value="Sync" default>
2947

3048
```python
31-
tag = client.get(kind="BuiltinTag", name__value="RED", populate_store=True)
49+
tag = client.get(kind="BuiltinTag", name__value="RED", populate_store=False)
3250
```
3351

3452
</TabItem>
@@ -42,15 +60,15 @@ You can store nodes in the object store manually using the `set` method. This ha
4260
<TabItem value="Async" default>
4361

4462
```python
45-
tag = await client.get(kind="BuiltinTag", name__value="RED")
63+
tag = await client.get(kind="BuiltinTag", name__value="RED", populate_store=False)
4664
client.store.set(key=tag.name.value, node=tag)
4765
```
4866

4967
</TabItem>
5068
<TabItem value="Sync" default>
5169

5270
```python
53-
tag = client.get(kind="BuiltinTag", name__value="RED")
71+
tag = client.get(kind="BuiltinTag", name__value="RED", populate_store=False)
5472
client.store.set(key=tag.name.value, node=tag)
5573
```
5674

@@ -60,13 +78,13 @@ You can store nodes in the object store manually using the `set` method. This ha
6078
## Retrieving object from the store
6179

6280
Nodes can be retrieved from the internal store using the key that was used to store them.
63-
For nodes that are stored using the `populate_store` argument on a query method, this will be their `id`.
81+
For nodes that are stored by the client's query methods, this will be their `id`.
6482

6583
<Tabs groupId="async-sync">
6684
<TabItem value="Async" default>
6785

6886
```python
69-
tag = await client.get(kind="BuiltinTag", name__value="RED", populate_store=True)
87+
tag = await client.get(kind="BuiltinTag", name__value="RED")
7088
tag_in_store = client.store.get(key=tag.id)
7189
assert tag == tag_in_store
7290
```
@@ -75,7 +93,7 @@ For nodes that are stored using the `populate_store` argument on a query method,
7593
<TabItem value="Sync" default>
7694

7795
```python
78-
tag = client.get(kind="BuiltinTag", name__value="RED", populate_store=True)
96+
tag = client.get(kind="BuiltinTag", name__value="RED")
7997
tag_in_store = client.store.get(key=tag.id)
8098
assert tag == tag_in_store
8199
```
@@ -89,7 +107,7 @@ For nodes that have been added manually to the store, this will be the key that
89107
<TabItem value="Async" default>
90108

91109
```python
92-
tag = await client.get(kind="BuiltinTag", name__value="RED")
110+
tag = await client.get(kind="BuiltinTag", name__value="RED", populate_store=False)
93111
client.store.set(key=tag.name.value, node=tag)
94112
tag_in_store = client.store.get(key=tag.name.value)
95113
assert tag == tag_in_store
@@ -99,7 +117,7 @@ For nodes that have been added manually to the store, this will be the key that
99117
<TabItem value="Sync" default>
100118

101119
```python
102-
tag = client.get(kind="BuiltinTag", name__value="RED")
120+
tag = client.get(kind="BuiltinTag", name__value="RED", populate_store=False)
103121
client.store.set(key=tag.name.value, node=tag)
104122
tag_in_store = client.store.get(key=tag.name.value)
105123
assert tag == tag_in_store
@@ -142,7 +160,7 @@ You can use a custom store, outside of the Infrahub SDK client. Storing or retri
142160
from infrahub_sdk.store import NodeStore
143161
store = NodeStore()
144162

145-
device = await client.get(kind="TestDevice", name__value="atl1-edge1")
163+
device = await client.get(kind="TestDevice", name__value="atl1-edge1", populate_store=False)
146164
store.set(key=device.name.value, node=device)
147165
store.get(key=device.name.value)
148166
```
@@ -154,7 +172,7 @@ You can use a custom store, outside of the Infrahub SDK client. Storing or retri
154172
from infrahub_sdk.store import NodeStoreSync
155173
store = NodeStoreSync()
156174

157-
device = await client.get(kind="TestDevice", name__value="atl1-edge1")
175+
device = await client.get(kind="TestDevice", name__value="atl1-edge1", populate_store=False)
158176
store.set(key=device.name.value, node=device)
159177
store.get(key=device.name.value)
160178
```

infrahub_sdk/client.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ async def get(
457457
hfid: list[str] | None = None,
458458
include: list[str] | None = None,
459459
exclude: list[str] | None = None,
460-
populate_store: bool = False,
460+
populate_store: bool = True,
461461
fragment: bool = False,
462462
prefetch_relationships: bool = False,
463463
property: bool = False,
@@ -611,7 +611,7 @@ async def all(
611611
at: Timestamp | None = None,
612612
branch: str | None = None,
613613
timeout: int | None = None,
614-
populate_store: bool = False,
614+
populate_store: bool = True,
615615
offset: int | None = None,
616616
limit: int | None = None,
617617
include: list[str] | None = None,
@@ -707,7 +707,7 @@ async def filters(
707707
at: Timestamp | None = None,
708708
branch: str | None = None,
709709
timeout: int | None = None,
710-
populate_store: bool = False,
710+
populate_store: bool = True,
711711
offset: int | None = None,
712712
limit: int | None = None,
713713
include: list[str] | None = None,
@@ -1726,7 +1726,7 @@ def all(
17261726
at: Timestamp | None = None,
17271727
branch: str | None = None,
17281728
timeout: int | None = None,
1729-
populate_store: bool = False,
1729+
populate_store: bool = True,
17301730
offset: int | None = None,
17311731
limit: int | None = None,
17321732
include: list[str] | None = None,
@@ -1857,7 +1857,7 @@ def filters(
18571857
at: Timestamp | None = None,
18581858
branch: str | None = None,
18591859
timeout: int | None = None,
1860-
populate_store: bool = False,
1860+
populate_store: bool = True,
18611861
offset: int | None = None,
18621862
limit: int | None = None,
18631863
include: list[str] | None = None,
@@ -2110,7 +2110,7 @@ def get(
21102110
hfid: list[str] | None = None,
21112111
include: list[str] | None = None,
21122112
exclude: list[str] | None = None,
2113-
populate_store: bool = False,
2113+
populate_store: bool = True,
21142114
fragment: bool = False,
21152115
prefetch_relationships: bool = False,
21162116
property: bool = False,

tests/unit/sdk/test_client.py

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,17 @@ async def test_method_get_user_permissions(clients, mock_query_infrahub_user, cl
128128
@pytest.mark.parametrize("client_type", client_types)
129129
async def test_method_all_with_limit(clients, mock_query_repository_page1_2, client_type):
130130
if client_type == "standard":
131-
repos = await clients.standard.all(kind="CoreRepository", limit=3)
131+
repos = await clients.standard.all(kind="CoreRepository", populate_store=False, limit=3)
132132
assert not clients.standard.store._store["CoreRepository"]
133133

134-
repos = await clients.standard.all(kind="CoreRepository", populate_store=True, limit=3)
134+
repos = await clients.standard.all(kind="CoreRepository", limit=3)
135135
assert len(clients.standard.store._store["CoreRepository"]) == 3
136136
else:
137-
repos = clients.sync.all(kind="CoreRepository", limit=3)
137+
repos = clients.sync.all(kind="CoreRepository", populate_store=False, limit=3)
138138
assert not clients.sync.store._store["CoreRepository"]
139139

140-
repos = clients.sync.all(kind="CoreRepository", populate_store=True, limit=3)
140+
repos = clients.sync.all(kind="CoreRepository", limit=3)
141141
assert len(clients.sync.store._store["CoreRepository"]) == 3
142-
143142
assert len(repos) == 3
144143

145144

@@ -148,16 +147,16 @@ async def test_method_all_multiple_pages(
148147
clients, mock_query_repository_page1_2, mock_query_repository_page2_2, client_type
149148
):
150149
if client_type == "standard":
151-
repos = await clients.standard.all(kind="CoreRepository")
150+
repos = await clients.standard.all(kind="CoreRepository", populate_store=False)
152151
assert not clients.standard.store._store["CoreRepository"]
153152

154-
repos = await clients.standard.all(kind="CoreRepository", populate_store=True)
153+
repos = await clients.standard.all(kind="CoreRepository")
155154
assert len(clients.standard.store._store["CoreRepository"]) == 5
156155
else:
157-
repos = clients.sync.all(kind="CoreRepository")
156+
repos = clients.sync.all(kind="CoreRepository", populate_store=False)
158157
assert not clients.sync.store._store["CoreRepository"]
159158

160-
repos = clients.sync.all(kind="CoreRepository", populate_store=True)
159+
repos = clients.sync.all(kind="CoreRepository")
161160
assert len(clients.sync.store._store["CoreRepository"]) == 5
162161

163162
assert len(repos) == 5
@@ -168,16 +167,16 @@ async def test_method_all_batching(
168167
clients, mock_query_location_batch_count, mock_query_location_batch, client_type, use_parallel
169168
):
170169
if client_type == "standard":
171-
locations = await clients.standard.all(kind="BuiltinLocation", parallel=use_parallel)
170+
locations = await clients.standard.all(kind="BuiltinLocation", populate_store=False, parallel=use_parallel)
172171
assert not clients.standard.store._store["BuiltinLocation"]
173172

174-
locations = await clients.standard.all(kind="BuiltinLocation", populate_store=True, parallel=use_parallel)
173+
locations = await clients.standard.all(kind="BuiltinLocation", parallel=use_parallel)
175174
assert len(clients.standard.store._store["BuiltinLocation"]) == 30
176175
else:
177-
locations = clients.sync.all(kind="BuiltinLocation", parallel=use_parallel)
176+
locations = clients.sync.all(kind="BuiltinLocation", populate_store=False, parallel=use_parallel)
178177
assert not clients.sync.store._store["BuiltinLocation"]
179178

180-
locations = clients.sync.all(kind="BuiltinLocation", populate_store=True, parallel=use_parallel)
179+
locations = clients.sync.all(kind="BuiltinLocation", parallel=use_parallel)
181180
assert len(clients.sync.store._store["BuiltinLocation"]) == 30
182181

183182
assert len(locations) == 30
@@ -186,16 +185,16 @@ async def test_method_all_batching(
186185
@pytest.mark.parametrize("client_type", client_types)
187186
async def test_method_all_single_page(clients, mock_query_repository_page1_1, client_type):
188187
if client_type == "standard":
189-
repos = await clients.standard.all(kind="CoreRepository")
188+
repos = await clients.standard.all(kind="CoreRepository", populate_store=False)
190189
assert not clients.standard.store._store["CoreRepository"]
191190

192-
repos = await clients.standard.all(kind="CoreRepository", populate_store=True)
191+
repos = await clients.standard.all(kind="CoreRepository")
193192
assert len(clients.standard.store._store["CoreRepository"]) == 2
194193
else:
195-
repos = clients.sync.all(kind="CoreRepository")
194+
repos = clients.sync.all(kind="CoreRepository", populate_store=False)
196195
assert not clients.sync.store._store["CoreRepository"]
197196

198-
repos = clients.sync.all(kind="CoreRepository", populate_store=True)
197+
repos = clients.sync.all(kind="CoreRepository")
199198
assert len(clients.sync.store._store["CoreRepository"]) == 2
200199

201200
assert len(repos) == 2
@@ -241,20 +240,22 @@ async def test_method_get_by_id(httpx_mock: HTTPXMock, clients, mock_schema_quer
241240
)
242241

243242
if client_type == "standard":
244-
repo = await clients.standard.get(kind="CoreRepository", id=response_id)
243+
repo = await clients.standard.get(kind="CoreRepository", id=response_id, populate_store=False)
245244
assert isinstance(repo, InfrahubNode)
246245
with pytest.raises(NodeNotFoundError):
247246
assert clients.standard.store.get(key=response_id)
248247

249-
repo = await clients.standard.get(kind="CoreRepository", id=response_id, populate_store=True)
248+
repo = await clients.standard.get(kind="CoreRepository", id=response_id)
249+
assert isinstance(repo, InfrahubNode)
250250
assert clients.standard.store.get(key=response_id)
251251
else:
252-
repo = clients.sync.get(kind="CoreRepository", id=response_id)
252+
repo = clients.sync.get(kind="CoreRepository", id=response_id, populate_store=False)
253253
assert isinstance(repo, InfrahubNodeSync)
254254
with pytest.raises(NodeNotFoundError):
255255
assert clients.sync.store.get(key=response_id)
256256

257-
repo = clients.sync.get(kind="CoreRepository", id=response_id, populate_store=True)
257+
repo = clients.sync.get(kind="CoreRepository", id=response_id)
258+
assert isinstance(repo, InfrahubNodeSync)
258259
assert clients.sync.store.get(key=response_id)
259260

260261

@@ -287,20 +288,22 @@ async def test_method_get_by_hfid(httpx_mock: HTTPXMock, clients, mock_schema_qu
287288
)
288289

289290
if client_type == "standard":
290-
repo = await clients.standard.get(kind="CoreRepository", hfid=["infrahub-demo-core"])
291+
repo = await clients.standard.get(kind="CoreRepository", hfid=["infrahub-demo-core"], populate_store=False)
291292
assert isinstance(repo, InfrahubNode)
292293
with pytest.raises(NodeNotFoundError):
293294
assert clients.standard.store.get(key=response_id)
294295

295-
repo = await clients.standard.get(kind="CoreRepository", hfid=["infrahub-demo-core"], populate_store=True)
296+
repo = await clients.standard.get(kind="CoreRepository", hfid=["infrahub-demo-core"])
297+
assert isinstance(repo, InfrahubNode)
296298
assert clients.standard.store.get(key=response_id)
297299
else:
298-
repo = clients.sync.get(kind="CoreRepository", hfid=["infrahub-demo-core"])
300+
repo = clients.sync.get(kind="CoreRepository", hfid=["infrahub-demo-core"], populate_store=False)
299301
assert isinstance(repo, InfrahubNodeSync)
300302
with pytest.raises(NodeNotFoundError):
301303
assert clients.sync.store.get(key="infrahub-demo-core")
302304

303-
repo = clients.sync.get(kind="CoreRepository", hfid=["infrahub-demo-core"], populate_store=True)
305+
repo = clients.sync.get(kind="CoreRepository", hfid=["infrahub-demo-core"])
306+
assert isinstance(repo, InfrahubNodeSync)
304307
assert clients.sync.store.get(key=response_id)
305308

306309

@@ -332,20 +335,20 @@ async def test_method_get_by_default_filter(httpx_mock: HTTPXMock, clients, mock
332335
)
333336

334337
if client_type == "standard":
335-
repo = await clients.standard.get(kind="CoreRepository", id="infrahub-demo-core")
338+
repo = await clients.standard.get(kind="CoreRepository", id="infrahub-demo-core", populate_store=False)
336339
assert isinstance(repo, InfrahubNode)
337340
with pytest.raises(NodeNotFoundError):
338341
assert clients.standard.store.get(key=response_id)
339342

340-
repo = await clients.standard.get(kind="CoreRepository", id="infrahub-demo-core", populate_store=True)
343+
repo = await clients.standard.get(kind="CoreRepository", id="infrahub-demo-core")
341344
assert clients.standard.store.get(key=response_id)
342345
else:
343-
repo = clients.sync.get(kind="CoreRepository", id="infrahub-demo-core")
346+
repo = clients.sync.get(kind="CoreRepository", id="infrahub-demo-core", populate_store=False)
344347
assert isinstance(repo, InfrahubNodeSync)
345348
with pytest.raises(NodeNotFoundError):
346349
assert clients.sync.store.get(key="infrahub-demo-core")
347350

348-
repo = clients.sync.get(kind="CoreRepository", id="infrahub-demo-core", populate_store=True)
351+
repo = clients.sync.get(kind="CoreRepository", id="infrahub-demo-core")
349352
assert clients.sync.store.get(key=response_id)
350353

351354

@@ -431,6 +434,7 @@ async def test_method_filters_many(httpx_mock: HTTPXMock, clients, mock_query_re
431434
"bfae43e8-5ebb-456c-a946-bf64e930710a",
432435
"9486cfce-87db-479d-ad73-07d80ba96a0f",
433436
],
437+
populate_store=False,
434438
)
435439
assert len(repos) == 2
436440
assert not clients.standard.store._store["CoreRepository"]
@@ -441,7 +445,6 @@ async def test_method_filters_many(httpx_mock: HTTPXMock, clients, mock_query_re
441445
"bfae43e8-5ebb-456c-a946-bf64e930710a",
442446
"9486cfce-87db-479d-ad73-07d80ba96a0f",
443447
],
444-
populate_store=True,
445448
)
446449
assert len(clients.standard.store._store["CoreRepository"]) == 2
447450
assert len(repos) == 2
@@ -452,6 +455,7 @@ async def test_method_filters_many(httpx_mock: HTTPXMock, clients, mock_query_re
452455
"bfae43e8-5ebb-456c-a946-bf64e930710a",
453456
"9486cfce-87db-479d-ad73-07d80ba96a0f",
454457
],
458+
populate_store=False,
455459
)
456460
assert len(repos) == 2
457461
assert not clients.sync.store._store["CoreRepository"]
@@ -462,7 +466,6 @@ async def test_method_filters_many(httpx_mock: HTTPXMock, clients, mock_query_re
462466
"bfae43e8-5ebb-456c-a946-bf64e930710a",
463467
"9486cfce-87db-479d-ad73-07d80ba96a0f",
464468
],
465-
populate_store=True,
466469
)
467470
assert len(clients.sync.store._store["CoreRepository"]) == 2
468471
assert len(repos) == 2

0 commit comments

Comments
 (0)