diff --git a/src/core/processing/models/qgsprocessingmodelalgorithm.cpp b/src/core/processing/models/qgsprocessingmodelalgorithm.cpp index 0292e7972a21..17825726dc88 100644 --- a/src/core/processing/models/qgsprocessingmodelalgorithm.cpp +++ b/src/core/processing/models/qgsprocessingmodelalgorithm.cpp @@ -771,10 +771,13 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth { // add specific parameter type imports const auto params = parameterDefinitions(); - importLines.reserve( params.count() + 3 ); + importLines.reserve( params.count() + 6 ); + importLines << QStringLiteral( "from typing import Any, Optional" ); + importLines << QString(); importLines << QStringLiteral( "from qgis.core import QgsProcessing" ); importLines << QStringLiteral( "from qgis.core import QgsProcessingAlgorithm" ); - importLines << QStringLiteral( "from qgis.core import QgsProcessingMultiStepFeedback" ); + importLines << QStringLiteral( "from qgis.core import QgsProcessingContext" ); + importLines << QStringLiteral( "from qgis.core import QgsProcessingFeedback, QgsProcessingMultiStepFeedback" ); bool hasAdvancedParams = false; for ( const QgsProcessingParameterDefinition *def : params ) @@ -790,14 +793,14 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth if ( hasAdvancedParams ) importLines << QStringLiteral( "from qgis.core import QgsProcessingParameterDefinition" ); - lines << QStringLiteral( "import processing" ); + lines << QStringLiteral( "from qgis import processing" ); lines << QString() << QString(); lines << QStringLiteral( "class %1(QgsProcessingAlgorithm):" ).arg( algorithmClassName ); lines << QString(); // initAlgorithm, parameter definitions - lines << indent + QStringLiteral( "def initAlgorithm(self, config=None):" ); + lines << indent + QStringLiteral( "def initAlgorithm(self, config: Optional[dict[str, Any]] = None):" ); if ( params.empty() ) { lines << indent + indent + QStringLiteral( "pass" ); @@ -842,7 +845,7 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth } lines << QString(); - lines << indent + QStringLiteral( "def processAlgorithm(self, parameters, context, model_feedback):" ); + lines << indent + QStringLiteral( "def processAlgorithm(self, parameters: dict[str, Any], context: QgsProcessingContext, model_feedback: QgsProcessingFeedback) -> dict[str, Any]:" ); currentIndent = indent + indent; lines << currentIndent + QStringLiteral( "# Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the" ); @@ -988,38 +991,38 @@ QStringList QgsProcessingModelAlgorithm::asPythonCode( const QgsProcessing::Pyth lines << QString(); // name, displayName - lines << indent + QStringLiteral( "def name(self):" ); + lines << indent + QStringLiteral( "def name(self) -> str:" ); lines << indent + indent + QStringLiteral( "return '%1'" ).arg( mModelName ); lines << QString(); - lines << indent + QStringLiteral( "def displayName(self):" ); + lines << indent + QStringLiteral( "def displayName(self) -> str:" ); lines << indent + indent + QStringLiteral( "return '%1'" ).arg( mModelName ); lines << QString(); // group, groupId - lines << indent + QStringLiteral( "def group(self):" ); + lines << indent + QStringLiteral( "def group(self) -> str:" ); lines << indent + indent + QStringLiteral( "return '%1'" ).arg( mModelGroup ); lines << QString(); - lines << indent + QStringLiteral( "def groupId(self):" ); + lines << indent + QStringLiteral( "def groupId(self) -> str:" ); lines << indent + indent + QStringLiteral( "return '%1'" ).arg( mModelGroupId ); lines << QString(); // help if ( !shortHelpString().isEmpty() ) { - lines << indent + QStringLiteral( "def shortHelpString(self):" ); + lines << indent + QStringLiteral( "def shortHelpString(self) -> str:" ); lines << indent + indent + QStringLiteral( "return \"\"\"%1\"\"\"" ).arg( shortHelpString() ); lines << QString(); } if ( !helpUrl().isEmpty() ) { - lines << indent + QStringLiteral( "def helpUrl(self):" ); + lines << indent + QStringLiteral( "def helpUrl(self) -> str:" ); lines << indent + indent + QStringLiteral( "return '%1'" ).arg( helpUrl() ); lines << QString(); } // createInstance lines << indent + QStringLiteral( "def createInstance(self):" ); - lines << indent + indent + QStringLiteral( "return %1()" ).arg( algorithmClassName ); + lines << indent + indent + QStringLiteral( "return self.__class__()" ); // additional import lines static QMap< QString, QString > sAdditionalImports diff --git a/tests/src/analysis/testqgsprocessingmodelalgorithm.cpp b/tests/src/analysis/testqgsprocessingmodelalgorithm.cpp index 7a72c5d0df3c..133ef2c70c2f 100644 --- a/tests/src/analysis/testqgsprocessingmodelalgorithm.cpp +++ b/tests/src/analysis/testqgsprocessingmodelalgorithm.cpp @@ -1454,9 +1454,12 @@ void TestQgsProcessingModelAlgorithm::modelExecution() "Group : \n" "With QGIS : %1\n" "\"\"\"\n\n" + "from typing import Any, Optional\n" + "\n" "from qgis.core import QgsProcessing\n" "from qgis.core import QgsProcessingAlgorithm\n" - "from qgis.core import QgsProcessingMultiStepFeedback\n" + "from qgis.core import QgsProcessingContext\n" + "from qgis.core import QgsProcessingFeedback, QgsProcessingMultiStepFeedback\n" "from qgis.core import QgsProcessingParameterFeatureSource\n" "from qgis.core import QgsProcessingParameterNumber\n" "from qgis.core import QgsProcessingParameterCrs\n" @@ -1464,12 +1467,12 @@ void TestQgsProcessingModelAlgorithm::modelExecution() "from qgis.core import QgsProcessingParameterDefinition\n" "from qgis.core import QgsCoordinateReferenceSystem\n" "from qgis.core import QgsExpression\n" - "import processing\n" + "from qgis import processing\n" "\n" "\n" "class MyModel(QgsProcessingAlgorithm):\n" "\n" - " def initAlgorithm(self, config=None):\n" + " def initAlgorithm(self, config: Optional[dict[str, Any]] = None):\n" " # an input\n" " self.addParameter(QgsProcessingParameterFeatureSource('SOURCE_LAYER', '', defaultValue=None))\n" " self.addParameter(QgsProcessingParameterNumber('DIST', '', type=QgsProcessingParameterNumber.Double, defaultValue=None))\n" @@ -1479,7 +1482,7 @@ void TestQgsProcessingModelAlgorithm::modelExecution() " self.addParameter(QgsProcessingParameterFeatureSink('MyModelOutput', 'my model output', type=QgsProcessing.TypeVectorPolygon, createByDefault=True, supportsAppend=True, defaultValue=None))\n" " self.addParameter(QgsProcessingParameterFeatureSink('MyOutput', 'My output', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, defaultValue=None))\n" "\n" - " def processAlgorithm(self, parameters, context, model_feedback):\n" + " def processAlgorithm(self, parameters: dict[str, Any], context: QgsProcessingContext, model_feedback: QgsProcessingFeedback) -> dict[str, Any]:\n" " # Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the\n" " # overall progress through the model\n" " feedback = QgsProcessingMultiStepFeedback(3, model_feedback)\n" @@ -1523,20 +1526,20 @@ void TestQgsProcessingModelAlgorithm::modelExecution() " results['MyOutput'] = outputs['cx3']['OUTPUT']\n" " return results\n" "\n" - " def name(self):\n" + " def name(self) -> str:\n" " return '2my model'\n" "\n" - " def displayName(self):\n" + " def displayName(self) -> str:\n" " return '2my model'\n" "\n" - " def group(self):\n" + " def group(self) -> str:\n" " return ''\n" "\n" - " def groupId(self):\n" + " def groupId(self) -> str:\n" " return ''\n" "\n" " def createInstance(self):\n" - " return MyModel()\n" ) + " return self.__class__()\n" ) .arg( Qgis::versionInt() ) .split( '\n' ); QCOMPARE( actualParts, expectedParts );