Skip to content

Commit

Permalink
fix typing
Browse files Browse the repository at this point in the history
  • Loading branch information
JHolba committed Jan 24, 2025
1 parent 4633081 commit e0f477e
Show file tree
Hide file tree
Showing 29 changed files with 162 additions and 299 deletions.
12 changes: 5 additions & 7 deletions src/ert/gui/ertwidgets/closabledialog.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from collections.abc import Callable
from typing import TYPE_CHECKING, cast
from typing import TYPE_CHECKING

from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QDialog, QHBoxLayout, QPushButton, QVBoxLayout, QWidget
Expand Down Expand Up @@ -42,9 +42,9 @@ def disableCloseButton(self) -> None:
def enableCloseButton(self) -> None:
self.close_button.setEnabled(True)

def keyPressEvent(self, arg__1: QKeyEvent) -> None:
if self.close_button.isEnabled() or arg__1.key() != Qt.Key.Key_Escape:
QDialog.keyPressEvent(self, arg__1)
def keyPressEvent(self, a0: QKeyEvent | None) -> None:
if self.close_button.isEnabled() or a0 is None or a0.key() != Qt.Key.Key_Escape:
QDialog.keyPressEvent(self, a0)

def addButton(self, caption: str, listener: Callable[..., None]) -> QPushButton:
button = QPushButton(caption)
Expand All @@ -54,8 +54,6 @@ def addButton(self, caption: str, listener: Callable[..., None]) -> QPushButton:
return button

def toggleButton(self, caption: str, enabled: bool) -> None:
button = cast(
QPushButton, self.findChild(QPushButton, str(caption).capitalize())
)
button = self.findChild(QPushButton, str(caption).capitalize())
if button is not None:
button.setEnabled(enabled)
12 changes: 6 additions & 6 deletions src/ert/gui/ertwidgets/listeditbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,23 @@ def textUnderCursor(self) -> str:
i -= 1
return text_under_cursor

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

super().keyPressEvent(arg__1)
super().keyPressEvent(a0)

completion_prefix = self.textUnderCursor()
if completion_prefix != self._completer.completionPrefix():
self.__updateCompleterPopupItems(completion_prefix)
if arg__1 is not None and len(arg__1.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 popup is not None and len(completion_prefix) == 0:
popup.hide()
Expand Down
14 changes: 7 additions & 7 deletions src/ert/gui/ertwidgets/searchbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ def exitSearch(self) -> None:
if not self.text():
self.presentSearch()

def focusInEvent(self, arg__1: QFocusEvent) -> None:
QLineEdit.focusInEvent(self, arg__1)
def focusInEvent(self, a0: QFocusEvent | None) -> None:
QLineEdit.focusInEvent(self, a0)
self.enterSearch()

def focusOutEvent(self, arg__1: QFocusEvent) -> None:
QLineEdit.focusOutEvent(self, arg__1)
def focusOutEvent(self, a0: QFocusEvent | None) -> None:
QLineEdit.focusOutEvent(self, a0)
self.exitSearch()

def keyPressEvent(self, arg__1: QKeyEvent) -> None:
if arg__1.key() == Qt.Key.Key_Escape:
def keyPressEvent(self, a0: QKeyEvent | None) -> None:
if a0 and a0.key() == Qt.Key.Key_Escape:
self.clear()
self.clearFocus()
else:
QLineEdit.keyPressEvent(self, arg__1)
QLineEdit.keyPressEvent(self, a0)
10 changes: 5 additions & 5 deletions src/ert/gui/ertwidgets/validationsupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def __init__(self, validation_target: QWidget) -> None:
self._originalLeaveEvent = validation_target.leaveEvent
self._originalHideEvent = validation_target.hideEvent

def enterEvent(event: QEnterEvent) -> None:
def enterEvent(event: QEnterEvent | None) -> None:
self._originalEnterEvent(event)

if not self.isValid():
Expand All @@ -93,17 +93,17 @@ def enterEvent(event: QEnterEvent) -> None:

validation_target.enterEvent = enterEvent # type: ignore[method-assign]

def leaveEvent(event: QEvent) -> None:
self._originalLeaveEvent(event)
def leaveEvent(a0: QEvent | None) -> None:
self._originalLeaveEvent(a0)

if self._error_popup is not None:
self._error_popup.hide()

validation_target.leaveEvent = leaveEvent # type: ignore[method-assign]

def hideEvent(event: QHideEvent) -> None:
def hideEvent(a0: QHideEvent | None) -> None:
self._error_popup.hide()
self._originalHideEvent(event)
self._originalHideEvent(a0)

validation_target.hideEvent = hideEvent # type: ignore[method-assign]

Expand Down
29 changes: 9 additions & 20 deletions src/ert/gui/model/fm_step_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
QAbstractProxyModel,
QModelIndex,
QObject,
QPersistentModelIndex,
Qt,
)
from PyQt6.QtCore import pyqtSlot as Slot
Expand Down Expand Up @@ -96,14 +95,10 @@ def headerData(
return None

@override
def columnCount(
self, parent: QModelIndex | QPersistentModelIndex | None = None
) -> int:
def columnCount(self, parent: QModelIndex | None = None) -> int:
return FM_STEP_COLUMN_SIZE

def rowCount(
self, parent: QModelIndex | QPersistentModelIndex | None = None
) -> int:
def rowCount(self, parent: QModelIndex | None = None) -> int:
parent = parent if parent else QModelIndex()
if not parent.isValid():
source_model = self.sourceModel()
Expand All @@ -114,31 +109,27 @@ def rowCount(
return 0

@overload
def parent(self) -> QObject: ...
def parent(self, child: QModelIndex) -> QModelIndex: ...
@overload
def parent(self, child: QModelIndex | QPersistentModelIndex) -> QModelIndex: ...
def parent(self) -> QObject: ...
@override
def parent(
self, child: QModelIndex | QPersistentModelIndex | None = None
) -> QObject | QModelIndex:
def parent(self, child: QModelIndex | None = None) -> QObject | QModelIndex:
return QModelIndex()

@override
def index(
self,
row: int,
column: int,
parent: QModelIndex | QPersistentModelIndex | None = None,
parent: QModelIndex | None = None,
) -> QModelIndex:
parent = parent if parent else QModelIndex()
if not parent.isValid():
job_index = self.mapToSource(self.createIndex(row, column, parent))
return self.createIndex(row, column, job_index.data(NodeRole))
return QModelIndex()

def mapToSource(
self, proxyIndex: QModelIndex | QPersistentModelIndex
) -> QModelIndex:
def mapToSource(self, proxyIndex: QModelIndex) -> QModelIndex:
if proxyIndex.isValid():
sm = self.sourceModel()
assert sm is not None
Expand All @@ -149,9 +140,7 @@ def mapToSource(
return sm.index(proxyIndex.row(), proxyIndex.column(), real_index)
return QModelIndex()

def mapFromSource(
self, sourceIndex: QModelIndex | QPersistentModelIndex
) -> QModelIndex:
def mapFromSource(self, sourceIndex: QModelIndex) -> QModelIndex:
return (
self.index(sourceIndex.row(), sourceIndex.column(), QModelIndex())
if sourceIndex.isValid() and self._accept_index(sourceIndex)
Expand All @@ -167,7 +156,7 @@ def _source_data_changed(
if all([proxy_top_left.isValid(), proxy_bottom_right.isValid()]):
self.dataChanged.emit(proxy_top_left, proxy_bottom_right, roles)

def _accept_index(self, index: QModelIndex | QPersistentModelIndex) -> bool:
def _accept_index(self, index: QModelIndex) -> bool:
if not index.internalPointer() or not index.data(IsFMStepRole):
return False

Expand Down
43 changes: 11 additions & 32 deletions src/ert/gui/model/real_list.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
from typing import overload

from PyQt6.QtCore import (
QAbstractItemModel,
QAbstractProxyModel,
QModelIndex,
QObject,
QPersistentModelIndex,
)
from PyQt6.QtCore import QAbstractItemModel, QAbstractProxyModel, QModelIndex, QObject
from PyQt6.QtCore import pyqtSlot as Slot
from typing_extensions import override

Expand Down Expand Up @@ -62,14 +56,10 @@ def setSourceModel(self, sourceModel: QAbstractItemModel | None) -> None:
self.endResetModel()

@override
def columnCount(
self, parent: QModelIndex | QPersistentModelIndex | None = None
) -> int:
def columnCount(self, parent: QModelIndex | None = None) -> int:
return 1

def rowCount(
self, parent: QModelIndex | QPersistentModelIndex | None = None
) -> int:
def rowCount(self, parent: QModelIndex | None = None) -> int:
parent = parent if parent else QModelIndex()
if not parent.isValid():
source_model = self.sourceModel()
Expand All @@ -80,21 +70,16 @@ def rowCount(
return 0

@overload
def parent(self) -> QObject: ...
def parent(self, child: QModelIndex) -> QModelIndex: ...
@overload
def parent(self, child: QModelIndex | QPersistentModelIndex) -> QModelIndex: ...
def parent(self) -> QObject: ...
@override
def parent(
self, child: QModelIndex | QPersistentModelIndex | None = None
) -> QObject | QModelIndex:
def parent(self, child: QModelIndex | None = None) -> QObject | QModelIndex:
return QModelIndex()

@override
def index(
self,
row: int,
column: int,
parent: QModelIndex | QPersistentModelIndex | None = None,
self, row: int, column: int, parent: QModelIndex | None = None
) -> QModelIndex:
parent = parent if parent else QModelIndex()
if not parent.isValid():
Expand All @@ -103,9 +88,7 @@ def index(
return QModelIndex()

@override
def hasChildren(
self, parent: QModelIndex | QPersistentModelIndex | None = None
) -> bool:
def hasChildren(self, parent: QModelIndex | None = None) -> bool:
# Reimplemented, since in the source model, the realizations have
# children (i.e. valid indices.). Realizations do not have children in
# this model.
Expand All @@ -117,9 +100,7 @@ def hasChildren(
return False

@override
def mapToSource(
self, proxyIndex: QModelIndex | QPersistentModelIndex
) -> QModelIndex:
def mapToSource(self, proxyIndex: QModelIndex) -> QModelIndex:
if proxyIndex.isValid():
sm = self.sourceModel()
assert sm is not None
Expand All @@ -129,9 +110,7 @@ def mapToSource(
return QModelIndex()

@override
def mapFromSource(
self, sourceIndex: QModelIndex | QPersistentModelIndex
) -> QModelIndex:
def mapFromSource(self, sourceIndex: QModelIndex) -> QModelIndex:
return (
self.index(sourceIndex.row(), sourceIndex.column(), QModelIndex())
if sourceIndex.isValid() and self._accept_index(sourceIndex)
Expand Down Expand Up @@ -160,7 +139,7 @@ def _source_rows_inserted(
if parent.isValid() and self._accept_index(parent):
self.endInsertRows()

def _accept_index(self, index: QModelIndex | QPersistentModelIndex) -> bool:
def _accept_index(self, index: QModelIndex) -> bool:
# If the index under test isn't a realization, it is of no interest as
# this model should only consist of realization indices.
if not index.internalPointer() or not index.data(IsRealizationRole):
Expand Down
42 changes: 10 additions & 32 deletions src/ert/gui/model/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@
from datetime import datetime, timedelta
from typing import Any, Final, cast, overload

from PyQt6.QtCore import (
QAbstractItemModel,
QModelIndex,
QObject,
QPersistentModelIndex,
QSize,
Qt,
)
from PyQt6.QtCore import QAbstractItemModel, QModelIndex, QObject, QSize, Qt
from PyQt6.QtGui import QColor, QFont
from typing_extensions import override

Expand Down Expand Up @@ -283,16 +276,12 @@ def _add_snapshot(self, snapshot: EnsembleSnapshot, iter_: str) -> None:
self.rowsInserted.emit(parent, snapshot_tree.row(), snapshot_tree.row())

@override
def columnCount(
self, parent: QModelIndex | QPersistentModelIndex | None = None
) -> int:
def columnCount(self, parent: QModelIndex | None = None) -> int:
if parent and isinstance(parent.internalPointer(), RealNode):
return FM_STEP_COLUMN_SIZE
return 1

def rowCount(
self, parent: QModelIndex | QPersistentModelIndex | None = None
) -> int:
def rowCount(self, parent: QModelIndex | None = None) -> int:
if parent is None:
parent = QModelIndex()
parent_item = (
Expand All @@ -307,13 +296,11 @@ def rowCount(
return len(parent_item.children)

@overload
def parent(self) -> QObject: ...
def parent(self, child: QModelIndex) -> QModelIndex: ...
@overload
def parent(self, child: QModelIndex | QPersistentModelIndex) -> QModelIndex: ...
def parent(self) -> QObject: ...
@override
def parent(
self, child: QModelIndex | QPersistentModelIndex | None = None
) -> QObject | QModelIndex:
def parent(self, child: QModelIndex | None = None) -> QObject | QModelIndex:
if child is None or not child.isValid():
return QModelIndex()

Expand All @@ -327,11 +314,7 @@ def parent(
return self.createIndex(parent_item.row(), 0, parent_item)

@override
def data(
self,
index: QModelIndex | QPersistentModelIndex,
role: int = Qt.ItemDataRole.DisplayRole,
) -> Any:
def data(self, index: QModelIndex, role: int = Qt.ItemDataRole.DisplayRole) -> Any:
if not index.isValid():
return None

Expand Down Expand Up @@ -411,9 +394,7 @@ def _real_data(node: RealNode, role: int) -> Any:

@staticmethod
def _fm_step_data(
index: QModelIndex | QPersistentModelIndex,
node: ForwardModelStepNode,
role: Qt.ItemDataRole,
index: QModelIndex, node: ForwardModelStepNode, role: Qt.ItemDataRole
) -> Any:
node_id = str(node.id_)

Expand Down Expand Up @@ -498,10 +479,7 @@ def _fm_step_data(

@override
def index(
self,
row: int,
column: int,
parent: QModelIndex | QPersistentModelIndex | None = None,
self, row: int, column: int, parent: QModelIndex | None = None
) -> QModelIndex:
if parent is None:
parent = QModelIndex()
Expand All @@ -510,7 +488,7 @@ def index(

parent_item = self.root if not parent.isValid() else parent.internalPointer()
try:
child_item = list(parent_item.children.values())[row] # type:ignore
child_item = list(parent_item.children.values())[row]
except KeyError:
return QModelIndex()
else:
Expand Down
Loading

0 comments on commit e0f477e

Please sign in to comment.