Browse Source

Fix Mypy errors

Maarten van den Berg 5 years ago
parent
commit
7b3e643142
1 changed files with 20 additions and 10 deletions
  1. 20 10
      piket_client/gui.py

+ 20 - 10
piket_client/gui.py

@@ -3,8 +3,10 @@ Provides the graphical front-end for Piket.
3 3
 """
4 4
 import collections
5 5
 import logging
6
+import math
6 7
 import os
7 8
 import sys
9
+from typing import Deque
8 10
 
9 11
 import qdarkstyle
10 12
 
@@ -78,7 +80,7 @@ class NameButton(QPushButton):
78 80
     @Slot()
79 81
     def rebuild(self) -> None:
80 82
         """ Refresh the Person object and the label. """
81
-        self.person = self.person.reload()
83
+        self.person = self.person.reload()  # type: ignore
82 84
         self.setText(self.current_label)
83 85
 
84 86
     @property
@@ -149,7 +151,8 @@ class NameButtons(QWidget):
149 151
         LOG.debug("Initializing NameButtons.")
150 152
 
151 153
         ps = Person.get_all(True)
152
-        num_columns = round(len(ps) / 10) + 1
154
+        assert not isinstance(ps, NetworkError)
155
+        num_columns = math.ceil(math.sqrt(len(ps)))
153 156
 
154 157
         if self.layout:
155 158
             LOG.debug("Removing %s widgets for rebuild", self.layout.count())
@@ -183,7 +186,7 @@ class PiketMainWindow(QMainWindow):
183 186
         self.toolbar = None
184 187
         self.osk = None
185 188
         self.undo_action = None
186
-        self.undo_queue = collections.deque([], 15)
189
+        self.undo_queue: Deque[Consumption] = collections.deque([], 15)
187 190
         self.init_ui()
188 191
 
189 192
     def init_ui(self) -> None:
@@ -212,6 +215,7 @@ class PiketMainWindow(QMainWindow):
212 215
 
213 216
         # Initialize toolbar
214 217
         self.toolbar = QToolBar()
218
+        assert self.toolbar is not None
215 219
         self.toolbar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
216 220
         self.toolbar.setIconSize(QSize(icon_size, icon_size))
217 221
 
@@ -239,7 +243,7 @@ class PiketMainWindow(QMainWindow):
239 243
         self.toolbar.setContextMenuPolicy(Qt.PreventContextMenu)
240 244
         self.toolbar.setFloatable(False)
241 245
         self.toolbar.setMovable(False)
242
-        self.ct_ag = QActionGroup(self.toolbar)
246
+        self.ct_ag: QActionGroup = QActionGroup(self.toolbar)
243 247
         self.ct_ag.setExclusive(True)
244 248
 
245 249
         cts = ConsumptionType.get_all()
@@ -311,6 +315,8 @@ class PiketMainWindow(QMainWindow):
311 315
         """ Ask for a new Person and register it, then rebuild the central
312 316
         widget. """
313 317
         inactive_persons = Person.get_all(False)
318
+        assert not isinstance(inactive_persons, NetworkError)
319
+
314 320
         inactive_persons.sort(key=lambda p: p.name)
315 321
         inactive_names = [p.name for p in inactive_persons]
316 322
 
@@ -331,9 +337,10 @@ class PiketMainWindow(QMainWindow):
331 337
                 person.set_active(True)
332 338
 
333 339
             else:
334
-                person = Person(name=name)
335
-                person = person.create()
340
+                person = Person(full_name=name, display_name=None, )
341
+                person.create()
336 342
 
343
+            assert self.main_widget is not None
337 344
             self.main_widget.init_ui()
338 345
 
339 346
     def add_consumption_type(self) -> None:
@@ -344,8 +351,8 @@ class PiketMainWindow(QMainWindow):
344 351
         self.hide_keyboard()
345 352
 
346 353
         if ok and name:
347
-            ct = ConsumptionType(name=name)
348
-            ct = ct.create()
354
+            ct = ConsumptionType(name=name).create()
355
+            assert not isinstance(ct, NetworkError)
349 356
 
350 357
             action = QAction(
351 358
                 self.load_icon(ct.icon or "beer_bottle.svg"), ct.name, self.ct_ag
@@ -353,6 +360,7 @@ class PiketMainWindow(QMainWindow):
353 360
             action.setCheckable(True)
354 361
             action.setData(str(ct.consumption_type_id))
355 362
 
363
+            assert self.toolbar is not None
356 364
             self.toolbar.addAction(action)
357 365
 
358 366
     def confirm_quit(self) -> None:
@@ -383,8 +391,10 @@ class PiketMainWindow(QMainWindow):
383 391
             self.undo_queue.append(to_undo)
384 392
 
385 393
         elif not self.undo_queue:
394
+            assert self.undo_action is not None
386 395
             self.undo_action.setDisabled(True)
387 396
 
397
+        assert self.main_widget is not None
388 398
         self.main_widget.init_ui()
389 399
 
390 400
     @Slot(Consumption)
@@ -480,9 +490,9 @@ def main() -> None:
480 490
                     f'{item["count"]} {item["name"]}'
481 491
                     for item in settlement.consumption_summary.values()
482 492
                 ]
483
-                info = ", ".join(info)
493
+                info2 = ", ".join(info)
484 494
                 QMessageBox.information(
485
-                    None, "Lijst afgesloten", f"VO! Op deze lijst stonden: {info}"
495
+                    None, "Lijst afgesloten", f"VO! Op deze lijst stonden: {info2}"
486 496
                 )
487 497
 
488 498
                 main_window = PiketMainWindow()