0
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2025-07-14 03:31:50 +02:00

refactor: improve pylint score

This commit is contained in:
wyzula-jan
2023-11-22 13:24:23 +01:00
parent b21c1db2a9
commit a4d9713785
3 changed files with 34 additions and 29 deletions

View File

@ -3,9 +3,11 @@ import subprocess
import qdarktheme
from jedi import Script
from jedi.api import Completion
# pylint: disable=no-name-in-module
from qtpy.Qsci import QsciScintilla, QsciLexerPython, QsciAPIs
from qtpy.QtCore import QFile, QTextStream, Signal, QThread
from qtpy.QtCore import Qt
from qtpy.QtCore import Signal, QThread
from qtpy.QtGui import QColor, QFont
from qtpy.QtWidgets import (
QApplication,
@ -29,7 +31,7 @@ class AutoCompleter(QThread):
"""
def __init__(self, file_path: str, api: QsciAPIs, enable_docstring: bool = False):
super(AutoCompleter, self).__init__(None)
super().__init__(None)
self.file_path = file_path
self.script: Script = None
self.api: QsciAPIs = api
@ -78,7 +80,7 @@ class AutoCompleter(QThread):
full_docstring = signatures[0].docstring(raw=True)
compact_docstring = self.get_compact_docstring(full_docstring)
return compact_docstring
elif signatures and self.enable_docstring is False:
if signatures and self.enable_docstring is False:
return signatures[0].to_string()
except Exception as err:
print(f"Signature Error:{err}")
@ -91,7 +93,8 @@ class AutoCompleter(QThread):
completions (list[Completion]): A list of Completion objects to be added to the API.
"""
self.api.clear()
[self.api.add(i.name) for i in completions]
for i in completions:
self.api.add(i.name)
self.api.prepare()
def get_completions(self, line: int, index: int, text: str):
@ -176,7 +179,7 @@ class BECEditor(QWidget):
def __init__(self, toolbar_enabled=True, docstring_tooltip=False):
super().__init__()
self.scriptRunnerThread = None
self.script_runner_thread = None
self.file_path = None
self.docstring_tooltip = docstring_tooltip
# TODO just temporary solution, could be extended to other languages
@ -267,7 +270,6 @@ class BECEditor(QWidget):
def loaded_autocomplete(self):
"""Placeholder method for actions after autocompletion data is loaded."""
pass
def set_editor_style(self):
"""Sets the style and color scheme for the editor."""
@ -317,9 +319,9 @@ class BECEditor(QWidget):
def run_script(self):
"""Runs the current script in the editor."""
script = self.editor.text()
self.scriptRunnerThread = ScriptRunnerThread(script)
self.scriptRunnerThread.outputSignal.connect(self.update_terminal)
self.scriptRunnerThread.start()
self.script_runner_thread = ScriptRunnerThread(script)
self.script_runner_thread.outputSignal.connect(self.update_terminal)
self.script_runner_thread.start()
def update_terminal(self, text):
"""Updates the terminal with new text.

View File

@ -9,6 +9,8 @@ from qtpy.QtWidgets import QWidget
class ToolBarAction(ABC):
"""Abstract base class for action creators for the toolbar."""
@abstractmethod
def create(self, target: QWidget):
"""Creates and returns an action to be added to a toolbar.
@ -21,10 +23,11 @@ class ToolBarAction(ABC):
Returns:
QAction: The action created for the toolbar.
"""
pass
class OpenFileAction: # (ToolBarAction):
"""Action creator for the 'Open File' action in the toolbar."""
def create(self, target: QWidget):
"""Creates an 'Open File' action for the toolbar.
@ -42,6 +45,8 @@ class OpenFileAction: # (ToolBarAction):
class SaveFileAction:
"""Action creator for the 'Save File' action in the toolbar."""
def create(self, target):
"""Creates a 'Save File' action for the toolbar.
@ -59,6 +64,8 @@ class SaveFileAction:
class RunScriptAction:
"""Action creator for the 'Run Script' action in the toolbar."""
def create(self, target):
"""Creates a 'Run Script' action for the toolbar.

View File

@ -1,16 +1,16 @@
# pylint: disable=no-name-in-module
import os
import subprocess
import tempfile
import time
from unittest.mock import MagicMock
from unittest.mock import patch, mock_open
import pytest
from unittest.mock import MagicMock, Mock, patch
from unittest.mock import patch, mock_open
from PyQt6.QtWidgets import QTextEdit
from qtpy.Qsci import QsciScintilla, QsciLexerPython, QsciAPIs
from bec_widgets.widgets import BECEditor
from bec_widgets.widgets.editor.editor import AutoCompleter, ScriptRunnerThread, BECEditor
from qtpy.QtWidgets import QTextEdit
from qtpy.Qsci import QsciScintilla
from bec_widgets.widgets.editor.editor import AutoCompleter, BECEditor
@pytest.fixture(scope="function")
@ -66,7 +66,7 @@ def test_autocompleter_suggestions(mock_script, editor, qtbot):
"docstring_enabled, expected_signature",
[(True, "Mocked signature with docstring"), (False, "Mocked signature")],
)
def test_autocompleter_signature(mock_script, editor, qtbot, docstring_enabled, expected_signature):
def test_autocompleter_signature(mock_script, editor, docstring_enabled, expected_signature):
"""Test if the autocompleter provides correct function signature based on docstring setting."""
# Set docstring mode based on parameter
editor.docstring_tooltip = docstring_enabled
@ -92,7 +92,7 @@ def test_autocompleter_signature(mock_script, editor, qtbot, docstring_enabled,
assert signature == expected_signature
def test_open_file(editor, qtbot):
def test_open_file(editor):
"""Test open_file method of BECEditor."""
# Create a temporary file with some content
with tempfile.NamedTemporaryFile(delete=False, suffix=".py") as temp_file:
@ -104,15 +104,13 @@ def test_open_file(editor, qtbot):
editor.open_file()
# Verify if the editor's text is set to the file content
assert (
editor.editor.text() == "test file content"
), "Editor should contain the text from the opened file"
assert editor.editor.text() == "test file content"
# Clean up by removing the temporary file
os.remove(temp_file.name)
def test_save_file(editor, qtbot):
def test_save_file(editor):
"""Test save_file method of BECEditor."""
# Set some text in the editor
editor.editor.setText("test save content")
@ -131,7 +129,7 @@ def test_save_file(editor, qtbot):
mock_file().write.assert_called_with("test save content")
def test_open_file_through_toolbar(editor, qtbot):
def test_open_file_through_toolbar(editor):
"""Test the open_file method through the ModularToolBar."""
# Create a temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix=".py") as temp_file:
@ -146,15 +144,13 @@ def test_open_file_through_toolbar(editor, qtbot):
with patch("builtins.open", new_callable=mock_open, read_data="test file content"):
open_action.trigger()
# Verify if the editor's text is set to the file content
assert (
editor.editor.text() == "test file content"
), "Editor should contain the text from the opened file"
assert editor.editor.text() == "test file content"
# Clean up
os.remove(temp_file.name)
def test_save_file_through_toolbar(editor, qtbot):
def test_save_file_through_toolbar(editor):
"""Test the save_file method through the ModularToolBar."""
# Set some text in the editor
editor.editor.setText("test save content")