Skip to content

Commit 4c3b939

Browse files
authored
Merge pull request #1671 from jstol/feature/extract-nested-object-array-props-from-data-models
Add support for extracting nested object array properties from data models
2 parents fcbe47e + fe35ef8 commit 4c3b939

7 files changed

Lines changed: 78 additions & 31 deletions

File tree

integration/test_collection_nested.py

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -516,34 +516,77 @@ def test_nested_return_specific_properties(
516516
assert out.properties["nested"] == expected
517517

518518

519-
def test_nested_return_generic_properties(collection_factory: CollectionFactory) -> None:
520-
class Child(TypedDict):
521-
name: str
522-
age: int
519+
class Child(TypedDict):
520+
name: str
521+
age: int
523522

524-
class Parent(TypedDict):
525-
child: Nested[Child]
526523

527-
collection = collection_factory(
528-
properties=[
529-
Property(
530-
name="child",
531-
data_type=DataType.OBJECT,
532-
nested_properties=[
533-
Property(
534-
name="name",
535-
data_type=DataType.TEXT,
536-
),
537-
Property(
538-
name="age",
539-
data_type=DataType.INT,
540-
),
524+
@pytest.mark.parametrize(
525+
"data_model_cls,data_object,properties",
526+
[
527+
(
528+
Child,
529+
{"name": "Timmy", "age": 10},
530+
[
531+
Property(name="name", data_type=DataType.TEXT),
532+
Property(name="age", data_type=DataType.INT),
533+
],
534+
),
535+
(
536+
TypedDict("ParentWithChild", {"name": str, "age": int, "child": Nested[Child]}),
537+
{
538+
"name": "Bob",
539+
"age": 39,
540+
"child": {"name": "Timmy", "age": 10},
541+
},
542+
[
543+
Property(name="name", data_type=DataType.TEXT),
544+
Property(name="age", data_type=DataType.INT),
545+
Property(
546+
name="child",
547+
data_type=DataType.OBJECT,
548+
nested_properties=[
549+
Property(name="name", data_type=DataType.TEXT),
550+
Property(name="age", data_type=DataType.INT),
551+
],
552+
),
553+
],
554+
),
555+
(
556+
TypedDict(
557+
"ParentWithChildList", {"name": str, "age": int, "children": Nested[list[Child]]}
558+
),
559+
{
560+
"name": "Bob",
561+
"age": 39,
562+
"children": [
563+
{"name": "Timmy", "age": 10},
564+
{"name": "Daisy", "age": 8},
541565
],
542-
)
543-
],
544-
data_model_properties=Parent,
566+
},
567+
[
568+
Property(name="name", data_type=DataType.TEXT),
569+
Property(name="age", data_type=DataType.INT),
570+
Property(
571+
name="children",
572+
data_type=DataType.OBJECT_ARRAY,
573+
nested_properties=[
574+
Property(name="name", data_type=DataType.TEXT),
575+
Property(name="age", data_type=DataType.INT),
576+
],
577+
),
578+
],
579+
),
580+
],
581+
)
582+
def test_nested_return_generic_properties(
583+
collection_factory, data_model_cls, data_object, properties
584+
):
585+
collection = collection_factory(
586+
properties=properties,
587+
data_model_properties=data_model_cls,
545588
)
589+
collection.data.insert(data_object)
590+
results = collection.query.fetch_objects(return_properties=data_model_cls)
546591

547-
collection.data.insert(Parent(child=Child(name="Timmy", age=10)))
548-
results = collection.query.fetch_objects(return_properties=Parent)
549-
assert results.objects[0].properties["child"] == {"name": "Timmy", "age": 10}
592+
assert results.objects[0].properties == data_object

weaviate/collections/aggregations/hybrid/executor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
from weaviate.connect import executor
1313
from weaviate.connect.v4 import ConnectionType
1414
from weaviate.exceptions import WeaviateUnsupportedFeatureError
15-
from weaviate.types import NUMBER
1615
from weaviate.proto.v1 import aggregate_pb2
16+
from weaviate.types import NUMBER
1717

1818

1919
class _HybridExecutor(Generic[ConnectionType], _BaseExecutor[ConnectionType]):

weaviate/collections/aggregations/near_image/executor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
from weaviate.collections.filters import _FilterToGRPC
1212
from weaviate.connect import executor
1313
from weaviate.connect.v4 import ConnectionType
14+
from weaviate.proto.v1 import aggregate_pb2
1415
from weaviate.types import BLOB_INPUT, NUMBER
1516
from weaviate.util import parse_blob
16-
from weaviate.proto.v1 import aggregate_pb2
1717

1818

1919
class _NearImageExecutor(Generic[ConnectionType], _BaseExecutor[ConnectionType]):

weaviate/collections/aggregations/near_object/executor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
from weaviate.collections.filters import _FilterToGRPC
1212
from weaviate.connect import executor
1313
from weaviate.connect.v4 import ConnectionType
14-
from weaviate.types import NUMBER, UUID
1514
from weaviate.proto.v1 import aggregate_pb2
15+
from weaviate.types import NUMBER, UUID
1616

1717

1818
class _NearObjectExecutor(Generic[ConnectionType], _BaseExecutor[ConnectionType]):

weaviate/collections/aggregations/near_text/executor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
from weaviate.collections.filters import _FilterToGRPC
1313
from weaviate.connect import executor
1414
from weaviate.connect.v4 import ConnectionType
15-
from weaviate.types import NUMBER
1615
from weaviate.proto.v1 import aggregate_pb2
16+
from weaviate.types import NUMBER
1717

1818

1919
class _NearTextExecutor(Generic[ConnectionType], _BaseExecutor[ConnectionType]):

weaviate/collections/aggregations/near_vector/executor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
from weaviate.connect import executor
1717
from weaviate.connect.v4 import ConnectionType
1818
from weaviate.exceptions import WeaviateInvalidInputError
19-
from weaviate.types import NUMBER
2019
from weaviate.proto.v1 import aggregate_pb2
20+
from weaviate.types import NUMBER
2121

2222

2323
class _NearVectorExecutor(Generic[ConnectionType], _BaseExecutor[ConnectionType]):

weaviate/collections/classes/internal.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,10 @@ def __is_nested(value: Any) -> bool:
438438

439439
def __create_nested_property_from_nested(name: str, value: Any) -> QueryNested:
440440
inner_type = get_args(value)[0]
441+
# If this nested property contains an object array, use the element type
442+
if get_origin(inner_type) is list:
443+
inner_type = get_args(inner_type)[0]
444+
441445
return QueryNested(
442446
name=name,
443447
properties=[

0 commit comments

Comments
 (0)