21
21
Type ,
22
22
TypeVar ,
23
23
Union ,
24
+ cast ,
24
25
get_args ,
25
26
get_origin ,
26
27
)
27
28
29
+ import pydantic .v1
30
+ import pydantic .v1 .fields
31
+
28
32
import reflex .state
29
33
from reflex .base import Base
30
34
from reflex .compiler .templates import STATEFUL_COMPONENT
@@ -73,16 +77,16 @@ class BaseComponent(Base, ABC):
73
77
"""
74
78
75
79
# The children nested within the component.
76
- children : list [BaseComponent ] = []
80
+ children : list [BaseComponent ] = pydantic . v1 . Field ( default_factory = list )
77
81
78
82
# The library that the component is based on.
79
83
library : str | None = None
80
84
81
85
# List here the non-react dependency needed by `library`
82
- lib_dependencies : list [str ] = []
86
+ lib_dependencies : list [str ] = pydantic . v1 . Field ( default_factory = list )
83
87
84
88
# List here the dependencies that need to be transpiled by Next.js
85
- transpile_packages : list [str ] = []
89
+ transpile_packages : list [str ] = pydantic . v1 . Field ( default_factory = list )
86
90
87
91
# The tag to use when rendering the component.
88
92
tag : str | None = None
@@ -262,10 +266,12 @@ class Component(BaseComponent, ABC):
262
266
"""A component with style, event trigger and other props."""
263
267
264
268
# The style of the component.
265
- style : Style = Style ( )
269
+ style : Style = pydantic . v1 . Field ( default_factory = Style )
266
270
267
271
# A mapping from event triggers to event chains.
268
- event_triggers : dict [str , EventChain | Var ] = {}
272
+ event_triggers : dict [str , EventChain | Var ] = pydantic .v1 .Field (
273
+ default_factory = dict
274
+ )
269
275
270
276
# The alias for the tag.
271
277
alias : str | None = None
@@ -283,28 +289,30 @@ class Component(BaseComponent, ABC):
283
289
class_name : Any = None
284
290
285
291
# Special component props.
286
- special_props : list [Var ] = []
292
+ special_props : list [Var ] = pydantic . v1 . Field ( default_factory = list )
287
293
288
294
# Whether the component should take the focus once the page is loaded
289
295
autofocus : bool = False
290
296
291
297
# components that cannot be children
292
- _invalid_children : list [str ] = []
298
+ _invalid_children : ClassVar [ list [str ] ] = []
293
299
294
300
# only components that are allowed as children
295
- _valid_children : list [str ] = []
301
+ _valid_children : ClassVar [ list [str ] ] = []
296
302
297
303
# only components that are allowed as parent
298
- _valid_parents : list [str ] = []
304
+ _valid_parents : ClassVar [ list [str ] ] = []
299
305
300
306
# props to change the name of
301
- _rename_props : dict [str , str ] = {}
307
+ _rename_props : ClassVar [ dict [str , str ] ] = {}
302
308
303
309
# custom attribute
304
- custom_attrs : dict [str , Var | Any ] = {}
310
+ custom_attrs : dict [str , Var | Any ] = pydantic . v1 . Field ( default_factory = dict )
305
311
306
312
# When to memoize this component and its children.
307
- _memoization_mode : MemoizationMode = MemoizationMode ()
313
+ _memoization_mode : MemoizationMode = pydantic .v1 .PrivateAttr (
314
+ default_factory = MemoizationMode
315
+ )
308
316
309
317
# State class associated with this component instance
310
318
State : Type [reflex .state .State ] | None = None
@@ -582,9 +590,15 @@ def determine_key(value: Any):
582
590
"&" : style ,
583
591
}
584
592
593
+ fields_style = self .get_fields ()["style" ]
594
+
585
595
kwargs ["style" ] = Style (
586
596
{
587
- ** self .get_fields ()["style" ].default ,
597
+ ** (
598
+ fields_style .default_factory ()
599
+ if fields_style .default_factory
600
+ else fields_style .default
601
+ ),
588
602
** style ,
589
603
** {attr : value for attr , value in kwargs .items () if attr not in fields },
590
604
}
0 commit comments