Skip to content

Commit

Permalink
Add handling for lists with just one or zero entries
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkkul committed Feb 29, 2024
1 parent e0db3a6 commit b89f5c0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
18 changes: 18 additions & 0 deletions test/collection/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,21 @@ def test_filter_lists() -> None:
or_direct = f1 | f2
assert isinstance(or_list, _FilterOr)
assert or_list.filters == or_direct.filters


def test_filter_lists_one_entry() -> None:
f1 = wvc.query.Filter.by_property("test").equal("test")

and_list = wvc.query.Filter.all_of([f1])
assert and_list == f1

or_list = wvc.query.Filter.any_of([f1])
assert or_list == f1


def test_filter_lists_empty() -> None:
with pytest.raises(weaviate.exceptions.WeaviateInvalidInputError):
wvc.query.Filter.all_of([])

with pytest.raises(weaviate.exceptions.WeaviateInvalidInputError):
wvc.query.Filter.any_of([])
8 changes: 8 additions & 0 deletions weaviate/collections/classes/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,11 +564,19 @@ def by_property(name: str, length: bool = False) -> _FilterByProperty:
@staticmethod
def all_of(filters: List[_Filters]) -> _Filters:
"""Combine all filters in the input list with an AND operator."""
if len(filters) == 1:
return filters[0]
elif len(filters) == 0:
raise WeaviateInvalidInputError("Filter.all_of must have at least one filter")
return _FilterAnd(filters)

@staticmethod
def any_of(filters: List[_Filters]) -> _Filters:
"""Combine all filters in the input list with an OR operator."""
if len(filters) == 1:
return filters[0]
elif len(filters) == 0:
raise WeaviateInvalidInputError("Filter.any_of must have at least one filter")
return _FilterOr(filters)


Expand Down

0 comments on commit b89f5c0

Please sign in to comment.