@@ -31,7 +31,7 @@ def create(
3131 cls ,
3232 cond : Var ,
3333 comp1 : BaseComponent ,
34- comp2 : BaseComponent | None = None ,
34+ comp2 : BaseComponent | types . Unset = types . Unset () ,
3535 ) -> Component :
3636 """Create a conditional component.
3737
@@ -44,10 +44,14 @@ def create(
4444 The conditional component.
4545 """
4646 # Wrap everything in fragments.
47- if type (comp1 ). __name__ != " Fragment" :
47+ if type (comp1 ) is not Fragment :
4848 comp1 = Fragment .create (comp1 )
49- if comp2 is None or type (comp2 ).__name__ != "Fragment" :
50- comp2 = Fragment .create (comp2 ) if comp2 else Fragment .create ()
49+ if isinstance (comp2 , types .Unset ) or type (comp2 ) is not Fragment :
50+ comp2 = (
51+ Fragment .create (comp2 )
52+ if not isinstance (comp2 , types .Unset )
53+ else Fragment .create ()
54+ )
5155 return Fragment .create (
5256 cls ._create (
5357 children = [comp1 , comp2 ],
@@ -96,18 +100,22 @@ def add_imports(self) -> ImportDict:
96100
97101
98102@overload
99- def cond (condition : Any , c1 : Component , c2 : Any ) -> Component : ... # pyright: ignore [reportOverlappingOverload]
103+ def cond (condition : Any , c1 : Component , c2 : Any , / ) -> Component : ... # pyright: ignore [reportOverlappingOverload]
104+
105+
106+ @overload
107+ def cond (condition : Any , c1 : Component , / ) -> Component : ...
100108
101109
102110@overload
103- def cond (condition : Any , c1 : Component ) -> Component : ...
111+ def cond (condition : Any , c1 : Any , c2 : Component , / ) -> Component : ... # pyright: ignore [reportOverlappingOverload]
104112
105113
106114@overload
107- def cond (condition : Any , c1 : Any , c2 : Any ) -> Var : ...
115+ def cond (condition : Any , c1 : Any , c2 : Any , / ) -> Var : ...
108116
109117
110- def cond (condition : Any , c1 : Any , c2 : Any = None ) -> Component | Var :
118+ def cond (condition : Any , c1 : Any , c2 : Any = types . Unset (), / ) -> Component | Var :
111119 """Create a conditional component or Prop.
112120
113121 Args:
@@ -128,15 +136,15 @@ def cond(condition: Any, c1: Any, c2: Any = None) -> Component | Var:
128136
129137 # If the first component is a component, create a Cond component.
130138 if isinstance (c1 , BaseComponent ):
131- if c2 is not None and not isinstance (c2 , BaseComponent ):
132- raise ValueError ( "Both arguments must be components." )
139+ if not isinstance ( c2 , types . Unset ) and not isinstance (c2 , BaseComponent ):
140+ return Cond . create ( cond_var . bool (), c1 , Fragment . create ( c2 ) )
133141 return Cond .create (cond_var .bool (), c1 , c2 )
134142
135143 # Otherwise, create a conditional Var.
136144 # Check that the second argument is valid.
137145 if isinstance (c2 , BaseComponent ):
138- raise ValueError ( "Both arguments must be props." )
139- if c2 is None :
146+ return Cond . create ( cond_var . bool (), Fragment . create ( c1 ), c2 )
147+ if isinstance ( c2 , types . Unset ) :
140148 raise ValueError ("For conditional vars, the second argument must be set." )
141149
142150 # convert the truth and false cond parts into vars so the _var_data can be obtained.
0 commit comments