@@ -184,7 +184,7 @@ class C(metaclass=abc_ABCMeta):
184184 @abc .abstractmethod
185185 def method_one (self ):
186186 pass
187- msg = r"class C without an implementation for abstract method 'method_one'"
187+ msg = r"class .*\. C without an implementation for abstract method 'method_one'"
188188 self .assertRaisesRegex (TypeError , msg , C )
189189
190190 def test_object_new_with_many_abstractmethods (self ):
@@ -195,7 +195,7 @@ def method_one(self):
195195 @abc .abstractmethod
196196 def method_two (self ):
197197 pass
198- msg = r"class C without an implementation for abstract methods 'method_one', 'method_two'"
198+ msg = r"class .*\. C without an implementation for abstract methods 'method_one', 'method_two'"
199199 self .assertRaisesRegex (TypeError , msg , C )
200200
201201 @warnings_helper .ignore_warnings (category = DeprecationWarning )
@@ -586,7 +586,7 @@ def updated_foo(self):
586586 A .foo = updated_foo
587587 abc .update_abstractmethods (A )
588588 self .assertEqual (A .__abstractmethods__ , {'foo' , 'bar' })
589- msg = "class A without an implementation for abstract methods 'bar', 'foo'"
589+ msg = r "class .*\. A without an implementation for abstract methods 'bar', 'foo'"
590590 self .assertRaisesRegex (TypeError , msg , A )
591591
592592 def test_update_implementation (self ):
@@ -598,7 +598,7 @@ def foo(self):
598598 class B (A ):
599599 pass
600600
601- msg = "class B without an implementation for abstract method 'foo'"
601+ msg = r "class .*\. B without an implementation for abstract method 'foo'"
602602 self .assertRaisesRegex (TypeError , msg , B )
603603 self .assertEqual (B .__abstractmethods__ , {'foo' })
604604
@@ -656,7 +656,7 @@ def foo(self):
656656
657657 abc .update_abstractmethods (B )
658658
659- msg = "class B without an implementation for abstract method 'foo'"
659+ msg = r "class .*\. B without an implementation for abstract method 'foo'"
660660 self .assertRaisesRegex (TypeError , msg , B )
661661
662662 def test_update_layered_implementation (self ):
@@ -678,7 +678,7 @@ def foo(self):
678678
679679 abc .update_abstractmethods (C )
680680
681- msg = "class C without an implementation for abstract method 'foo'"
681+ msg = r "class .*\. C without an implementation for abstract method 'foo'"
682682 self .assertRaisesRegex (TypeError , msg , C )
683683
684684 def test_update_multi_inheritance (self ):
@@ -732,18 +732,60 @@ class B(A, metaclass=abc_ABCMeta, name="test"):
732732 pass
733733 self .assertEqual (saved_kwargs , dict (name = "test" ))
734734
735- return TestLegacyAPI , TestABC , TestABCWithInitSubclass
736735
737- TestLegacyAPI_Py , TestABC_Py , TestABCWithInitSubclass_Py = test_factory (_py_abc .ABCMeta ,
738- _py_abc .get_cache_token )
739- TestLegacyAPI_C , TestABC_C , TestABCWithInitSubclass_C = test_factory (abc .ABCMeta ,
740- abc .get_cache_token )
736+ class TestAbstractClassErrorMessage (unittest .TestCase ):
737+
738+ class MyAbstractClass (abc .ABC ):
739+ @abc .abstractmethod
740+ def my_method (self ):
741+ pass
742+
743+ def test_error_contains_class_name (self ):
744+ with self .assertRaises (TypeError ) as cm :
745+ self .MyAbstractClass ()
746+ self .assertIn ("MyAbstractClass" , str (cm .exception ))
747+
748+ def test_error_contains_module_when_not_main (self ):
749+ original_module = self .MyAbstractClass .__module__
750+
751+ try :
752+ self .MyAbstractClass .__module__ = "test_module"
753+
754+ with self .assertRaises (TypeError ) as cm :
755+ self .MyAbstractClass ()
756+
757+ print (str (cm .exception ))
758+ self .assertRegex (str (cm .exception ), r"test_module\..*\.MyAbstractClass" )
759+ finally :
760+ self .MyAbstractClass .__module__ = original_module
761+
762+ def test_error_without_module_when_main (self ):
763+ original_module = self .MyAbstractClass .__module__
764+
765+ try :
766+ self .MyAbstractClass .__module__ = "__main__"
767+
768+ with self .assertRaises (TypeError ) as cm :
769+ self .MyAbstractClass ()
770+
771+ self .assertRegex (str (cm .exception ), r"MyAbstractClass" )
772+ self .assertNotRegex (str (cm .exception ), r"__main__\." )
773+ finally :
774+ self .MyAbstractClass .__module__ = original_module
775+
776+ return TestLegacyAPI , TestABC , TestABCWithInitSubclass , TestAbstractClassErrorMessage
777+
778+ TestLegacyAPI_Py , TestABC_Py , TestABCWithInitSubclass_Py , TestAbstractClassErrorMessage_Py = test_factory (_py_abc .ABCMeta ,
779+ _py_abc .get_cache_token )
780+ TestLegacyAPI_C , TestABC_C , TestABCWithInitSubclass_C , TestAbstractClassErrorMessage_C = test_factory (abc .ABCMeta ,
781+ abc .get_cache_token )
741782
742783# gh-130095: The _py_abc tests are not thread-safe when run with
743784# `--parallel-threads`
744785TestLegacyAPI_Py .__unittest_thread_unsafe__ = True
745786TestABC_Py .__unittest_thread_unsafe__ = True
746787TestABCWithInitSubclass_Py .__unittest_thread_unsafe__ = True
788+ TestAbstractClassErrorMessage_Py .__unittest_thread_unsafe__ = True
747789
748790if __name__ == "__main__" :
749791 unittest .main ()
0 commit comments