Page MenuHomePhabricator

classes derived from object should always call super in Initializer
Open, LowPublic

Description

If a class is derived from multiple instances, the initializer must alway call ist super initializer even the class is derived from object. Otherwise the MRO chain is broken:

>>> class foo(object):
	def __init__(self):
		print('foo')
		
>>> class bar(object):
	def __init__(self):
		print('bar')
		
>>> class baz(foo, bar):
	def __init__(self):
		print('baz')
		super(baz, self).__init__()

>>> x = baz()
baz
foo

bar initializer isn't called here whereas the following works as expected:

>>> class foo(object):
	def __init__(self):
		print('foo')
		super(foo, self).__init__()

>>> class bar(object):
	def __init__(self):
		print('bar')
		super(bar, self).__init__()

>>> class baz(foo, bar):
	def __init__(self):
		print('baz')
		super(baz, self).__init__()

>>> x = baz()
baz
foo
bar

Event Timeline

Xqt created this task.Jan 1 2019, 2:36 PM
Restricted Application added subscribers: pywikibot-bugs-list, Aklapper. · View Herald TranscriptJan 1 2019, 2:36 PM
Xqt triaged this task as High priority.Jan 1 2019, 2:36 PM
Xqt removed Xqt as the assignee of this task.Jan 1 2019, 6:59 PM
Mpaa added a subscriber: Mpaa.Jan 4 2019, 10:49 PM

Not sure this is a bug or a must, it is more about how you want the classes to cooperate with each other.
MRO chain is not broken in the first example, the design is such that the chain of calls does not propagate all the way up (it might also be a design choice, see e.g. https://rhettinger.wordpress.com/2011/05/26/super-considered-super/#comment-86).

>>> baz.mro()                                                                                                             
[__main__.baz, __main__.foo, __main__.bar, object]
Xqt added a comment.EditedJan 5 2019, 3:17 AM

The MRO chain is not safe and can be different between py2.7 and py 3.5 I guess. All I want to do is to check whether the initialising process is done as wanted or broken by accident leaving out the super call in a class directly derived from object.

Xqt lowered the priority of this task from High to Low.Jan 5 2019, 3:32 AM