Skip to content

Commit

Permalink
Add some type hints to gui
Browse files Browse the repository at this point in the history
  • Loading branch information
eivindjahren committed Jun 6, 2024
1 parent 44f73be commit 56dc74a
Show file tree
Hide file tree
Showing 101 changed files with 921 additions and 635 deletions.
10 changes: 9 additions & 1 deletion src/ert/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
from .ext_param_config import ExtParamConfig
from .external_ert_script import ExternalErtScript
from .field import Field, field_transform
from .forward_model_step import ForwardModelStep
from .forward_model_step import (
ForwardModelStep,
ForwardModelStepJSON,
ForwardModelStepPlugin,
ForwardModelStepValidationError,
)
from .gen_data_config import GenDataConfig
from .gen_kw_config import GenKwConfig, PriorDict, TransformFunction
from .lint_file import lint_file
Expand Down Expand Up @@ -54,6 +59,9 @@
"ExternalErtScript",
"Field",
"ForwardModelStep",
"ForwardModelStepPlugin",
"ForwardModelStepJSON",
"ForwardModelStepValidationError",
"GenDataConfig",
"GenKwConfig",
"TransformFunction",
Expand Down
2 changes: 1 addition & 1 deletion src/ert/gui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import ert.shared


def headless():
def headless() -> bool:
return "DISPLAY" not in os.environ


Expand Down
21 changes: 15 additions & 6 deletions src/ert/gui/about_dialog.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
from typing import Optional

from qtpy.QtCore import QSize, Qt
from qtpy.QtGui import QFont
from qtpy.QtWidgets import QDialog, QHBoxLayout, QLabel, QPushButton, QVBoxLayout
from qtpy.QtWidgets import (
QDialog,
QHBoxLayout,
QLabel,
QPushButton,
QVBoxLayout,
QWidget,
)

import ert.gui as ert_gui


class AboutDialog(QDialog):
def __init__(self, parent):
def __init__(self, parent: Optional[QWidget]) -> None:
QDialog.__init__(self, parent)

self.setWindowTitle("About")
Expand All @@ -23,14 +32,14 @@ def __init__(self, parent):

self.setLayout(main_layout)

def createTopLayout(self):
def createTopLayout(self) -> QHBoxLayout:
top_layout = QHBoxLayout()
top_layout.addLayout(self.createInfoLayout(), 1)

return top_layout

@staticmethod
def createInfoLayout():
def createInfoLayout() -> QVBoxLayout:
info_layout = QVBoxLayout()

ert = QLabel()
Expand Down Expand Up @@ -59,7 +68,7 @@ def createInfoLayout():
return info_layout

@staticmethod
def createGplLayout():
def createGplLayout() -> QVBoxLayout:
gpl = QLabel()
gpl.setText(
'ERT is free software: you can redistribute it and/or modify \
Expand All @@ -80,7 +89,7 @@ def createGplLayout():
gpl_layout.addWidget(gpl)
return gpl_layout

def createButtonLayout(self):
def createButtonLayout(self) -> QHBoxLayout:
button_layout = QHBoxLayout()

close_button = QPushButton("Close")
Expand Down
4 changes: 2 additions & 2 deletions src/ert/gui/ertnotifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self, config_file: str):
QObject.__init__(self)
self._config_file = config_file
self._storage: Optional[Storage] = None
self._current_ensemble = None
self._current_ensemble: Optional[Ensemble] = None
self._is_simulation_running = False

@property
Expand Down Expand Up @@ -49,7 +49,7 @@ def is_simulation_running(self) -> bool:
return self._is_simulation_running

@Slot()
def emitErtChange(self):
def emitErtChange(self) -> None:
self.ertChanged.emit()

@Slot(object)
Expand Down
11 changes: 9 additions & 2 deletions src/ert/gui/ertwidgets/activelabel.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from qtpy.QtGui import QFont
from qtpy.QtWidgets import QLabel

if TYPE_CHECKING:
from .models.valuemodel import ValueModel


class ActiveLabel(QLabel):
def __init__(self, model):
def __init__(self, model: ValueModel) -> None:
QLabel.__init__(self)

self._model = model
Expand All @@ -16,7 +23,7 @@ def __init__(self, model):

self.updateLabel()

def updateLabel(self):
def updateLabel(self) -> None:
"""Retrieves data from the model and inserts it into the edit line"""
model_value = self._model.getValue()
if model_value is None:
Expand Down
2 changes: 1 addition & 1 deletion src/ert/gui/ertwidgets/analysismoduleedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(

self.setLayout(layout)

def showVariablesPopup(self):
def showVariablesPopup(self) -> None:
variable_dialog = AnalysisModuleVariablesPanel(
self.analysis_module, self.ensemble_size
)
Expand Down
14 changes: 7 additions & 7 deletions src/ert/gui/ertwidgets/analysismodulevariablespanel.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def __init__(self, analysis_module: AnalysisModule, ensemble_size: int):
self.setLayout(layout)
self.blockSignals(False)

def update_inversion_algorithm(self, text):
def update_inversion_algorithm(self, text: str) -> None:
self.truncation_spinner.setEnabled(
not any(val in text.lower() for val in ["direct", "exact"])
)
Expand All @@ -147,12 +147,12 @@ def create_horizontal_line() -> QFrame:

def createDoubleSpinBox(
self,
variable_name,
variable_value,
min_value,
max_value,
step_length,
):
variable_name: str,
variable_value: float,
min_value: float,
max_value: float,
step_length: float,
) -> QDoubleSpinBox:
spinner = QDoubleSpinBox()
spinner.setDecimals(6)
spinner.setFixedWidth(180)
Expand Down
14 changes: 7 additions & 7 deletions src/ert/gui/ertwidgets/checklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def __init__(self, model, label: str = "", custom_filter_button=None):

self.modelChanged()

def _createCheckButtons(self):
def _createCheckButtons(self) -> None:
self._checkAllButton = QToolButton()
self._checkAllButton.setIcon(QIcon("img:check.svg"))
self._checkAllButton.setIconSize(QSize(16, 16))
Expand All @@ -94,7 +94,7 @@ def itemChanged(self, item: QListWidgetItem):
else:
raise AssertionError("Unhandled checkstate!")

def modelChanged(self):
def modelChanged(self) -> None:
self._list.clear()

items = self._model.getList()
Expand All @@ -112,7 +112,7 @@ def modelChanged(self):

self.filterList(self._search_box.filter())

def filterList(self, _filter: str):
def filterList(self, _filter: str) -> None:
_filter = _filter.lower()

for index in range(0, self._list.count()):
Expand All @@ -124,7 +124,7 @@ def filterList(self, _filter: str):
else:
item.setHidden(True)

def checkAll(self):
def checkAll(self) -> None:
"""
Checks all visible items in the list.
"""
Expand All @@ -133,21 +133,21 @@ def checkAll(self):
if not item.isHidden():
self._model.selectValue(str(item.text()))

def uncheckAll(self):
def uncheckAll(self) -> None:
"""
Unchecks all items in the list, visible or not
"""
self._model.unselectAll()

def checkSelected(self):
def checkSelected(self) -> None:
items = []
for item in self._list.selectedItems():
items.append(str(item.text()))

for item in items:
self._model.selectValue(item)

def uncheckSelected(self):
def uncheckSelected(self) -> None:
items = []
for item in self._list.selectedItems():
items.append(str(item.text()))
Expand Down
21 changes: 15 additions & 6 deletions src/ert/gui/ertwidgets/closabledialog.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Optional

from qtpy.QtCore import Qt
from qtpy.QtWidgets import QDialog, QHBoxLayout, QPushButton, QVBoxLayout
from qtpy.QtWidgets import QDialog, QHBoxLayout, QPushButton, QVBoxLayout, QWidget

if TYPE_CHECKING:
from qtpy.QtWidgets import QT_SLOT


class ClosableDialog(QDialog):
def __init__(self, title, widget, parent=None):
def __init__(
self, title: Optional[str], widget: QWidget, parent: Optional[QWidget] = None
) -> None:
QDialog.__init__(self, parent)
self.setWindowTitle(title)
self.setModal(True)
Expand All @@ -26,23 +35,23 @@ def __init__(self, title, widget, parent=None):

self.setLayout(layout)

def disableCloseButton(self):
def disableCloseButton(self) -> None:
self.close_button.setEnabled(False)

def enableCloseButton(self):
def enableCloseButton(self) -> None:
self.close_button.setEnabled(True)

def keyPressEvent(self, q_key_event):
if self.close_button.isEnabled() or q_key_event.key() != Qt.Key_Escape:
QDialog.keyPressEvent(self, q_key_event)

def addButton(self, caption, listener):
def addButton(self, caption: str, listener: QT_SLOT) -> None:
button = QPushButton(caption)
button.setObjectName(str(caption).capitalize())
self.__button_layout.insertWidget(1, button)
button.clicked.connect(listener)

def toggleButton(self, caption, enabled):
def toggleButton(self, caption: str, enabled: bool) -> None:
button = self.findChild(QPushButton, str(caption).capitalize())
if button is not None:
button.setEnabled(enabled)
9 changes: 6 additions & 3 deletions src/ert/gui/ertwidgets/create_experiment_dialog.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

from qtpy.QtCore import (
Qt,
Signal,
Expand All @@ -8,6 +10,7 @@
QGridLayout,
QLabel,
QLineEdit,
QWidget,
)


Expand All @@ -17,8 +20,8 @@ class CreateExperimentDialog(QDialog):
def __init__(
self,
title: str = "Create new experiment",
parent=None,
):
parent: Optional[QWidget] = None,
) -> None:
QDialog.__init__(self, parent=parent)
self.setModal(True)
self.setWindowTitle(title)
Expand Down Expand Up @@ -51,7 +54,7 @@ def __init__(
)
self._ok_button.setEnabled(False)

def enableOkButton():
def enableOkButton() -> None:
self._ok_button.setEnabled(
len(self._experiment_edit.text()) != 0
and len(self._ensemble_edit.text()) != 0
Expand Down
5 changes: 3 additions & 2 deletions src/ert/gui/ertwidgets/customdialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
QFormLayout,
QLabel,
QLayout,
QPushButton,
QWidget,
)

Expand Down Expand Up @@ -41,7 +42,7 @@ def __init__(
self._layout.addRow(label)
self._layout.addRow(self.createSpace(10))

self.ok_button = None
self.ok_button: Optional[QPushButton] = None

self.setLayout(self._layout)

Expand Down Expand Up @@ -92,7 +93,7 @@ def addLabeledOption(self, label: Any, option_widget: QWidget) -> None:

self._layout.addRow(f"{label}:", option_widget)

def addWidget(self, widget: Union[QWidget, QLayout, None], label: str = ""):
def addWidget(self, widget: Union[QWidget, QLayout, None], label: str = "") -> None:
if not label.endswith(":"):
label = f"{label}:"
self._layout.addRow(label, widget)
Expand Down
6 changes: 3 additions & 3 deletions src/ert/gui/ertwidgets/ensemblelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class AddWidget(QWidget):
addFunction: Callable to be connected to the add button.
"""

def __init__(self, addFunction: Callable):
def __init__(self, addFunction: Callable[[], None]) -> None:
super().__init__()

self.addButton = QToolButton(self)
Expand Down Expand Up @@ -78,7 +78,7 @@ def __init__(self, config: ErtConfig, notifier: ErtNotifier, ensemble_size: int)
def storage(self) -> Storage:
return self.notifier.storage

def addItem(self):
def addItem(self) -> None:
dialog = ValidatedDialog(
"New ensemble",
"Enter name of new ensemble:",
Expand All @@ -98,7 +98,7 @@ def addItem(self):
self.notifier.set_current_ensemble(ensemble)
self.notifier.ertChanged.emit()

def updateList(self):
def updateList(self) -> None:
"""Retrieves data from the model and inserts it into the list"""
ensemble_list = sorted(
self.storage.ensembles, key=lambda x: x.started_at, reverse=True
Expand Down
8 changes: 5 additions & 3 deletions src/ert/gui/ertwidgets/legend.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from typing import Optional

from qtpy.QtCore import QSize
from qtpy.QtGui import QPainter
from qtpy.QtGui import QColor, QPainter
from qtpy.QtWidgets import QHBoxLayout, QLabel, QWidget


class LegendMarker(QWidget):
"""A widget that shows a colored box"""

def __init__(self, color):
def __init__(self, color: QColor):
QWidget.__init__(self)

self.setMaximumSize(QSize(12, 12))
Expand All @@ -31,7 +33,7 @@ def paintEvent(self, paintevent):
class Legend(QWidget):
"""Combines a LegendMarker with a label"""

def __init__(self, legend, color):
def __init__(self, legend: Optional[str], color: QColor):
QWidget.__init__(self)

self.setMinimumWidth(140)
Expand Down
Loading

0 comments on commit 56dc74a

Please sign in to comment.