argument count error is wrong when subclass calls super().__init__()
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
super().__init__(*args, **kwargs) doesn't pass kwargs to parent C class.
Port, board and/or hardware
openmv
MicroPython version
v1.23.0
Reproduction
See below.
Expected behaviour
There should be no error.
Observed behaviour
We have a class like this:
class Model(uml.Model):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Its parent is a custom C class that implements make_new and accepts positional and keyword arguments.
When you pass just positional arguments to Model() in code using this subclass things work as expected. When you pass kwargs you get this error:
TypeError: function doesn't take keyword arguments
It appears MicroPython refuses to pass the kwargs to the parent class. Note that the error is not coming from code being executed in our make_new in C. It appears something else has rejected the kwargs being passed.
Additional Information
No response
Code of Conduct
Yes, I agree