diff --git a/reflex/components/component.py b/reflex/components/component.py index eea570a40b..f1d7833bfd 100644 --- a/reflex/components/component.py +++ b/reflex/components/component.py @@ -1029,16 +1029,6 @@ def _get_hooks_imports(self) -> imports.ImportDict: ) return _imports - def add_imports( - self, - ) -> Dict[str, Union[str, ImportVar, List[str | ImportVar]]]: - """User defined imports for the component. Need to be overriden in subclass. - - Returns: - The user defined imports as a dict. - """ - return {} - def _get_imports(self) -> imports.ImportDict: """Get all the libraries and fields that are used by the component. @@ -1059,29 +1049,12 @@ def _get_imports(self) -> imports.ImportDict: var._var_data.imports for var in self._get_vars() if var._var_data ] - # If the subclass implements add_imports, merge the imports. - def _make_list( - value: str | ImportVar | list[str | ImportVar], - ) -> list[str | ImportVar]: - if isinstance(value, (str, ImportVar)): - return [value] - return value - - added_imports = { - package: [ - ImportVar(tag=tag) if not isinstance(tag, ImportVar) else tag - for tag in _make_list(maybe_tags) - ] - for package, maybe_tags in self.add_imports().items() - } - return imports.merge_imports( *self._get_props_imports(), self._get_dependencies_imports(), self._get_hooks_imports(), _imports, event_imports, - added_imports, *var_imports, ) diff --git a/tests/components/test_component.py b/tests/components/test_component.py index c5f5f9d516..fef5506e51 100644 --- a/tests/components/test_component.py +++ b/tests/components/test_component.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, List, Type, Union +from typing import Any, Dict, List, Type import pytest @@ -1310,48 +1310,6 @@ def outer(c: Component): assert "other" in imports_outer -@pytest.mark.parametrize( - "tags", - ( - ["Component"], - ["Component", "useState"], - [ImportVar(tag="Component")], - [ImportVar(tag="Component"), ImportVar(tag="useState")], - ["Component", ImportVar(tag="useState")], - ), -) -def test_custom_component_add_imports(tags): - def _list_to_import_vars(tags: List[str]) -> List[ImportVar]: - return [ - ImportVar(tag=tag) if not isinstance(tag, ImportVar) else tag - for tag in tags - ] - - class BaseComponent(Component): - def _get_imports(self) -> imports.ImportDict: - return {} - - class Reference(Component): - def _get_imports(self) -> imports.ImportDict: - return imports.merge_imports( - super()._get_imports(), - {"react": _list_to_import_vars(tags)}, - ) - - class Test(Component): - def add_imports( - self, - ) -> Dict[str, Union[str, ImportVar, List[str], List[ImportVar]]]: - - return {"react": (tags[0] if len(tags) == 1 else tags)} - - baseline = Reference.create() - test = Test.create() - - assert baseline.get_imports() == {"react": _list_to_import_vars(tags)} - assert test.get_imports() == baseline.get_imports() - - def test_custom_component_declare_event_handlers_in_fields(): class ReferenceComponent(Component): def get_event_triggers(self) -> Dict[str, Any]: