Skip to content

Commit 4a12a17

Browse files
dangromatrixise
authored andcommitted
bpo-34331: Fix incorrectly pluralized abstract class error message. (GH-8670)
1 parent 19f6940 commit 4a12a17

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

Lib/test/test_abc.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,25 @@ def foo(): return 4
149149
self.assertEqual(D.foo(), 4)
150150
self.assertEqual(D().foo(), 4)
151151

152+
def test_object_new_with_one_abstractmethod(self):
153+
class C(metaclass=abc_ABCMeta):
154+
@abc.abstractmethod
155+
def method_one(self):
156+
pass
157+
msg = r"class C with abstract method method_one"
158+
self.assertRaisesRegex(TypeError, msg, C)
159+
160+
def test_object_new_with_many_abstractmethods(self):
161+
class C(metaclass=abc_ABCMeta):
162+
@abc.abstractmethod
163+
def method_one(self):
164+
pass
165+
@abc.abstractmethod
166+
def method_two(self):
167+
pass
168+
msg = r"class C with abstract methods method_one, method_two"
169+
self.assertRaisesRegex(TypeError, msg, C)
170+
152171
def test_abstractmethod_integration(self):
153172
for abstractthing in [abc.abstractmethod, abc.abstractproperty,
154173
abc.abstractclassmethod,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Use singular/plural noun in error message when instantiating an abstract
2+
class with non-overriden abstract method(s).

Objects/typeobject.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3753,6 +3753,7 @@ object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
37533753
PyObject *joined;
37543754
PyObject *comma;
37553755
_Py_static_string(comma_id, ", ");
3756+
Py_ssize_t method_count;
37563757

37573758
/* Compute ", ".join(sorted(type.__abstractmethods__))
37583759
into joined. */
@@ -3773,14 +3774,18 @@ object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
37733774
return NULL;
37743775
}
37753776
joined = PyUnicode_Join(comma, sorted_methods);
3777+
method_count = PyObject_Length(sorted_methods);
37763778
Py_DECREF(sorted_methods);
37773779
if (joined == NULL)
37783780
return NULL;
3781+
if (method_count == -1)
3782+
return NULL;
37793783

37803784
PyErr_Format(PyExc_TypeError,
37813785
"Can't instantiate abstract class %s "
3782-
"with abstract methods %U",
3786+
"with abstract method%s %U",
37833787
type->tp_name,
3788+
method_count > 1 ? "s" : "",
37843789
joined);
37853790
Py_DECREF(joined);
37863791
return NULL;

0 commit comments

Comments
 (0)