Skip to content

Commit

Permalink
src/class: fix init on dispatch of twice derived classes
Browse files Browse the repository at this point in the history
  • Loading branch information
rfrowe committed Jul 22, 2020
1 parent dd155e5 commit 1796719
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dynamic_dispatch/_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Registered(wrap):

def __init__(self, *args, **kwargs):
# Certain scenarios can cause __init__ to be called twice. This prevents it.
if not self.__dispatch_init:
if self.__class__ == __class__ and not self.__dispatch_init:
return

self.__dispatch_init = False
Expand Down
28 changes: 28 additions & 0 deletions tests/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,3 +541,31 @@ def __init__(self, a):
self.assertEqual(obj.abc_count, 1)
self.assertEqual(obj.a, 5)
self.assertEqual(obj.a_count, 1)

def test_dispatch_twice_derived(self):
@dynamic_dispatch
class Foo(OneArgInit):
pass

@Foo.dispatch(on='bar')
class Bar(Foo):
def __init__(self, abc, d):
super().__init__(abc)
self.d = d
self.d_count = getattr(self, 'b_count', 0) + 1

@Foo.dispatch(on='baz')
class Baz(Bar):
def __init__(self, abc, d, e):
super().__init__(abc, d)
self.e = e
self.e_count = getattr(self, 'e_count', 0) + 1

obj = Foo('baz', 'd', 'e')
self.assertIsInstance(obj, Baz)
self.assertEqual(obj.abc, 'baz')
self.assertEqual(obj.abc_count, 1)
self.assertEqual(obj.d, 'd')
self.assertEqual(obj.d_count, 1)
self.assertEqual(obj.e, 'e')
self.assertEqual(obj.e_count, 1)

0 comments on commit 1796719

Please sign in to comment.