Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport release-3_40] Script template - Add Python annotations #59817

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 28 additions & 26 deletions python/plugins/processing/script/ScriptTemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -104,7 +98,7 @@ def initAlgorithm(self, config=None):
self.addParameter(
QgsProcessingParameterFeatureSource(
self.INPUT,
self.tr("Input layer"),
"Input layer",
[QgsProcessing.SourceType.TypeVectorAnyGeometry],
)
)
Expand All @@ -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.
"""
Expand Down Expand Up @@ -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__()
Loading