From b89f5c085f656968a347961fbe42199de80feaa4 Mon Sep 17 00:00:00 2001 From: Dirk Kulawiak Date: Thu, 29 Feb 2024 11:34:41 +0100 Subject: [PATCH] Add handling for lists with just one or zero entries --- test/collection/test_filter.py | 18 ++++++++++++++++++ weaviate/collections/classes/filters.py | 8 ++++++++ 2 files changed, 26 insertions(+) diff --git a/test/collection/test_filter.py b/test/collection/test_filter.py index 1cc7a0f8b..0a3dd5c4b 100644 --- a/test/collection/test_filter.py +++ b/test/collection/test_filter.py @@ -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([]) diff --git a/weaviate/collections/classes/filters.py b/weaviate/collections/classes/filters.py index 9e44ab3ae..2cbeddd48 100644 --- a/weaviate/collections/classes/filters.py +++ b/weaviate/collections/classes/filters.py @@ -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)