Skip to content

Commit 03cef16

Browse files
committed
Always create a new copy of the function table
When defining a subclass of ``Functions``, the ``getattr()`` call would retrieve the base classes function table, mutate the table, and then assign a reference of that table to the child class. We want to allow each subclass to have its own separate copy of the function table so its created from scratch each time. I think there's room for optimization here. We could copy the parent func table if it exists and iterate through the ``attrs``, which will only contain the functions defined in the actual class. Fixes #133.
1 parent 71f4485 commit 03cef16

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ Next Release (TBD)
66
(`issue 125 <https://github.com/jmespath/jmespath.py/issues/125>`__)
77
* Handle numbers in scientific notation in ``to_number()`` function
88
(`issue 120 <https://github.com/jmespath/jmespath.py/issues/120>`__)
9+
* Fix issue where custom functions would override the function table
10+
of the builtin function class
11+
(`issue 133 <https://github.com/jmespath/jmespath.py/issues/133>`__)
912

1013

1114
0.9.2

jmespath/functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def __init__(cls, name, bases, attrs):
4747
super(FunctionRegistry, cls).__init__(name, bases, attrs)
4848

4949
def _populate_function_table(cls):
50-
function_table = getattr(cls, 'FUNCTION_TABLE', {})
50+
function_table = {}
5151
# Any method with a @signature decorator that also
5252
# starts with "_func_" is registered as a function.
5353
# _func_max_by -> max_by function.

tests/test_search.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ def _func_my_subtract(self, x, y):
3838
jmespath.search('my_subtract(`10`, `3`)', {}, options=options),
3939
7
4040
)
41+
# Should still be able to use the original functions without
42+
# any interference from the CustomFunctions class.
43+
self.assertEqual(
44+
jmespath.search('length(`[1, 2]`)', {}), 2
45+
)
46+
4147

4248

4349
class TestPythonSpecificCases(unittest.TestCase):

0 commit comments

Comments
 (0)