From 0e6d2a4c39322e08312bbec5d9368bb8284b33db Mon Sep 17 00:00:00 2001 From: Per Lunnemann Hansen Date: Mon, 27 Jan 2025 14:52:24 +0100 Subject: [PATCH] fix: update component registration to use new class reference (#8715) The pyright language server is now able to resolve the import and provide completions for the component. Co-authored-by: Michele Pangrazzi --- haystack/core/component/component.py | 16 +++++++++------- .../component-registration-052467bb409d2e4c.yaml | 4 ++++ 2 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 releasenotes/notes/component-registration-052467bb409d2e4c.yaml diff --git a/haystack/core/component/component.py b/haystack/core/component/component.py index d77fd77593..740b3d154e 100644 --- a/haystack/core/component/component.py +++ b/haystack/core/component/component.py @@ -512,10 +512,12 @@ 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( @@ -523,15 +525,15 @@ def copy_class_namespace(namespace): 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 diff --git a/releasenotes/notes/component-registration-052467bb409d2e4c.yaml b/releasenotes/notes/component-registration-052467bb409d2e4c.yaml new file mode 100644 index 0000000000..a3c575f656 --- /dev/null +++ b/releasenotes/notes/component-registration-052467bb409d2e4c.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - | + Fixes a bug that causes pyright type checker to fail for all component objects.