Skip to content

Commit 9284f1e

Browse files
committed
refactor(document): format document classes based on linting rules
1 parent 9cbabeb commit 9284f1e

2 files changed

Lines changed: 352 additions & 100 deletions

File tree

src/typesense/document.py

Lines changed: 99 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
1+
"""
2+
This module provides functionality for managing individual documents in Typesense collections.
3+
4+
Classes:
5+
- Document: Handles operations related to a specific document within a collection.
6+
7+
Methods:
8+
- __init__: Initializes the Document object.
9+
- _endpoint_path: Constructs the API endpoint path for this specific document.
10+
- retrieve: Retrieves the details of this specific document.
11+
- update: Updates this specific document.
12+
- delete: Deletes this specific document.
13+
14+
The Document class interacts with the Typesense API to manage operations on a
15+
specific document within a collection. It provides methods to retrieve, update,
16+
and delete individual documents.
17+
18+
This module uses type hinting and is compatible with Python 3.11+ as well as earlier
19+
versions through the use of the typing_extensions library.
20+
"""
21+
122
import sys
223

324
from typesense.api_call import ApiCall
4-
from typesense.configuration import Configuration
5-
from typesense.types.collection import CollectionSchema
6-
from typesense.types.document import (
7-
DeleteQueryParameters,
8-
DirtyValuesParameters,
9-
DocumentSchema,
10-
DocumentWriteParameters,
11-
)
25+
from typesense.types.document import DirtyValuesParameters, DocumentSchema
1226

1327
if sys.version_info >= (3, 11):
1428
import typing
@@ -19,48 +33,103 @@
1933

2034

2135
class Document(typing.Generic[TDoc]):
36+
"""
37+
Class for managing individual documents in a Typesense collection.
38+
39+
This class provides methods to interact with a specific document,
40+
including retrieving, updating, and deleting it.
41+
42+
Attributes:
43+
api_call (ApiCall): The API call object for making requests.
44+
collection_name (str): The name of the collection.
45+
document_id (str): The ID of the document.
46+
"""
47+
2248
def __init__(
23-
self, api_call: ApiCall, collection_name: str, document_id: str
49+
self,
50+
api_call: ApiCall,
51+
collection_name: str,
52+
document_id: str,
2453
) -> None:
54+
"""
55+
Initialize the Document object.
56+
57+
Args:
58+
api_call (ApiCall): The API call object for making requests.
59+
collection_name (str): The name of the collection.
60+
document_id (str): The ID of the document.
61+
"""
2562
self.api_call = api_call
2663
self.collection_name = collection_name
2764
self.document_id = document_id
2865

29-
@property
30-
def _endpoint_path(self) -> str:
31-
from .collections import Collections
32-
from .documents import Documents
33-
34-
return "{0}/{1}/{2}/{3}".format(
35-
Collections.RESOURCE_PATH,
36-
self.collection_name,
37-
Documents.RESOURCE_PATH,
38-
self.document_id,
39-
)
40-
4166
def retrieve(self) -> TDoc:
42-
response = self.api_call.get(
67+
"""
68+
Retrieve this specific document.
69+
70+
Returns:
71+
TDoc: The retrieved document.
72+
"""
73+
response: TDoc = self.api_call.get(
4374
endpoint=self._endpoint_path,
4475
entity_type=typing.Dict[str, str],
4576
as_json=True,
4677
)
47-
48-
return typing.cast(TDoc, response)
78+
return response
4979

5080
def update(
51-
self, document: TDoc, params: typing.Union[DirtyValuesParameters, None] = None
81+
self,
82+
document: TDoc,
83+
dirty_values_parameters: typing.Union[DirtyValuesParameters, None] = None,
5284
) -> TDoc:
85+
"""
86+
Update this specific document.
87+
88+
Args:
89+
document (TDoc): The updated document data.
90+
dirty_values_parameters (Union[DirtyValuesParameters, None], optional):
91+
Parameters for handling dirty values.
92+
93+
Returns:
94+
TDoc: The updated document.
95+
"""
5396
response = self.api_call.patch(
5497
self._endpoint_path,
5598
body=document,
56-
params=params,
99+
params=dirty_values_parameters,
57100
entity_type=typing.Dict[str, str],
58101
)
59-
60102
return typing.cast(TDoc, response)
61103

62104
def delete(self) -> TDoc:
63-
response = self.api_call.delete(
64-
self._endpoint_path, entity_type=typing.Dict[str, str]
105+
"""
106+
Delete this specific document.
107+
108+
Returns:
109+
TDoc: The deleted document.
110+
"""
111+
response: TDoc = self.api_call.delete(
112+
self._endpoint_path,
113+
entity_type=typing.Dict[str, str],
114+
)
115+
return response
116+
117+
@property
118+
def _endpoint_path(self) -> str:
119+
"""
120+
Construct the API endpoint path for this specific document.
121+
122+
Returns:
123+
str: The constructed endpoint path.
124+
"""
125+
from typesense.collections import Collections
126+
from typesense.documents import Documents
127+
128+
return "/".join(
129+
[
130+
Collections.RESOURCE_PATH,
131+
self.collection_name,
132+
Documents.resource_path,
133+
self.document_id,
134+
],
65135
)
66-
return typing.cast(TDoc, response)

0 commit comments

Comments
 (0)