Skip to content

Commit 4acf23a

Browse files
committed
tests/docs(query_list): Document object lookups
1 parent fe98727 commit 4acf23a

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

src/libvcs/_internal/query_list.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ def parse_lookup(obj: Mapping[str, Any], path: str, lookup: str) -> Optional[Any
4949
5050
>>> parse_lookup({ "food": "red apple" }, "food__istartswith", "__istartswith")
5151
'red apple'
52+
53+
It can also look up objects:
54+
55+
>>> from dataclasses import dataclass
56+
57+
>>> @dataclass()
58+
... class Inventory:
59+
... food: str
60+
61+
>>> item = Inventory(food="red apple")
62+
63+
>>> item
64+
Inventory(food='red apple')
65+
66+
>>> parse_lookup(item, "food__istartswith", "__istartswith")
67+
'red apple'
5268
"""
5369
try:
5470
if isinstance(path, str) and isinstance(lookup, str) and path.endswith(lookup):
@@ -262,6 +278,89 @@ class QueryList(list[T]):
262278
'Elmhurst'
263279
>>> query.filter(foods__fruit__in="orange")[0]['city']
264280
'Tampa'
281+
282+
Examples of object lookups:
283+
284+
>>> from typing import Any
285+
>>> from dataclasses import dataclass, field
286+
287+
>>> @dataclass()
288+
... class Restaurant:
289+
... place: str
290+
... city: str
291+
... state: str
292+
... foods: dict[str, Any]
293+
294+
>>> restaurant = Restaurant(
295+
... place="Largo",
296+
... city="Tampa",
297+
... state="Florida",
298+
... foods={
299+
... "fruit": ["banana", "orange"], "breakfast": "cereal"
300+
... }
301+
... )
302+
303+
>>> restaurant
304+
Restaurant(place='Largo',
305+
city='Tampa',
306+
state='Florida',
307+
foods={'fruit': ['banana', 'orange'], 'breakfast': 'cereal'})
308+
309+
>>> query = QueryList([restaurant])
310+
311+
>>> query.filter(foods__fruit__in="banana")
312+
[Restaurant(place='Largo',
313+
city='Tampa',
314+
state='Florida',
315+
foods={'fruit': ['banana', 'orange'], 'breakfast': 'cereal'})]
316+
317+
>>> query.filter(foods__fruit__in="banana")[0].city
318+
'Tampa'
319+
320+
Example of deeper object lookups:
321+
322+
>>> from typing import Optional
323+
>>> from dataclasses import dataclass, field
324+
325+
>>> @dataclass()
326+
... class Menu:
327+
... fruit: list[str] = field(default_factory=list)
328+
... breakfast: Optional[str] = None
329+
330+
331+
>>> @dataclass()
332+
... class Restaurant:
333+
... place: str
334+
... city: str
335+
... state: str
336+
... menu: Menu = field(default_factory=Menu)
337+
338+
339+
>>> restaurant = Restaurant(
340+
... place="Largo",
341+
... city="Tampa",
342+
... state="Florida",
343+
... menu=Menu(
344+
... fruit=["banana", "orange"], breakfast="cereal"
345+
... )
346+
... )
347+
348+
>>> restaurant
349+
Restaurant(place='Largo',
350+
city='Tampa',
351+
state='Florida',
352+
menu=Menu(fruit=['banana', 'orange'], breakfast='cereal'))
353+
354+
>>> query = QueryList([restaurant])
355+
356+
>>> query.filter(menu__fruit__in="banana")
357+
[Restaurant(place='Largo',
358+
city='Tampa',
359+
state='Florida',
360+
menu=Menu(fruit=['banana', 'orange'], breakfast='cereal'))]
361+
362+
>>> query.filter(menu__fruit__in="banana")[0].city
363+
'Tampa'
265364
"""
266365

267366
data: Sequence[T]

0 commit comments

Comments
 (0)