1616# under the License.
1717
1818from datetime import datetime , timedelta
19- from typing import TYPE_CHECKING , Any , Dict , Generic , List , Optional , Tuple , Union , cast
19+ from typing import (
20+ TYPE_CHECKING ,
21+ Any ,
22+ Dict ,
23+ Generic ,
24+ List ,
25+ Optional ,
26+ Sequence ,
27+ Tuple ,
28+ Type ,
29+ Union ,
30+ cast ,
31+ )
2032
2133from typing_extensions import Self
2234
2638from .utils import _R , AttrDict
2739
2840if TYPE_CHECKING :
41+ from .document_base import DocumentBase
2942 from .response .aggs import BucketData
3043 from .search_base import SearchBase
3144
32- FilterValueType = Union [str , datetime ]
45+ FilterValueType = Union [str , datetime , Sequence [ str ] ]
3346
3447__all__ = [
3548 "FacetedSearchBase" ,
@@ -51,7 +64,7 @@ class Facet(Generic[_R]):
5164 agg_type : str = ""
5265
5366 def __init__ (
54- self , metric : Optional [str ] = None , metric_sort : str = "desc" , ** kwargs : Any
67+ self , metric : Optional [Agg [ _R ] ] = None , metric_sort : str = "desc" , ** kwargs : Any
5568 ):
5669 self .filter_values = ()
5770 self ._params = kwargs
@@ -137,7 +150,9 @@ def add_filter(self, filter_values: List[FilterValueType]) -> Optional[Query]:
137150class RangeFacet (Facet [_R ]):
138151 agg_type = "range"
139152
140- def _range_to_dict (self , range : Tuple [Any , Tuple [int , int ]]) -> Dict [str , Any ]:
153+ def _range_to_dict (
154+ self , range : Tuple [Any , Tuple [Optional [int ], Optional [int ]]]
155+ ) -> Dict [str , Any ]:
141156 key , _range = range
142157 out : Dict [str , Any ] = {"key" : key }
143158 if _range [0 ] is not None :
@@ -146,7 +161,11 @@ def _range_to_dict(self, range: Tuple[Any, Tuple[int, int]]) -> Dict[str, Any]:
146161 out ["to" ] = _range [1 ]
147162 return out
148163
149- def __init__ (self , ranges : List [Tuple [Any , Tuple [int , int ]]], ** kwargs : Any ):
164+ def __init__ (
165+ self ,
166+ ranges : Sequence [Tuple [Any , Tuple [Optional [int ], Optional [int ]]]],
167+ ** kwargs : Any ,
168+ ):
150169 super ().__init__ (** kwargs )
151170 self ._params ["ranges" ] = list (map (self ._range_to_dict , ranges ))
152171 self ._params ["keyed" ] = False
@@ -277,7 +296,7 @@ class FacetedResponse(Response[_R]):
277296 _facets : Dict [str , List [Tuple [Any , int , bool ]]]
278297
279298 @property
280- def query_string (self ) -> Optional [Query ]:
299+ def query_string (self ) -> Optional [Union [ str , Query ] ]:
281300 return self ._faceted_search ._query
282301
283302 @property
@@ -334,9 +353,9 @@ def search(self):
334353
335354 """
336355
337- index = None
338- doc_types = None
339- fields : List [str ] = []
356+ index : Optional [ str ] = None
357+ doc_types : Optional [ List [ Union [ str , Type [ "DocumentBase" ]]]] = None
358+ fields : Sequence [str ] = []
340359 facets : Dict [str , Facet [_R ]] = {}
341360 using = "default"
342361
@@ -346,9 +365,9 @@ def search(self) -> "SearchBase[_R]": ...
346365
347366 def __init__ (
348367 self ,
349- query : Optional [Query ] = None ,
368+ query : Optional [Union [ str , Query ] ] = None ,
350369 filters : Dict [str , FilterValueType ] = {},
351- sort : List [str ] = [],
370+ sort : Sequence [str ] = [],
352371 ):
353372 """
354373 :arg query: the text to search for
@@ -383,16 +402,18 @@ def add_filter(
383402 ]
384403
385404 # remember the filter values for use in FacetedResponse
386- self .filter_values [name ] = filter_values
405+ self .filter_values [name ] = filter_values # type: ignore[assignment]
387406
388407 # get the filter from the facet
389- f = self .facets [name ].add_filter (filter_values )
408+ f = self .facets [name ].add_filter (filter_values ) # type: ignore[arg-type]
390409 if f is None :
391410 return
392411
393412 self ._filters [name ] = f
394413
395- def query (self , search : "SearchBase[_R]" , query : Query ) -> "SearchBase[_R]" :
414+ def query (
415+ self , search : "SearchBase[_R]" , query : Union [str , Query ]
416+ ) -> "SearchBase[_R]" :
396417 """
397418 Add query part to ``search``.
398419
0 commit comments