22
33import operator
44from abc import ABC
5- from typing import TypeVar , Generic
5+ from typing import TypeVar , Generic , overload , TYPE_CHECKING , TypeAlias
6+ from spellbind .values import Value , OneToOneValue , Constant , SimpleVariable , TwoToOneValue , SelectValue
67
7- from spellbind .values import Value , OneToOneValue , Constant , SimpleVariable , TwoToOneValue
8+ if TYPE_CHECKING :
9+ from spellbind .float_values import FloatValue # pragma: no cover
10+ from spellbind .int_values import IntValue # pragma: no cover
11+ from spellbind .str_values import StrValue # pragma: no cover
12+
13+ IntValueLike : TypeAlias = 'IntValue | int'
14+ FloatValueLike : TypeAlias = 'FloatValue | float'
15+ StrValueLike : TypeAlias = 'StrValue | str'
16+ BoolValueLike : TypeAlias = 'BoolValue | bool'
817
918_S = TypeVar ('_S' )
1019
11- BoolLike = Value [bool ] | bool
20+ BoolLike = bool | Value [bool ]
21+ IntLike = int | Value [int ]
22+ FloatLike = float | Value [float ]
23+ StrLike = str | Value [str ]
1224
1325
1426class BoolValue (Value [bool ], ABC ):
@@ -33,6 +45,37 @@ def __xor__(self, other: BoolLike) -> BoolValue:
3345 def __rxor__ (self , other : bool ) -> BoolValue :
3446 return XorBoolValues (other , self )
3547
48+ @overload
49+ def select (self , if_true : IntValueLike , if_false : IntValueLike ) -> IntValue : ...
50+
51+ @overload
52+ def select (self , if_true : FloatValueLike , if_false : FloatValueLike ) -> FloatValue : ...
53+
54+ @overload
55+ def select (self , if_true : StrValueLike , if_false : StrValueLike ) -> StrValue : ...
56+
57+ @overload
58+ def select (self , if_true : BoolValue , if_false : BoolValue ) -> BoolValue : ...
59+
60+ @overload
61+ def select (self , if_true : Value [_S ] | _S , if_false : Value [_S ] | _S ) -> Value [_S ]: ...
62+
63+ def select (self , if_true , if_false ):
64+ from spellbind .float_values import FloatValue , SelectFloatValue
65+ from spellbind .int_values import IntValue , SelectIntValue
66+ from spellbind .str_values import StrValue , SelectStrValue
67+
68+ if isinstance (if_true , (FloatValue , float )) and isinstance (if_false , (FloatValue , float )):
69+ return SelectFloatValue (self , if_true , if_false )
70+ elif isinstance (if_true , (StrValue , str )) and isinstance (if_false , (StrValue , str )):
71+ return SelectStrValue (self , if_true , if_false )
72+ elif isinstance (if_true , (BoolValue , bool )) and isinstance (if_false , (BoolValue , bool )):
73+ return SelectBoolValue (self , if_true , if_false )
74+ elif isinstance (if_true , (IntValue , int )) and isinstance (if_false , (IntValue , int )):
75+ return SelectIntValue (self , if_true , if_false )
76+ else :
77+ return SelectValue (self , if_true , if_false )
78+
3679
3780class OneToBoolValue (OneToOneValue [_S , bool ], BoolValue , Generic [_S ]):
3881 pass
@@ -66,5 +109,10 @@ class BoolVariable(SimpleVariable[bool], BoolValue):
66109 pass
67110
68111
112+ class SelectBoolValue (SelectValue [bool ], BoolValue ):
113+ def __init__ (self , condition : BoolLike , if_true : BoolLike , if_false : BoolLike ):
114+ super ().__init__ (condition , if_true , if_false )
115+
116+
69117TRUE = BoolConstant (True )
70118FALSE = BoolConstant (False )
0 commit comments