Skip to content

Commit 25b751c

Browse files
committed
gh-137626: fix error messages of dict() and dict().update()
1 parent deb0020 commit 25b751c

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

Lib/test/test_dict.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@ def test_constructor(self):
2828
self.assertEqual(dict(), {})
2929
self.assertIsNot(dict(), {})
3030

31+
def test_constructor_positional_argument_error_messages(self):
32+
with self.assertRaisesRegex(TypeError,
33+
r"dict expected at most 1 positional argument, got 2"):
34+
dict('John', 36)
35+
with self.assertRaisesRegex(TypeError,
36+
r"dict expected at most 1 positional argument, got 3"):
37+
dict('a', 'b', 'c')
38+
39+
def test_update_positional_argument_error_messages(self):
40+
d = {}
41+
with self.assertRaisesRegex(TypeError,
42+
r"update expected at most 1 positional argument, got 2"):
43+
d.update('John', 36)
44+
with self.assertRaisesRegex(TypeError,
45+
r"update expected at most 1 positional argument, got 3"):
46+
d.update('a', 'b', 'c')
47+
3148
def test_literal_constructor(self):
3249
# check literal constructor for different sized dicts
3350
# (to exercise the BUILD_MAP oparg).

Python/getargs.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2740,11 +2740,14 @@ _PyArg_CheckPositional(const char *name, Py_ssize_t nargs,
27402740
assert(min <= max);
27412741

27422742
if (nargs < min) {
2743-
if (name != NULL)
2743+
if (name != NULL) {
2744+
const char *arg_type = (strcmp(name, "dict") == 0 || strcmp(name, "update") == 0)
2745+
? "positional argument" : "argument";
27442746
PyErr_Format(
27452747
PyExc_TypeError,
2746-
"%.200s expected %s%zd argument%s, got %zd",
2747-
name, (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs);
2748+
"%.200s expected %s%zd %s%s, got %zd",
2749+
name, (min == max ? "" : "at least "), min, arg_type, min == 1 ? "" : "s", nargs);
2750+
}
27482751
else
27492752
PyErr_Format(
27502753
PyExc_TypeError,
@@ -2759,11 +2762,14 @@ _PyArg_CheckPositional(const char *name, Py_ssize_t nargs,
27592762
}
27602763

27612764
if (nargs > max) {
2762-
if (name != NULL)
2765+
if (name != NULL) {
2766+
const char *arg_type = (strcmp(name, "dict") == 0 || strcmp(name, "update") == 0)
2767+
? "positional argument" : "argument";
27632768
PyErr_Format(
27642769
PyExc_TypeError,
2765-
"%.200s expected %s%zd argument%s, got %zd",
2766-
name, (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs);
2770+
"%.200s expected %s%zd %s%s, got %zd",
2771+
name, (min == max ? "" : "at most "), max, arg_type, max == 1 ? "" : "s", nargs);
2772+
}
27672773
else
27682774
PyErr_Format(
27692775
PyExc_TypeError,

0 commit comments

Comments
 (0)