Skip to content
Permalink
Browse files

Default to requesting all attributes for python expression functions

Fix #14985
  • Loading branch information
nyalldawson committed Jun 9, 2016
1 parent 9e54287 commit 1bc17e6c4f40ab64a7d3443886e13f926dab23b7
Showing with 23 additions and 5 deletions.
  1. +4 −4 python/core/__init__.py
  2. +19 −1 tests/src/python/test_qgsexpression.py
@@ -30,7 +30,7 @@
from qgis._core import *


def register_function(function, arg_count, group, usesgeometry=False, **kwargs):
def register_function(function, arg_count, group, usesgeometry=False, referenced_columns=[QgsFeatureRequest.AllAttributes], **kwargs):
"""
Register a Python function to be used as a expression function.
@@ -59,8 +59,8 @@ def myfunc(values, *args):
"""
class QgsExpressionFunction(QgsExpression.Function):

def __init__(self, func, name, args, group, helptext='', usesgeometry=True, expandargs=False):
QgsExpression.Function.__init__(self, name, args, group, helptext, usesgeometry)
def __init__(self, func, name, args, group, helptext='', usesgeometry=True, referencedColumns=QgsFeatureRequest.AllAttributes, expandargs=False):
QgsExpression.Function.__init__(self, name, args, group, helptext, usesgeometry, referencedColumns)
self.function = func
self.expandargs = expandargs

@@ -100,7 +100,7 @@ def func(self, values, feature, parent):

function.__name__ = name
helptext = helptemplate.safe_substitute(name=name, doc=helptext)
f = QgsExpressionFunction(function, name, arg_count, group, helptext, usesgeometry, expandargs)
f = QgsExpressionFunction(function, name, arg_count, group, helptext, usesgeometry, referenced_columns, expandargs)

# This doesn't really make any sense here but does when used from a decorator context
# so it can stay.
@@ -16,7 +16,7 @@

from qgis.testing import unittest
from qgis.utils import qgsfunction
from qgis.core import QgsExpression
from qgis.core import QgsExpression, QgsFeatureRequest


class TestQgsExpressionCustomFunctions(unittest.TestCase):
@@ -46,6 +46,14 @@ def sqrt(values, feature, parent):
def geomtest(values, feature, parent):
pass

@qgsfunction(args=0, group='testing', register=False)
def no_referenced_columns_set(values, feature, parent):
return 1

@qgsfunction(args=0, group='testing', register=False, referenced_columns=['a', 'b'])
def referenced_columns_set(values, feature, parent):
return 2

def tearDown(self):
QgsExpression.unregisterFunction('testfun')

@@ -118,6 +126,16 @@ def testCanRegisterGeometryFunction(self):
success = QgsExpression.registerFunction(self.geomtest)
self.assertTrue(success)

def testReferencedColumnsNoSet(self):
success = QgsExpression.registerFunction(self.no_referenced_columns_set)
exp = QgsExpression('no_referenced_columns_set()')
self.assertEqual(exp.referencedColumns(), [QgsFeatureRequest.AllAttributes])

def testReferencedColumnsSet(self):
success = QgsExpression.registerFunction(self.referenced_columns_set)
exp = QgsExpression('referenced_columns_set()')
self.assertEqual(exp.referencedColumns(), ['a', 'b'])

def testCantOverrideBuiltinsWithUnregister(self):
success = QgsExpression.unregisterFunction("sqrt")
self.assertFalse(success)

0 comments on commit 1bc17e6

Please sign in to comment.
You can’t perform that action at this time.