From 9674c54efb089d8445ab3ec84ec649c1b2b5e091 Mon Sep 17 00:00:00 2001 From: Ghostkeeper Date: Wed, 8 Dec 2021 17:36:09 +0100 Subject: [PATCH] Better type checking and handling of optionals Adds type checking here and there to catch such errors in the future. This makes it handle the optional fields of this model better. Contributes to issue CURA-8730. --- cura/PrinterOutput/Models/PrintJobOutputModel.py | 14 +++++++------- .../src/Models/Http/ClusterPrintJobStatus.py | 10 ++++++---- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/cura/PrinterOutput/Models/PrintJobOutputModel.py b/cura/PrinterOutput/Models/PrintJobOutputModel.py index 256999b96f..f7404f71ed 100644 --- a/cura/PrinterOutput/Models/PrintJobOutputModel.py +++ b/cura/PrinterOutput/Models/PrintJobOutputModel.py @@ -42,7 +42,7 @@ class PrintJobOutputModel(QObject): self._preview_image = None # type: Optional[QImage] @pyqtProperty("QStringList", notify=compatibleMachineFamiliesChanged) - def compatibleMachineFamilies(self): + def compatibleMachineFamilies(self) -> List[str]: # Hack; Some versions of cluster will return a family more than once... return list(set(self._compatible_machine_families)) @@ -77,11 +77,11 @@ class PrintJobOutputModel(QObject): self._configuration = configuration self.configurationChanged.emit() - @pyqtProperty(str, notify=ownerChanged) - def owner(self): + @pyqtProperty(str, notify = ownerChanged) + def owner(self) -> str: return self._owner - def updateOwner(self, owner): + def updateOwner(self, owner: str) -> None: if self._owner != owner: self._owner = owner self.ownerChanged.emit() @@ -132,7 +132,7 @@ class PrintJobOutputModel(QObject): @pyqtProperty(float, notify = timeElapsedChanged) def progress(self) -> float: - result = float(self.timeElapsed) / max(self.timeTotal, 1.0) # Prevent a division by zero exception. + result = float(self.timeElapsed) / max(self.timeTotal, 1.0) # Prevent a division by zero exception. return min(result, 1.0) # Never get a progress past 1.0 @pyqtProperty(str, notify=stateChanged) @@ -151,12 +151,12 @@ class PrintJobOutputModel(QObject): return False return True - def updateTimeTotal(self, new_time_total): + def updateTimeTotal(self, new_time_total: int) -> None: if self._time_total != new_time_total: self._time_total = new_time_total self.timeTotalChanged.emit() - def updateTimeElapsed(self, new_time_elapsed): + def updateTimeElapsed(self, new_time_elapsed: int) -> None: if self._time_elapsed != new_time_elapsed: self._time_elapsed = new_time_elapsed self.timeElapsedChanged.emit() diff --git a/plugins/UM3NetworkPrinting/src/Models/Http/ClusterPrintJobStatus.py b/plugins/UM3NetworkPrinting/src/Models/Http/ClusterPrintJobStatus.py index 987ca9fab1..6873582074 100644 --- a/plugins/UM3NetworkPrinting/src/Models/Http/ClusterPrintJobStatus.py +++ b/plugins/UM3NetworkPrinting/src/Models/Http/ClusterPrintJobStatus.py @@ -40,7 +40,7 @@ class ClusterPrintJobStatus(BaseModel): configuration_changes_required: List[ Union[Dict[str, Any], ClusterPrintJobConfigurationChange]] = None, build_plate: Union[Dict[str, Any], ClusterBuildPlate] = None, - compatible_machine_families: List[str] = None, + compatible_machine_families: Optional[List[str]] = None, impediments_to_printing: List[Union[Dict[str, Any], ClusterPrintJobImpediment]] = None, preview_url: Optional[str] = None, **kwargs) -> None: @@ -97,7 +97,7 @@ class ClusterPrintJobStatus(BaseModel): configuration_changes_required) \ if configuration_changes_required else [] self.build_plate = self.parseModel(ClusterBuildPlate, build_plate) if build_plate else None - self.compatible_machine_families = compatible_machine_families if compatible_machine_families else [] + self.compatible_machine_families = compatible_machine_families if compatible_machine_families is not None else [] self.impediments_to_printing = self.parseModels(ClusterPrintJobImpediment, impediments_to_printing) \ if impediments_to_printing else [] @@ -130,8 +130,10 @@ class ClusterPrintJobStatus(BaseModel): model.updateConfiguration(self._createConfigurationModel()) model.updateTimeTotal(self.time_total) - model.updateTimeElapsed(self.time_elapsed) - model.updateOwner(self.owner) + if self.time_elapsed is not None: + model.updateTimeElapsed(self.time_elapsed) + if self.owner is not None: + model.updateOwner(self.owner) model.updateState(self.status) model.setCompatibleMachineFamilies(self.compatible_machine_families)