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 ,
@@ -1333,7 +1357,7 @@ def generic_visit(self, node, children):
1333
1357
def parse_search_query (
1334
1358
query : str ,
1335
1359
* ,
1336
- config : SearchConfig | None = None ,
1360
+ config : SearchConfig [ Any ] | None = None ,
1337
1361
params = None ,
1338
1362
get_field_type : Callable [[str ], str | None ] | None = None ,
1339
1363
get_function_result_type : Callable [[str ], str | None ] | None = None ,
0 commit comments