Skip to content

Commit a67d4f7

Browse files
author
Sylvain MARIE
committed
Fixed bug with super().__init__ not behaving as expected, also with python < 3.6 . Fixed #53
1 parent 6597175 commit a67d4f7

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

pyfields/init_makers.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# Copyright (c) Schneider Electric Industries, 2019. All right reserved.
44
import sys
5-
from inspect import isfunction
5+
from inspect import isfunction, getmro
66
from itertools import islice
77

88
try:
@@ -296,6 +296,21 @@ def __get__(self, obj, objtype):
296296
# see https://github.com/smarie/python-pyfields/issues/53
297297
if self.ownercls is not None:
298298
objtype = self.ownercls
299+
elif objtype is not None:
300+
# workaround in case of python < 3.6: at least, when a subclass init is created, make sure that all super
301+
# classes init have their owner class properly set, .
302+
# That way, when the subclass __init__ will be called, containing potential calls to super(), the parents'
303+
# __init__ method descriptors will be correctly configured.
304+
for _c in reversed(getmro(objtype)[1:-1]):
305+
try:
306+
_init_member = _c.__dict__['__init__']
307+
except KeyError:
308+
continue
309+
else:
310+
if isinstance(_init_member, InitDescriptor):
311+
if _init_member.ownercls is None:
312+
# call __set_name__ explicitly (python < 3.6) to register the descriptor with the class
313+
_init_member.__set_name__(_c, '__init__')
299314

300315
# <objtype>.__init__ has been accessed. Create the modified init
301316
fields = self.fields

pyfields/tests/issues/test_issue_53.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ class B(A):
2121
def __init__(self):
2222
super(B, self).__init__(a=self.a)
2323

24+
# note that with the issue, this was raising an exception
2425
print(B('a', 'b'))

0 commit comments

Comments
 (0)