marqueemoon
(Pierre Quentel)
1
Since the latest versions, the interpreter has provided suggestions in case of NameError or AttributeError. This is very helpful.
I suggest to add another kind of suggestions for invalid keyword arguments.
>>> s = 'abcd'
>>> s.split(',', maxplit=1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
s.split(',', maxplit=1)
~~~~~~~^^^^^^^^^^^^^^^^
TypeError: 'maxplit' is an invalid keyword argument for split()
>>>
The error message could be completed as
TypeError: 'maxplit' is an invalid keyword argument for split(). Did you mean: 'maxsplit'?
2 Likes
So the good news is Pablo used his time machine to mostly already implement this feature: gh-107944: Improve error message for function calls with bad keyword arguments by pablogsal · Pull Request #107969 · python/cpython · GitHub
(The bad news is it looks like that only implements it for pure Python, so doesn’t trigger for str.split
, but that seems fixable)
7 Likes
Let’s make str.split
offer suggestions as well:
5 Likes
marqueemoon
(Pierre Quentel)
4
Thanks @hauntsaninja !
I tested with Python 3.13 but only with str.split()
, the feature is actually already implemented for user-defined functions.