This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: more argument error improving
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, ncoghlan, python-dev
Priority: normal Keywords: patch

Created on 2011-06-18 00:45 by benjamin.peterson, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
argerror.patch benjamin.peterson, 2011-06-18 00:45 review
Messages (4)
msg138563 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2011-06-18 00:45
After completing #12265, it was pointed out to me that the error message is still not perfect:

>>> def f(a, b, c=3, d=4, e=6, f=3, g=32): pass
... 
>>> f(1, f=4, d=90)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() takes from 2 to 7 positional arguments but 3 were given

Here is a new patch.

Some samples:

>>> def f(a): pass
... 
>>> f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() missing 1 required positional argument: 'a'
>>> def f(a, b): pass
... 
>>> f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() missing 2 required positional arguments: 'a' and 'b'

>>> def f(a, b, c): pass
... 
>>> f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() missing 3 required positional arguments: 'a', 'b', and 'c
>>> def f(a, b, c, d): pass
>>> f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() missing 4 required positional arguments: 'a', 'b', 'c', and 'd'

Same with kwonly:

>>> def f(*, w): pass
... 
>>> f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() missing 1 required keyword-only argument: 'w'
>>> def f(*, a, b, c, d, e): pass
... 
>>> f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() missing 5 required keyword-only arguments: 'a', 'b', 'c', 'd', and 'e'

For too many positional arguments, the old (new) error is retained:

>>> def f(a): pass
... 
>>> f(1, 2, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() takes 1 positional argument but 3 were given
>>> f(3, 4, 5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() takes from 1 to 2 positional arguments but 3 were given
msg138868 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2011-06-23 14:15
Nick (or anyone else), do you want to look at this?
msg138887 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2011-06-24 02:44
Revised error messages and tests look reasonable and the code seems fine on a visual scan.

+1 from me.
msg138950 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-06-24 14:37
New changeset 52744a5a9260 by Benjamin Peterson in branch 'default':
give the names of missing positional or keyword-only arguments (closes #12356)
http://hg.python.org/cpython/rev/52744a5a9260
History
Date User Action Args
2022-04-11 14:57:18adminsetgithub: 56565
2011-06-24 14:37:39python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg138950

resolution: fixed
stage: patch review -> resolved
2011-06-24 02:44:01ncoghlansetmessages: + msg138887
2011-06-23 14:15:41benjamin.petersonsetmessages: + msg138868
2011-06-20 13:44:27benjamin.petersonsetnosy: + ncoghlan
2011-06-18 00:45:31benjamin.petersoncreate