Skip to content

Commit 8c9c597

Browse files
authored
Merge pull request #1 from encukou/delattr-suggestions-re
Delattr suggestions re
2 parents e008ea4 + 30d8043 commit 8c9c597

File tree

1 file changed

+18
-40
lines changed

1 file changed

+18
-40
lines changed

Lib/test/test_traceback.py

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4022,10 +4022,12 @@ def test_dont_swallow_subexceptions_of_falsey_exceptiongroup(self):
40224022

40234023

40244024
class SuggestionFormattingTestMixin:
4025+
attr_function = getattr
4026+
40254027
def get_suggestion(self, obj, attr_name=None):
40264028
if attr_name is not None:
40274029
def callable():
4028-
getattr(obj, attr_name)
4030+
self.attr_function(obj, attr_name)
40294031
else:
40304032
callable = obj
40314033

@@ -4087,15 +4089,21 @@ class A:
40874089
self.assertIn("'bluch'", self.get_suggestion(A(), '_luch'))
40884090
self.assertIn("'bluch'", self.get_suggestion(A(), '_bluch'))
40894091

4092+
attr_function = self.attr_function
40904093
class B:
40914094
_bluch = None
40924095
def method(self, name):
4093-
getattr(self, name)
4096+
attr_function(self, name)
40944097

40954098
self.assertIn("'_bluch'", self.get_suggestion(B(), '_blach'))
40964099
self.assertIn("'_bluch'", self.get_suggestion(B(), '_luch'))
40974100
self.assertNotIn("'_bluch'", self.get_suggestion(B(), 'bluch'))
40984101

4102+
self.assertIn("'_bluch'", self.get_suggestion(partial(B().method, '_blach')))
4103+
self.assertIn("'_bluch'", self.get_suggestion(partial(B().method, '_luch')))
4104+
self.assertIn("'_bluch'", self.get_suggestion(partial(B().method, 'bluch')))
4105+
4106+
40994107
def test_do_not_trigger_for_long_attributes(self):
41004108
class A:
41014109
blech = None
@@ -4128,20 +4136,15 @@ class A:
41284136
actual = self.get_suggestion(A(), 'bluch')
41294137
self.assertNotIn("blech", actual)
41304138

4139+
def test_suggestions_for_same_name(self):
4140+
class A:
4141+
def __dir__(self):
4142+
return ['blech']
4143+
actual = self.get_suggestion(A(), 'blech')
4144+
self.assertNotIn("Did you mean", actual)
41314145

4132-
class GetattrSuggestionTests(BaseSuggestionTests):
4133-
def get_suggestion(self, obj, attr_name=None):
4134-
if attr_name is not None:
4135-
def callable():
4136-
getattr(obj, attr_name)
4137-
else:
4138-
callable = obj
4139-
4140-
result_lines = self.get_exception(
4141-
callable, slice_start=-1, slice_end=None
4142-
)
4143-
return result_lines[0]
41444146

4147+
class GetattrSuggestionTests(BaseSuggestionTests):
41454148
def test_suggestions_no_args(self):
41464149
class A:
41474150
blech = None
@@ -4183,34 +4186,9 @@ def __getattr__(self, attr):
41834186
actual = self.get_suggestion(cls(), 'bluch')
41844187
self.assertIn("blech", actual)
41854188

4186-
def test_suggestions_for_same_name(self):
4187-
class A:
4188-
def __dir__(self):
4189-
return ['blech']
4190-
actual = self.get_suggestion(A(), 'blech')
4191-
self.assertNotIn("Did you mean", actual)
4192-
4193-
def test_suggestions_with_method_call(self):
4194-
class B:
4195-
_bluch = None
4196-
def method(self, name):
4197-
getattr(self, name)
4198-
4199-
obj = B()
4200-
self.assertIn("'_bluch'", self.get_suggestion(partial(obj.method, '_blach')))
4201-
self.assertIn("'_bluch'", self.get_suggestion(partial(obj.method, '_luch')))
4202-
self.assertIn("'_bluch'", self.get_suggestion(partial(obj.method, 'bluch')))
4203-
42044189

42054190
class DelattrSuggestionTests(BaseSuggestionTests):
4206-
def get_suggestion(self, obj, attr_name):
4207-
def callable():
4208-
delattr(obj, attr_name)
4209-
4210-
result_lines = self.get_exception(
4211-
callable, slice_start=-1, slice_end=None
4212-
)
4213-
return result_lines[0]
4191+
attr_function = delattr
42144192

42154193

42164194
class SuggestionFormattingTestBase(SuggestionFormattingTestMixin):

0 commit comments

Comments
 (0)