From 6f4304bc0e43a7ce9f7f015efda92a8e165fc956 Mon Sep 17 00:00:00 2001 From: Etienne Trimaille Date: Tue, 10 Dec 2024 19:11:56 +0100 Subject: [PATCH] Review the Processing script template : add annotations, remove self.tr --- .../processing/script/ScriptTemplate.py | 54 ++++++++++--------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/python/plugins/processing/script/ScriptTemplate.py b/python/plugins/processing/script/ScriptTemplate.py index 2db9a15c142a..2c6669269120 100644 --- a/python/plugins/processing/script/ScriptTemplate.py +++ b/python/plugins/processing/script/ScriptTemplate.py @@ -9,14 +9,17 @@ *************************************************************************** """ -from qgis.PyQt.QtCore import QCoreApplication +from typing import Any, Optional + from qgis.core import ( - QgsProcessing, QgsFeatureSink, - QgsProcessingException, + QgsProcessing, QgsProcessingAlgorithm, - QgsProcessingParameterFeatureSource, + QgsProcessingContext, + QgsProcessingException, + QgsProcessingFeedback, QgsProcessingParameterFeatureSink, + QgsProcessingParameterFeatureSource, ) from qgis import processing @@ -42,16 +45,7 @@ class ExampleProcessingAlgorithm(QgsProcessingAlgorithm): INPUT = "INPUT" OUTPUT = "OUTPUT" - def tr(self, string): - """ - Returns a translatable string with the self.tr() function. - """ - return QCoreApplication.translate("Processing", string) - - def createInstance(self): - return ExampleProcessingAlgorithm() - - def name(self): + def name(self) -> str: """ Returns the algorithm name, used for identifying the algorithm. This string should be fixed for the algorithm, and must not be localised. @@ -61,21 +55,21 @@ def name(self): """ return "myscript" - def displayName(self): + def displayName(self) -> str: """ Returns the translated algorithm name, which should be used for any user-visible display of the algorithm name. """ - return self.tr("My Script") + return "My Script" - def group(self): + def group(self) -> str: """ Returns the name of the group this algorithm belongs to. This string should be localised. """ - return self.tr("Example scripts") + return "Example scripts" - def groupId(self): + def groupId(self) -> str: """ Returns the unique ID of the group this algorithm belongs to. This string should be fixed for the algorithm, and must not be localised. @@ -85,15 +79,15 @@ def groupId(self): """ return "examplescripts" - def shortHelpString(self): + def shortHelpString(self) -> str: """ Returns a localised short helper string for the algorithm. This string should provide a basic description about what the algorithm does and the - parameters and outputs associated with it.. + parameters and outputs associated with it. """ - return self.tr("Example algorithm short description") + return "Example algorithm short description" - def initAlgorithm(self, config=None): + def initAlgorithm(self, config: Optional[dict[str, Any]] = None): """ Here we define the inputs and output of the algorithm, along with some other properties. @@ -104,7 +98,7 @@ def initAlgorithm(self, config=None): self.addParameter( QgsProcessingParameterFeatureSource( self.INPUT, - self.tr("Input layer"), + "Input layer", [QgsProcessing.SourceType.TypeVectorAnyGeometry], ) ) @@ -113,10 +107,15 @@ def initAlgorithm(self, config=None): # usually takes the form of a newly created vector layer when the # algorithm is run in QGIS). self.addParameter( - QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr("Output layer")) + QgsProcessingParameterFeatureSink(self.OUTPUT, "Output layer") ) - def processAlgorithm(self, parameters, context, feedback): + def processAlgorithm( + self, + parameters: dict[str, Any], + context: QgsProcessingContext, + feedback: QgsProcessingFeedback, + ) -> dict[str, Any]: """ Here is where the processing itself takes place. """ @@ -199,3 +198,6 @@ def processAlgorithm(self, parameters, context, feedback): # dictionary, with keys matching the feature corresponding parameter # or output names. return {self.OUTPUT: dest_id} + + def createInstance(self): + return self.__class__()