Skip to content

Commit

Permalink
gh-91578: improved error message when trying to instantiate an abstra…
Browse files Browse the repository at this point in the history
…ct class with missing methods (gh-47246)
  • Loading branch information
ravi140222 committed May 12, 2022
1 parent 8a0d9a6 commit 079f0dd
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
12 changes: 6 additions & 6 deletions Lib/test/test_abc.py
Expand Up @@ -154,7 +154,7 @@ class C(metaclass=abc_ABCMeta):
@abc.abstractmethod
def method_one(self):
pass
msg = r"class C with abstract method method_one"
msg = r"class C without an implementation for abstract method method_one"
self.assertRaisesRegex(TypeError, msg, C)

def test_object_new_with_many_abstractmethods(self):
Expand All @@ -165,7 +165,7 @@ def method_one(self):
@abc.abstractmethod
def method_two(self):
pass
msg = r"class C with abstract methods method_one, method_two"
msg = r"class C without an implementation for abstract methods method_one, method_two"
self.assertRaisesRegex(TypeError, msg, C)

def test_abstractmethod_integration(self):
Expand Down Expand Up @@ -535,7 +535,7 @@ def updated_foo(self):
A.foo = updated_foo
abc.update_abstractmethods(A)
self.assertEqual(A.__abstractmethods__, {'foo', 'bar'})
msg = "class A with abstract methods bar, foo"
msg = "class A without an implementation for abstract methods bar, foo"
self.assertRaisesRegex(TypeError, msg, A)

def test_update_implementation(self):
Expand All @@ -547,7 +547,7 @@ def foo(self):
class B(A):
pass

msg = "class B with abstract method foo"
msg = "class B without an implementation for abstract method foo"
self.assertRaisesRegex(TypeError, msg, B)
self.assertEqual(B.__abstractmethods__, {'foo'})

Expand Down Expand Up @@ -605,7 +605,7 @@ def foo(self):

abc.update_abstractmethods(B)

msg = "class B with abstract method foo"
msg = "class B without an implementation for abstract method foo"
self.assertRaisesRegex(TypeError, msg, B)

def test_update_layered_implementation(self):
Expand All @@ -627,7 +627,7 @@ def foo(self):

abc.update_abstractmethods(C)

msg = "class C with abstract method foo"
msg = "class C without an implementation for abstract method foo"
self.assertRaisesRegex(TypeError, msg, C)

def test_update_multi_inheritance(self):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_dataclasses.py
Expand Up @@ -3762,7 +3762,7 @@ class Date(A):
day: 'int'

self.assertTrue(inspect.isabstract(Date))
msg = 'class Date with abstract method foo'
msg = 'class Date without an implementation for abstract method foo'
self.assertRaisesRegex(TypeError, msg, Date)


Expand Down
@@ -0,0 +1 @@
Updates the error message for abstract class.
2 changes: 1 addition & 1 deletion Objects/typeobject.c
Expand Up @@ -4559,7 +4559,7 @@ object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)

PyErr_Format(PyExc_TypeError,
"Can't instantiate abstract class %s "
"with abstract method%s %U",
"without an implementation for abstract method%s %U",
type->tp_name,
method_count > 1 ? "s" : "",
joined);
Expand Down

0 comments on commit 079f0dd

Please sign in to comment.