Skip to content

Commit c606abc

Browse files
Gustrym-kuhn
authored andcommitted
enable custom help in python expressions
1 parent 8a2cf3f commit c606abc

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

python/core/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def referencedColumns(self, node):
112112

113113
helptemplate = string.Template("""<h3>$name function</h3><br>$doc""")
114114
name = kwargs.get('name', function.__name__)
115-
helptext = function.__doc__ or ''
115+
helptext = kwargs.get('helpText') or function.__doc__ or ''
116116
helptext = helptext.strip()
117117
expandargs = False
118118

tests/src/python/test_qgsexpression.py

+23
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ def special(values, feature, parent):
4343
def sqrt(values, feature, parent):
4444
pass
4545

46+
@qgsfunction(1, 'testing', register=False)
47+
def help_with_docstring(values, feature, parent):
48+
"""The help comes from the python docstring."""
49+
pass
50+
51+
help_text = 'The help comes from a variable.'
52+
53+
@qgsfunction(1, 'testing', register=False, helpText=help_text)
54+
def help_with_variable(values, feature, parent):
55+
"""This docstring is not used for the help."""
56+
pass
57+
4658
@qgsfunction(1, 'testing', register=False, usesgeometry=True)
4759
def geomtest(values, feature, parent):
4860
pass
@@ -68,6 +80,17 @@ def testAutoCountsCorrectArgs(self):
6880
args = function.params()
6981
self.assertEqual(args, 3)
7082

83+
def testHelp(self):
84+
QgsExpression.registerFunction(self.help_with_variable)
85+
html = ('<h3>help_with_variable function</h3><br>'
86+
'The help comes from a variable.')
87+
self.assertEqual(self.help_with_variable.helpText(), html)
88+
89+
QgsExpression.registerFunction(self.help_with_docstring)
90+
html = ('<h3>help_with_docstring function</h3><br>'
91+
'The help comes from the python docstring.')
92+
self.assertEqual(self.help_with_docstring.helpText(), html)
93+
7194
def testAutoArgsAreExpanded(self):
7295
function = self.expandargs
7396
args = function.params()

0 commit comments

Comments
 (0)