Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 53 additions & 11 deletions Lib/test/test_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class C(metaclass=abc_ABCMeta):
@abc.abstractmethod
def method_one(self):
pass
msg = r"class C without an implementation for 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 @@ -195,7 +195,7 @@ def method_one(self):
@abc.abstractmethod
def method_two(self):
pass
msg = r"class C without an implementation for 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)

@warnings_helper.ignore_warnings(category=DeprecationWarning)
Expand Down Expand Up @@ -586,7 +586,7 @@ def updated_foo(self):
A.foo = updated_foo
abc.update_abstractmethods(A)
self.assertEqual(A.__abstractmethods__, {'foo', 'bar'})
msg = "class A without an implementation for abstract methods 'bar', 'foo'"
msg = r"class .*\.A without an implementation for abstract methods 'bar', 'foo'"
self.assertRaisesRegex(TypeError, msg, A)

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

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

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

abc.update_abstractmethods(B)

msg = "class B without an implementation for abstract method 'foo'"
msg = r"class .*\.B without an implementation for abstract method 'foo'"
self.assertRaisesRegex(TypeError, msg, B)

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

abc.update_abstractmethods(C)

msg = "class C without an implementation for abstract method 'foo'"
msg = r"class .*\.C without an implementation for abstract method 'foo'"
self.assertRaisesRegex(TypeError, msg, C)

def test_update_multi_inheritance(self):
Expand Down Expand Up @@ -732,18 +732,60 @@ class B(A, metaclass=abc_ABCMeta, name="test"):
pass
self.assertEqual(saved_kwargs, dict(name="test"))

return TestLegacyAPI, TestABC, TestABCWithInitSubclass

TestLegacyAPI_Py, TestABC_Py, TestABCWithInitSubclass_Py = test_factory(_py_abc.ABCMeta,
_py_abc.get_cache_token)
TestLegacyAPI_C, TestABC_C, TestABCWithInitSubclass_C = test_factory(abc.ABCMeta,
abc.get_cache_token)
class TestAbstractClassErrorMessage(unittest.TestCase):

class MyAbstractClass(abc.ABC):
@abc.abstractmethod
def my_method(self):
pass

def test_error_contains_class_name(self):
with self.assertRaises(TypeError) as cm:
self.MyAbstractClass()
self.assertIn("MyAbstractClass", str(cm.exception))

def test_error_contains_module_when_not_main(self):
original_module = self.MyAbstractClass.__module__

try:
self.MyAbstractClass.__module__ = "test_module"

with self.assertRaises(TypeError) as cm:
self.MyAbstractClass()

print(str(cm.exception))
self.assertRegex(str(cm.exception), r"test_module\..*\.MyAbstractClass")
finally:
self.MyAbstractClass.__module__ = original_module

def test_error_without_module_when_main(self):
original_module = self.MyAbstractClass.__module__

try:
self.MyAbstractClass.__module__ = "__main__"

with self.assertRaises(TypeError) as cm:
self.MyAbstractClass()

self.assertRegex(str(cm.exception), r"MyAbstractClass")
self.assertNotRegex(str(cm.exception), r"__main__\.")
finally:
self.MyAbstractClass.__module__ = original_module

return TestLegacyAPI, TestABC, TestABCWithInitSubclass, TestAbstractClassErrorMessage

TestLegacyAPI_Py, TestABC_Py, TestABCWithInitSubclass_Py, TestAbstractClassErrorMessage_Py = test_factory(_py_abc.ABCMeta,
_py_abc.get_cache_token)
TestLegacyAPI_C, TestABC_C, TestABCWithInitSubclass_C, TestAbstractClassErrorMessage_C = test_factory(abc.ABCMeta,
abc.get_cache_token)

# gh-130095: The _py_abc tests are not thread-safe when run with
# `--parallel-threads`
TestLegacyAPI_Py.__unittest_thread_unsafe__ = True
TestABC_Py.__unittest_thread_unsafe__ = True
TestABCWithInitSubclass_Py.__unittest_thread_unsafe__ = True
TestAbstractClassErrorMessage_Py.__unittest_thread_unsafe__ = True

if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion Lib/test/test_dataclasses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5066,7 +5066,7 @@ class Date(A):
day: 'int'

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


Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ def _nogil_filtered_err(err: str, mod_name: str) -> str:

def check_program_exitcode(self, *args, check_stderr=True, **kwargs):
out, err = self.run_embedded_interpreter(*args, **kwargs)
self.assertEqual(out.rstrip(), 'ok! Py_RunMain() returned 123')

@skv0zsneg skv0zsneg Aug 30, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After adding changes to tests and typeobjec.c the strange error appear

AssertionError: 'Cmd click to launch VS Code Native REPL\nok! Py_RunMain() returned 123' != 'ok! Py_RunMain() returned 123'
- Cmd click to launch VS Code Native REPL
  ok! Py_RunMain() returned 123

So, I changed it for checking exactly "ok!..." string.

self.assertIn('ok! Py_RunMain() returned 123', out.rstrip())
if check_stderr:
self.assertEqual(err, '')

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add fully qualified name for abc class initialization exception.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it should describe the user-visible change more, eg:

Suggested change
Add fully qualified name for abc class initialization exception.
The :exc:`TypeError` raised when instantiating an abstract class with unimplemented abstract methods now includes the fully qualified name of the class.

11 changes: 9 additions & 2 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -7340,13 +7340,20 @@ object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL;
}

PyObject *type_name = _PyType_GetFullyQualifiedName(type, '.');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you check if PyErr_Format already does this via %N?

if (type_name == NULL) {
Py_DECREF(joined);
return NULL;
}

PyErr_Format(PyExc_TypeError,
"Can't instantiate abstract class %s "
"Can't instantiate abstract class %U "
"without an implementation for abstract method%s '%U'",
type->tp_name,
type_name,
method_count > 1 ? "s" : "",
joined);
Py_DECREF(joined);
Py_DECREF(type_name);
return NULL;
}
PyObject *obj = type->tp_alloc(type, 0);
Expand Down
Loading