From 36be3c4b8fbd3448a9c867a36e19e6903fe6d96a Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Fri, 17 Feb 2023 16:11:17 -0600 Subject: [PATCH] Fix or ignore MyPy issues in PyQt desktop GUI code - Remove unneeded type ignore for mps with the latest mypy - Stop excluding PyQT desktop GUI code from MyPy checks - Do not warn about unused ignores. Some issue with mypy giving different errors in different environments (venv, system and pre-commit) --- pyproject.toml | 7 +----- src/khoj/interface/desktop/file_browser.py | 4 ++-- src/khoj/interface/desktop/main_window.py | 26 +++++++++++----------- src/khoj/interface/desktop/system_tray.py | 2 +- src/khoj/utils/state.py | 2 +- 5 files changed, 18 insertions(+), 23 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 8194525b..119f4f3e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -87,12 +87,7 @@ install_types = true ignore_missing_imports = true non_interactive = true show_error_codes = true -warn_unused_ignores = true -exclude = [ - "src/khoj/interface/desktop/main_window.py", - "src/khoj/interface/desktop/file_browser.py", - "src/khoj/interface/desktop/system_tray.py", -] +warn_unused_ignores = false [tool.black] line-length = 120 diff --git a/src/khoj/interface/desktop/file_browser.py b/src/khoj/interface/desktop/file_browser.py index 97c2ddda..4ce9725e 100644 --- a/src/khoj/interface/desktop/file_browser.py +++ b/src/khoj/interface/desktop/file_browser.py @@ -27,11 +27,11 @@ class FileBrowser(QtWidgets.QWidget): self.lineEdit.setFixedWidth(330) self.setFiles(default_files) self.lineEdit.setFixedHeight(min(7 + 20 * len(self.lineEdit.toPlainText().split("\n")), 90)) - self.lineEdit.textChanged.connect(self.updateFieldHeight) + self.lineEdit.textChanged.connect(self.updateFieldHeight) # type: ignore[attr-defined] layout.addWidget(self.lineEdit) self.button = QtWidgets.QPushButton("Add") - self.button.clicked.connect(self.storeFilesSelectedInFileDialog) + self.button.clicked.connect(self.storeFilesSelectedInFileDialog) # type: ignore[attr-defined] layout.addWidget(self.button) layout.addStretch() diff --git a/src/khoj/interface/desktop/main_window.py b/src/khoj/interface/desktop/main_window.py index d2150ed1..a6379682 100644 --- a/src/khoj/interface/desktop/main_window.py +++ b/src/khoj/interface/desktop/main_window.py @@ -53,7 +53,7 @@ class MainWindow(QtWidgets.QMainWindow): self.setWindowIcon(QtGui.QIcon(f"{icon_path.absolute()}")) # Initialize Configure Window Layout - self.layout = QtWidgets.QVBoxLayout() + self.wlayout = QtWidgets.QVBoxLayout() # Add Settings Panels for each Search Type to Configure Window Layout self.search_settings_panels = [] @@ -73,7 +73,7 @@ class MainWindow(QtWidgets.QMainWindow): # Set the central widget of the Window. Widget will expand # to take up all the space in the window by default. self.config_window = QtWidgets.QWidget() - self.config_window.setLayout(self.layout) + self.config_window.setLayout(self.wlayout) self.setCentralWidget(self.config_window) self.position_window() @@ -97,12 +97,12 @@ class MainWindow(QtWidgets.QMainWindow): # Set enabled/disabled based on checkbox state enable_search_type.setChecked(current_content_files is not None and len(current_content_files) > 0) input_files.setEnabled(enable_search_type.isChecked()) - enable_search_type.stateChanged.connect(lambda _: input_files.setEnabled(enable_search_type.isChecked())) + enable_search_type.stateChanged.connect(lambda _: input_files.setEnabled(enable_search_type.isChecked())) # type: ignore[attr-defined] # Add setting widgets for given search type to panel search_type_layout.addWidget(enable_search_type) search_type_layout.addWidget(input_files) - self.layout.addWidget(search_type_settings) + self.wlayout.addWidget(search_type_settings) return search_type_settings @@ -121,12 +121,12 @@ class MainWindow(QtWidgets.QMainWindow): # Set enabled/disabled based on checkbox state enable_conversation.setChecked(current_openai_api_key is not None) input_field.setEnabled(enable_conversation.isChecked()) - enable_conversation.stateChanged.connect(lambda _: input_field.setEnabled(enable_conversation.isChecked())) + enable_conversation.stateChanged.connect(lambda _: input_field.setEnabled(enable_conversation.isChecked())) # type: ignore[attr-defined] # Add setting widgets for given processor type to panel processor_type_layout.addWidget(enable_conversation) processor_type_layout.addWidget(input_field) - self.layout.addWidget(processor_type_settings) + self.wlayout.addWidget(processor_type_settings) return processor_type_settings @@ -144,15 +144,15 @@ class MainWindow(QtWidgets.QMainWindow): action_bar_layout.addWidget(self.configure_button) action_bar_layout.addWidget(self.search_button) - self.layout.addWidget(action_bar) + self.wlayout.addWidget(action_bar) def get_default_config(self, search_type: SearchType = None, processor_type: ProcessorType = None): "Get default config" config = constants.default_config if search_type: - return config["content-type"][search_type] + return config["content-type"][search_type] # type: ignore elif processor_type: - return config["processor"][processor_type] + return config["processor"][processor_type] # type: ignore else: return config @@ -160,12 +160,12 @@ class MainWindow(QtWidgets.QMainWindow): "Add Error Message to Configure Screen" # Remove any existing error messages for message_prefix in ErrorType: - for i in reversed(range(self.layout.count())): - current_widget = self.layout.itemAt(i).widget() + for i in reversed(range(self.wlayout.count())): + current_widget = self.wlayout.itemAt(i).widget() if isinstance(current_widget, QtWidgets.QLabel) and current_widget.text().startswith( message_prefix.value ): - self.layout.removeWidget(current_widget) + self.wlayout.removeWidget(current_widget) current_widget.deleteLater() # Add new error message @@ -174,7 +174,7 @@ class MainWindow(QtWidgets.QMainWindow): error_message.setWordWrap(True) error_message.setText(message) error_message.setStyleSheet("color: red") - self.layout.addWidget(error_message) + self.wlayout.addWidget(error_message) def update_search_settings(self): "Update config with search settings from UI" diff --git a/src/khoj/interface/desktop/system_tray.py b/src/khoj/interface/desktop/system_tray.py index 65f4f5c4..52baec80 100644 --- a/src/khoj/interface/desktop/system_tray.py +++ b/src/khoj/interface/desktop/system_tray.py @@ -33,7 +33,7 @@ def create_system_tray(gui: QtWidgets.QApplication, main_window: MainWindow): # Add the menu actions to the menu for action_text, action_function in menu_actions: menu_action = QtGui.QAction(action_text, menu) - menu_action.triggered.connect(action_function) + menu_action.triggered.connect(action_function) # type: ignore[attr-defined] menu.addAction(menu_action) # Add the menu to the system tray diff --git a/src/khoj/utils/state.py b/src/khoj/utils/state.py index 7616cf11..7e6abc1e 100644 --- a/src/khoj/utils/state.py +++ b/src/khoj/utils/state.py @@ -27,7 +27,7 @@ search_index_lock = threading.Lock() if torch.cuda.is_available(): # Use CUDA GPU device = torch.device("cuda:0") -elif version.parse(torch.__version__) >= version.parse("1.13.0.dev") and torch.backends.mps.is_available(): # type: ignore[attr-defined] +elif version.parse(torch.__version__) >= version.parse("1.13.0.dev") and torch.backends.mps.is_available(): # Use Apple M1 Metal Acceleration device = torch.device("mps") else: