@@ -4176,6 +4176,15 @@ def __dir__(self):
41764176 self .assertNotIn ("blech" , actual )
41774177 self .assertNotIn ("oh no!" , actual )
41784178
4179+ def test_attribute_error_with_non_string_candidates (self ):
4180+ class T :
4181+ bluch = 1
4182+
4183+ instance = T ()
4184+ instance .__dict__ [0 ] = 1
4185+ actual = self .get_suggestion (instance , 'blich' )
4186+ self .assertIn ("bluch" , actual )
4187+
41794188 def test_attribute_error_with_bad_name (self ):
41804189 def raise_attribute_error_with_bad_name ():
41814190 raise AttributeError (name = 12 , obj = 23 )
@@ -4211,8 +4220,8 @@ def make_module(self, code):
42114220
42124221 return mod_name
42134222
4214- def get_import_from_suggestion (self , mod_dict , name ):
4215- modname = self .make_module (mod_dict )
4223+ def get_import_from_suggestion (self , code , name ):
4224+ modname = self .make_module (code )
42164225
42174226 def callable ():
42184227 try :
@@ -4289,6 +4298,13 @@ def test_import_from_suggestions_underscored(self):
42894298 self .assertIn ("'_bluch'" , self .get_import_from_suggestion (code , '_luch' ))
42904299 self .assertNotIn ("'_bluch'" , self .get_import_from_suggestion (code , 'bluch' ))
42914300
4301+ def test_import_from_suggestions_non_string (self ):
4302+ modWithNonStringAttr = textwrap .dedent ("""\
4303+ globals()[0] = 1
4304+ bluch = 1
4305+ """ )
4306+ self .assertIn ("'bluch'" , self .get_import_from_suggestion (modWithNonStringAttr , 'blech' ))
4307+
42924308 def test_import_from_suggestions_do_not_trigger_for_long_attributes (self ):
42934309 code = "blech = None"
42944310
@@ -4385,6 +4401,15 @@ def func():
43854401 actual = self .get_suggestion (func )
43864402 self .assertIn ("'ZeroDivisionError'?" , actual )
43874403
4404+ def test_name_error_suggestions_with_non_string_candidates (self ):
4405+ def func ():
4406+ abc = 1
4407+ custom_globals = globals ().copy ()
4408+ custom_globals [0 ] = 1
4409+ print (eval ("abv" , custom_globals , locals ()))
4410+ actual = self .get_suggestion (func )
4411+ self .assertIn ("abc" , actual )
4412+
43884413 def test_name_error_suggestions_do_not_trigger_for_long_names (self ):
43894414 def func ():
43904415 somethingverywronghehehehehehe = None
0 commit comments