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
8 changes: 8 additions & 0 deletions pinecone/utils/filter_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,20 @@ 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`` — value is in the given list."""
self._require_list("is_in", values)
return Condition({self._name: {"$in": values}})

def not_in(self, values: list[str | int | float | bool]) -> Condition:
"""``$nin`` — value is not in the given list."""
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