Skip to content

Commit 9f6d715

Browse files
committed
Use regex
1 parent 59d6d1b commit 9f6d715

1 file changed

Lines changed: 32 additions & 38 deletions

File tree

‎Lib/test/test_call.py‎

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,97 +1003,91 @@ class AClassWithMetaclass(metaclass=AMeta):
10031003

10041004
@cpython_only
10051005
class TestIncorrectNumberOfPositionalArgs(unittest.TestCase):
1006-
@contextlib.contextmanager
1007-
def assert_type_error_and_msg_in(self, message):
1008-
with self.assertRaises(TypeError) as cm:
1009-
yield cm
1010-
self.assertIn(message, str(cm.exception))
1011-
10121006
def test_too_many_positional_with_defaults_suggests_missing_self(self):
1013-
msg = ".method_two_args() takes from 0 to 2 positional arguments but 3 were given. Did you forget the 'self' parameter in the function definition?"
1014-
with self.assert_type_error_and_msg_in(msg):
1007+
msg = r"\.method_two_args\(\) takes from 0 to 2 positional arguments but 3 were given\. Did you forget the 'self' parameter in the function definition\?"
1008+
with self.assertRaisesRegex(TypeError, msg):
10151009
A().method_two_args("woof", "loud")
10161010

10171011
def test_too_many_positional_but_missing_self_no_args(self):
1018-
msg = "takes 0 positional arguments but 1 was given. Did you forget the 'self' parameter in the function definition?"
1019-
with self.assert_type_error_and_msg_in(msg):
1012+
msg = r"takes 0 positional arguments but 1 was given\. Did you forget the 'self' parameter in the function definition\?"
1013+
with self.assertRaisesRegex(TypeError, msg):
10201014
A().method_zero_arg()
10211015

10221016
def test_too_many_positional_but_missing_self(self):
1023-
msg = ".method_one_arg() takes 1 positional argument but 2 were given. Did you forget the 'self' parameter in the function definition?"
1024-
with self.assert_type_error_and_msg_in(msg):
1017+
msg = r"\.method_one_arg\(\) takes 1 positional argument but 2 were given\. Did you forget the 'self' parameter in the function definition\?"
1018+
with self.assertRaisesRegex(TypeError, msg):
10251019
A().method_one_arg("quiet")
10261020

10271021
def test_too_many_positional(self):
1028-
msg = "takes 0 positional arguments but 1 was given"
1029-
with self.assert_type_error_and_msg_in(msg):
1022+
msg = r"takes 0 positional arguments but 1 was given"
1023+
with self.assertRaisesRegex(TypeError, msg):
10301024
A.static_no_args("oops it's an arg")
10311025

10321026
def test_positional_only_passed_as_keyword(self):
1033-
msg = ".static_positional_only_one_arg() got some positional-only arguments passed as keyword arguments: 'arg1'"
1034-
with self.assert_type_error_and_msg_in(msg):
1027+
msg = r"\.static_positional_only_one_arg\(\) got some positional-only arguments passed as keyword arguments: 'arg1'"
1028+
with self.assertRaisesRegex(TypeError, msg):
10351029
A.static_positional_only_one_arg(arg1="ball")
10361030

10371031
def test_unexpected_keyword(self):
1038-
msg = "method_two_args() got an unexpected keyword argument 'bad'"
1039-
with self.assert_type_error_and_msg_in(msg):
1032+
msg = r"method_two_args\(\) got an unexpected keyword argument 'bad'"
1033+
with self.assertRaisesRegex(TypeError, msg):
10401034
A().method_two_args(bad="x")
10411035

10421036
def test_multiple_values(self):
1043-
msg = ".method_two_args() got multiple values for argument 'arg1'"
1044-
with self.assert_type_error_and_msg_in(msg):
1037+
msg = r"\.method_two_args\(\) got multiple values for argument 'arg1'"
1038+
with self.assertRaisesRegex(TypeError, msg):
10451039
A().method_two_args("quiet", "low", arg1="oops")
10461040

10471041
def test_unbound_method_with_self_keeps_missing_argument_error(self):
1048-
msg = ".method_one_arg() missing 1 required positional argument: 'arg1'"
1049-
with self.assert_type_error_and_msg_in(msg):
1042+
msg = r"\.method_one_arg\(\) missing 1 required positional argument: 'arg1'"
1043+
with self.assertRaisesRegex(TypeError, msg):
10501044
A.method_one_arg()
10511045

10521046
def test_classmethod_missing_cls_does_not_suggest_missing_self(self):
1053-
msg = ".classmethod_one_arg() takes 1 positional argument but 2 were given"
1054-
with self.assert_type_error_and_msg_in(msg) as cm:
1047+
msg = r"\.classmethod_one_arg\(\) takes 1 positional argument but 2 were given"
1048+
with self.assertRaisesRegex(TypeError, msg) as cm:
10551049
A.classmethod_one_arg("arg1")
10561050
self.assertNotIn("Did you forget", str(cm.exception))
10571051

10581052
def test_classmethod_missing_cls_via_instance_does_not_suggest_missing_self(self):
1059-
msg = ".classmethod_one_arg() takes 1 positional argument but 2 were given"
1060-
with self.assert_type_error_and_msg_in(msg) as cm:
1053+
msg = r"\.classmethod_one_arg\(\) takes 1 positional argument but 2 were given"
1054+
with self.assertRaisesRegex(TypeError, msg) as cm:
10611055
A().classmethod_one_arg("poodle")
10621056
self.assertNotIn("Did you forget", str(cm.exception))
10631057

10641058
def test_staticmethod_too_many_args_does_not_suggest_missing_self(self):
1065-
msg = ".static_one_arg() takes 1 positional argument but 2 were given"
1066-
with self.assert_type_error_and_msg_in(msg) as cm:
1059+
msg = r"\.static_one_arg\(\) takes 1 positional argument but 2 were given"
1060+
with self.assertRaisesRegex(TypeError, msg) as cm:
10671061
A.static_one_arg(1, 2)
10681062
self.assertNotIn("Did you forget", str(cm.exception))
10691063

10701064
def test_staticmethod_too_many_args_via_instance_does_not_suggest_missing_self(self):
1071-
msg = ".static_one_arg() takes 1 positional argument but 2 were given"
1072-
with self.assert_type_error_and_msg_in(msg) as cm:
1065+
msg = r"\.static_one_arg\(\) takes 1 positional argument but 2 were given"
1066+
with self.assertRaisesRegex(TypeError, msg) as cm:
10731067
A().static_one_arg(1, 2)
10741068
self.assertNotIn("Did you forget", str(cm.exception))
10751069

10761070
def test_metaclass_missing_receiver_does_not_suggest_missing_self(self):
1077-
msg = "AMeta.method_one_arg() takes 1 positional argument but 2 were given"
1078-
with self.assert_type_error_and_msg_in(msg) as cm:
1071+
msg = r"AMeta\.method_one_arg\(\) takes 1 positional argument but 2 were given"
1072+
with self.assertRaisesRegex(TypeError, msg) as cm:
10791073
AClassWithMetaclass.method_one_arg("standard")
10801074
self.assertNotIn("Did you forget", str(cm.exception))
10811075

10821076
def test_metaclass_method_too_many_args_does_not_suggest_missing_self(self):
1083-
msg = "AMeta.method_two_arg() takes 2 positional arguments but 3 were given"
1084-
with self.assert_type_error_and_msg_in(msg) as cm:
1077+
msg = r"AMeta\.method_two_arg\(\) takes 2 positional arguments but 3 were given"
1078+
with self.assertRaisesRegex(TypeError, msg) as cm:
10851079
AClassWithMetaclass.method_two_arg("trail", "river")
10861080
self.assertNotIn("Did you forget", str(cm.exception))
10871081

10881082
def test_metaclass_classmethod_does_not_suggest_missing_self(self):
1089-
msg = "AMeta.classmethod_one_arg() takes 1 positional argument but 2 were given"
1090-
with self.assert_type_error_and_msg_in(msg) as cm:
1083+
msg = r"AMeta\.classmethod_one_arg\(\) takes 1 positional argument but 2 were given"
1084+
with self.assertRaisesRegex(TypeError, msg) as cm:
10911085
AClassWithMetaclass.classmethod_one_arg("standard")
10921086
self.assertNotIn("Did you forget", str(cm.exception))
10931087

10941088
def test_metaclass_staticmethod_does_not_suggest_missing_self(self):
1095-
msg = "AMeta.static_one_arg() takes 1 positional argument but 2 were given"
1096-
with self.assert_type_error_and_msg_in(msg) as cm:
1089+
msg = r"AMeta\.static_one_arg\(\) takes 1 positional argument but 2 were given"
1090+
with self.assertRaisesRegex(TypeError, msg) as cm:
10971091
AClassWithMetaclass.static_one_arg("show", "working")
10981092
self.assertNotIn("Did you forget", str(cm.exception))
10991093

0 commit comments

Comments
 (0)