Skip to content

Commit 75cd1a8

Browse files
committed
fix(document): fix types for document deletion
1 parent bd8a43d commit 75cd1a8

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/typesense/types/document.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -915,23 +915,42 @@ class DeleteSingleDocumentParameters(typing.TypedDict):
915915
ignore_not_found: typing.NotRequired[bool]
916916

917917

918-
class DeleteQueryParameters(typing.TypedDict):
918+
class TruncateDeleteParameters(typing.TypedDict):
919919
"""
920-
Parameters for deleting documents.
920+
Parameters for truncating a collection (deleting all documents, keeping schema).
921+
922+
Attributes:
923+
truncate (bool): Truncate the collection, keeping just the schema.
924+
"""
925+
926+
truncate: bool
927+
928+
929+
class FilterDeleteParameters(typing.TypedDict):
930+
"""
931+
Parameters for deleting documents by filter.
921932
922933
Attributes:
923-
truncate (str): Truncate the collection, keeping just the schema.
924934
filter_by (str): Filter to apply to documents.
925935
batch_size (int): Batch size for deleting documents.
926936
ignore_not_found (bool): Ignore not found documents.
927937
"""
928938

929-
truncate: typing.NotRequired[bool]
930939
filter_by: str
931940
batch_size: typing.NotRequired[int]
932941
ignore_not_found: typing.NotRequired[bool]
933942

934943

944+
DeleteQueryParameters = typing.Union[TruncateDeleteParameters, FilterDeleteParameters]
945+
"""
946+
Discriminated union of parameters for deleting documents.
947+
948+
Either:
949+
- TruncateDeleteParameters: Use truncate to delete all documents, keeping the schema.
950+
- FilterDeleteParameters: Use filter_by (and optionally batch_size, ignore_not_found) to delete specific documents.
951+
"""
952+
953+
935954
class DeleteResponse(typing.TypedDict):
936955
"""
937956
Response from deleting documents.

tests/documents_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,17 @@ def test_delete(
206206
assert response == {"num_deleted": 1}
207207

208208

209+
def test_truncate(
210+
actual_documents: Documents[Companies],
211+
delete_all: None,
212+
create_collection: None,
213+
create_document: None,
214+
) -> None:
215+
"""Test that the Documents object can delete a document from Typesense server."""
216+
response = actual_documents.delete({"truncate": True})
217+
assert response == {"num_deleted": 1}
218+
219+
209220
def test_delete_ignore_missing(
210221
actual_documents: Documents[Companies],
211222
delete_all: None,

0 commit comments

Comments
 (0)