8000 [abc] better error message for undefined abstractmethod · python/cpython@f5a9680 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit f5a9680

Browse files
committed
[abc] better error message for undefined abstractmethod
1 parent a04656e commit f5a9680

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

Lib/test/test_abc.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class C(metaclass=abc_ABCMeta):
154154
@abc.abstractmethod
155155
def method_one(self):
156156
pass
157-
msg = r"class C without an implementation for abstract method method_one"
157+
msg = r"class C without an implementation for abstract method 'method_one'"
158158
self.assertRaisesRegex(TypeError, msg, C)
159159

160160
def test_object_new_with_many_abstractmethods(self):
@@ -165,7 +165,7 @@ def method_one(self):
165165
@abc.abstractmethod
166166
def method_two(self):
167167
pass
168-
msg = r"class C without an implementation for abstract methods method_one, method_two"
168+
msg = r"class C without an implementation for abstract methods 'method_one', 'method_two'"
169169
self.assertRaisesRegex(TypeError, msg, C)
170170

171171
def test_abstractmethod_integration(self):
@@ -535,7 +535,7 @@ def updated_foo(self):
535535
A.foo = updated_foo
536536
abc.update_abstractmethods(A)
537537
self.assertEqual(A.__abstractmethods__, {'foo', 'bar'})
538-
msg = "class A without an implementation for abstract methods bar, foo"
538+
msg = "class A without an implementation for abstract methods 'bar', 'foo'"
539539
self.assertRaisesRegex(TypeError, msg, A)
540540

541541
def test_update_implementation(self):
@@ -547,7 +547,7 @@ def foo(self):
547547
class B(A):
548548
pass
549549

550-
msg = "class B without an implementation for abstract method foo"
550+
msg = "class B without an implementation for abstract method 'foo'"
551551
self.assertRaisesRegex(TypeError, msg, B)
552552
self.assertEqual(B.__abstractmethods__, {'foo'})
553553

@@ -605,7 +605,7 @@ def foo(self):
605605

606606
abc.update_abstractmethods(B)
607607

608-
msg = "class B without an implementation for abstract method foo"
608+
msg = "class B without an implementation for abstract method 'foo'"
609609
self.assertRaisesRegex(TypeError, msg, B)
610610

611611
def test_update_layered_implementation(self):
@@ -627,7 +627,7 @@ def foo(self):
627627

628628
abc.update_abstractmethods(C)
629629

630-
msg = "class C without an implementation for abstract method foo"
630+
msg = "class C without an implementation for abstract method 'foo'"
631631
self.assertRaisesRegex(TypeError, msg, C)
632632

633633
def test_update_multi_inheritance(self):

Lib/test/test_dataclasses.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3962,7 +3962,7 @@ class Date(A):
39623962
day: 'int'
39633963

39643964
self.assertTrue(inspect.isabstract(Date))
3965-
msg = 'class Date without an implementation for abstract method foo'
3965+
msg = "class Date without an implementation for abstract method 'foo'"
39663966
self.assertRaisesRegex(TypeError, msg, Date)
39673967

39683968

Ob 8000 jects/typeobject.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4861,9 +4861,10 @@ object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
48614861
PyObject *abstract_methods;
48624862
PyObject *sorted_methods;
48634863
PyObject *joined;
4864+
PyObject* comma_w_quotes_sep;
48644865
Py_ssize_t method_count;
48654866

4866-
/* Compute ", ".join(sorted(type.__abstractmethods__))
4867+
/* Compute "', '".join(sorted(type.__abstractmethods__))
48674868
into joined. */
48684869
abstract_methods = type_abstractmethods(type, NULL);
48694870
if (abstract_methods == NULL)
@@ -4876,8 +4877,9 @@ object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
48764877
Py_DECREF(sorted_methods);
48774878
return NULL;
48784879
}
4879-
_Py_DECLARE_STR(comma_sep, ", ");
4880-
joined = PyUnicode_Join(&_Py_STR(comma_sep), sorted_methods);
4880+
4881+
comma_quotes_sep = PyUnicode_FromString("', '");
4882+
joined = PyUnicode_Join(comma_w_quotes_sep, sorted_methods);
48814883
method_count = PyObject_Length(sorted_methods);
48824884
Py_DECREF(sorted_methods);
48834885
if (joined == NULL)
@@ -4887,11 +4889,12 @@ object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
48874889

48884890
PyErr_Format(PyExc_TypeError,
48894891
"Can't instantiate abstract class %s "
4890-
"without an implementation for abstract method%s %U",
4892+
"without an implementation for abstract method%s '%U'",
48914893
type->tp_name,
48924894
method_count > 1 ? "s" : "",
48934895
joined);
48944896
Py_DECREF(joined);
4897+
Py_DECREF(comma_w_quotes_sep);
48954898
return NULL;
48964899
}
48974900
PyObject *obj = type->tp_alloc(type, 0);

0 commit comments

Comments
 (0)
0