29
29
from hypothesis .control import _current_build_context , assume
30
30
from hypothesis .errors import (
31
31
HypothesisException ,
32
+ HypothesisWarning ,
32
33
InvalidArgument ,
33
34
NonInteractiveExampleWarning ,
34
35
UnsatisfiedAssumption ,
@@ -396,6 +397,14 @@ def __or__(self, other: "SearchStrategy[T]") -> "SearchStrategy[Union[Ex, T]]":
396
397
raise ValueError (f"Cannot | a SearchStrategy with { other !r} " )
397
398
return OneOfStrategy ((self , other ))
398
399
400
+ def __bool__ (self ) -> bool :
401
+ warnings .warn (
402
+ f"bool({ self !r} ) is always True, did you mean to draw a value?" ,
403
+ HypothesisWarning ,
404
+ stacklevel = 2 ,
405
+ )
406
+ return True
407
+
399
408
def validate (self ) -> None :
400
409
"""Throw an exception if the strategy is not valid.
401
410
@@ -589,7 +598,7 @@ def do_filtered_draw(self, data):
589
598
return filter_not_satisfied
590
599
591
600
592
- class OneOfStrategy (SearchStrategy ):
601
+ class OneOfStrategy (SearchStrategy [ Ex ] ):
593
602
"""Implements a union of strategies. Given a number of strategies this
594
603
generates values which could have come from any of them.
595
604
@@ -781,7 +790,7 @@ def one_of(
781
790
return OneOfStrategy (args )
782
791
783
792
784
- class MappedSearchStrategy (SearchStrategy ):
793
+ class MappedSearchStrategy (SearchStrategy [ Ex ] ):
785
794
"""A strategy which is defined purely by conversion to and from another
786
795
strategy.
787
796
@@ -816,7 +825,7 @@ def pack(self, x):
816
825
into a value suitable for outputting from this strategy."""
817
826
raise NotImplementedError (f"{ self .__class__ .__name__ } .pack()" )
818
827
819
- def do_draw (self , data : ConjectureData ) -> Ex :
828
+ def do_draw (self , data : ConjectureData ) -> Any :
820
829
with warnings .catch_warnings ():
821
830
if isinstance (self .pack , type ) and issubclass (
822
831
self .pack , (abc .Mapping , abc .Set )
@@ -826,7 +835,7 @@ def do_draw(self, data: ConjectureData) -> Ex:
826
835
i = data .index
827
836
try :
828
837
data .start_example (MAPPED_SEARCH_STRATEGY_DO_DRAW_LABEL )
829
- result = self .pack (data .draw (self .mapped_strategy ))
838
+ result = self .pack (data .draw (self .mapped_strategy )) # type: ignore
830
839
data .stop_example ()
831
840
return result
832
841
except UnsatisfiedAssumption :
@@ -846,7 +855,7 @@ def branches(self) -> List[SearchStrategy[Ex]]:
846
855
filter_not_satisfied = UniqueIdentifier ("filter not satisfied" )
847
856
848
857
849
- class FilteredStrategy (SearchStrategy ):
858
+ class FilteredStrategy (SearchStrategy [ Ex ] ):
850
859
def __init__ (self , strategy , conditions ):
851
860
super ().__init__ ()
852
861
if isinstance (strategy , FilteredStrategy ):
0 commit comments