actual = self.get_suggestion(instance.foo)
self.assertNotIn("self.blech", actual)
+ def test_unbound_local_error_with_side_effect(self):
+ # gh-132385
+ class A:
+ def __getattr__(self, key):
+ if key == 'foo':
+ raise AttributeError('foo')
+ if key == 'spam':
+ raise ValueError('spam')
+
+ def bar(self):
+ foo
+ def baz(self):
+ spam
+
+ suggestion = self.get_suggestion(A().bar)
+ self.assertNotIn('self.', suggestion)
+ self.assertIn("'foo'", suggestion)
+
+ suggestion = self.get_suggestion(A().baz)
+ self.assertNotIn('self.', suggestion)
+ self.assertIn("'spam'", suggestion)
+
def test_unbound_local_error_does_not_match(self):
def func():
something = 3
# has the wrong name as attribute
if 'self' in frame.f_locals:
self = frame.f_locals['self']
- if hasattr(self, wrong_name):
+ try:
+ has_wrong_name = hasattr(self, wrong_name)
+ except Exception:
+ has_wrong_name = False
+ if has_wrong_name:
return f"self.{wrong_name}"
try: