Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pinecone/utils/filter_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ def __ne__(self, value: object) -> Condition: # type: ignore[override]

# -- set operators --------------------------------------------------------

def _require_list(self, op: str, values: Any) -> None:
if not isinstance(values, (list, tuple)):
raise TypeError(f"{op} requires a list of values, got {type(values).__name__}")

def is_in(self, values: list[str | int | float | bool]) -> Condition:
"""``$in`` — the field's value is one of *values*.

Expand All @@ -212,6 +216,7 @@ def is_in(self, values: list[str | int | float | bool]) -> Condition:
>>> Field("genre").is_in(["drama", "thriller"]).to_dict()
{'genre': {'$in': ['drama', 'thriller']}}
"""
self._require_list("is_in", values)
return Condition({self._name: {"$in": values}})

def not_in(self, values: list[str | int | float | bool]) -> Condition:
Expand All @@ -222,6 +227,7 @@ def not_in(self, values: list[str | int | float | bool]) -> Condition:
>>> Field("genre").not_in(["horror"]).to_dict()
{'genre': {'$nin': ['horror']}}
"""
self._require_list("not_in", values)
return Condition({self._name: {"$nin": values}})

# -- exists operator ------------------------------------------------------
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/utils/test_filter_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ def test_field_not_in(self) -> None:
result = Field("tag").not_in(["spam"]).to_dict()
assert result == {"tag": {"$nin": ["spam"]}}

def test_field_is_in_accepts_tuple(self) -> None:
result = Field("color").is_in(("red", "blue")).to_dict()
assert result == {"color": {"$in": ("red", "blue")}}

def test_is_in_requires_list(self) -> None:
with pytest.raises(TypeError, match="list"):
Field("genre").is_in("drama") # type: ignore[arg-type]

def test_not_in_requires_list(self) -> None:
with pytest.raises(TypeError, match="list"):
Field("genre").not_in("drama") # type: ignore[arg-type]


class TestFieldExists:
def test_field_exists(self) -> None:
Expand Down