From 68ea8f1881a98bec79f21142ad3d05d3494b8618 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 19 Aug 2025 10:25:33 +0200 Subject: [PATCH 1/2] Parametrize getattr/setattr in get_suggestion This allows moving test_suggestions_with_method_call back to test_getattr_suggestions_underscored, so that delattr is tested for these as well. --- Lib/test/test_traceback.py | 44 ++++++++++---------------------------- 1 file changed, 11 insertions(+), 33 deletions(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index b9112411c943a3..beb5ec11361bc3 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -4022,10 +4022,12 @@ def test_dont_swallow_subexceptions_of_falsey_exceptiongroup(self): class SuggestionFormattingTestMixin: + attr_function = getattr + def get_suggestion(self, obj, attr_name=None): if attr_name is not None: def callable(): - getattr(obj, attr_name) + self.attr_function(obj, attr_name) else: callable = obj @@ -4087,15 +4089,21 @@ class A: self.assertIn("'bluch'", self.get_suggestion(A(), '_luch')) self.assertIn("'bluch'", self.get_suggestion(A(), '_bluch')) + attr_function = self.attr_function class B: _bluch = None def method(self, name): - getattr(self, name) + attr_function(self, name) self.assertIn("'_bluch'", self.get_suggestion(B(), '_blach')) self.assertIn("'_bluch'", self.get_suggestion(B(), '_luch')) self.assertNotIn("'_bluch'", self.get_suggestion(B(), 'bluch')) + self.assertIn("'_bluch'", self.get_suggestion(partial(B().method, '_blach'))) + self.assertIn("'_bluch'", self.get_suggestion(partial(B().method, '_luch'))) + self.assertIn("'_bluch'", self.get_suggestion(partial(B().method, 'bluch'))) + + def test_do_not_trigger_for_long_attributes(self): class A: blech = None @@ -4130,18 +4138,6 @@ class A: class GetattrSuggestionTests(BaseSuggestionTests): - def get_suggestion(self, obj, attr_name=None): - if attr_name is not None: - def callable(): - getattr(obj, attr_name) - else: - callable = obj - - result_lines = self.get_exception( - callable, slice_start=-1, slice_end=None - ) - return result_lines[0] - def test_suggestions_no_args(self): class A: blech = None @@ -4190,27 +4186,9 @@ def __dir__(self): actual = self.get_suggestion(A(), 'blech') self.assertNotIn("Did you mean", actual) - def test_suggestions_with_method_call(self): - class B: - _bluch = None - def method(self, name): - getattr(self, name) - - obj = B() - self.assertIn("'_bluch'", self.get_suggestion(partial(obj.method, '_blach'))) - self.assertIn("'_bluch'", self.get_suggestion(partial(obj.method, '_luch'))) - self.assertIn("'_bluch'", self.get_suggestion(partial(obj.method, 'bluch'))) - class DelattrSuggestionTests(BaseSuggestionTests): - def get_suggestion(self, obj, attr_name): - def callable(): - delattr(obj, attr_name) - - result_lines = self.get_exception( - callable, slice_start=-1, slice_end=None - ) - return result_lines[0] + attr_function = delattr class SuggestionFormattingTestBase(SuggestionFormattingTestMixin): From 30d80436d52fed9f8a8be9a727968a80b650ce41 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 19 Aug 2025 10:31:02 +0200 Subject: [PATCH 2/2] Run test_suggestions_for_same_name for delattr too --- Lib/test/test_traceback.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index beb5ec11361bc3..08c4d38ae326b6 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -4136,6 +4136,13 @@ class A: actual = self.get_suggestion(A(), 'bluch') self.assertNotIn("blech", actual) + def test_suggestions_for_same_name(self): + class A: + def __dir__(self): + return ['blech'] + actual = self.get_suggestion(A(), 'blech') + self.assertNotIn("Did you mean", actual) + class GetattrSuggestionTests(BaseSuggestionTests): def test_suggestions_no_args(self): @@ -4179,13 +4186,6 @@ def __getattr__(self, attr): actual = self.get_suggestion(cls(), 'bluch') self.assertIn("blech", actual) - def test_suggestions_for_same_name(self): - class A: - def __dir__(self): - return ['blech'] - actual = self.get_suggestion(A(), 'blech') - self.assertNotIn("Did you mean", actual) - class DelattrSuggestionTests(BaseSuggestionTests): attr_function = delattr