@@ -946,26 +946,26 @@ def f(self, *args, **kwargs):
946946 self .assertEqual (args_captured , [("foo" ,)])
947947 self .assertEqual (kwargs_captured , [{"baz" : "bar" }])
948948
949- class Dog :
950- def bark (self , volume , pitch ):
949+ class A :
950+ def method_two_args (self , volume , pitch ):
951951 pass
952952
953953 @staticmethod
954- def wag_tail_no_args ():
954+ def static_no_args ():
955955 pass
956956
957957 @staticmethod
958- def wag_tail (times ):
958+ def static_method_one_arg (times ):
959959 pass
960960
961961 @staticmethod
962- def fetch (toy , / ):
962+ def positional_only (toy , / ):
963963 pass
964964
965- def bark_with_self (self , sound ):
965+ def instance_method_2args (self , sound ):
966966 pass
967967
968- def bark_missing_self (sound ):
968+ def instance_method_with_one_arg_not_self (sound ):
969969 pass
970970
971971 def wag_missing_self ():
@@ -1000,114 +1000,114 @@ class Poodle(metaclass=AnimalMeta):
10001000 pass
10011001
10021002@cpython_only
1003- class TestErrorMessagesUseQualifiedName (unittest .TestCase ):
1003+ class TestIncorrectNumberOfPositionalArgs (unittest .TestCase ):
10041004 @contextlib .contextmanager
1005- def check_raises_type_error (self , message ):
1005+ def assert_type_error_and_msg_equals (self , message : str ):
10061006 with self .assertRaises (TypeError ) as cm :
10071007 yield
1008- self .assertEqual ( str (cm .exception ), message )
1008+ self .assertIn ( message , str (cm .exception ))
10091009
10101010 def test_too_many_positional_with_self_does_not_suggest_missing_self (self ):
10111011 """A regular method with self should keep the normal too-many-args error."""
1012- msg = "Dog.bark_with_self() takes 2 positional arguments but 3 were given"
1013- with self .check_raises_type_error (msg ):
1014- Dog ().bark_with_self ("woof" , "loud" )
1012+ msg = "takes 2 positional arguments but 3 were given"
1013+ with self .assert_type_error_and_msg_equals (msg ):
1014+ A ().instance_method_2args ("woof" , "loud" )
10151015
10161016 def test_too_many_positional_but_missing_self (self ):
10171017 """A bound instance method missing self should get the targeted hint."""
1018- msg = "Dog.bark_missing_self() takes 1 positional argument but 2 were given. Did you forget the 'self' parameter in the function definition?"
1019- with self .check_raises_type_error (msg ):
1020- Dog ().bark_missing_self ("woof" )
1018+ msg = "takes 1 positional argument but 2 were given. Did you forget the 'self' parameter in the function definition?"
1019+ with self .assert_type_error_and_msg_equals (msg ):
1020+ A ().instance_method_with_one_arg_not_self ("woof" )
10211021
10221022 def test_too_many_positional_but_missing_self_no_args (self ):
10231023 """A zero-argument method called through an instance should get the hint."""
1024- msg = "Dog.wag_missing_self() takes 0 positional arguments but 1 was given. Did you forget the 'self' parameter in the function definition?"
1025- with self .check_raises_type_error (msg ):
1026- Dog ().wag_missing_self ()
1024+ msg = "takes 0 positional arguments but 1 was given. Did you forget the 'self' parameter in the function definition?"
1025+ with self .assert_type_error_and_msg_equals (msg ):
1026+ A ().wag_missing_self ()
10271027
10281028 def test_missing_arguments (self ):
1029- msg = "Dog.bark() missing 1 required positional argument: 'pitch'"
1030- with self .check_raises_type_error (msg ):
1031- Dog ().bark ("quiet" )
1029+ msg = "missing 1 required positional argument: 'pitch'"
1030+ with self .assert_type_error_and_msg_equals (msg ):
1031+ A ().method_two_args ("quiet" )
10321032
10331033 def test_too_many_positional (self ):
1034- msg = "Dog.wag_tail_no_args() takes 0 positional arguments but 1 was given"
1035- with self .check_raises_type_error (msg ):
1036- Dog . wag_tail_no_args ("oops it's an arg" )
1034+ msg = "takes 0 positional arguments but 1 was given"
1035+ with self .assert_type_error_and_msg_equals (msg ):
1036+ A . static_no_args ("oops it's an arg" )
10371037
10381038 def test_positional_only_passed_as_keyword (self ):
10391039 msg = "Dog.fetch() got some positional-only arguments passed as keyword arguments: 'toy'"
1040- with self .check_raises_type_error (msg ):
1041- Dog . fetch (toy = "ball" )
1040+ with self .assert_type_error_and_msg_equals (msg ):
1041+ A . positional_only (toy = "ball" )
10421042
10431043 def test_unexpected_keyword (self ):
10441044 msg = "Dog.bark() got an unexpected keyword argument 'bad'"
1045- with self .check_raises_type_error (msg ):
1046- Dog ().bark (bad = "x" )
1045+ with self .assert_type_error_and_msg_equals (msg ):
1046+ A ().method_two_args (bad = "x" )
10471047
10481048 def test_multiple_values (self ):
10491049 msg = "Dog.bark() got multiple values for argument 'volume'"
1050- with self .check_raises_type_error (msg ):
1051- Dog ().bark ("quiet" , "low" , volume = "oops" )
1050+ with self .assert_type_error_and_msg_equals (msg ):
1051+ A ().method_two_args ("quiet" , "low" , volume = "oops" )
10521052
10531053 def test_self_in_wrong_position_keeps_missing_argument_error (self ):
10541054 """A parameter named self in the wrong position is a missing-arg error."""
10551055 msg = "Dog.bark_with_self_arg() missing 1 required positional argument: 'self'"
1056- with self .check_raises_type_error (msg ):
1057- Dog ().bark_with_self_arg ()
1056+ with self .assert_type_error_and_msg_equals (msg ):
1057+ A ().bark_with_self_arg ()
10581058
10591059 def test_unbound_method_with_self_keeps_missing_argument_error (self ):
10601060 """Calling an unbound method without self should keep the missing-arg error."""
10611061 msg = "Dog.sit() missing 1 required positional argument: 'self'"
1062- with self .check_raises_type_error (msg ):
1063- Dog .sit ()
1062+ with self .assert_type_error_and_msg_equals (msg ):
1063+ A .sit ()
10641064
10651065 def test_classmethod_missing_cls_does_not_suggest_missing_self (self ):
10661066 """A classmethod missing cls conceptually should not suggest self."""
10671067 msg = "Dog.register_breed() takes 1 positional argument but 2 were given"
1068- with self .check_raises_type_error (msg ):
1069- Dog .register_breed ("poodle" )
1068+ with self .assert_type_error_and_msg_equals (msg ):
1069+ A .register_breed ("poodle" )
10701070
10711071 def test_classmethod_missing_cls_via_instance_does_not_suggest_missing_self (self ):
10721072 """A classmethod called through an instance should not suggest self."""
10731073 msg = "Dog.register_breed() takes 1 positional argument but 2 were given"
1074- with self .check_raises_type_error (msg ):
1075- Dog ().register_breed ("poodle" )
1074+ with self .assert_type_error_and_msg_equals (msg ):
1075+ A ().register_breed ("poodle" )
10761076
10771077 def test_staticmethod_too_many_args_does_not_suggest_missing_self (self ):
10781078 """A staticmethod with too many arguments should not suggest self."""
10791079 msg = "Dog.wag_tail() takes 1 positional argument but 2 were given"
1080- with self .check_raises_type_error (msg ):
1081- Dog . wag_tail (1 , 2 )
1080+ with self .assert_type_error_and_msg_equals (msg ):
1081+ A . static_method_one_arg (1 , 2 )
10821082
10831083 def test_staticmethod_too_many_args_via_instance_does_not_suggest_missing_self (self ):
10841084 """A staticmethod called through an instance should not suggest self."""
10851085 msg = "Dog.wag_tail() takes 1 positional argument but 2 were given"
1086- with self .check_raises_type_error (msg ):
1087- Dog ().wag_tail (1 , 2 )
1086+ with self .assert_type_error_and_msg_equals (msg ):
1087+ A ().static_method_one_arg (1 , 2 )
10881088
10891089 def test_metaclass_missing_receiver_does_not_suggest_missing_self (self ):
10901090 """A metaclass receiver error should not suggest an instance self."""
10911091 msg = "AnimalMeta.register_pack() takes 1 positional argument but 2 were given"
1092- with self .check_raises_type_error (msg ):
1092+ with self .assert_type_error_and_msg_equals (msg ):
10931093 Poodle .register_pack ("standard" )
10941094
10951095 def test_metaclass_method_too_many_args_does_not_suggest_missing_self (self ):
10961096 """A metaclass method with too many arguments should not suggest self."""
10971097 msg = "AnimalMeta.track_pack() takes 2 positional arguments but 3 were given"
1098- with self .check_raises_type_error (msg ):
1098+ with self .assert_type_error_and_msg_equals (msg ):
10991099 Poodle .track_pack ("trail" , "river" )
11001100
11011101 def test_metaclass_classmethod_does_not_suggest_missing_self (self ):
11021102 """A classmethod on a metaclass should not suggest instance self."""
11031103 msg = "AnimalMeta.classify_pack() takes 1 positional argument but 2 were given"
1104- with self .check_raises_type_error (msg ):
1104+ with self .assert_type_error_and_msg_equals (msg ):
11051105 Poodle .classify_pack ("standard" )
11061106
11071107 def test_metaclass_staticmethod_does_not_suggest_missing_self (self ):
11081108 """A staticmethod on a metaclass should not suggest instance self."""
11091109 msg = "AnimalMeta.tag_pack() takes 1 positional argument but 2 were given"
1110- with self .check_raises_type_error (msg ):
1110+ with self .assert_type_error_and_msg_equals (msg ):
11111111 Poodle .tag_pack ("show" , "working" )
11121112
11131113@cpython_only
0 commit comments