← index #6225Issue #400
Related · high · value 2.096
QUERY · ISSUE

argument count error is wrong when subclass calls super().__init__()

openby dhalbertopened 2020-07-03updated 2020-07-08
py-core

A CircuitPython user noticed this error (https://github.com/adafruit/circuitpython/issues/3112). It is also present in the latest build of MicroPython. An incorrect number of arguments is reported in the exception when a subclass constructor is given the wrong number of arguments, and that constructor calls the superclass constructor.

class A:
    def __init__(self):
        pass

class B(A):
    def __init__(self):
        super().__init__()  # if this is replaced with pass, the problem goes away

try:
    print("A(1)")
    A(1)
except Exception as e:
    print(e)

try:
    print("B(1)")
    B(1)
except Exception as e:
    print(e)
MicroPython v1.12-614-gc2317a3a8 on 2020-07-03; PYBv1.1 with STM32F405RG
Type "help()" for more information.
>>> import args
A(1)
function takes 1 positional arguments but 2 were given
B(1)
function takes 2 positional arguments but 3 were given      # should be 1 and 2
CANDIDATE · ISSUE

Problems with constructors of subclassed C-level classes

closedby pfalconopened 2014-03-30updated 2014-05-10
class MyError(Exception):
    pass

e = MyError()
e2 = MyError("foobar")

Traceback (most recent call last):
  File "exc-sub.py", line 5, in <module>
TypeError: function takes 0 positional arguments but 1 were given

Exception is for 2nd instantiation line. Superclass constructor has following signature: Exception.init(*args).

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied