diff --git a/src/tagstudio/qt/modals/tag_search.py b/src/tagstudio/qt/modals/tag_search.py index ca980a682..8f8c89a90 100644 --- a/src/tagstudio/qt/modals/tag_search.py +++ b/src/tagstudio/qt/modals/tag_search.py @@ -42,7 +42,7 @@ class TagSearchPanel(PanelWidget): tag_chosen = Signal(int) lib: Library - driver: "QtDriver" + driver: "QtDriver | None" = None is_initialized: bool = False first_tag_id: int | None = None is_tag_chooser: bool @@ -56,12 +56,11 @@ class TagSearchPanel(PanelWidget): def __init__( self, library: Library, - exclude: list[int] = None, + exclude: list[int] | None = None, is_tag_chooser: bool = True, ): super().__init__() self.lib = library - self.driver = None self.exclude = exclude or [] self.is_tag_chooser = is_tag_chooser @@ -194,7 +193,8 @@ def update_tags(self, query: str | None = None): create_button: QPushButton | None = None if self.create_button_in_layout and self.scroll_layout.count(): create_button = self.scroll_layout.takeAt(self.scroll_layout.count() - 1).widget() # type: ignore - create_button.deleteLater() + if create_button: + create_button.deleteLater() self.create_button_in_layout = False # Get results for the search query @@ -264,7 +264,7 @@ def set_tag_widget(self, tag: Tag | None, index: int): self.scroll_layout.addWidget(new_tw) # Assign the tag to the widget at the given index. - tag_widget: TagWidget = self.scroll_layout.itemAt(index).widget() + assert isinstance(tag_widget := self.scroll_layout.itemAt(index).widget(), TagWidget) tag_widget.set_tag(tag) # Set tag widget viability and potentially return early @@ -279,14 +279,13 @@ def set_tag_widget(self, tag: Tag | None, index: int): tag_widget.has_remove = has_remove_button with catch_warnings(record=True): + tag_widget.on_click.disconnect() tag_widget.on_edit.disconnect() tag_widget.on_remove.disconnect() - tag_widget.bg_button.clicked.disconnect() - tag_id = tag.id + tag_widget.on_click.connect(lambda t=tag: self.edit_tag(t)) tag_widget.on_edit.connect(lambda t=tag: self.edit_tag(t)) tag_widget.on_remove.connect(lambda t=tag: self.delete_tag(t)) - tag_widget.bg_button.clicked.connect(lambda: self.tag_chosen.emit(tag_id)) if self.driver: tag_widget.search_for_tag_action.triggered.connect( @@ -294,6 +293,8 @@ def set_tag_widget(self, tag: Tag | None, index: int): self.driver.main_window.search_field.setText(f"tag_id:{tag_id}"), self.driver.update_browsing_state(BrowsingState.from_tag_id(tag_id)), ) + if self.driver + else None ) tag_widget.search_for_tag_action.setEnabled(True) else: