3
3
# Licensed under the MIT License. See License.txt in the project root for
4
4
# license information.
5
5
# --------------------------------------------------------------------------
6
- from typing import TYPE_CHECKING
6
+ from typing import Union , List , Dict , Any
7
7
from threading import Lock
8
8
9
9
from ._generated .models import IndexAction
10
10
11
- if TYPE_CHECKING :
12
- # pylint:disable=unused-import,ungrouped-imports
13
- from typing import List
14
11
15
-
16
- def _flatten_args (args ):
17
- # type (Union[List[dict], List[List[dict]]]) -> List[dict]
12
+ def _flatten_args (args : Union [List [Dict ], List [List [Dict ]]]) -> List [Dict ]:
18
13
if len (args ) == 1 and isinstance (args [0 ], (list , tuple )):
19
14
return args [0 ]
20
15
return args
21
16
22
-
23
- class IndexDocumentsBatch (object ):
17
+ class IndexDocumentsBatch :
24
18
"""Represent a batch of update operations for documents in an Azure
25
19
Search index.
26
20
@@ -29,17 +23,14 @@ class IndexDocumentsBatch(object):
29
23
30
24
"""
31
25
32
- def __init__ (self ):
33
- # type: () -> None
34
- self ._actions = [] # type: List[IndexAction]
26
+ def __init__ (self ) -> None :
27
+ self ._actions : List [IndexAction ] = []
35
28
self ._lock = Lock ()
36
29
37
- def __repr__ (self ):
38
- # type: () -> str
30
+ def __repr__ (self ) -> str :
39
31
return "<IndexDocumentsBatch [{} actions]>" .format (len (self .actions ))[:1024 ]
40
32
41
- def add_upload_actions (self , * documents ):
42
- # type (Union[List[dict], List[List[dict]]]) -> List[IndexAction]
33
+ def add_upload_actions (self , * documents : Union [List [Dict ], List [List [Dict ]]]) -> List [IndexAction ]:
43
34
"""Add documents to upload to the Azure search index.
44
35
45
36
An upload action is similar to an "upsert" where the document will be
@@ -48,16 +39,16 @@ def add_upload_actions(self, *documents):
48
39
49
40
:param documents: Documents to upload to an Azure search index. May be
50
41
a single list of documents, or documents as individual parameters.
51
- :type documents: dict or list[dict ]
42
+ :type documents: Dict or List[Dict ]
52
43
:return: the added actions
53
44
:rtype: List[IndexAction]
54
45
"""
55
46
return self ._extend_batch (_flatten_args (documents ), "upload" )
56
47
57
48
def add_delete_actions (
58
- self , * documents , ** kwargs
59
- ): # pylint: disable=unused-argument
60
- # type (Union[List[dict], List[List[dict]]]) -> List[IndexAction]
49
+ self , * documents : Union [ List [ Dict ], List [ List [ Dict ]]], ** kwargs : Any
50
+ ) -> List [ IndexAction ]:
51
+ # pylint: disable=unused-argument
61
52
"""Add documents to delete to the Azure search index.
62
53
63
54
Delete removes the specified document from the index. Any field you
@@ -71,16 +62,16 @@ def add_delete_actions(
71
62
72
63
:param documents: Documents to delete from an Azure search index. May be
73
64
a single list of documents, or documents as individual parameters.
74
- :type documents: dict or list[dict ]
65
+ :type documents: Dict or List[Dict ]
75
66
:return: the added actions
76
67
:rtype: List[IndexAction]
77
68
"""
78
69
return self ._extend_batch (_flatten_args (documents ), "delete" )
79
70
80
71
def add_merge_actions (
81
- self , * documents , ** kwargs
82
- ): # pylint: disable=unused-argument
83
- # type (Union[List[dict], List[List[dict]]]) -> List[IndexAction]
72
+ self , * documents : Union [ List [ Dict ], List [ List [ Dict ]]], ** kwargs : Any
73
+ ) -> List [ IndexAction ]:
74
+ # pylint: disable=unused-argument
84
75
"""Add documents to merge in to existing documents in the Azure search
85
76
index.
86
77
@@ -91,16 +82,16 @@ def add_merge_actions(
91
82
92
83
:param documents: Documents to merge into an Azure search index. May be
93
84
a single list of documents, or documents as individual parameters.
94
- :type documents: dict or list[dict ]
85
+ :type documents: Dict or List[Dict ]
95
86
:return: the added actions
96
87
:rtype: List[IndexAction]
97
88
"""
98
89
return self ._extend_batch (_flatten_args (documents ), "merge" )
99
90
100
91
def add_merge_or_upload_actions (
101
- self , * documents , ** kwargs
102
- ): # pylint: disable=unused-argument
103
- # type (Union[List[dict], List[List[dict]]]) -> List[IndexAction]
92
+ self , * documents : Union [ List [ Dict ], List [ List [ Dict ]]], ** kwargs : Any
93
+ ) -> List [ IndexAction ]:
94
+ # pylint: disable=unused-argument
104
95
"""Add documents to merge in to existing documents in the Azure search
105
96
index, or upload if they do not yet exist.
106
97
@@ -111,23 +102,21 @@ def add_merge_or_upload_actions(
111
102
:param documents: Documents to merge or upload into an Azure search
112
103
index. May be a single list of documents, or documents as individual
113
104
parameters.
114
- :type documents: dict or list[dict ]
105
+ :type documents: Dict or List[Dict ]
115
106
:return: the added actions
116
107
:rtype: List[IndexAction]
117
108
"""
118
109
return self ._extend_batch (_flatten_args (documents ), "mergeOrUpload" )
119
110
120
111
@property
121
- def actions (self ):
122
- # type: () -> List[IndexAction]
112
+ def actions (self ) -> List [IndexAction ]:
123
113
"""The list of currently index actions to index.
124
114
125
115
:rtype: List[IndexAction]
126
116
"""
127
117
return list (self ._actions )
128
118
129
- def dequeue_actions (self , ** kwargs ): # pylint: disable=unused-argument
130
- # type: () -> List[IndexAction]
119
+ def dequeue_actions (self , ** kwargs : Any ) -> List [IndexAction ]: # pylint: disable=unused-argument
131
120
"""Get the list of currently configured index actions and clear it.
132
121
133
122
:rtype: List[IndexAction]
@@ -137,8 +126,10 @@ def dequeue_actions(self, **kwargs): # pylint: disable=unused-argument
137
126
self ._actions = []
138
127
return result
139
128
140
- def enqueue_actions (self , new_actions , ** kwargs ): # pylint: disable=unused-argument
141
- # type: (Union[IndexAction, List[IndexAction]]) -> None
129
+ def enqueue_actions (
130
+ self , new_actions : Union [IndexAction , List [IndexAction ]], ** kwargs : Any
131
+ ) -> None :
132
+ # pylint: disable=unused-argument
142
133
"""Enqueue a list of index actions to index."""
143
134
if isinstance (new_actions , IndexAction ):
144
135
with self ._lock :
@@ -147,8 +138,7 @@ def enqueue_actions(self, new_actions, **kwargs): # pylint: disable=unused-argu
147
138
with self ._lock :
148
139
self ._actions .extend (new_actions )
149
140
150
- def _extend_batch (self , documents , action_type ):
151
- # type: (List[dict], str) -> List[IndexAction]
141
+ def _extend_batch (self , documents : List [Dict ], action_type : str ) -> List [IndexAction ]:
152
142
new_actions = [
153
143
IndexAction (additional_properties = document , action_type = action_type )
154
144
for document in documents
0 commit comments