← index #3281Issue #2969
Related · high · value 1.819
QUERY · ISSUE

Repeated keyword argument should cause SyntaxError, not TypeError

openby alex-robbinsopened 2017-08-22updated 2024-08-28
bug

CPython:

>>> def f(x): pass
... 
>>> f(x=1, x=1)
  File "<stdin>", line 1
SyntaxError: keyword argument repeated

MicroPython:

>>> def f(x): pass
... 
>>> f(x=1, x=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: function got multiple values for argument 'x'

Nitpicky? Yes. But I doubt fixing this would change code size, so...

See also #466. I would have submitted a patch instead of an issue, but it looks like that exception can be raised from two different places, and I don't know the difference.

Thanks,
Alex

CANDIDATE · ISSUE

py: Confusing error message when function passed a keyword arg that's not expected

closedby dpgeorgeopened 2017-03-21updated 2017-03-22

Consider this code snippet:

def f(x):
    pass
f(y=1)

Errors reported are:

  • In CPython: TypeError: f() got an unexpected keyword argument 'y'
  • In uPy: TypeError: function does not take keyword arguments

I hit this when calling a user-defined function and was very confused because all user-defined functions accept keyword arguments (only built-in ones may not accept kw args). IMO the error message should at least be changed to be less confusing, but if we can spare bytes it should report which particular kw arg is causing the problem (and that's quite a simple change since at that point in the code it is known what the kw is that couldn't be found).

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