Skip to content

Commit

Permalink
Change typing
Browse files Browse the repository at this point in the history
  • Loading branch information
jfeil committed May 6, 2022
1 parent d7b9304 commit fee801f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/basic_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pathlib
import platform
import sys
from typing import Any, Tuple, Union
from typing import Any, Tuple, Optional

import requests
from appdirs import AppDirs
Expand All @@ -16,7 +16,7 @@

log_level = logging.WARN
current_platform = platform.system()
VERSION_INFO = Union[None, Tuple[str, str, str, str]]
VERSION_INFO = Optional[Tuple[str, str, str, str]]

if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
is_bundled = True
Expand Down
12 changes: 7 additions & 5 deletions src/database.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from __future__ import annotations

import logging
import os
import sys
from typing import List, Union, Tuple
from typing import List, Tuple

from alembic import command
from alembic.config import Config
from alembic.runtime.migration import MigrationContext
from sqlalchemy import create_engine, func
from sqlalchemy.orm import Session
from sqlalchemy.orm import Session, Query

from src.basic_config import database_name, Base, is_bundled, app_dirs
from src.datatypes import QuestionGroup, Question, MultipleChoice
Expand Down Expand Up @@ -107,7 +109,7 @@ def get_question(self, signature: str):
return question

def get_questions_by_foreignkey(self, question_groups: List[QuestionGroup], mchoice=None, randomize: bool = False,
as_query: bool = False):
as_query: bool = False) -> Query | List[Question]:
question_groups_ids = [question_group.id for question_group in question_groups]
questions = self.session.query(Question)
# noinspection PyNoneFunctionAssignment
Expand All @@ -129,15 +131,15 @@ def get_multiplechoice_by_foreignkey(self, question: Question):
MultipleChoice.question == question).all()
return mchoice

def fill_database(self, dataset: List[Union[QuestionGroup, Question, MultipleChoice]]):
def fill_database(self, dataset: List[QuestionGroup | Question | MultipleChoice]):
# insert processed values into db
if not self.initialized:
self._init_database()

self.session.add_all(dataset)
self.session.commit()

def delete(self, item: Union[QuestionGroup, Question]):
def delete(self, item: QuestionGroup | Question):
self.session.delete(item)
self.session.commit()

Expand Down
4 changes: 2 additions & 2 deletions src/filter_editor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime, date
from typing import Dict, Tuple, Callable, Union, Any
from typing import Dict, Tuple, Callable, Any, Optional

from PySide6.QtCore import Qt
from PySide6.QtWidgets import QDialog, QLineEdit, QCheckBox, QDateEdit, QSpinBox, QPushButton, QDialogButtonBox
Expand All @@ -10,7 +10,7 @@

class FilterEditor(QDialog, Ui_FilterEditor):
def __init__(self, filter_configuration: Dict[str, Question.QuestionParameters],
current_filter: Union[None, Tuple[str, FilterOption, Any]] = None,
current_filter: Optional[Tuple[str, FilterOption, Any]] = None,
# dict_key, FilterOption, filter_data
parent=None, window_flags=Qt.Dialog):
super(FilterEditor, self).__init__(parent, window_flags)
Expand Down
12 changes: 6 additions & 6 deletions src/main_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import datetime
from enum import Enum, auto
from typing import TYPE_CHECKING
from typing import Union, List, Tuple, Dict
from typing import List, Tuple, Dict
from typing import TYPE_CHECKING, Optional

from PySide6.QtCore import Signal, QSortFilterProxyModel
from PySide6.QtGui import QKeySequence, QShortcut, Qt
Expand Down Expand Up @@ -138,7 +138,7 @@ def create_question_group_tab(self, question_group: QuestionGroup):
self.ui.tabWidget.addTab(tab, "")
self._update_tabtitle(self.ui.tabWidget.indexOf(tab))

def _question_group_editor(self, question_group: Union[QuestionGroup, None],
def _question_group_editor(self, question_group: QuestionGroup | None,
editor: QuestionGroupEditor) -> EditorResult:
if editor.exec() == QDialog.Accepted:
if question_group and question_group.id == editor.id:
Expand Down Expand Up @@ -185,7 +185,7 @@ def _update_tabtitle(self, index):
question_group, _, _ = self.question_group_tabs[index]
self.ui.tabWidget.setTabText(index, f"{question_group.id:02d} {question_group.name}")

def add_filter(self, list_entry: Union[QListWidgetItem, bool] = False):
def add_filter(self, list_entry: QListWidgetItem | bool = False):
if not list_entry or type(list_entry) == bool:
# Add filter mode -> no list entry double_clicked
current_configuration = None
Expand Down Expand Up @@ -273,9 +273,9 @@ def __init__(self, main_window: MainWindow, dock_widget: SelfTestDockWidget):

self._next_questions = [] # type: List[Question]
self._previous_questions = [] # type: List[Question]
self._current_question = None # type: Union[Question, None]
self._current_question = None # type: Optional[Question]

self.current_question = None # type: Union[Question, None]
self.current_question = None # type: Optional[Question]
self.previous_questions = []
self.next_questions = []
self.dock_widget.changed.connect(self.selected_groups_changed)
Expand Down
38 changes: 20 additions & 18 deletions src/question_table.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import datetime
from typing import Union, Any, List
from typing import Any, List

import PySide6
from PySide6.QtCore import Qt, QPoint, QAbstractTableModel, QSortFilterProxyModel
Expand Down Expand Up @@ -57,14 +59,14 @@ def reset(self) -> None:
self.read_data()
self.endResetModel()

def rowCount(self, parent: Union[PySide6.QtCore.QModelIndex, PySide6.QtCore.QPersistentModelIndex] = ...) -> int:
def rowCount(self, parent: PySide6.QtCore.QModelIndex | PySide6.QtCore.QPersistentModelIndex = ...) -> int:
return len(self.questions)

def columnCount(self, parent: Union[PySide6.QtCore.QModelIndex, PySide6.QtCore.QPersistentModelIndex] = ...) -> int:
def columnCount(self, parent: PySide6.QtCore.QModelIndex | PySide6.QtCore.QPersistentModelIndex = ...) -> int:
return len(QuestionGroupDataModel.activated_headers)

def insertColumns(self, column: int, count: int,
parent: Union[PySide6.QtCore.QModelIndex, PySide6.QtCore.QPersistentModelIndex] = ...) -> bool:
parent: PySide6.QtCore.QModelIndex | PySide6.QtCore.QPersistentModelIndex = ...) -> bool:
changed_columns = -1
for i in range(column, column + count):
if self.insertColumn(i):
Expand All @@ -76,7 +78,7 @@ def insertColumns(self, column: int, count: int,
return True

def removeColumns(self, column: int, count: int,
parent: Union[PySide6.QtCore.QModelIndex, PySide6.QtCore.QPersistentModelIndex] = ...) -> bool:
parent: PySide6.QtCore.QModelIndex | PySide6.QtCore.QPersistentModelIndex = ...) -> bool:
changed_columns = -1
for i in range(column, column + count):
if self.removeColumn(i):
Expand All @@ -96,20 +98,20 @@ def toggle_column(column: int):
QuestionGroupDataModel.activated_headers = new_activated_headers

def insertColumn(self, column: int,
parent: Union[PySide6.QtCore.QModelIndex, PySide6.QtCore.QPersistentModelIndex] = ...) -> bool:
parent: PySide6.QtCore.QModelIndex | PySide6.QtCore.QPersistentModelIndex = ...) -> bool:
if QuestionGroupDataModel.headers[column][1]:
return False
self.toggle_column(column)
return True

def removeColumn(self, column: int,
parent: Union[PySide6.QtCore.QModelIndex, PySide6.QtCore.QPersistentModelIndex] = ...) -> bool:
parent: PySide6.QtCore.QModelIndex | PySide6.QtCore.QPersistentModelIndex = ...) -> bool:
if not QuestionGroupDataModel.headers[column][1]:
return False
self.toggle_column(column)
return True

def data(self, index: Union[PySide6.QtCore.QModelIndex, PySide6.QtCore.QPersistentModelIndex, int],
def data(self, index: PySide6.QtCore.QModelIndex | PySide6.QtCore.QPersistentModelIndex | int,
role: int = ...) -> Any:
if type(index) == int:
row = index
Expand All @@ -136,7 +138,7 @@ def data(self, index: Union[PySide6.QtCore.QModelIndex, PySide6.QtCore.QPersiste
tooltip = str(self.questions[row].values(QuestionGroupDataModel.activated_headers[col]).table_tooltip)
return tooltip

def setData(self, index: Union[PySide6.QtCore.QModelIndex, PySide6.QtCore.QPersistentModelIndex], value: Any,
def setData(self, index: PySide6.QtCore.QModelIndex | PySide6.QtCore.QPersistentModelIndex, value: Any,
role: int = ...) -> bool:
if role == Qt.UserRole:
db.add_object(value)
Expand All @@ -154,7 +156,7 @@ def headerData(self, section: int, orientation: PySide6.QtCore.Qt.Orientation, r
Question.parameters[QuestionGroupDataModel.activated_headers[section]]}

def insertRows(self, row: int, count: int,
parent: Union[PySide6.QtCore.QModelIndex, PySide6.QtCore.QPersistentModelIndex] = ...) -> bool:
parent: PySide6.QtCore.QModelIndex | PySide6.QtCore.QPersistentModelIndex = ...) -> bool:
inserted_count = -1
for i in range(count):
if self.insertRow(row + i):
Expand All @@ -166,7 +168,7 @@ def insertRows(self, row: int, count: int,
return True

def removeRows(self, row: int, count: int,
parent: Union[PySide6.QtCore.QModelIndex, PySide6.QtCore.QPersistentModelIndex] = ...) -> bool:
parent: PySide6.QtCore.QModelIndex | PySide6.QtCore.QPersistentModelIndex = ...) -> bool:
removed_count = -1
for i in range(count):
if self.removeRow(row + i):
Expand All @@ -178,13 +180,13 @@ def removeRows(self, row: int, count: int,
return True

def removeRow(self, row: int,
parent: Union[PySide6.QtCore.QModelIndex, PySide6.QtCore.QPersistentModelIndex] = ...) -> bool:
parent: PySide6.QtCore.QModelIndex | PySide6.QtCore.QPersistentModelIndex = ...) -> bool:
db.delete(self.questions[row])
self.questions.pop(row)
return True

def insertRow(self, row: int,
parent: Union[PySide6.QtCore.QModelIndex, PySide6.QtCore.QPersistentModelIndex] = ...) -> bool:
parent: PySide6.QtCore.QModelIndex | PySide6.QtCore.QPersistentModelIndex = ...) -> bool:
new_question = Question()
new_question.question_group = self.question_group
new_question.question_id = db.get_new_question_id(self.question_group)
Expand All @@ -197,8 +199,8 @@ def insertRow(self, row: int,
db.abort()
return False

def flags(self, index: Union[
PySide6.QtCore.QModelIndex, PySide6.QtCore.QPersistentModelIndex]) -> PySide6.QtCore.Qt.ItemFlags:
def flags(self, index: PySide6.QtCore.QModelIndex |
PySide6.QtCore.QPersistentModelIndex) -> PySide6.QtCore.Qt.ItemFlags:
return Qt.ItemIsDragEnabled | Qt.ItemIsEditable | Qt.ItemIsEnabled | Qt.ItemIsSelectable

def supportedDragActions(self) -> PySide6.QtCore.Qt.DropActions:
Expand All @@ -207,7 +209,7 @@ def supportedDragActions(self) -> PySide6.QtCore.Qt.DropActions:

class RuleDelegate(QStyledItemDelegate):
def createEditor(self, parent: PySide6.QtWidgets.QWidget, option: PySide6.QtWidgets.QStyleOptionViewItem,
index: Union[PySide6.QtCore.QModelIndex, PySide6.QtCore.QPersistentModelIndex]) \
index: PySide6.QtCore.QModelIndex | PySide6.QtCore.QPersistentModelIndex) \
-> PySide6.QtWidgets.QWidget:
editor = QWidget(parent)
question = index.model().data(index, role=Qt.UserRole)
Expand Down Expand Up @@ -323,8 +325,8 @@ def startDrag(self, supportedActions: Qt.DropActions) -> None:
class RuleSortFilterProxyModel(QSortFilterProxyModel):
filters = [] # List[Tuple[Tuple[dict_key, Callable], Tuple[str, FilterOption, Any]]]

def filterAcceptsRow(self, source_row: int, source_parent: Union[
PySide6.QtCore.QModelIndex, PySide6.QtCore.QPersistentModelIndex]) -> bool:
def filterAcceptsRow(self, source_row: int, source_parent: PySide6.QtCore.QModelIndex |
PySide6.QtCore.QPersistentModelIndex) -> bool:
if not RuleSortFilterProxyModel.filters:
return True

Expand Down
3 changes: 2 additions & 1 deletion src/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import stat
import subprocess
import sys
from typing import Optional

import markdown2
import requests
Expand Down Expand Up @@ -30,7 +31,7 @@ def __init__(self, parent, versions, display_dev=False):
self.ui.text.setTextInteractionFlags(Qt.TextBrowserInteraction)
self.ui.text.setOpenExternalLinks(True)

self.download_link = None # type: Union[str, None]
self.download_link = None # type: Optional[str]
self.ui.install_update_button.clicked.connect(self.update)
self.ui.install_update_button.setDisabled(True)
self.ui.download_progress.setVisible(False)
Expand Down

0 comments on commit fee801f

Please sign in to comment.