Skip to content

Commit

Permalink
Type check ert.gui.ertwidgets
Browse files Browse the repository at this point in the history
  • Loading branch information
eivindjahren committed Jun 20, 2024
1 parent 3f3e682 commit 3ce383f
Show file tree
Hide file tree
Showing 54 changed files with 660 additions and 421 deletions.
8 changes: 0 additions & 8 deletions .mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,6 @@ ignore_errors = True
ignore_missing_imports = True
ignore_errors = True

[mypy-ert.gui.simulation.*]
ignore_missing_imports = True
ignore_errors = True

[mypy-ert.gui.tools.*]
ignore_missing_imports = True
ignore_errors = True

[mypy-mpl_toolkits.*]
ignore_missing_imports = True

Expand Down
49 changes: 32 additions & 17 deletions src/ert/gui/ertwidgets/listeditbox.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List
from typing import Iterable, List, Optional

from qtpy.QtCore import QSize, Qt
from qtpy.QtGui import QIcon
from qtpy.QtGui import QIcon, QKeyEvent
from qtpy.QtWidgets import (
QCompleter,
QHBoxLayout,
Expand All @@ -17,19 +17,26 @@

class AutoCompleteLineEdit(QLineEdit):
# http://blog.elentok.com/2011/08/autocomplete-textbox-for-multiple.html
def __init__(self, items, parent=None):
def __init__(
self, items: Iterable[Optional[str]], parent: Optional[QWidget] = None
):
super().__init__(parent)

self._separators = [",", " "]

self._completer = QCompleter(items, self)
self._completer.setWidget(self)
self._completer.activated[str].connect(self.__insertCompletion)
self._completer.setCaseSensitivity(Qt.CaseInsensitive)
self._completer.setCaseSensitivity(Qt.CaseSensitivity.CaseInsensitive)

self.__keysToIgnore = [Qt.Key_Enter, Qt.Key_Return, Qt.Key_Escape, Qt.Key_Tab]
self.__keysToIgnore = [
Qt.Key.Key_Enter,
Qt.Key.Key_Return,
Qt.Key.Key_Escape,
Qt.Key.Key_Tab,
]

def __insertCompletion(self, completion):
def __insertCompletion(self, completion: str) -> None:
extra = len(completion) - len(self._completer.completionPrefix())
extra_text = completion[-extra:]
extra_text += ", "
Expand All @@ -44,26 +51,34 @@ def textUnderCursor(self) -> str:
i -= 1
return text_under_cursor

def keyPressEvent(self, event):
if self._completer.popup().isVisible() and event.key() in self.__keysToIgnore:
event.ignore()
def keyPressEvent(self, a0: Optional[QKeyEvent]) -> None:
popup = self._completer.popup()
if (
popup is not None
and popup.isVisible()
and a0 is not None
and a0.key() in self.__keysToIgnore
):
a0.ignore()
return

super().keyPressEvent(event)
super().keyPressEvent(a0)

completion_prefix = self.textUnderCursor()
if completion_prefix != self._completer.completionPrefix():
self.__updateCompleterPopupItems(completion_prefix)
if len(event.text()) > 0 and len(completion_prefix) > 0:
if a0 is not None and len(a0.text()) > 0 and len(completion_prefix) > 0:
self._completer.complete()
if len(completion_prefix) == 0:
self._completer.popup().hide()
if popup is not None and len(completion_prefix) == 0:
popup.hide()

def __updateCompleterPopupItems(self, completionPrefix):
def __updateCompleterPopupItems(self, completionPrefix: Optional[str]) -> None:
self._completer.setCompletionPrefix(completionPrefix)
self._completer.popup().setCurrentIndex(
self._completer.completionModel().index(0, 0)
)
popup = self._completer.popup()
assert popup is not None
model = self._completer.completionModel()
assert model is not None
popup.setCurrentIndex(model.index(0, 0))


class ListEditBox(QWidget):
Expand Down
4 changes: 2 additions & 2 deletions src/ert/gui/plottery/plot_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PlotConfig:
def __init__(
self,
plot_settings: Optional[dict[str, Any]] = None,
title: str = "Unnamed",
title: Optional[str] = "Unnamed",
x_label: Optional[str] = None,
y_label: Optional[str] = None,
):
Expand Down Expand Up @@ -109,7 +109,7 @@ def addLegendItem(self, label: str, item: Any) -> None:
def title(self) -> str:
return self._title if self._title is not None else "Unnamed"

def setTitle(self, title: str) -> None:
def setTitle(self, title: Optional[str]) -> None:
self._title = title

def isUnnamed(self) -> bool:
Expand Down
16 changes: 8 additions & 8 deletions src/ert/gui/simulation/ensemble_experiment_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def __init__(self, ensemble_size: int, run_path: str, notifier: ErtNotifier):
self.setObjectName("Ensemble_experiment_panel")

layout = QFormLayout()
lab = QLabel(" ".join(EnsembleExperiment.__doc__.split()))
lab = QLabel(" ".join(EnsembleExperiment.__doc__.split())) # type: ignore
lab.setWordWrap(True)
lab.setAlignment(QtCore.Qt.AlignLeft)
lab.setAlignment(QtCore.Qt.AlignmentFlag.AlignLeft)
layout.addRow(lab)

self._name_field = StringBox(
Expand All @@ -41,14 +41,14 @@ def __init__(self, ensemble_size: int, run_path: str, notifier: ErtNotifier):
self._name_field.setMinimumWidth(250)
layout.addRow("Experiment name:", self._name_field)
self._name_field.setValidator(
NotInStorage(self.notifier.storage, "experiments")
NotInStorage(self.notifier.storage, "experiments") # type: ignore
)
self._ensemble_name_field = StringBox(
TextModel(""), placeholder_text="ensemble"
)
self._ensemble_name_field.setMinimumWidth(250)
self._ensemble_name_field.setValidator(
NotInStorage(self.notifier.storage, "ensembles")
NotInStorage(self.notifier.storage, "ensembles") # type: ignore
)

layout.addRow("Ensemble name:", self._ensemble_name_field)
Expand All @@ -60,7 +60,7 @@ def __init__(self, ensemble_size: int, run_path: str, notifier: ErtNotifier):
layout.addRow(QLabel("Number of realizations:"), number_of_realizations_label)

self._active_realizations_field = StringBox(
ActiveRealizationsModel(ensemble_size),
ActiveRealizationsModel(ensemble_size), # type: ignore
"config/simulation/active_realizations",
)
self._active_realizations_field.setValidator(
Expand All @@ -70,13 +70,13 @@ def __init__(self, ensemble_size: int, run_path: str, notifier: ErtNotifier):

self.setLayout(layout)

self._active_realizations_field.getValidationSupport().validationChanged.connect( # noqa
self._active_realizations_field.getValidationSupport().validationChanged.connect(
self.simulationConfigurationChanged
)
self._name_field.getValidationSupport().validationChanged.connect( # noqa
self._name_field.getValidationSupport().validationChanged.connect(
self.simulationConfigurationChanged
)
self._ensemble_name_field.getValidationSupport().validationChanged.connect( # noqa
self._ensemble_name_field.getValidationSupport().validationChanged.connect(
self.simulationConfigurationChanged
)

Expand Down
11 changes: 6 additions & 5 deletions src/ert/gui/simulation/ensemble_smoother_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def __init__(

self._ensemble_format_model = TargetEnsembleModel(analysis_config, notifier)
self._ensemble_format_field = StringBox(
self._ensemble_format_model,
self._ensemble_format_model.getDefaultValue(),
self._ensemble_format_model, # type: ignore
self._ensemble_format_model.getDefaultValue(), # type: ignore
True,
)
self._ensemble_format_field.setValidator(ProperNameFormatArgument())
Expand All @@ -71,7 +71,8 @@ def __init__(

active_realizations_model = ActiveRealizationsModel(ensemble_size)
self._active_realizations_field = StringBox(
active_realizations_model, "config/simulation/active_realizations"
active_realizations_model, # type: ignore
"config/simulation/active_realizations",
)
self._active_realizations_field.setValidator(RangeStringArgument(ensemble_size))
layout.addRow("Active realizations", self._active_realizations_field)
Expand All @@ -94,8 +95,8 @@ def isConfigurationValid(self) -> bool:
def get_experiment_arguments(self) -> Arguments:
arguments = Arguments(
mode=ENSEMBLE_SMOOTHER_MODE,
current_ensemble=self._ensemble_format_model.getValue() % 0,
target_ensemble=self._ensemble_format_model.getValue() % 1,
current_ensemble=self._ensemble_format_model.getValue() % 0, # type: ignore
target_ensemble=self._ensemble_format_model.getValue() % 1, # type: ignore
realizations=self._active_realizations_field.text(),
experiment_name=(
self._name_field.text()
Expand Down
10 changes: 5 additions & 5 deletions src/ert/gui/simulation/evaluate_ensemble_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def __init__(self, ensemble_size: int, run_path: str, notifier: ErtNotifier):
self.setObjectName("Evaluate_parameters_panel")

layout = QFormLayout()
lab = QLabel(" ".join(EvaluateEnsemble.__doc__.split()))
lab = QLabel(" ".join(EvaluateEnsemble.__doc__.split())) # type: ignore
lab.setWordWrap(True)
lab.setAlignment(QtCore.Qt.AlignLeft)
lab.setAlignment(QtCore.Qt.AlignmentFlag.AlignLeft)
layout.addRow(lab)
self._ensemble_selector = EnsembleSelector(notifier, show_only_no_children=True)
layout.addRow("Ensemble:", self._ensemble_selector)
Expand All @@ -42,7 +42,7 @@ def __init__(self, ensemble_size: int, run_path: str, notifier: ErtNotifier):
layout.addRow(QLabel("Number of realizations:"), number_of_realizations_label)

self._active_realizations_field = StringBox(
ActiveRealizationsModel(ensemble_size, show_default=False),
ActiveRealizationsModel(ensemble_size, show_default=False), # type: ignore
"config/simulation/active_realizations",
)
self._active_realizations_field.setValidator(
Expand All @@ -53,7 +53,7 @@ def __init__(self, ensemble_size: int, run_path: str, notifier: ErtNotifier):

self.setLayout(layout)

self._active_realizations_field.getValidationSupport().validationChanged.connect( # noqa
self._active_realizations_field.getValidationSupport().validationChanged.connect(
self.simulationConfigurationChanged
)
self._ensemble_selector.ensemble_populated.connect(self._realizations_from_fs)
Expand Down Expand Up @@ -85,4 +85,4 @@ def _realizations_from_fs(self) -> None:
mask = np.logical_and(
parameters, np.logical_or(missing_responses, failures)
)
self._active_realizations_field.model.setValueFromMask(mask)
self._active_realizations_field.model.setValueFromMask(mask) # type: ignore
15 changes: 8 additions & 7 deletions src/ert/gui/simulation/experiment_config_panel.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
from __future__ import annotations

from typing import Any, Dict
from typing import TYPE_CHECKING, Any, Type

from qtpy.QtCore import Signal
from qtpy.QtWidgets import QWidget

if TYPE_CHECKING:
from ert.run_models import BaseRunModel


class ExperimentConfigPanel(QWidget):
simulationConfigurationChanged = Signal()

def __init__(self, simulation_model):
def __init__(self, simulation_model: Type[BaseRunModel]):
QWidget.__init__(self)
self.setContentsMargins(10, 10, 10, 10)
self.__simulation_model = simulation_model

def get_experiment_type(self):
def get_experiment_type(self) -> Type[BaseRunModel]:
return self.__simulation_model

@staticmethod
def isConfigurationValid() -> bool:
def isConfigurationValid(self) -> bool:
return True

@staticmethod
def get_experiment_arguments() -> Dict[str, Any]:
def get_experiment_arguments(self) -> Any:
return {}
Loading

0 comments on commit 3ce383f

Please sign in to comment.