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

fix: update component registration to use new class reference #8715

Merged
merged 2 commits into from
Jan 27, 2025
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
16 changes: 9 additions & 7 deletions haystack/core/component/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,26 +512,28 @@ def copy_class_namespace(namespace):
# We must explicitly redefine the type of the class to make sure language servers
# and type checkers understand that the class is of the correct type.
# mypy doesn't like that we do this though so we explicitly ignore the type check.
cls: cls.__name__ = new_class(cls.__name__, cls.__bases__, {"metaclass": ComponentMeta}, copy_class_namespace) # type: ignore[no-redef]
new_cls: cls.__name__ = new_class(
cls.__name__, cls.__bases__, {"metaclass": ComponentMeta}, copy_class_namespace
) # type: ignore[no-redef]

# Save the component in the class registry (for deserialization)
class_path = f"{cls.__module__}.{cls.__name__}"
class_path = f"{new_cls.__module__}.{new_cls.__name__}"
if class_path in self.registry:
# Corner case, but it may occur easily in notebooks when re-running cells.
logger.debug(
"Component {component} is already registered. Previous imported from '{module_name}', \
new imported from '{new_module_name}'",
component=class_path,
module_name=self.registry[class_path],
new_module_name=cls,
new_module_name=new_cls,
)
self.registry[class_path] = cls
logger.debug("Registered Component {component}", component=cls)
self.registry[class_path] = new_cls
logger.debug("Registered Component {component}", component=new_cls)

# Override the __repr__ method with a default one
cls.__repr__ = _component_repr
new_cls.__repr__ = _component_repr

return cls
return new_cls

def __call__(self, cls: Optional[type] = None):
# We must wrap the call to the decorator in a function for it to work
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
Fixes a bug that causes pyright type checker to fail for all component objects.
Loading