Skip to content
Permalink
Browse files

Default to requesting all attributes for python expression functions

Fix #14985

(cherry-picked from 1bc17e6)
  • Loading branch information
nyalldawson committed Jul 1, 2016
1 parent be8e4f9 commit 248ba385cff6d594f41de9abf84d811927ddd9ef
Showing with 23 additions and 5 deletions.
  1. +4 −4 python/core/__init__.py
  2. +19 −1 tests/src/python/test_qgsexpression.py
@@ -35,7 +35,7 @@
from PyQt4.QtCore import QCoreApplication


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.
@@ -64,8 +64,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

@@ -105,7 +105,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.
@@ -15,7 +15,7 @@
import qgis
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):
@@ -45,6 +45,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')

@@ -117,6 +125,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 248ba38

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