Skip to content

Commit 0fb2fcf

Browse files
committed
PYTHON-700 - Support subclassing of son manipulators
1 parent 9dda134 commit 0fb2fcf

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

pymongo/database.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from pymongo.read_preferences import (modes,
3030
secondary_ok_commands,
3131
ReadPreference)
32+
from pymongo.son_manipulator import SONManipulator
3233

3334

3435
def _check_name(name):
@@ -94,9 +95,10 @@ def add_son_manipulator(self, manipulator):
9495
:Parameters:
9596
- `manipulator`: the manipulator to add
9697
"""
98+
base = SONManipulator()
9799
def method_overwritten(instance, method):
98-
return getattr(instance, method) != \
99-
getattr(super(instance.__class__, instance), method)
100+
return (getattr(
101+
instance, method).im_func != getattr(base, method).im_func)
100102

101103
if manipulator.will_copy():
102104
if method_overwritten(manipulator, "transform_incoming"):

test/test_database.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,30 @@ def transform_incoming(self, thing, collection):
707707
out = db.test.find_one()
708708
self.assertEqual('value', out.get('value'))
709709

710+
def test_son_manipulator_inheritance(self):
711+
class Thing(object):
712+
def __init__(self, value):
713+
self.value = value
714+
715+
class ThingTransformer(SONManipulator):
716+
def transform_incoming(self, thing, collection):
717+
return {'value': thing.value}
718+
719+
def transform_outgoing(self, son, collection):
720+
return Thing(son['value'])
721+
722+
class Child(ThingTransformer):
723+
pass
724+
725+
db = self.client.foo
726+
db.add_son_manipulator(Child())
727+
t = Thing('value')
728+
729+
db.test.remove()
730+
db.test.insert([t])
731+
out = db.test.find_one()
732+
self.assertTrue(isinstance(out, Thing))
733+
self.assertEqual('value', out.value)
710734

711735
if __name__ == "__main__":
712736
unittest.main()

0 commit comments

Comments
 (0)