Skip to content

Commit 1d95153

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

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,7 +2743,7 @@ _PyArg_CheckPositional(const char *name, Py_ssize_t nargs,
27432743
if (name != NULL)
27442744
PyErr_Format(
27452745
PyExc_TypeError,
2746-
"%.200s expected %s%zd argument%s, got %zd",
2746+
"%.200s expected %s%zd positional argument%s, got %zd",
27472747
name, (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs);
27482748
else
27492749
PyErr_Format(
@@ -2762,7 +2762,7 @@ _PyArg_CheckPositional(const char *name, Py_ssize_t nargs,
27622762
if (name != NULL)
27632763
PyErr_Format(
27642764
PyExc_TypeError,
2765-
"%.200s expected %s%zd argument%s, got %zd",
2765+
"%.200s expected %s%zd positional argument%s, got %zd",
27662766
name, (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs);
27672767
else
27682768
PyErr_Format(

0 commit comments

Comments
 (0)