[issue44090] Add class binding to unbound super objects for allowing autosuper with class methods

Géry report at bugs.python.org
Tue Apr 5 07:59:10 EDT 2022


Géry <gery.ogam at gmail.com> added the comment:

> Anthony Sottile provided this search to showing that at least a few popular projects are using the one argument form of super():

Thanks for the link. But it gives lots of false positives. Only two popular projects (> 1k stars) use autosuper (urwid and evennia):

https://sourcegraph.com/search?q=context:global+setattr%5C%28.*super%5C%28.*%5C%29%5C%29+file:%5C.py%24+-file:test.*%5C.py%24&patternType=regexp&case=yes

The remaining projects use one-argument super incorrectly, as `super(cls).method()`, which looks up the method directly on class `super`:

https://sourcegraph.com/search?q=context:global+content:%5B%5E_%5Dsuper%5C%28%5Cw%2B%5C%29%5B%5E:%5D+file:%5C.py%24+-file:test.*%5C.py%24&patternType=regexp&case=yes

It is either a loud bug, which raises an `AttributeError`:

```
>>> class A:
...     def f(self): pass
... 
>>> class B(A):
...     def f(self): super(B).f()
... 
>>> B().f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in f
AttributeError: 'super' object has no attribute 'f'
```

Or worse with `super(cls).__init__()` (99% of the cases), it is a SILENT bug, which call the constructor of class `super` instead of the parent constructor, leaving the object in an incompletely initialized state:

```
>>> class A:
...     def __init__(self): print('hello')
... 
>>> class B(A):
...     def __init__(self): super(B).__init__()
... 
>>> A()
hello
<__main__.A object at 0x10926e460>
>>> B()
<__main__.B object at 0x10926e520>
```

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44090>
_______________________________________


More information about the Python-bugs-list mailing list