@@ -1314,6 +1314,91 @@ def test_attributes(self):
1314
1314
self .assertEqual (exc .name , 'carry' )
1315
1315
self .assertIs (exc .obj , sentinel )
1316
1316
1317
+ def test_getattr_suggestions (self ):
1318
+ class A :
1319
+ blech = None
1320
+
1321
+ try :
1322
+ A ().bluch
1323
+ except AttributeError as exc :
1324
+ with support .captured_stderr () as err :
1325
+ sys .__excepthook__ (* sys .exc_info ())
1326
+
1327
+ # self.assertIn("blech", err.getvalue())
1328
+
1329
+ def test_getattr_suggestions_do_not_trigger_for_long_attributes (self ):
1330
+ class A :
1331
+ blech = None
1332
+
1333
+ try :
1334
+ A ().somethingverywrong
1335
+ except AttributeError as exc :
1336
+ with support .captured_stderr () as err :
1337
+ sys .__excepthook__ (* sys .exc_info ())
1338
+
1339
+ self .assertNotIn ("blech" , err .getvalue ())
1340
+
1341
+ def test_getattr_suggestions_do_not_trigger_for_big_dicts (self ):
1342
+ class A :
1343
+ blech = None
1344
+ # A class with a very big __dict__ will not be consider
1345
+ # for suggestions.
1346
+ for index in range (101 ):
1347
+ setattr (A , f"index_{ index } " , None )
1348
+
1349
+ try :
1350
+ A ().bluch
1351
+ except AttributeError as exc :
1352
+ with support .captured_stderr () as err :
1353
+ sys .__excepthook__ (* sys .exc_info ())
1354
+
1355
+ self .assertNotIn ("blech" , err .getvalue ())
1356
+
1357
+ def test_getattr_suggestions_no_args (self ):
1358
+ class A :
1359
+ blech = None
1360
+ def __getattr__ (self , attr ):
1361
+ raise AttributeError ()
1362
+
1363
+ try :
1364
+ A ().bluch
1365
+ except AttributeError as exc :
1366
+ with support .captured_stderr () as err :
1367
+ sys .__excepthook__ (* sys .exc_info ())
1368
+
1369
+ self .assertIn ("blech" , err .getvalue ())
1370
+
1371
+ class A :
1372
+ blech = None
1373
+ def __getattr__ (self , attr ):
1374
+ raise AttributeError
1375
+
1376
+ try :
1377
+ A ().bluch
1378
+ except AttributeError as exc :
1379
+ with support .captured_stderr () as err :
1380
+ sys .__excepthook__ (* sys .exc_info ())
1381
+
1382
+ self .assertIn ("blech" , err .getvalue ())
1383
+
1384
+ def test_getattr_suggestions_invalid_args (self ):
1385
+ class NonStringifyClass :
1386
+ __str__ = None
1387
+ __repr__ = None
1388
+
1389
+ class A :
1390
+ blech = None
1391
+ def __getattr__ (self , attr ):
1392
+ raise AttributeError (NonStringifyClass ())
1393
+
1394
+ try :
1395
+ A ().bluch
1396
+ except AttributeError as exc :
1397
+ with support .captured_stderr () as err :
1398
+ sys .__excepthook__ (* sys .exc_info ())
1399
+
1400
+ self .assertIn ("blech" , err .getvalue ())
1401
+
1317
1402
1318
1403
class ImportErrorTests (unittest .TestCase ):
1319
1404
0 commit comments