Skip to content

Commit 15cbbd7

Browse files
committed
refactor(collection): format collection classes based on linting rules
1 parent 5b3aacb commit 15cbbd7

File tree

7 files changed

+195
-31
lines changed

7 files changed

+195
-31
lines changed

src/typesense/collection.py

Lines changed: 95 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
1-
from __future__ import annotations
1+
"""
2+
This module provides functionality for managing individual collections in the Typesense API.
3+
4+
It contains the Collection class, which allows for retrieving, updating, and deleting
5+
collections, as well as managing documents, overrides, and synonyms within a collection.
6+
7+
Classes:
8+
Collection: Manages operations on a single collection in the Typesense API.
9+
10+
Dependencies:
11+
- typesense.api_call: Provides the ApiCall class for making API requests.
12+
- typesense.documents: Provides the Documents class for managing documents.
13+
- typesense.overrides: Provides the Overrides class for managing overrides.
14+
- typesense.synonyms: Provides the Synonyms class for managing synonyms.
15+
- typesense.types.collection: Provides CollectionSchema and CollectionUpdateSchema types.
16+
- typesense.types.document: Provides DocumentSchema type.
17+
18+
Note: This module uses conditional imports to support both Python 3.11+ and earlier versions.
19+
"""
220

321
import sys
422

@@ -10,48 +28,108 @@
1028
import typing_extensions as typing
1129

1230
from typesense.api_call import ApiCall
31+
from typesense.documents import Documents
32+
from typesense.overrides import Overrides
33+
from typesense.synonyms import Synonyms
1334
from typesense.types.document import DocumentSchema
1435

15-
from .documents import Documents
16-
from .overrides import Overrides
17-
from .synonyms import Synonyms
18-
1936
TDoc = typing.TypeVar("TDoc", bound=DocumentSchema)
2037

2138

2239
class Collection(typing.Generic[TDoc]):
40+
"""
41+
Manages operations on a single collection in the Typesense API.
42+
43+
This class provides methods to retrieve, update, and delete a collection,
44+
as well as access to documents, overrides, and synonyms within the collection.
45+
It is generic over the document type TDoc, which should be a subtype of DocumentSchema.
46+
47+
Attributes:
48+
name (str): The name of the collection.
49+
api_call (ApiCall): The ApiCall instance for making API requests.
50+
documents (Documents[TDoc]): Instance for managing documents in this collection.
51+
overrides (Overrides): Instance for managing overrides in this collection.
52+
synonyms (Synonyms): Instance for managing synonyms in this collection.
53+
"""
54+
2355
def __init__(self, api_call: ApiCall, name: str):
56+
"""
57+
Initialize the Collection instance.
58+
59+
Args:
60+
api_call (ApiCall): The ApiCall instance for making API requests.
61+
name (str): The name of the collection.
62+
"""
2463
self.name = name
2564
self.api_call = api_call
26-
self.documents = Documents[TDoc](api_call, name)
65+
self.documents: Documents[TDoc] = Documents(api_call, name)
2766
self.overrides = Overrides(api_call, name)
2867
self.synonyms = Synonyms(api_call, name)
2968

30-
@property
31-
def _endpoint_path(self) -> str:
32-
from typesense.collections import Collections
33-
34-
return f"{Collections.RESOURCE_PATH}/{self.name}"
35-
3669
def retrieve(self) -> CollectionSchema:
70+
"""
71+
Retrieve the schema of this collection from Typesense.
72+
73+
Returns:
74+
CollectionSchema: The schema of the collection.
75+
"""
3776
response: CollectionSchema = self.api_call.get(
38-
endpoint=self._endpoint_path, entity_type=CollectionSchema, as_json=True
77+
endpoint=self._endpoint_path,
78+
entity_type=CollectionSchema,
79+
as_json=True,
3980
)
4081
return response
4182

4283
def update(self, schema_change: CollectionUpdateSchema) -> CollectionUpdateSchema:
84+
"""
85+
Update the schema of this collection in Typesense.
86+
87+
Args:
88+
schema_change (CollectionUpdateSchema):
89+
The changes to apply to the collection schema.
90+
91+
Returns:
92+
CollectionUpdateSchema: The updated schema of the collection.
93+
"""
4394
response: CollectionUpdateSchema = self.api_call.patch(
4495
endpoint=self._endpoint_path,
4596
body=schema_change,
4697
entity_type=CollectionUpdateSchema,
4798
)
4899
return response
49100

50-
# There's currently no parameters passed to Collection deletions, but ensuring future compatibility
51101
def delete(
52102
self,
53-
params: typing.Union[typing.Dict[str, typing.Union[str, bool]], None] = None,
103+
delete_parameters: typing.Union[
104+
typing.Dict[str, typing.Union[str, bool]],
105+
None,
106+
] = None,
54107
) -> CollectionSchema:
55-
return self.api_call.delete(
56-
self._endpoint_path, entity_type=CollectionSchema, params=params
108+
"""
109+
Delete this collection from Typesense.
110+
111+
Args:
112+
delete_parameters (Union[Dict[str, Union[str, bool]], None], optional):
113+
Additional parameters for the delete operation. Defaults to None.
114+
115+
Returns:
116+
CollectionSchema: The schema of the deleted collection.
117+
"""
118+
response: CollectionSchema = self.api_call.delete(
119+
self._endpoint_path,
120+
entity_type=CollectionSchema,
121+
params=delete_parameters,
57122
)
123+
return response
124+
125+
@property
126+
def _endpoint_path(self) -> str:
127+
"""
128+
Get the API endpoint path for this collection.
129+
130+
Returns:
131+
str: The full endpoint path for the collection.
132+
"""
133+
from typesense.collections import Collections
134+
135+
return "/".join([Collections.resource_path, self.name])

src/typesense/collections.py

Lines changed: 95 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,135 @@
1-
from __future__ import annotations
1+
"""
2+
This module provides functionality for managing collections in the Typesense API.
3+
4+
It contains the Collections class, which allows for creating, retrieving, and
5+
accessing individual collections.
6+
7+
Classes:
8+
Collections: Manages collections in the Typesense API.
9+
10+
Dependencies:
11+
- typesense.api_call: Provides the ApiCall class for making API requests.
12+
- typesense.collection: Provides the Collection class for individual collection operations.
13+
- typesense.types.collection: Provides CollectionCreateSchema and CollectionSchema types.
14+
- typesense.types.document: Provides DocumentSchema type.
15+
16+
Note: This module uses conditional imports to support both Python 3.11+ and earlier versions.
17+
"""
218

319
import sys
4-
from email.policy import default
5-
from typing import TYPE_CHECKING
620

721
if sys.version_info >= (3, 11):
822
import typing
923
else:
1024
import typing_extensions as typing
1125

1226
from typesense.api_call import ApiCall
27+
from typesense.collection import Collection
1328
from typesense.types.collection import CollectionCreateSchema, CollectionSchema
1429
from typesense.types.document import DocumentSchema
1530

16-
from .collection import Collection
17-
1831
TDoc = typing.TypeVar("TDoc", bound=DocumentSchema)
1932

2033

2134
class Collections(typing.Generic[TDoc]):
22-
RESOURCE_PATH = "/collections"
35+
"""
36+
Manages collections in the Typesense API.
37+
38+
This class provides methods to create, retrieve, and access individual collections.
39+
It is generic over the document type TDoc, which should be a subtype of DocumentSchema.
40+
41+
Attributes:
42+
resource_path (str): The API endpoint path for collections operations.
43+
api_call (ApiCall): The ApiCall instance for making API requests.
44+
collections (Dict[str, Collection[TDoc]]):
45+
A dictionary of Collection instances, keyed by collection name.
46+
"""
47+
48+
resource_path: typing.Final[str] = "/collections"
2349

2450
def __init__(self, api_call: ApiCall):
51+
"""
52+
Initialize the Collections instance.
53+
54+
Args:
55+
api_call (ApiCall): The ApiCall instance for making API requests.
56+
"""
2557
self.api_call = api_call
2658
self.collections: typing.Dict[str, Collection[TDoc]] = {}
2759

2860
def __getitem__(self, collection_name: str) -> Collection[TDoc]:
61+
"""
62+
Get or create a Collection instance for a given collection name.
63+
64+
This method allows accessing collections using dictionary-like syntax.
65+
If the Collection instance doesn't exist, it creates a new one.
66+
67+
Args:
68+
collection_name (str): The name of the collection to access.
69+
70+
Returns:
71+
Collection[TDoc]: The Collection instance for the specified collection name.
72+
73+
Example:
74+
>>> collections = Collections(api_call)
75+
>>> fruits_collection = collections['fruits']
76+
"""
2977
if not self.collections.get(collection_name):
3078
self.collections[collection_name] = Collection(
31-
self.api_call, collection_name
79+
self.api_call,
80+
collection_name,
3281
)
3382
return self.collections[collection_name]
3483

3584
def create(self, schema: CollectionCreateSchema) -> CollectionSchema:
85+
"""
86+
Create a new collection in Typesense.
87+
88+
Args:
89+
schema (CollectionCreateSchema):
90+
The schema defining the structure of the new collection.
91+
92+
Returns:
93+
CollectionSchema:
94+
The schema of the created collection, as returned by the API.
95+
96+
Example:
97+
>>> collections = Collections(api_call)
98+
>>> schema = {
99+
... "name": "companies",
100+
... "fields": [
101+
... {"name": "company_name", "type": "string" },
102+
... {"name": "num_employees", "type": "int32" },
103+
... {"name": "country", "type": "string", "facet": True }
104+
... ],
105+
... "default_sorting_field": "num_employees"
106+
... }
107+
>>> created_schema = collections.create(schema)
108+
"""
36109
call: CollectionSchema = self.api_call.post(
37-
endpoint=Collections.RESOURCE_PATH,
110+
endpoint=Collections.resource_path,
38111
entity_type=CollectionSchema,
39112
as_json=True,
40113
body=schema,
41114
)
42115
return call
43116

44117
def retrieve(self) -> typing.List[CollectionSchema]:
118+
"""
119+
Retrieve all collections from Typesense.
120+
121+
Returns:
122+
List[CollectionSchema]:
123+
A list of schemas for all collections in the Typesense instance.
124+
125+
Example:
126+
>>> collections = Collections(api_call)
127+
>>> all_collections = collections.retrieve()
128+
>>> for collection in all_collections:
129+
... print(collection['name'])
130+
"""
45131
call: typing.List[CollectionSchema] = self.api_call.get(
46-
endpoint=Collections.RESOURCE_PATH,
132+
endpoint=Collections.resource_path,
47133
as_json=True,
48134
entity_type=typing.List[CollectionSchema],
49135
)

src/typesense/document.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def _endpoint_path(self) -> str:
127127

128128
return "/".join(
129129
[
130-
Collections.RESOURCE_PATH,
130+
Collections.resource_path,
131131
self.collection_name,
132132
Documents.resource_path,
133133
self.document_id,

src/typesense/documents.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ def _endpoint_path(self, action: typing.Union[str, None] = None) -> str:
396396
action = action or ""
397397
return "/".join(
398398
[
399-
Collections.RESOURCE_PATH,
399+
Collections.resource_path,
400400
self.collection_name,
401401
self.resource_path,
402402
action,

src/typesense/override.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def _endpoint_path(self) -> str:
9595

9696
return "/".join(
9797
[
98-
Collections.RESOURCE_PATH,
98+
Collections.resource_path,
9999
self.collection_name,
100100
Overrides.resource_path,
101101
self.override_id,

src/typesense/overrides.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def _endpoint_path(self, override_id: typing.Union[str, None] = None) -> str:
141141

142142
return "/".join(
143143
[
144-
Collections.RESOURCE_PATH,
144+
Collections.resource_path,
145145
self.collection_name,
146146
Overrides.resource_path,
147147
override_id,

src/typesense/synonym.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def _endpoint_path(self) -> str:
8989

9090
return "/".join(
9191
[
92-
Collections.RESOURCE_PATH,
92+
Collections.resource_path,
9393
self.collection_name,
9494
Synonyms.resource_path,
9595
self.synonym_id,

0 commit comments

Comments
 (0)