From 1a83f85783943fe669a7ff1a5b7d6e83fd0616b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Brand=C3=A9ho?= Date: Fri, 17 Nov 2023 21:30:18 +0100 Subject: [PATCH] _valid_children and _invalid_children accessible from class (#2192) --- reflex/components/component.py | 18 +++++++++--------- reflex/components/datadisplay/table.py | 12 ++++++------ reflex/components/forms/button.py | 2 +- .../components/graphing/recharts/cartesian.py | 2 +- reflex/components/overlay/menu.py | 2 +- tests/components/test_component.py | 8 ++++---- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/reflex/components/component.py b/reflex/components/component.py index 1732eb14e..1771bea6d 100644 --- a/reflex/components/component.py +++ b/reflex/components/component.py @@ -67,10 +67,10 @@ class Component(Base, ABC): autofocus: bool = False # components that cannot be children - invalid_children: List[str] = [] + _invalid_children: List[str] = [] - # components that are only allowed as children - valid_children: List[str] = [] + # only components that are allowed as children + _valid_children: List[str] = [] # custom attribute custom_attrs: Dict[str, str] = {} @@ -532,21 +532,21 @@ class Component(Base, ABC): children: The children of the component. """ - if not self.invalid_children and not self.valid_children: + if not self._invalid_children and not self._valid_children: return comp_name = type(self).__name__ def validate_invalid_child(child_name): - if child_name in self.invalid_children: + if child_name in self._invalid_children: raise ValueError( f"The component `{comp_name}` cannot have `{child_name}` as a child component" ) def validate_valid_child(child_name): - if child_name not in self.valid_children: + if child_name not in self._valid_children: valid_child_list = ", ".join( - [f"`{v_child}`" for v_child in self.valid_children] + [f"`{v_child}`" for v_child in self._valid_children] ) raise ValueError( f"The component `{comp_name}` only allows the components: {valid_child_list} as children. Got `{child_name}` instead." @@ -555,10 +555,10 @@ class Component(Base, ABC): for child in children: name = type(child).__name__ - if self.invalid_children: + if self._invalid_children: validate_invalid_child(name) - if self.valid_children: + if self._valid_children: validate_valid_child(name) def _get_custom_code(self) -> str | None: diff --git a/reflex/components/datadisplay/table.py b/reflex/components/datadisplay/table.py index 3094f7470..cf4160133 100644 --- a/reflex/components/datadisplay/table.py +++ b/reflex/components/datadisplay/table.py @@ -65,7 +65,7 @@ class Thead(ChakraComponent): tag = "Thead" # invalid children components - invalid_children: List[str] = ["Tbody", "Thead", "Tfoot"] + _invalid_children: List[str] = ["Tbody", "Thead", "Tfoot"] @classmethod def create(cls, *children, headers=None, **props) -> Component: @@ -117,7 +117,7 @@ class Tbody(ChakraComponent): tag = "Tbody" # invalid children components - invalid_children: List[str] = ["Tbody", "Thead", "Tfoot", "Td", "Th"] + _invalid_children: List[str] = ["Tbody", "Thead", "Tfoot", "Td", "Th"] @classmethod def create(cls, *children, rows=None, **props) -> Component: @@ -191,7 +191,7 @@ class Tfoot(ChakraComponent): tag = "Tfoot" # invalid children components - invalid_children: List[str] = ["Tbody", "Thead", "Td", "Th", "Tfoot"] + _invalid_children: List[str] = ["Tbody", "Thead", "Td", "Th", "Tfoot"] @classmethod def create(cls, *children, footers=None, **props) -> Component: @@ -240,7 +240,7 @@ class Tr(ChakraComponent): tag = "Tr" # invalid children components - invalid_children: List[str] = ["Tbody", "Thead", "Tfoot", "Tr"] + _invalid_children: List[str] = ["Tbody", "Thead", "Tfoot", "Tr"] @classmethod def create(cls, *children, cell_type: str = "", cells=None, **props) -> Component: @@ -271,7 +271,7 @@ class Th(ChakraComponent): tag = "Th" # invalid children components - invalid_children: List[str] = ["Tbody", "Thead", "Tr", "Td", "Th"] + _invalid_children: List[str] = ["Tbody", "Thead", "Tr", "Td", "Th"] # Aligns the cell content to the right. is_numeric: Var[bool] @@ -283,7 +283,7 @@ class Td(ChakraComponent): tag = "Td" # invalid children components - invalid_children: List[str] = ["Tbody", "Thead"] + _invalid_children: List[str] = ["Tbody", "Thead"] # Aligns the cell content to the right. is_numeric: Var[bool] diff --git a/reflex/components/forms/button.py b/reflex/components/forms/button.py index f8990f0a9..63c34ffae 100644 --- a/reflex/components/forms/button.py +++ b/reflex/components/forms/button.py @@ -54,7 +54,7 @@ class Button(ChakraComponent): type_: Var[str] # Components that are not allowed as children. - invalid_children: List[str] = ["Button", "MenuButton"] + _invalid_children: List[str] = ["Button", "MenuButton"] # The name of the form field name: Var[str] diff --git a/reflex/components/graphing/recharts/cartesian.py b/reflex/components/graphing/recharts/cartesian.py index 503149fb2..24fd4bb08 100644 --- a/reflex/components/graphing/recharts/cartesian.py +++ b/reflex/components/graphing/recharts/cartesian.py @@ -243,7 +243,7 @@ class Area(Cartesian): stack_id: Var[str] # Valid children components - valid_children: List[str] = ["LabelList"] + _valid_children: List[str] = ["LabelList"] class Bar(Cartesian): diff --git a/reflex/components/overlay/menu.py b/reflex/components/overlay/menu.py index 4f5985030..2c38afa13 100644 --- a/reflex/components/overlay/menu.py +++ b/reflex/components/overlay/menu.py @@ -121,7 +121,7 @@ class MenuButton(ChakraComponent): variant: Var[str] # Components that are not allowed as children. - invalid_children: List[str] = ["Button", "MenuButton"] + _invalid_children: List[str] = ["Button", "MenuButton"] # The tag to use for the menu button. as_: Var[str] diff --git a/tests/components/test_component.py b/tests/components/test_component.py index c8f83fc42..f2f481e29 100644 --- a/tests/components/test_component.py +++ b/tests/components/test_component.py @@ -126,9 +126,9 @@ def component5() -> Type[Component]: class TestComponent5(Component): tag = "RandomComponent" - invalid_children: List[str] = ["Text"] + _invalid_children: List[str] = ["Text"] - valid_children: List[str] = ["Text"] + _valid_children: List[str] = ["Text"] return TestComponent5 @@ -144,7 +144,7 @@ def component6() -> Type[Component]: class TestComponent6(Component): tag = "RandomComponent" - invalid_children: List[str] = ["Text"] + _invalid_children: List[str] = ["Text"] return TestComponent6 @@ -160,7 +160,7 @@ def component7() -> Type[Component]: class TestComponent7(Component): tag = "RandomComponent" - valid_children: List[str] = ["Text"] + _valid_children: List[str] = ["Text"] return TestComponent7