Skip to content

Commit 813c898

Browse files
committed
Refactored getattr and delattr tests
1 parent 423d777 commit 813c898

File tree

1 file changed

+77
-116
lines changed

1 file changed

+77
-116
lines changed

Lib/test/test_traceback.py

Lines changed: 77 additions & 116 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,121 @@ 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+
40204026
self.assertIn(suggestion, actual)
40214027

4022-
def test_getattr_suggestions_underscored(self):
4028+
def test_getattr_suggestions(self):
4029+
self.run_suggestion_tests("getattr")
4030+
4031+
def test_delattr_suggestions(self):
4032+
self.run_suggestion_tests("delattr")
4033+
4034+
def run_underscored_tests(self, operation):
40234035
class A:
40244036
bluch = None
40254037

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'))
4038+
obj = A()
4039+
if operation == "getattr":
4040+
self.assertIn("'bluch'", self.get_suggestion(obj, 'blach'))
4041+
self.assertIn("'bluch'", self.get_suggestion(obj, '_luch'))
4042+
self.assertIn("'bluch'", self.get_suggestion(obj, '_bluch'))
4043+
elif operation == "delattr":
4044+
self.assertIn("'bluch'", self.get_suggestion(lambda: delattr(obj, 'blach')))
4045+
self.assertIn("'bluch'", self.get_suggestion(lambda: delattr(obj, '_luch')))
4046+
self.assertIn("'bluch'", self.get_suggestion(lambda: delattr(obj, '_bluch')))
40294047

40304048
class B:
40314049
_bluch = None
40324050
def method(self, name):
40334051
getattr(self, name)
40344052

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'))
4053+
obj = B()
4054+
if operation == "getattr":
4055+
self.assertIn("'_bluch'", self.get_suggestion(obj, '_blach'))
4056+
self.assertIn("'_bluch'", self.get_suggestion(obj, '_luch'))
4057+
self.assertNotIn("'_bluch'", self.get_suggestion(obj, 'bluch'))
4058+
self.assertIn("'_bluch'", self.get_suggestion(partial(obj.method, '_blach')))
4059+
self.assertIn("'_bluch'", self.get_suggestion(partial(obj.method, '_luch')))
4060+
self.assertIn("'_bluch'", self.get_suggestion(partial(obj.method, 'bluch')))
4061+
elif operation == "delattr":
4062+
self.assertIn("'_bluch'", self.get_suggestion(lambda: delattr(obj, '_blach')))
4063+
self.assertIn("'_bluch'", self.get_suggestion(lambda: delattr(obj, '_luch')))
4064+
self.assertNotIn("'_bluch'", self.get_suggestion(lambda: delattr(obj, 'bluch')))
40384065

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')))
4066+
def test_getattr_suggestions_underscored(self):
4067+
self.run_underscored_tests("getattr")
40424068

4043-
def test_getattr_suggestions_do_not_trigger_for_long_attributes(self):
4069+
def test_delattr_suggestions_underscored(self):
4070+
self.run_underscored_tests("delattr")
4071+
4072+
def run_do_not_trigger_for_long_attributes_tests(self, operation):
40444073
class A:
40454074
blech = None
40464075

4047-
actual = self.get_suggestion(A(), 'somethingverywrong')
4076+
obj = A()
4077+
if operation == "getattr":
4078+
actual = self.get_suggestion(obj, 'somethingverywrong')
4079+
elif operation == "delattr":
4080+
actual = self.get_suggestion(lambda: delattr(obj, 'somethingverywrong'))
40484081
self.assertNotIn("blech", actual)
40494082

4050-
def test_getattr_error_bad_suggestions_do_not_trigger_for_small_names(self):
4083+
def test_getattr_suggestions_do_not_trigger_for_long_attributes(self):
4084+
self.run_do_not_trigger_for_long_attributes_tests("getattr")
4085+
4086+
def test_delattr_suggestions_do_not_trigger_for_long_attributes(self):
4087+
self.run_do_not_trigger_for_long_attributes_tests("delattr")
4088+
4089+
def run_do_not_trigger_for_small_names_tests(self, operation):
40514090
class MyClass:
40524091
vvv = mom = w = id = pytho = None
40534092

4093+
obj = MyClass()
40544094
for name in ("b", "v", "m", "py"):
40554095
with self.subTest(name=name):
4056-
actual = self.get_suggestion(MyClass, name)
4096+
if operation == "getattr":
4097+
actual = self.get_suggestion(MyClass, name)
4098+
elif operation == "delattr":
4099+
actual = self.get_suggestion(lambda: delattr(obj, name))
40574100
self.assertNotIn("Did you mean", actual)
40584101
self.assertNotIn("'vvv", actual)
40594102
self.assertNotIn("'mom'", actual)
40604103
self.assertNotIn("'id'", actual)
40614104
self.assertNotIn("'w'", actual)
40624105
self.assertNotIn("'pytho'", actual)
40634106

4064-
def test_getattr_suggestions_do_not_trigger_for_big_dicts(self):
4107+
def test_getattr_error_bad_suggestions_do_not_trigger_for_small_names(self):
4108+
self.run_do_not_trigger_for_small_names_tests("getattr")
4109+
4110+
def test_delattr_error_bad_suggestions_do_not_trigger_for_small_names(self):
4111+
self.run_do_not_trigger_for_small_names_tests("delattr")
4112+
4113+
def run_do_not_trigger_for_big_dicts_tests(self, operation):
40654114
class A:
40664115
blech = None
40674116
# A class with a very big __dict__ will not be considered
40684117
# for suggestions.
40694118
for index in range(2000):
40704119
setattr(A, f"index_{index}", None)
40714120

4072-
actual = self.get_suggestion(A(), 'bluch')
4121+
obj = A()
4122+
if operation == "getattr":
4123+
actual = self.get_suggestion(obj, 'bluch')
4124+
elif operation == "delattr":
4125+
actual = self.get_suggestion(lambda: delattr(obj, 'bluch'))
40734126
self.assertNotIn("blech", actual)
40744127

4128+
def test_getattr_suggestions_do_not_trigger_for_big_dicts(self):
4129+
self.run_do_not_trigger_for_big_dicts_tests("getattr")
4130+
4131+
def test_delattr_suggestions_do_not_trigger_for_big_dicts(self):
4132+
self.run_do_not_trigger_for_big_dicts_tests("delattr")
4133+
40754134
def test_getattr_suggestions_no_args(self):
40764135
class A:
40774136
blech = None
@@ -4120,104 +4179,6 @@ def __dir__(self):
41204179
actual = self.get_suggestion(A(), 'blech')
41214180
self.assertNotIn("Did you mean", actual)
41224181

4123-
def test_delattr_suggestions(self):
4124-
class Substitution:
4125-
noise = more_noise = a = bc = None
4126-
blech = None
4127-
4128-
class Elimination:
4129-
noise = more_noise = a = bc = None
4130-
blch = None
4131-
4132-
class Addition:
4133-
noise = more_noise = a = bc = None
4134-
bluchin = None
4135-
4136-
class SubstitutionOverElimination:
4137-
blach = None
4138-
bluc = None
4139-
4140-
class SubstitutionOverAddition:
4141-
blach = None
4142-
bluchi = None
4143-
4144-
class EliminationOverAddition:
4145-
blucha = None
4146-
bluc = None
4147-
4148-
class CaseChangeOverSubstitution:
4149-
Luch = None
4150-
fluch = None
4151-
BLuch = None
4152-
4153-
for cls, suggestion in [
4154-
(Addition, "'bluchin'?"),
4155-
(Substitution, "'blech'?"),
4156-
(Elimination, "'blch'?"),
4157-
(Addition, "'bluchin'?"),
4158-
(SubstitutionOverElimination, "'blach'?"),
4159-
(SubstitutionOverAddition, "'blach'?"),
4160-
(EliminationOverAddition, "'bluc'?"),
4161-
(CaseChangeOverSubstitution, "'BLuch'?"),
4162-
]:
4163-
obj = cls()
4164-
def callable():
4165-
delattr(obj, 'bluch')
4166-
actual = self.get_suggestion(callable)
4167-
self.assertIn(suggestion, actual)
4168-
4169-
def test_delattr_suggestions_underscored(self):
4170-
class A:
4171-
bluch = None
4172-
4173-
obj = A()
4174-
self.assertIn("'bluch'", self.get_suggestion(lambda: delattr(obj, 'blach')))
4175-
self.assertIn("'bluch'", self.get_suggestion(lambda: delattr(obj, '_luch')))
4176-
self.assertIn("'bluch'", self.get_suggestion(lambda: delattr(obj, '_bluch')))
4177-
4178-
class B:
4179-
_bluch = None
4180-
4181-
obj = B()
4182-
self.assertIn("'_bluch'", self.get_suggestion(lambda: delattr(obj, '_blach')))
4183-
self.assertIn("'_bluch'", self.get_suggestion(lambda: delattr(obj, '_luch')))
4184-
self.assertNotIn("'_bluch'", self.get_suggestion(lambda: delattr(obj, 'bluch')))
4185-
4186-
def test_delattr_suggestions_do_not_trigger_for_long_attributes(self):
4187-
class A:
4188-
blech = None
4189-
4190-
obj = A()
4191-
actual = self.get_suggestion(lambda: delattr(obj, 'somethingverywrong'))
4192-
self.assertNotIn("blech", actual)
4193-
4194-
def test_delattr_error_bad_suggestions_do_not_trigger_for_small_names(self):
4195-
class MyClass:
4196-
vvv = mom = w = id = pytho = None
4197-
4198-
obj = MyClass()
4199-
for name in ("b", "v", "m", "py"):
4200-
with self.subTest(name=name):
4201-
actual = self.get_suggestion(lambda: delattr(obj, name))
4202-
self.assertNotIn("Did you mean", actual)
4203-
self.assertNotIn("'vvv", actual)
4204-
self.assertNotIn("'mom'", actual)
4205-
self.assertNotIn("'id'", actual)
4206-
self.assertNotIn("'w'", actual)
4207-
self.assertNotIn("'pytho'", actual)
4208-
4209-
def test_delattr_suggestions_do_not_trigger_for_big_dicts(self):
4210-
class A:
4211-
blech = None
4212-
# A class with a very big __dict__ will not be considered
4213-
# for suggestions.
4214-
obj = A()
4215-
for index in range(2000):
4216-
setattr(obj, f"index_{index}", None)
4217-
4218-
actual = self.get_suggestion(lambda: delattr(obj, 'bluch'))
4219-
self.assertNotIn("blech", actual)
4220-
42214182
def test_attribute_error_with_failing_dict(self):
42224183
class T:
42234184
bluch = 1

0 commit comments

Comments
 (0)