Skip to content

Commit

Permalink
Revert "[REF-2269] Add add_imports API for component class (#2937)" (
Browse files Browse the repository at this point in the history
…#2978)

This reverts commit 8edd1df.
  • Loading branch information
martinxu9 authored Apr 1, 2024
1 parent c357405 commit 6d400ed
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 70 deletions.
27 changes: 0 additions & 27 deletions reflex/components/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
)

Expand Down
44 changes: 1 addition & 43 deletions tests/components/test_component.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, List, Type, Union
from typing import Any, Dict, List, Type

import pytest

Expand Down Expand Up @@ -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]:
Expand Down

0 comments on commit 6d400ed

Please sign in to comment.