Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 88968ad

Browse files
committed
only take into account positional arguments count in related error messages
1 parent 5a3ef5b commit 88968ad

5 files changed

Lines changed: 16 additions & 6 deletions

File tree

Lib/inspect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@ def getcallargs(func, *positional, **named):
959959
else:
960960
arg2value[varargs] = ()
961961
elif 0 < num_args < num_pos:
962-
raise TypeError('%s() takes %s %d %s (%d given)' % (
962+
raise TypeError('%s() takes %s %d positional %s (%d given)' % (
963963
f_name, 'at most' if defaults else 'exactly', num_args,
964964
'arguments' if num_args > 1 else 'argument', num_total))
965965
elif num_args == 0 and num_total:

Lib/test/test_extcall.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,13 @@
279279
>>> f(6, a=4, *(1, 2, 3))
280280
Traceback (most recent call last):
281281
...
282-
TypeError: f() takes exactly 1 argument (5 given)
282+
TypeError: f() takes exactly 1 positional argument (5 given)
283283
>>> def f(a, *, kw):
284284
... pass
285285
>>> f(6, 4, kw=4)
286286
Traceback (most recent call last):
287287
...
288-
TypeError: f() takes exactly 2 arguments (3 given)
288+
TypeError: f() takes exactly 1 positional argument (3 given)
289289
"""
290290

291291
import sys

Lib/test/test_keywordonlyarg.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ def testSyntaxForManyArguments(self):
7373
fundef3 += "lastarg):\n pass\n"
7474
compile(fundef3, "<test>", "single")
7575

76+
def testTooManyPositionalErrorMessage(self):
77+
def f(a, b=None, *, c=None):
78+
pass
79+
with self.assertRaises(TypeError) as exc:
80+
f(1, 2, 3)
81+
expected = "f() takes at most 2 positional arguments (3 given)"
82+
self.assertEqual(str(exc.exception), expected)
83+
7684
def testSyntaxErrorForFunctionCall(self):
7785
self.assertRaisesSyntaxError("f(p, k=1, p2)")
7886
self.assertRaisesSyntaxError("f(p, k1=50, *(1,2), k1=100)")

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ What's New in Python 3.2 Alpha 1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Be more specific in error messages about positional arguments.
16+
1517
- Issue #8949: "z" format of PyArg_Parse*() functions doesn't accept bytes
1618
objects, as described in the documentation.
1719

Python/ceval.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3100,11 +3100,11 @@ PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
31003100
if (!(co->co_flags & CO_VARARGS)) {
31013101
PyErr_Format(PyExc_TypeError,
31023102
"%U() takes %s %d "
3103-
"argument%s (%d given)",
3103+
"positional argument%s (%d given)",
31043104
co->co_name,
31053105
defcount ? "at most" : "exactly",
3106-
total_args,
3107-
total_args == 1 ? "" : "s",
3106+
co->co_argcount,
3107+
co->co_argcount == 1 ? "" : "s",
31083108
argcount + kwcount);
31093109
goto fail;
31103110
}

0 commit comments

Comments
 (0)