Skip to content

Commit 2538806

Browse files
committed
Merge remote-tracking branch 'Pranjal095/delattr-suggestion-tests' into delattr-suggestions-combined
Adds test cases for the delattr suggestion feature implemented in PR #130427 by @sobolevn.
2 parents d9ceb27 + f980e4f commit 2538806

File tree

1 file changed

+89
-18
lines changed

1 file changed

+89
-18
lines changed

Lib/test/test_traceback.py

Lines changed: 89 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3976,7 +3976,7 @@ def callable():
39763976
)
39773977
return result_lines[0]
39783978

3979-
def test_getattr_suggestions(self):
3979+
def run_suggestion_tests(self, operation):
39803980
class Substitution:
39813981
noise = more_noise = a = bc = None
39823982
blech = None
@@ -4016,62 +4016,133 @@ class CaseChangeOverSubstitution:
40164016
(EliminationOverAddition, "'bluc'?"),
40174017
(CaseChangeOverSubstitution, "'BLuch'?"),
40184018
]:
4019-
actual = self.get_suggestion(cls(), 'bluch')
4019+
obj = cls()
4020+
4021+
if operation == "getattr":
4022+
actual = self.get_suggestion(obj, 'bluch')
4023+
elif operation == "delattr":
4024+
actual = self.get_suggestion(lambda: delattr(obj, 'bluch'))
4025+
else:
4026+
raise ValueError(f"operation '{operation}' not recognized")
4027+
40204028
self.assertIn(suggestion, actual)
40214029

4022-
def test_getattr_suggestions_underscored(self):
4030+
def test_getattr_suggestions(self):
4031+
self.run_suggestion_tests("getattr")
4032+
4033+
def test_delattr_suggestions(self):
4034+
self.run_suggestion_tests("delattr")
4035+
4036+
def run_underscored_tests(self, operation):
40234037
class A:
40244038
bluch = None
40254039

4026-
self.assertIn("'bluch'", self.get_suggestion(A(), 'blach'))
4027-
self.assertIn("'bluch'", self.get_suggestion(A(), '_luch'))
4028-
self.assertIn("'bluch'", self.get_suggestion(A(), '_bluch'))
4040+
obj = A()
4041+
if operation == "getattr":
4042+
self.assertIn("'bluch'", self.get_suggestion(obj, 'blach'))
4043+
self.assertIn("'bluch'", self.get_suggestion(obj, '_luch'))
4044+
self.assertIn("'bluch'", self.get_suggestion(obj, '_bluch'))
4045+
elif operation == "delattr":
4046+
self.assertIn("'bluch'", self.get_suggestion(lambda: delattr(obj, 'blach')))
4047+
self.assertIn("'bluch'", self.get_suggestion(lambda: delattr(obj, '_luch')))
4048+
self.assertIn("'bluch'", self.get_suggestion(lambda: delattr(obj, '_bluch')))
4049+
else:
4050+
raise ValueError(f"operation '{operation}' not recognized")
40294051

40304052
class B:
40314053
_bluch = None
40324054
def method(self, name):
40334055
getattr(self, name)
40344056

4035-
self.assertIn("'_bluch'", self.get_suggestion(B(), '_blach'))
4036-
self.assertIn("'_bluch'", self.get_suggestion(B(), '_luch'))
4037-
self.assertNotIn("'_bluch'", self.get_suggestion(B(), 'bluch'))
4057+
obj = B()
4058+
if operation == "getattr":
4059+
self.assertIn("'_bluch'", self.get_suggestion(obj, '_blach'))
4060+
self.assertIn("'_bluch'", self.get_suggestion(obj, '_luch'))
4061+
self.assertNotIn("'_bluch'", self.get_suggestion(obj, 'bluch'))
4062+
self.assertIn("'_bluch'", self.get_suggestion(partial(obj.method, '_blach')))
4063+
self.assertIn("'_bluch'", self.get_suggestion(partial(obj.method, '_luch')))
4064+
self.assertIn("'_bluch'", self.get_suggestion(partial(obj.method, 'bluch')))
4065+
elif operation == "delattr":
4066+
self.assertIn("'_bluch'", self.get_suggestion(lambda: delattr(obj, '_blach')))
4067+
self.assertIn("'_bluch'", self.get_suggestion(lambda: delattr(obj, '_luch')))
4068+
self.assertNotIn("'_bluch'", self.get_suggestion(lambda: delattr(obj, 'bluch')))
4069+
else:
4070+
raise ValueError(f"operation '{operation}' not recognized")
40384071

4039-
self.assertIn("'_bluch'", self.get_suggestion(partial(B().method, '_blach')))
4040-
self.assertIn("'_bluch'", self.get_suggestion(partial(B().method, '_luch')))
4041-
self.assertIn("'_bluch'", self.get_suggestion(partial(B().method, 'bluch')))
4072+
def test_getattr_suggestions_underscored(self):
4073+
self.run_underscored_tests("getattr")
40424074

4043-
def test_getattr_suggestions_do_not_trigger_for_long_attributes(self):
4075+
def test_delattr_suggestions_underscored(self):
4076+
self.run_underscored_tests("delattr")
4077+
4078+
def run_do_not_trigger_for_long_attributes_tests(self, operation):
40444079
class A:
40454080
blech = None
40464081

4047-
actual = self.get_suggestion(A(), 'somethingverywrong')
4082+
obj = A()
4083+
if operation == "getattr":
4084+
actual = self.get_suggestion(obj, 'somethingverywrong')
4085+
elif operation == "delattr":
4086+
actual = self.get_suggestion(lambda: delattr(obj, 'somethingverywrong'))
4087+
else:
4088+
raise ValueError(f"operation '{operation}' not recognized")
40484089
self.assertNotIn("blech", actual)
40494090

4050-
def test_getattr_error_bad_suggestions_do_not_trigger_for_small_names(self):
4091+
def test_getattr_suggestions_do_not_trigger_for_long_attributes(self):
4092+
self.run_do_not_trigger_for_long_attributes_tests("getattr")
4093+
4094+
def test_delattr_suggestions_do_not_trigger_for_long_attributes(self):
4095+
self.run_do_not_trigger_for_long_attributes_tests("delattr")
4096+
4097+
def run_do_not_trigger_for_small_names_tests(self, operation):
40514098
class MyClass:
40524099
vvv = mom = w = id = pytho = None
40534100

4101+
obj = MyClass()
40544102
for name in ("b", "v", "m", "py"):
40554103
with self.subTest(name=name):
4056-
actual = self.get_suggestion(MyClass, name)
4104+
if operation == "getattr":
4105+
actual = self.get_suggestion(MyClass, name)
4106+
elif operation == "delattr":
4107+
actual = self.get_suggestion(lambda: delattr(obj, name))
4108+
else:
4109+
raise ValueError(f"operation '{operation}' not recognized")
40574110
self.assertNotIn("Did you mean", actual)
40584111
self.assertNotIn("'vvv", actual)
40594112
self.assertNotIn("'mom'", actual)
40604113
self.assertNotIn("'id'", actual)
40614114
self.assertNotIn("'w'", actual)
40624115
self.assertNotIn("'pytho'", actual)
40634116

4064-
def test_getattr_suggestions_do_not_trigger_for_big_dicts(self):
4117+
def test_getattr_error_bad_suggestions_do_not_trigger_for_small_names(self):
4118+
self.run_do_not_trigger_for_small_names_tests("getattr")
4119+
4120+
def test_delattr_error_bad_suggestions_do_not_trigger_for_small_names(self):
4121+
self.run_do_not_trigger_for_small_names_tests("delattr")
4122+
4123+
def run_do_not_trigger_for_big_dicts_tests(self, operation):
40654124
class A:
40664125
blech = None
40674126
# A class with a very big __dict__ will not be considered
40684127
# for suggestions.
40694128
for index in range(2000):
40704129
setattr(A, f"index_{index}", None)
40714130

4072-
actual = self.get_suggestion(A(), 'bluch')
4131+
obj = A()
4132+
if operation == "getattr":
4133+
actual = self.get_suggestion(obj, 'bluch')
4134+
elif operation == "delattr":
4135+
actual = self.get_suggestion(lambda: delattr(obj, 'bluch'))
4136+
else:
4137+
raise ValueError(f"operation '{operation}' not recognized")
40734138
self.assertNotIn("blech", actual)
40744139

4140+
def test_getattr_suggestions_do_not_trigger_for_big_dicts(self):
4141+
self.run_do_not_trigger_for_big_dicts_tests("getattr")
4142+
4143+
def test_delattr_suggestions_do_not_trigger_for_big_dicts(self):
4144+
self.run_do_not_trigger_for_big_dicts_tests("delattr")
4145+
40754146
def test_getattr_suggestions_no_args(self):
40764147
class A:
40774148
blech = None

0 commit comments

Comments
 (0)