Skip to content

Commit 04254ad

Browse files
committed
Refactored getattr and delattr tests
1 parent c369876 commit 04254ad

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
@@ -4034,7 +4034,7 @@ def callable():
40344034
)
40354035
return result_lines[0]
40364036

4037-
def test_getattr_suggestions(self):
4037+
def run_suggestion_tests(self, operation):
40384038
class Substitution:
40394039
noise = more_noise = a = bc = None
40404040
blech = None
@@ -4074,62 +4074,121 @@ class CaseChangeOverSubstitution:
40744074
(EliminationOverAddition, "'bluc'?"),
40754075
(CaseChangeOverSubstitution, "'BLuch'?"),
40764076
]:
4077-
actual = self.get_suggestion(cls(), 'bluch')
4077+
obj = cls()
4078+
4079+
if operation == "getattr":
4080+
actual = self.get_suggestion(obj, 'bluch')
4081+
elif operation == "delattr":
4082+
actual = self.get_suggestion(lambda: delattr(obj, 'bluch'))
4083+
40784084
self.assertIn(suggestion, actual)
40794085

4080-
def test_getattr_suggestions_underscored(self):
4086+
def test_getattr_suggestions(self):
4087+
self.run_suggestion_tests("getattr")
4088+
4089+
def test_delattr_suggestions(self):
4090+
self.run_suggestion_tests("delattr")
4091+
4092+
def run_underscored_tests(self, operation):
40814093
class A:
40824094
bluch = None
40834095

4084-
self.assertIn("'bluch'", self.get_suggestion(A(), 'blach'))
4085-
self.assertIn("'bluch'", self.get_suggestion(A(), '_luch'))
4086-
self.assertIn("'bluch'", self.get_suggestion(A(), '_bluch'))
4096+
obj = A()
4097+
if operation == "getattr":
4098+
self.assertIn("'bluch'", self.get_suggestion(obj, 'blach'))
4099+
self.assertIn("'bluch'", self.get_suggestion(obj, '_luch'))
4100+
self.assertIn("'bluch'", self.get_suggestion(obj, '_bluch'))
4101+
elif operation == "delattr":
4102+
self.assertIn("'bluch'", self.get_suggestion(lambda: delattr(obj, 'blach')))
4103+
self.assertIn("'bluch'", self.get_suggestion(lambda: delattr(obj, '_luch')))
4104+
self.assertIn("'bluch'", self.get_suggestion(lambda: delattr(obj, '_bluch')))
40874105

40884106
class B:
40894107
_bluch = None
40904108
def method(self, name):
40914109
getattr(self, name)
40924110

4093-
self.assertIn("'_bluch'", self.get_suggestion(B(), '_blach'))
4094-
self.assertIn("'_bluch'", self.get_suggestion(B(), '_luch'))
4095-
self.assertNotIn("'_bluch'", self.get_suggestion(B(), 'bluch'))
4111+
obj = B()
4112+
if operation == "getattr":
4113+
self.assertIn("'_bluch'", self.get_suggestion(obj, '_blach'))
4114+
self.assertIn("'_bluch'", self.get_suggestion(obj, '_luch'))
4115+
self.assertNotIn("'_bluch'", self.get_suggestion(obj, 'bluch'))
4116+
self.assertIn("'_bluch'", self.get_suggestion(partial(obj.method, '_blach')))
4117+
self.assertIn("'_bluch'", self.get_suggestion(partial(obj.method, '_luch')))
4118+
self.assertIn("'_bluch'", self.get_suggestion(partial(obj.method, 'bluch')))
4119+
elif operation == "delattr":
4120+
self.assertIn("'_bluch'", self.get_suggestion(lambda: delattr(obj, '_blach')))
4121+
self.assertIn("'_bluch'", self.get_suggestion(lambda: delattr(obj, '_luch')))
4122+
self.assertNotIn("'_bluch'", self.get_suggestion(lambda: delattr(obj, 'bluch')))
40964123

4097-
self.assertIn("'_bluch'", self.get_suggestion(partial(B().method, '_blach')))
4098-
self.assertIn("'_bluch'", self.get_suggestion(partial(B().method, '_luch')))
4099-
self.assertIn("'_bluch'", self.get_suggestion(partial(B().method, 'bluch')))
4124+
def test_getattr_suggestions_underscored(self):
4125+
self.run_underscored_tests("getattr")
41004126

4101-
def test_getattr_suggestions_do_not_trigger_for_long_attributes(self):
4127+
def test_delattr_suggestions_underscored(self):
4128+
self.run_underscored_tests("delattr")
4129+
4130+
def run_do_not_trigger_for_long_attributes_tests(self, operation):
41024131
class A:
41034132
blech = None
41044133

4105-
actual = self.get_suggestion(A(), 'somethingverywrong')
4134+
obj = A()
4135+
if operation == "getattr":
4136+
actual = self.get_suggestion(obj, 'somethingverywrong')
4137+
elif operation == "delattr":
4138+
actual = self.get_suggestion(lambda: delattr(obj, 'somethingverywrong'))
41064139
self.assertNotIn("blech", actual)
41074140

4108-
def test_getattr_error_bad_suggestions_do_not_trigger_for_small_names(self):
4141+
def test_getattr_suggestions_do_not_trigger_for_long_attributes(self):
4142+
self.run_do_not_trigger_for_long_attributes_tests("getattr")
4143+
4144+
def test_delattr_suggestions_do_not_trigger_for_long_attributes(self):
4145+
self.run_do_not_trigger_for_long_attributes_tests("delattr")
4146+
4147+
def run_do_not_trigger_for_small_names_tests(self, operation):
41094148
class MyClass:
41104149
vvv = mom = w = id = pytho = None
41114150

4151+
obj = MyClass()
41124152
for name in ("b", "v", "m", "py"):
41134153
with self.subTest(name=name):
4114-
actual = self.get_suggestion(MyClass, name)
4154+
if operation == "getattr":
4155+
actual = self.get_suggestion(MyClass, name)
4156+
elif operation == "delattr":
4157+
actual = self.get_suggestion(lambda: delattr(obj, name))
41154158
self.assertNotIn("Did you mean", actual)
41164159
self.assertNotIn("'vvv", actual)
41174160
self.assertNotIn("'mom'", actual)
41184161
self.assertNotIn("'id'", actual)
41194162
self.assertNotIn("'w'", actual)
41204163
self.assertNotIn("'pytho'", actual)
41214164

4122-
def test_getattr_suggestions_do_not_trigger_for_big_dicts(self):
4165+
def test_getattr_error_bad_suggestions_do_not_trigger_for_small_names(self):
4166+
self.run_do_not_trigger_for_small_names_tests("getattr")
4167+
4168+
def test_delattr_error_bad_suggestions_do_not_trigger_for_small_names(self):
4169+
self.run_do_not_trigger_for_small_names_tests("delattr")
4170+
4171+
def run_do_not_trigger_for_big_dicts_tests(self, operation):
41234172
class A:
41244173
blech = None
41254174
# A class with a very big __dict__ will not be considered
41264175
# for suggestions.
41274176
for index in range(2000):
41284177
setattr(A, f"index_{index}", None)
41294178

4130-
actual = self.get_suggestion(A(), 'bluch')
4179+
obj = A()
4180+
if operation == "getattr":
4181+
actual = self.get_suggestion(obj, 'bluch')
4182+
elif operation == "delattr":
4183+
actual = self.get_suggestion(lambda: delattr(obj, 'bluch'))
41314184
self.assertNotIn("blech", actual)
41324185

4186+
def test_getattr_suggestions_do_not_trigger_for_big_dicts(self):
4187+
self.run_do_not_trigger_for_big_dicts_tests("getattr")
4188+
4189+
def test_delattr_suggestions_do_not_trigger_for_big_dicts(self):
4190+
self.run_do_not_trigger_for_big_dicts_tests("delattr")
4191+
41334192
def test_getattr_suggestions_no_args(self):
41344193
class A:
41354194
blech = None
@@ -4178,104 +4237,6 @@ def __dir__(self):
41784237
actual = self.get_suggestion(A(), 'blech')
41794238
self.assertNotIn("Did you mean", actual)
41804239

4181-
def test_delattr_suggestions(self):
4182-
class Substitution:
4183-
noise = more_noise = a = bc = None
4184-
blech = None
4185-
4186-
class Elimination:
4187-
noise = more_noise = a = bc = None
4188-
blch = None
4189-
4190-
class Addition:
4191-
noise = more_noise = a = bc = None
4192-
bluchin = None
4193-
4194-
class SubstitutionOverElimination:
4195-
blach = None
4196-
bluc = None
4197-
4198-
class SubstitutionOverAddition:
4199-
blach = None
4200-
bluchi = None
4201-
4202-
class EliminationOverAddition:
4203-
blucha = None
4204-
bluc = None
4205-
4206-
class CaseChangeOverSubstitution:
4207-
Luch = None
4208-
fluch = None
4209-
BLuch = None
4210-
4211-
for cls, suggestion in [
4212-
(Addition, "'bluchin'?"),
4213-
(Substitution, "'blech'?"),
4214-
(Elimination, "'blch'?"),
4215-
(Addition, "'bluchin'?"),
4216-
(SubstitutionOverElimination, "'blach'?"),
4217-
(SubstitutionOverAddition, "'blach'?"),
4218-
(EliminationOverAddition, "'bluc'?"),
4219-
(CaseChangeOverSubstitution, "'BLuch'?"),
4220-
]:
4221-
obj = cls()
4222-
def callable():
4223-
delattr(obj, 'bluch')
4224-
actual = self.get_suggestion(callable)
4225-
self.assertIn(suggestion, actual)
4226-
4227-
def test_delattr_suggestions_underscored(self):
4228-
class A:
4229-
bluch = None
4230-
4231-
obj = A()
4232-
self.assertIn("'bluch'", self.get_suggestion(lambda: delattr(obj, 'blach')))
4233-
self.assertIn("'bluch'", self.get_suggestion(lambda: delattr(obj, '_luch')))
4234-
self.assertIn("'bluch'", self.get_suggestion(lambda: delattr(obj, '_bluch')))
4235-
4236-
class B:
4237-
_bluch = None
4238-
4239-
obj = B()
4240-
self.assertIn("'_bluch'", self.get_suggestion(lambda: delattr(obj, '_blach')))
4241-
self.assertIn("'_bluch'", self.get_suggestion(lambda: delattr(obj, '_luch')))
4242-
self.assertNotIn("'_bluch'", self.get_suggestion(lambda: delattr(obj, 'bluch')))
4243-
4244-
def test_delattr_suggestions_do_not_trigger_for_long_attributes(self):
4245-
class A:
4246-
blech = None
4247-
4248-
obj = A()
4249-
actual = self.get_suggestion(lambda: delattr(obj, 'somethingverywrong'))
4250-
self.assertNotIn("blech", actual)
4251-
4252-
def test_delattr_error_bad_suggestions_do_not_trigger_for_small_names(self):
4253-
class MyClass:
4254-
vvv = mom = w = id = pytho = None
4255-
4256-
obj = MyClass()
4257-
for name in ("b", "v", "m", "py"):
4258-
with self.subTest(name=name):
4259-
actual = self.get_suggestion(lambda: delattr(obj, name))
4260-
self.assertNotIn("Did you mean", actual)
4261-
self.assertNotIn("'vvv", actual)
4262-
self.assertNotIn("'mom'", actual)
4263-
self.assertNotIn("'id'", actual)
4264-
self.assertNotIn("'w'", actual)
4265-
self.assertNotIn("'pytho'", actual)
4266-
4267-
def test_delattr_suggestions_do_not_trigger_for_big_dicts(self):
4268-
class A:
4269-
blech = None
4270-
# A class with a very big __dict__ will not be considered
4271-
# for suggestions.
4272-
obj = A()
4273-
for index in range(2000):
4274-
setattr(obj, f"index_{index}", None)
4275-
4276-
actual = self.get_suggestion(lambda: delattr(obj, 'bluch'))
4277-
self.assertNotIn("blech", actual)
4278-
42794240
def test_attribute_error_with_failing_dict(self):
42804241
class T:
42814242
bluch = 1

0 commit comments

Comments
 (0)