Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "[REF-2269] Add add_imports API for component class (#2937)" #2978

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading