Skip to content

Commit 31d6c8b

Browse files
committed
Rebuild models if necessary
1 parent 5d9d604 commit 31d6c8b

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

src/python-fastui/fastui/components/__init__.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from ..base import BaseModel
1616
from .display import Details, Display
1717
from .forms import (
18+
BaseForm,
1819
Form,
1920
FormField,
2021
FormFieldBoolean,
@@ -103,7 +104,7 @@ class PageTitle(BaseModel, extra='forbid'):
103104
"""The type of the component. Always 'PageTitle'."""
104105

105106

106-
class Div(BaseModel, extra='forbid'):
107+
class Div(BaseModel, defer_build=True, extra='forbid'):
107108
"""A generic container component."""
108109

109110
components: '_t.List[AnyComponent]'
@@ -116,7 +117,7 @@ class Div(BaseModel, extra='forbid'):
116117
"""The type of the component. Always 'Div'."""
117118

118119

119-
class Page(BaseModel, extra='forbid'):
120+
class Page(BaseModel, defer_build=True, extra='forbid'):
120121
"""Similar to `container` in many UI frameworks, this acts as a root component for most pages."""
121122

122123
components: '_t.List[AnyComponent]'
@@ -240,7 +241,7 @@ class Button(BaseModel, extra='forbid'):
240241
"""The type of the component. Always 'Button'."""
241242

242243

243-
class Link(BaseModel, extra='forbid'):
244+
class Link(BaseModel, defer_build=True, extra='forbid'):
244245
"""Link component."""
245246

246247
components: '_t.List[AnyComponent]'
@@ -328,7 +329,7 @@ class Footer(BaseModel, extra='forbid'):
328329
"""The type of the component. Always 'Footer'."""
329330

330331

331-
class Modal(BaseModel, extra='forbid'):
332+
class Modal(BaseModel, defer_build=True, extra='forbid'):
332333
"""Modal component that displays a modal dialog."""
333334

334335
title: str
@@ -353,7 +354,7 @@ class Modal(BaseModel, extra='forbid'):
353354
"""The type of the component. Always 'Modal'."""
354355

355356

356-
class ServerLoad(BaseModel, extra='forbid'):
357+
class ServerLoad(BaseModel, defer_build=True, extra='forbid'):
357358
"""A component that will be replaced by the server with the component returned by the given URL."""
358359

359360
path: str
@@ -539,7 +540,7 @@ class Spinner(BaseModel, extra='forbid'):
539540
"""The type of the component. Always 'Spinner'."""
540541

541542

542-
class Toast(BaseModel, extra='forbid'):
543+
class Toast(BaseModel, defer_build=True, extra='forbid'):
543544
"""Toast component that displays a toast message (small temporary message)."""
544545

545546
title: str
@@ -636,3 +637,14 @@ class Custom(BaseModel, extra='forbid'):
636637
"""Union of all components.
637638
638639
Pydantic discriminator field is set to 'type' to allow for efficient serialization and deserialization of the components."""
640+
641+
# Rebuild models:
642+
BaseForm.model_rebuild(_types_namespace={'AnyComponent': AnyComponent})
643+
Form.model_rebuild(_types_namespace={'AnyComponent': AnyComponent})
644+
ModelForm.model_rebuild(_types_namespace={'AnyComponent': AnyComponent})
645+
Div.model_rebuild(_types_namespace={'AnyComponent': AnyComponent})
646+
Page.model_rebuild(_types_namespace={'AnyComponent': AnyComponent})
647+
Link.model_rebuild(_types_namespace={'AnyComponent': AnyComponent})
648+
Modal.model_rebuild(_types_namespace={'AnyComponent': AnyComponent})
649+
ServerLoad.model_rebuild(_types_namespace={'AnyComponent': AnyComponent})
650+
Toast.model_rebuild(_types_namespace={'AnyComponent': AnyComponent})

src/python-fastui/fastui/components/forms.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def default_footer(self) -> _te.Self:
200200
return self
201201

202202

203-
class Form(BaseForm):
203+
class Form(BaseForm, defer_build=True):
204204
"""Form component."""
205205

206206
form_fields: _t.List[FormField]
@@ -213,7 +213,7 @@ class Form(BaseForm):
213213
FormFieldsModel = _t.TypeVar('FormFieldsModel', bound=pydantic.BaseModel)
214214

215215

216-
class ModelForm(BaseForm):
216+
class ModelForm(BaseForm, defer_build=True):
217217
"""Form component generated from a Pydantic model."""
218218

219219
model: _t.Type[pydantic.BaseModel] = pydantic.Field(exclude=True)

src/python-fastui/fastui/events.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
ContextType = TypeAliasType('ContextType', Dict[str, Union[str, int]])
99

1010

11-
class PageEvent(BaseModel):
11+
class PageEvent(BaseModel, defer_build=True):
1212
name: str
1313
push_path: Union[str, None] = None
1414
context: Union[ContextType, None] = None
@@ -37,3 +37,5 @@ class AuthEvent(BaseModel):
3737

3838

3939
AnyEvent = Annotated[Union[PageEvent, GoToEvent, BackEvent, AuthEvent], Field(discriminator='type')]
40+
41+
PageEvent.model_rebuild(_types_namespace={'AnyEvent': AnyEvent})

0 commit comments

Comments
 (0)