6
6
from collections .abc import Callable , Generator , Mapping , Sequence
7
7
from dataclasses import asdict , dataclass , field
8
8
from datetime import datetime
9
- from typing import TYPE_CHECKING , Any , Literal , NamedTuple , TypeIs , Union
9
+ from typing import TYPE_CHECKING , Any , Literal , NamedTuple , TypeIs , Union , overload
10
10
11
11
from django .utils .functional import cached_property
12
12
from parsimonious .exceptions import IncompleteParseError
@@ -574,8 +574,8 @@ def __str__(self) -> str:
574
574
return f"{ self .key .name } { self .operator } { self .value .raw_value } "
575
575
576
576
577
- @dataclass
578
- class SearchConfig :
577
+ @dataclass # pycqa/pycodestyle#1277
578
+ class SearchConfig [ TAllowBoolean : ( Literal [ True ], Literal [ False ]) = Literal [ True ]]: # noqa: E251
579
579
"""
580
580
Configures how the search parser interprets a search query
581
581
"""
@@ -604,7 +604,7 @@ class SearchConfig:
604
604
is_filter_translation : Mapping [str , tuple [str , Any ]] = field (default_factory = dict )
605
605
606
606
# Enables boolean filtering (AND / OR)
607
- allow_boolean = True
607
+ allow_boolean : TAllowBoolean = True # type: ignore[assignment] # python/mypy#18812
608
608
609
609
# Allows us to specify an allowlist of keys we will accept for this search.
610
610
# If empty, allow all keys.
@@ -619,8 +619,32 @@ class SearchConfig:
619
619
# Whether to wrap free_text_keys in asterisks
620
620
wildcard_free_text : bool = False
621
621
622
+ @overload
622
623
@classmethod
623
- def create_from (cls , search_config : SearchConfig , ** overrides ):
624
+ def create_from [
625
+ TBool : (Literal [True ], Literal [False ])
626
+ ](
627
+ cls : type [SearchConfig [Any ]],
628
+ search_config : SearchConfig [Any ],
629
+ * ,
630
+ allow_boolean : TBool ,
631
+ ** overrides : Any ,
632
+ ) -> SearchConfig [TBool ]: ...
633
+
634
+ @overload
635
+ @classmethod
636
+ def create_from [
637
+ TBool : (Literal [True ], Literal [False ])
638
+ ](
639
+ cls : type [SearchConfig [Any ]],
640
+ search_config : SearchConfig [TBool ],
641
+ ** overrides : Any ,
642
+ ) -> SearchConfig [TBool ]: ...
643
+
644
+ @classmethod
645
+ def create_from (
646
+ cls : type [SearchConfig [Any ]], search_config : SearchConfig [Any ], ** overrides : Any
647
+ ) -> SearchConfig [Any ]:
624
648
config = cls (** asdict (search_config ))
625
649
for key , val in overrides .items ():
626
650
setattr (config , key , val )
@@ -632,7 +656,7 @@ class SearchVisitor(NodeVisitor):
632
656
633
657
def __init__ (
634
658
self ,
635
- config : SearchConfig ,
659
+ config : SearchConfig [ Any ] ,
636
660
params : ParamsType | None = None ,
637
661
get_field_type : Callable [[str ], str | None ] | None = None ,
638
662
get_function_result_type : Callable [[str ], str | None ] | None = None ,
@@ -1330,16 +1354,36 @@ def generic_visit(self, node, children):
1330
1354
QueryToken = Union [SearchFilter , AggregateFilter , QueryOp , ParenExpression ]
1331
1355
1332
1356
1357
+ @overload
1358
+ def parse_search_query (
1359
+ query : str ,
1360
+ * ,
1361
+ config : SearchConfig [Literal [False ]],
1362
+ params = None ,
1363
+ get_field_type : Callable [[str ], str | None ] | None = None ,
1364
+ get_function_result_type : Callable [[str ], str | None ] | None = None ,
1365
+ ) -> Sequence [SearchFilter | AggregateFilter ]: ...
1366
+
1367
+
1368
+ @overload
1369
+ def parse_search_query (
1370
+ query : str ,
1371
+ * ,
1372
+ config : SearchConfig [Literal [True ]] | None = None ,
1373
+ params = None ,
1374
+ get_field_type : Callable [[str ], str | None ] | None = None ,
1375
+ get_function_result_type : Callable [[str ], str | None ] | None = None ,
1376
+ ) -> Sequence [QueryToken ]: ...
1377
+
1378
+
1333
1379
def parse_search_query (
1334
1380
query : str ,
1335
1381
* ,
1336
- config : SearchConfig | None = None ,
1382
+ config : SearchConfig [ Any ] | None = None ,
1337
1383
params = None ,
1338
1384
get_field_type : Callable [[str ], str | None ] | None = None ,
1339
1385
get_function_result_type : Callable [[str ], str | None ] | None = None ,
1340
- ) -> list [
1341
- SearchFilter
1342
- ]: # TODO: use the `Sequence[QueryToken]` type and update the code that fails type checking.
1386
+ ) -> Sequence [QueryToken ]:
1343
1387
if config is None :
1344
1388
config = default_config
1345
1389
0 commit comments