adafruit / circuitpython

CircuitPython - a Python implementation for teaching coding with microcontrollers
https://circuitpython.org
Other
4.02k stars 1.19k forks source link

Object init error string shows wrong number of arguments #3112

Open dunkmann00 opened 4 years ago

dunkmann00 commented 4 years ago

When initializing an object, the error that is produced when the wrong number of positional arguments is given shows the wrong number of arguments given and required. It is off by +1. From my testing, this only happens when the object being initialized is a subclass.

Example:

import board
import neopixel # subclass of either _pixelbuf or pypixelbuf, depending on CP version and/or board

extra_arg = None

pixels = neopixel.NeoPixel(board.D5, 24, extra_arg) # This is supposed to have only 2 positional arguments here

Output:

TypeError: function takes 4 positional arguments but 5 were given

Expected Output:

TypeError: function takes 3 positional arguments but 4 were given

I tested this on an itsybitsy_m4_express running CP 5.3.0 and 5.4.0-beta.1

dhalbert commented 4 years ago

Sorry, i misunderstood and posted an incorrect response, which I've deleted.

dhalbert commented 4 years ago

Reproduced. I also tried a third layer of class nesting (not shown), and got the same off-by-1 error (as opposed to, say, off by 2).

This bug is also present in MicroPython, so I'll file an issue there.

class A:
    def __init__(self):
        pass

class B(A):
    def __init__(self):
        super().__init__()

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

try:
    print("B(1)")
    B(1)
except Exception as e:
    print(e)
Adafruit CircuitPython 6.0.0-alpha.0-6-g11cb3e3b4-dirty on 2020-07-01; [...]
>>> import args
A(1)
function takes 1 positional arguments but 3 were given
B(1)
function takes 2 positional arguments but 3 were given
>>> 
dunkmann00 commented 4 years ago

Sounds good!