From 47285fc63b0be1f23afbfdf760f2c4cd8eaaac84 Mon Sep 17 00:00:00 2001 From: Fanis Tharropoulos Date: Wed, 23 Apr 2025 13:37:38 +0300 Subject: [PATCH 1/4] feat(collections): add `__contains__` method for collection existence - implement `__contains__` method to check if collection exists - enable use of `in` operator with collection names - handle exceptions gracefully when unable to retrieve collections --- src/typesense/collections.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/typesense/collections.py b/src/typesense/collections.py index 723b2de..65489ba 100644 --- a/src/typesense/collections.py +++ b/src/typesense/collections.py @@ -57,6 +57,22 @@ def __init__(self, api_call: ApiCall): self.api_call = api_call self.collections: typing.Dict[str, Collection[TDoc]] = {} + def __contains__(self, collection_name: str) -> bool: + """ + Check if a collection exists in Typesense. + + Args: + collection_name (str): The name of the collection to check. + + Returns: + bool: True if the collection exists, False otherwise. + """ + try: + all_collections = self.retrieve() + except Exception: + return False + return any(coll["name"] == collection_name for coll in all_collections) + def __getitem__(self, collection_name: str) -> Collection[TDoc]: """ Get or create a Collection instance for a given collection name. From b02f552cf3ed6158092a3514bde3e4bfad118bed Mon Sep 17 00:00:00 2001 From: Fanis Tharropoulos Date: Wed, 23 Apr 2025 14:13:10 +0300 Subject: [PATCH 2/4] fix(collections): correct logic in `exists()` method - fix existing condition in `Collections.exists()` method - add tests to verify collection existence checking - ensure proper return when collection is in cache --- src/typesense/collections.py | 15 +++++++++++++-- tests/collections_test.py | 13 +++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/typesense/collections.py b/src/typesense/collections.py index 65489ba..89d1fbb 100644 --- a/src/typesense/collections.py +++ b/src/typesense/collections.py @@ -61,17 +61,28 @@ def __contains__(self, collection_name: str) -> bool: """ Check if a collection exists in Typesense. + This method tries to retrieve the specified collection to check for its existence, + utilizing the Collection.retrieve() method but without caching non-existent collections. + Args: collection_name (str): The name of the collection to check. Returns: bool: True if the collection exists, False otherwise. """ + if collection_name in self.collections: + try: + self.collections[collection_name].retrieve() + return True + except Exception: + self.collections.pop(collection_name, None) + return False + try: - all_collections = self.retrieve() + Collection(self.api_call, collection_name).retrieve() + return True except Exception: return False - return any(coll["name"] == collection_name for coll in all_collections) def __getitem__(self, collection_name: str) -> Collection[TDoc]: """ diff --git a/tests/collections_test.py b/tests/collections_test.py index 82f19ef..189a3f4 100644 --- a/tests/collections_test.py +++ b/tests/collections_test.py @@ -293,3 +293,16 @@ def test_actual_retrieve( response[0].pop("created_at") assert response == expected + + +def test_actual_contains( + actual_collections: Collections, + delete_all: None, + create_collection: None, +) -> None: + """Test that the Collections object can check if a collection exists in Typesense.""" + # Test for existing collection + assert "companies" in actual_collections + + # Test for non-existing collection + assert "non_existent_collection" not in actual_collections \ No newline at end of file From 3cb33f641a92cc7eba837561c98cfd923b5e2ecf Mon Sep 17 00:00:00 2001 From: Fanis Tharropoulos Date: Thu, 24 Apr 2025 11:53:04 +0300 Subject: [PATCH 3/4] test(collection): add another non-existent test --- tests/collections_test.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/collections_test.py b/tests/collections_test.py index 189a3f4..84971bd 100644 --- a/tests/collections_test.py +++ b/tests/collections_test.py @@ -305,4 +305,6 @@ def test_actual_contains( assert "companies" in actual_collections # Test for non-existing collection - assert "non_existent_collection" not in actual_collections \ No newline at end of file + assert "non_existent_collection" not in actual_collections + # Test again + assert "non_existent_collection" not in actual_collections From f59ac3765dba9f11b8b9800ab2b7c5165a18f85f Mon Sep 17 00:00:00 2001 From: Fanis Tharropoulos Date: Thu, 24 Apr 2025 12:09:29 +0300 Subject: [PATCH 4/4] chore: lint --- src/typesense/collections.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/typesense/collections.py b/src/typesense/collections.py index 89d1fbb..72fa381 100644 --- a/src/typesense/collections.py +++ b/src/typesense/collections.py @@ -71,14 +71,15 @@ def __contains__(self, collection_name: str) -> bool: bool: True if the collection exists, False otherwise. """ if collection_name in self.collections: - try: - self.collections[collection_name].retrieve() + try: # noqa: WPS229, WPS529 + + self.collections[collection_name].retrieve() # noqa: WPS529 return True except Exception: self.collections.pop(collection_name, None) return False - - try: + + try: # noqa: WPS229, WPS529 Collection(self.api_call, collection_name).retrieve() return True except Exception: