diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py index 60c62430370e96..a0c66b0a16f915 100644 --- a/Lib/test/test_dict.py +++ b/Lib/test/test_dict.py @@ -28,6 +28,23 @@ def test_constructor(self): self.assertEqual(dict(), {}) self.assertIsNot(dict(), {}) + def test_constructor_positional_argument_error_messages(self): + with self.assertRaisesRegex(TypeError, + r"dict expected at most 1 positional argument, got 2"): + dict('John', 36) + with self.assertRaisesRegex(TypeError, + r"dict expected at most 1 positional argument, got 3"): + dict('a', 'b', 'c') + + def test_update_positional_argument_error_messages(self): + d = {} + with self.assertRaisesRegex(TypeError, + r"update expected at most 1 positional argument, got 2"): + d.update('John', 36) + with self.assertRaisesRegex(TypeError, + r"update expected at most 1 positional argument, got 3"): + d.update('a', 'b', 'c') + def test_literal_constructor(self): # check literal constructor for different sized dicts # (to exercise the BUILD_MAP oparg). diff --git a/Python/getargs.c b/Python/getargs.c index c119ca5c35398b..9404db79700380 100644 --- a/Python/getargs.c +++ b/Python/getargs.c @@ -2740,11 +2740,14 @@ _PyArg_CheckPositional(const char *name, Py_ssize_t nargs, assert(min <= max); if (nargs < min) { - if (name != NULL) + if (name != NULL) { + const char *arg_type = (strcmp(name, "dict") == 0 || strcmp(name, "update") == 0) + ? "positional argument" : "argument"; PyErr_Format( PyExc_TypeError, - "%.200s expected %s%zd argument%s, got %zd", - name, (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs); + "%.200s expected %s%zd %s%s, got %zd", + name, (min == max ? "" : "at least "), min, arg_type, min == 1 ? "" : "s", nargs); + } else PyErr_Format( PyExc_TypeError, @@ -2759,11 +2762,14 @@ _PyArg_CheckPositional(const char *name, Py_ssize_t nargs, } if (nargs > max) { - if (name != NULL) + if (name != NULL) { + const char *arg_type = (strcmp(name, "dict") == 0 || strcmp(name, "update") == 0) + ? "positional argument" : "argument"; PyErr_Format( PyExc_TypeError, - "%.200s expected %s%zd argument%s, got %zd", - name, (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs); + "%.200s expected %s%zd %s%s, got %zd", + name, (min == max ? "" : "at most "), max, arg_type, max == 1 ? "" : "s", nargs); + } else PyErr_Format( PyExc_TypeError,