Author benjamin.peterson
Recipients benjamin.peterson
Date 2011-06-05.02:20:41
SpamBayes Score 2.75361e-09
Marked as misclassified No
Message-id <1307240443.69.0.736494632157.issue12265@psf.upfronthosting.co.za>
In-reply-to
Content
This patch completely rewrites errors given for positional error mismatches. The goal is to give more informative and correct errors.

Some examples:

>>> def f(): pass
... 
>>> f(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() takes 0 positional arguments but 1 was given
>>> f(1, kw=4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() got an unexpected keyword argument 'kw'
>>> def f(a, b=2): pass
... 
>>> f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() takes from 1 to 2 positional arguments but 0 were given
>>> f(1, 2, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() takes from 1 to 2 positional arguments but 3 were given
>>> def f(a, *b): pass
... 
>>> f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() takes at least 1 positional argument but 0 were given
>>> f(kw=4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() got an unexpected keyword argument 'kw'

When keyword-only arguments come into play, things get a bit more complicated. When a positional argument error occurs, it's confusing to report only the positional arguments, but not completely relevant to report keyword-only arguments. I comprise by putting the keyword-only argument information in parenthesis. Tell me if you have a better idea.

>>> def f(*, kw): pass
... 
>>> f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() requires keyword-only argument 'kw'
>>> f(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() takes 0 positional arguments but 1 was given
>>> f(3, kw=4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() takes 0 positional arguments but 1 positional arguments (and 1 keyword-only argument) were given
History
Date User Action Args
2011-06-05 02:20:44benjamin.petersonsetrecipients: + benjamin.peterson
2011-06-05 02:20:43benjamin.petersonsetmessageid: <1307240443.69.0.736494632157.issue12265@psf.upfronthosting.co.za>
2011-06-05 02:20:43benjamin.petersonlinkissue12265 messages
2011-06-05 02:20:42benjamin.petersoncreate